/**
 * Additional Template Functionality
 * 
 * by Andi Dittrich <andi.dittrich@a3non.org>
 * Copyright 2009 - All Rights reserved
 */

/**
 * CHAT User Online Display
 */
var xchatUserOnline = chatUserOnline;
document.observe("dom:loaded", function(){
	// get channel info
	$('chatMainMenuItem').update('Chat <strong>(' + xchatUserOnline + ')</strong>');
});

/**
 * SPOILER
 */
document.observe("dom:loaded", function(){
	$$('.spoiler').each(function(item){
		// hide
		item.toggle();
		
		var txt = ( item.readAttribute('rel') ) ?  item.readAttribute('rel') : 'Spoiler';
		
		button = new Element('input', {
			'type': 'button',
			'value':'Toggle: ' + txt,
			'class':'spoilerbutton'
		});
		
		button.observe('click', function(){
			this.next().toggle();
		});
		
		item.insert({
			before: button
		});
	
	});
});

/**
 *  open external links in new window
 */
document.observe("dom:loaded", function() {
	$$('a[href!=""]').each(function(link){
		var siteregex = /^(http:\/\/)([a-z]*\.)?(gaming-awards.de)/i;
        if(link.readAttribute('href').startsWith('http://') && !link.readAttribute('href').match(siteregex)){
        	link.writeAttribute('target', '_blank');
        }
	});
});

/**
 * Board Search
 */

// make data avaible
var xboards = boards;

document.observe("dom:loaded", function() {						
	// hide default text on click
	$('searchInput2').setAttribute('autocomplete', 'off');
	$('searchInput2').onfocus = function() { if (this.value == 'Game Boardsuche') this.value=''; };
	$('searchInput2').onblur = function() { if (this.value == '') this.value = 'Game Boardsuche'; };
			
	// add submit event
	$('boardautocompleteform').onsubmit = function(){
		// read value
		value = $('searchInput2').value;
		
		// id loookup
		xboards.each(function(item){
			if (value==item.title){
				location.href = '/index.php?page=Board&boardID=' + item.boardID;
			}
		});
		
		// deny submit
		return false;
	}
					
					
	// convert json array to simple string array with board titles
	var boardnames = new Array();
	xboards.each(function(item){
		boardnames.push(item.title + "");
	});

	// vreate autocompleter
	new Autocompleter.Local('searchInput2', 'boardautocompletelist', boardnames, { 
		fullSearch:true,
	});
});

