var menuWindowWidth = 800;
var menuWindowHeight = 600;
var topMenuTimeout = null;
var topMenuShowTimeout = null;

function showSubMenuTimeout(id, fromParentElement, submenuPosition) {
	clearTimeout(topMenuTimeout);
	clearTimeout(topMenuShowTimeout);
	topMenuShowTimeout = window.setTimeout("showSubMenu(" + id + ", window.document.getElementById('" + fromParentElement.id + "'), '" + submenuPosition + "')", 200);
}

function showSubMenu(id, fromParentElement, submenuPosition)	{
	clearTimeout(topMenuTimeout);
	var element = window.document.getElementById("subMenu_" + id);
	//var elementIframe = window.document.getElementById("subMenuIframe_" + id);
	hideSubMenuRecursive(fromParentElement);

	if(element) {
		element.style.display = "block"; // Element, ktorý rozbalím (UL)
		//elementIframe.style.display = "block"; // IFRAME, ktorý prekryje formulárové elementy, ako je napríklad select - BUG v IE

		if(submenuPosition == "R") { // Ak sa má zobraziť menu vpravo
			var leftPosition	= parseInt(fromParentElement.offsetWidth); // Left pozícia z šírky materského elementu
			var topPosition		= parseInt(fromParentElement.offsetHeight); // Height pozícia z výšky materského elementu
			
			//alert(leftPosition + " " + topPosition);
			element.style.left	= (leftPosition - 5) + "px"; // Síce má element (UL) absolútnu pozíciu, v zozname znamená 0px tam, kde začína materský element
			element.style.top	= (fromParentElement.offsetTop + 3) + "px"; 
			
			// Kontrola, či náhodou nebude menu zachádzať za obrazovku a tak by vznikli rolovátka
			var offsetLeftPosition = recursiveLeftPosition(element);
			var elementWidth = element.offsetWidth;
			if((offsetLeftPosition + elementWidth) > menuWindowWidth) {
				element.style.left	= (5 - elementWidth) + "px";
				//elementIframe.style.left = (5 - elementWidth) + "px";
			} else {
				//elementIframe.style.left = (leftPosition - 5) + "px";
			}
			
			//elementIframe.style.top = (fromParentElement.offsetTop + 3) + "px";			
		}
		
		try {
			// Všetkým elementom LI nastavým rovnakú šírku ako má materský UL
			var elementWidth = element.offsetWidth;
			var childNodes = element.childNodes;
			for(var i = 0; i < childNodes.length; i++) {
				if(childNodes[i].tagName == "LI") {
					childNodes[i].style.width = (elementWidth - 2) + "px";
				}
			}
			
			//elementIframe.style.width = elementWidth + "px";
			//elementIframe.style.height = element.offsetHeight + "px";
		} catch(e) {
			
		}		
	}
}

// Rekurzívne skrytie menu
function hideSubMenuRecursive(element) {
	if(element.tagName == "UL") {
		var thisSiblingNodes = element.childNodes;
	} else {
		var thisSiblingNodes = element.parentNode.childNodes;
	}
	var thisSiblingChildNodes;
	for(var i = 0; i < thisSiblingNodes.length; i++) {
		if(thisSiblingNodes[i].tagName == "LI") {
			if(thisSiblingNodes[i] != element) {
				thisSiblingChildNodes = thisSiblingNodes[i].childNodes;
				for(var y = 0; y < thisSiblingChildNodes.length; y++) {
					if(thisSiblingChildNodes[y].tagName == "UL") {
						if(thisSiblingChildNodes[y].style.display != "none") {
							hideSubMenuRecursive(thisSiblingChildNodes[y]);
							thisSiblingChildNodes[y].style.display = "none";
						}
					} /*else if(thisSiblingChildNodes[y].tagName == "IFRAME") {
						thisSiblingChildNodes[y].style.display = "none";
					}*/
				}
			}
		}
	}
}

// Kompletné skrytie menu pomocou setTimeout, aby to nebolo okamžite ale s nejakým oneskorením
function hideSubMenu(menuElement)	{
	topMenuTimeout = window.setTimeout("hideSubMenuRecursive(window.document.getElementById('" + menuElement + "'))", 800);
}

// Pozícia elementu rekurzívne pomocou offset elementov
function recursiveLeftPosition(element) {
	var leftPosition = element.offsetLeft;
	if(element.offsetParent) {
		leftPosition = leftPosition + recursiveLeftPosition(element.offsetParent);
	}
	return leftPosition;
}

function getWindowSize() {
	var getWindowSize = new Array();
	var myWidth = 785, myHeight = 550;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth - 1;
		myHeight = window.innerHeight - 1;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	getWindowSize['width'] = myWidth;
	getWindowSize['height'] = myHeight;
	
	menuWindowWidth = myWidth;
	menuWindowHeight = myHeight;
	//var debugElement = window.document.getElementById("debugDiv");
	//debugElement.innerHTML = menuWindowWidth + " x " + menuWindowHeight;
	//return 	getWindowSize;
}

DOM.aE(window, "load", getWindowSize, true);
DOM.aE(window, "resize", getWindowSize, true);

