function hideAll() {
	var menu_heads = $(".menuItemHead");
	for(var i = 1; i < menu_heads.length + 1; i++) {
		menu_heads[i - 1].onclick = function() {
			return (anim) ? openMenu(this, 0, true) : false;
		}
	}
	$('.subMenuItem,.subMenuItemLast').css("visibility", "hidden");
	$('.menuItemHolder').css("height", "35px");
}

function hideOwnMenu() {
	$("#menu14").css("height", "35px");
}

var menu_open;

function hideForumMenu() {
	$("#menuHolder").css("position", "absolute");
	$("#menuHolder").css("left", "-200px");
	$("#menuHolder").css("display", "none");
	$("#mainPage").css("margin-left", "20px");
	$("#logoDiv").css("left", "65px");
	menu_open = 0;
}

var menu_timeout;

function moveMenu() {
	if(!menu_open) {
		var the_left = $("#menuHolder").css("left");
		the_left = parseInt(the_left) + 20 + "px";
		$("#menuHolder").css("left", the_left);
		$("#menuHolder").css("display", "");
		$("#mainPage").css("margin-left", "205px");
		$("#logoDiv").css("left", "250px");
		if($("#menuHolder").css("left") == "0px") {
			clearTimeout(menu_timeout);
			//zmiana paska menu po lewej
			$("#menuHider").css("background-position", "100% 0px");
			menu_open = 1;
			return true;
		}
		menu_timeout = setTimeout('moveMenu()', 10);
	} else {		
		var the_left = $("#menuHolder").css("left");
		the_left = parseInt(the_left) - 20 + "px";
		$("#menuHolder").css("left", the_left);

		if($("#menuHolder").css("left") == "-200px") {
			clearTimeout(menu_timeout);
			$("#menuHolder").css("display", "none");
			$("#mainPage").css("margin-left", "20px");
			$("#logoDiv").css("left", "65px");
			//zmiana paska menu po lewej
			$("#menuHider").css("background-position", "0px 0px");
			menu_open = 0;
			return true;
		}
		menu_timeout = setTimeout('moveMenu()', 10);
	}
}

var the_left;
var the_top;
var znak;
var the_timeout = new Array();
var anim = 1;
var the_divs = new Array();
var the_divs_runout = new Array();
var the_list;
var count = new Array();
var countout = 0;
var all_menus = new Array();

var menus_ids = new Array();

menus_ids['news'] 		= 1;
menus_ids['forum'] 		= 2;
menus_ids['download'] 	= 3;
menus_ids['spells'] 	= 4;
menus_ids['monsters'] 	= 5;
menus_ids['items'] 		= 6;
menus_ids['quests'] 	= 7;
menus_ids['npcs'] 		= 8;
menus_ids['achs'] 		= 9;
menus_ids['gallery'] 	= 10;
menus_ids['maps'] 		= 11;
menus_ids['stats'] 		= 12;
menus_ids['support'] 	= 13;
menus_ids['ownm'] 		= 14;

menus_ids['1'] = 'news';
menus_ids['2'] = 'forum';
menus_ids['3'] = 'download';
menus_ids['4'] = 'spells';
menus_ids['5'] = 'monsters';
menus_ids['6'] = 'items';
menus_ids['7'] = 'quests';
menus_ids['8'] = 'npcs';
menus_ids['9'] = 'achs';
menus_ids['10'] = 'gallery';
menus_ids['11'] = 'maps';
menus_ids['12'] = 'stats';
menus_ids['13'] = 'support';
menus_ids['14'] = 'ownm';

function createObj() {
	this.top = 0;
	this.left = 0;
	return this;
}

function openMenu(caller, id, arg) {
	if(!id) {
		id = caller.id.substr(4);
		id = menus_ids[id];
	}
	if(anim) {
		var xpos = parseInt(caller.style.backgroundPosition);
		caller.style.backgroundPosition = (xpos + 100) % 200 + "% " + ( (id - 1) * -35 ) + "px";
	}
	anim = 0;

	if(all_menus["submenu" + id] == 'set') {
		if(arg) {
			the_divs[id] = new Array();
			the_list = document.getElementById("submenu" + id).childNodes;
			for(var i = 0, j = 0; i < the_list.length; i++) {
				if(the_list[i].id != undefined) {
					the_divs[id][j] = the_list[i];
					j++;
				}
			}
			
			for(var i = 0; i < the_divs[id].length; i++) {
				the_divs_runout[i] = new createObj();
				the_divs_runout[i].top = parseInt( Math.random() * 60 ) - 30 + i * 20;
				the_divs_runout[i].left = parseInt( Math.random() * 60 ) - 30;
			}			
		}
		
		for(var i = 0; i < the_divs[id].length; i++) {
			the_left = the_divs[id][i].style.left;
			the_top = the_divs[id][i].style.top;
			var top_diff = the_divs_runout[i].top - parseInt(the_top) + i * 20;
			var left_diff = the_divs_runout[i].left - parseInt(the_left);
			
			if(top_diff < i * 20) {
				the_divs[id][i].style.top = parseInt(the_divs[id][i].style.top) - 1 + "px";
			} else if(top_diff > i * 20) {
				the_divs[id][i].style.top = parseInt(the_divs[id][i].style.top) + 1 + "px";
			}
			
			if(left_diff < 0) {
				the_divs[id][i].style.left = parseInt(the_divs[id][i].style.left) - 1 + "px";
			} else if(left_diff > 0) {
				the_divs[id][i].style.left = parseInt(the_divs[id][i].style.left) + 1 + "px";
			}
			
			if( parseInt(the_divs[id][i].style.top) == the_divs_runout[i].top && parseInt(the_divs[id][i].style.left) == the_divs_runout[i].left) countout++

			if(countout == the_divs[id].length) {
				clearTimeout(the_timeout[id]);
				all_menus["submenu" + id] = '';
				for(var j = 0; j < the_divs[id].length; j++) {
					the_divs[id][j].style.visibility = 'hidden';
				}
				the_divs[id] = new Array();
				anim = 1;
				document.getElementById("menu" + id).style.height = 35 + "px";
				countout = 0;
				saveMenu(id, 0);
				return true;
			}
				
		}
		countout = 0;
		the_timeout[id] = setTimeout("openMenu(this," + id + ",false);", 2);
	} else {
	
		if(arg) {
			the_divs[id] = new Array();
			count[id] = 0;
			the_list = document.getElementById("submenu" + id).childNodes;
			for(var i = 0, j = 0; i < the_list.length; i++) {
				if(the_list[i].id != undefined) {
					the_divs[id][j] = the_list[i];
					j++;
				}
			}

			for(var i = 0; i < the_divs[id].length; i++) {
				the_left = parseInt( Math.random() * 20 ) + 5;
				the_top = parseInt( Math.random() * 20 ) + 5;
				znak = (parseInt( Math.random() * 10 ) < 5) ? true : false;
				if(znak) {
					the_left = 0 - the_left;
				}
				the_divs[id][i].style.left = the_left + "px";
				
				znak = (parseInt( Math.random() * 10 ) < 5) ? true : false;
				if(znak) {
					the_top = 0 - the_top;
				}
				the_divs[id][i].style.top = i * 20 + the_top + "px";
				
				the_divs[id][i].style.visibility = 'visible';
				document.getElementById("menu" + id).style.height = 38 + (the_divs[id].length * 20) + "px";
			}
		}
		
		for(var i = 0; i < the_divs[id].length; i++) {
			the_left = the_divs[id][i].style.left;
			the_top = the_divs[id][i].style.top;
			var top_diff = parseInt(the_top) - i * 20;
			if( parseInt(top_diff) < 0 ) {
				the_divs[id][i].style.top = (parseInt(the_top) + 1) + "px";
			} else if( parseInt(top_diff) > 0 ) {
				the_divs[id][i].style.top = (parseInt(the_top) - 1) + "px";
			}
			
			if( parseInt(the_left) < 0 ) {
				the_divs[id][i].style.left = (parseInt(the_left) + 1) + "px";
			} else if( parseInt(the_left) > 0 ) {
				the_divs[id][i].style.left = (parseInt(the_left) - 1) + "px";
			}

			if( parseInt(the_divs[id][i].style.left) == 0 && parseInt(the_divs[id][i].style.top) == i * 20 ) {
				count[id]++;
				if(the_divs[id].length == count[id]) {
					clearTimeout(the_timeout[id]);
					all_menus["submenu" + id] = 'set';
					count[id] = 0;
					anim = 1;
					the_divs[id] = new Array();
					saveMenu(id, 1);
					return true;
				}
			}
		}
		count[id] = 0;
		the_timeout[id] = setTimeout("openMenu(this," + id + ",false);", 2);
	}
}

function openMenuOnLoad(id) {
	the_divs[id] = new Array();
	the_list = document.getElementById("submenu" + id).childNodes;
	for(var i = 0, j = 0; i < the_list.length; i++) {
		if(the_list[i].id != undefined) {
			the_divs[id][j] = the_list[i];
			j++;
		}
	}
	for(var i = 0; i < the_divs[id].length; i++) {
		the_divs[id][i].style.top = i * 20 + "px";
		the_divs[id][i].style.left = 0 + "px";
				
		the_divs[id][i].style.visibility = 'visible';
		document.getElementById("menu" + id).style.height = 38 + (the_divs[id].length * 20) + "px";
	}
	
	all_menus["submenu" + id] = 'set';
	the_divs[id] = new Array();
}

function loadMenu() {
	if( self.name.indexOf(';') == -1 ) {
		self.name = "news=1;forum=0;download=0;spells=0;monsters=0;items=0;quests=0;npcs=0;achs=0;gallery=0;maps=0;stats=0;support=0;ownm=0;";
	}
	var splitter = self.name.split(";");
	for(var i = 0; i < splitter.length; i++) {
		var item = splitter[i].substring(0, splitter[i].indexOf("=") );
		if( splitter[i].charAt(splitter[i].indexOf("=") + 1) == 1 ) {
			openMenuOnLoad(menus_ids[item]);
			$('#menu'+ item).css("background-position", "100% " + (i * -35) + "px");
		}
	}
}

function saveMenu(id, todo) {
	var splitter = self.name.split(";");
	splitter[id - 1] = splitter[id - 1].substring(0, splitter[id - 1].indexOf("=") + 1);
	if(todo) {
		splitter[id - 1] += '1';
	} else {
		splitter[id - 1] += '0';
	}
	self.name = splitter.join(";");
}
