addLoadEvent(menu);
addLoadEvent(casino_links);

//constants
var INACTIVE   = 'rgb(255, 255, 255)'
var ACTIVE     = 'rgb(178, 227, 178)';
var HOVER      = 'rgb(225, 255, 225)';
var ARROW_DOWN = 'url(/pokerimages/casino/redesign/bullet_arrow_down.png) no-repeat 100% 50%';
var ARROW_LEFT = 'url(/pokerimages/casino/redesign/bullet_arrow_left.png) no-repeat 100% 50%';

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/**
 * opens links to casino client / help in a new window
 */
function casino_links () {
	//checkpoints
	if (!document.getElementsByTagName) return false;
	if (!document.getElementsByTagName('a')) return false;

	//get all links in page
	var links = document.getElementsByTagName('a');

	//foreach
	for (var i=0; i < links.length; i++) {
		//friendlier var name
		var link = links[i];

		//link hrefs to search for
		var link_hrefs = new Array (
			'/casino/demo/',
			'/casino/play/'
		);

		//if link links to casino client
		if (str_contains(link.href, link_hrefs)) {
			//set on click behaviour
			link.onclick = function () {
				//open link in a new window
				window.open(this.href, 'casinoWindow', 'width=800, height=667');

				//do not let browser to follow link
				return false;
			}
		}

		var link_hrefs = new Array (
			'/casino/rules/'
		);

		//if link links to casino rules/help
		if (str_contains(link.href, link_hrefs)) {
			//set on click behaviour
			link.onclick = function () {
				//open link in a new window with particular settings
				window.open(this.href, 'casinoHelp', 'width=580, height=650, scrollbars=yes');

				//do not let browser to follow link
				return false;
			}
		}
	}
}

/**
 * searches for different sub strings inside a string
 *
 * @param str string : string to search in
 * @param needles array : strings to search for
 * @return bool : true if found, false otherwise
 */
function str_contains (str, needles) {
	//foreach needle
	for (var i=0; i < needles.length; i++) {
		//if needle is inside string
		if (str.indexOf(needles[i]) !== -1) {
			return true;
		}
	}

	return false;
}

function menu () {
	//checkpoints
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById('gamesmenu')) return false;

	var menu = document.getElementById('gamesmenu');

	activateGameTypes(menu);
	collapseGameLists(menu);
	expandCollapse(menu);
}

/**
 * goes through game Types and set active color
 */
function activateGameTypes (menu) {
	//get a list of all LI nodes
	var li = menu.getElementsByTagName('li');

	//foreach
	for (var i=0; i < li.length; i++) {
		//if this is a game type
		if (li[i].className == 'gametype') {
			//set bg color to active
			li[i].style.backgroundColor = ACTIVE;
		}
	}
}

/**
 * hides all game lists which have no currently loaded game
 */
function collapseGameLists (menu) {
	//get a list of all LI nodes
	var li = menu.getElementsByTagName('li');

	//foreach
	for (var i=0; i < li.length; i++) {
		//if this contains a list of games for a particular type
		if (li[i].className == 'gamelist') {
			//if user is not on the page of any of these games 
			if (!check_active(li[i])) {
				//hide list
				li[i].style.display = 'none';

				//if there is a game type for this list (there should be)
				if (prevElem(li[i])) {
					//change arrow
					prevElem(li[i]).style.background = ARROW_LEFT;
				}
			}
		}
	}
}

/**
 * checks if there are any LI nodes with class 'active' inside 'obj'
 */
function check_active (obj) {
	//get a list of all LI nodes inside object
	var li = obj.getElementsByTagName('li');

	//foreach
	for (var i=0; i < li.length; i++) {
		//if class name of li is active
		if (li[i].className == 'active') {
			return true;
		}
	}

	return false;
}

/**
 * sets behaviour re mouse over, onclick etc
 */
function expandCollapse (menu) {
	//get a list of all li node inside menu
	var li = menu.getElementsByTagName('li');

	//foreach
	for (var i=0; i < li.length; i++) {
		//if this is a game type
		if (li[i].className == 'gametype') {
			var orig;

			//set on click behaviour
			li[i].onclick = function () {
				//if there is a list of games for this gametype
				if (nextElem(this)) {
					showHide(this, nextElem(this));
				}
			}
			li[i].onmouseover = function () {
				//if game type is not active
				if (this.style.backgroundColor != ACTIVE) {
					//change color on mouse over
					this.style.backgroundColor = HOVER;
				}
			}
			li[i].onmouseout = function () {
				//if game type is not active
				if (this.style.backgroundColor != ACTIVE)
					//change color on mouse out
					this.style.backgroundColor = INACTIVE;
			}
		}
	}
}

/**
 * expands of collapse a list of games and sets arrows and game type colors
 */
function showHide (type, obj) {
	//if hidden
	if (obj.style.display == 'none') {
		//show
		obj.style.display = '';

		//change arrow of game type to down
		type.style.background =  ACTIVE + ' ' + ARROW_DOWN;
	}
	else {
		//hide
		obj.style.display = 'none';

		//change arrow of game type to left
		type.style.background =  HOVER + ' ' + ARROW_LEFT;
	}
}

/**
 * returns previous element before 'obj'
 */
function prevElem (obj) {
	//get parent node of obj
	var obj_parent = obj.parentNode;

	//get all elements inside parent node
	var child_elems = obj_parent.getElementsByTagName('*');

	//foreach
	for (var i=0; i < child_elems.length; i++) {
		if (child_elems[i] == obj) {
			return child_elems[i-1];
		}
	}

	return false;
}

/**
 * returns next element after 'obj'
 */
function nextElem (obj) {
	//get parent node of obj
	var obj_parent = obj.parentNode;

	//get all elements inside parent node
	var child_elems = obj_parent.getElementsByTagName('*');

	//loop through them
	for (var i=0; i<child_elems.length; i++) {
		if (child_elems[i] == obj) {
			return child_elems[i+1];
		}
	}

	return false;
}
