
menu = new Object;

var IE = document.all;
var oldNS = document.layers;
var newNS = !document.all && document.getElementById;
var opera = window.opera;

function startMenu(strRoot)
{
	distributeMenu();
	//Set global variables
	menu.strFrameId		= "frmMenu";
	menu.strIFrameId	= "ifrMenu";
	menu.strDivId 		= "divMenu";
	menu.strParentUlId 	= "ulMenu";
	menu.strParentDivId	= "divMenu";
	menu.styleSheetUrl	= strRoot+"library/css/styleMenu.css";
	menu.strSubMenuFile	= strRoot+"library/util/subMenu.htm";

	menu.numBorder			= 1;
	menu.numMenuTop			= 0;
	menu.numMenuLeft		= 0;
	menu.numSubMenuTop		= 84;
	menu.numSubMenuHeight	= 30;

	menu.strErroMsg			= "O menu não pode ser renderizado.";

	menu.strCSSliFirst		= "liFisrt";
	menu.strCSSliFirstOver	= "liFisrtOver";
	menu.strCSSulMenu		= "ulMenu";
	menu.strCSSulChild		= "ulChild";
	menu.strCSSliChild		= "liChild";
	menu.strCSSliChildOver	= "liChildOver";
	menu.strCSSliParent		= "liParent";
	menu.strCSSliParentOver	= "liParentOver";

	//Check Menu Status and Start It or Show Error Message
	if (checkMenuStatus())
	{
		buildMenu(document.getElementById(menu.strParentUlId));
		document.getElementById(menu.strParentUlId).style.visibility = "visible";
	}
	else
	{
		var objErrorSpan = document.createElement("SPAN");
		objErrorSpan.innerHTML = menu.strErroMsg;
		objErrorSpan.className = menu.strCSSliFirst;
		
		document.getElementById(menu.strParentDivId).appendChild(objErrorSpan);
	}
	
}

//######################################################################################
//################################ CHECK MENU STATUS ###################################
//######################################################################################

function checkMenuStatus()
{
	//if (navigator.appVersion.indexOf("MSIE")==-1) return false;
	//if (document.getElementById(menu.strParentUlId == null) return false;
	//if (document.getElementById(menu.strParentUlId).childNodes.length == 0) return false;
	return true;
}

//######################################################################################
//################################### BUILD MENU #######################################
//######################################################################################

function buildMenu(ul)
{
	if (ul.childNodes.length==0) return; 
		
	for (var i=0; i<ul.childNodes.length; i++) 
	{
		var itemLI = ul.childNodes[i];
		if (itemLI.nodeName == "LI") 
		{
			numMinQtd = 1;
			if (IE) numMinQtd = 2;
			if (itemLI.childNodes.length > numMinQtd)
			{
				//Set the events for parent LI's
				itemLI.onmouseover = function () {
					this.isOver = true;
					execMenuOver(this);
					this.className = menu.strCSSliFirstOver;
				}
				itemLI.onmouseout = function () {
					this.isOver = false;
					execMenuOut(this);
					this.className = menu.strCSSliFirst;
				}
			}
			else
			{
				//Set the events for parent LI's
				itemLI.onmouseover = function () {
					this.className = menu.strCSSliFirstOver;
				}
				itemLI.onfocus = function () {
					//alert('asdsa');
				}
				itemLI.onmouseout = function () {
					this.className = menu.strCSSliFirst;
				}
			}

			//Set the className of the parent LI
			if (ul.id == menu.strParentUlId)
			{
				ul.className = menu.strCSSulMenu;
				itemLI.className = menu.strCSSliFirst;
				itemLI.id = itemLI.parentNode.id + "-" + i;
			}
			else
			{
				ul.className = menu.strCSSulChild;
				itemLI.id = itemLI.parentNode.parentNode.id + "-" + i;
				//ul.style.display = 'none';
			}
			
			//Call recursive function for each UL inside the LI
			for (var j=0; j<itemLI.childNodes.length; j++) 
			{
				var itemUL = itemLI.childNodes[j];
				if (itemUL.nodeName=="UL") 
				{
					buildMenu(itemUL);
				}
			}
		}
	}
}


//######################################################################################
//################################### MOUSE OUT EVENTS #################################
//######################################################################################

function execMenuOut(objLi)
{
	window.setTimeout(function(){execMenuOutByTime(objLi)}, 500);
}

function execMenuOutByTime(objLi)
{
	if (!objLi.isOver)
	{
		objDivMenu = document.getElementById(menu.strDivId+objLi.id);

		menu.status = false;
		check(document.getElementById(objLi.id).parentNode);
		if (!menu.status && objDivMenu != null)
		{
			menu.status = false;
			document.body.removeChild(objDivMenu);
			removeSubMenu(document.getElementById(menu.strParentUlId));
			removeParentMenu(document.getElementById(menu.strParentUlId));
		}
	}
}

function execSubMenuOut(idLiParent, idLi)
{
	window.setTimeout(function(){execSubMenuOutByTime(idLiParent, idLi)}, 500);
}

function execSubMenuOutByTime(idLiParent, idLi)
{
	
	if (document.getElementById(menu.strDivId+idLi) == null) return;

	if (idLiParent == menu.strParentDivId) //Action for First Child Only!
	{
		menu.status = false;
		check(document.getElementById(idLi).parentNode);
		if (!menu.status)
		{
			menu.status = false;
			removeSubMenu(document.getElementById(idLi).parentNode);
			removeParentMenu(document.getElementById(idLi).parentNode);
		}
		return;
	}

	if (document.getElementById(menu.strDivId+idLiParent) == null) return;

	if (! document.getElementById(menu.strDivId+idLiParent).isOver) //Action for Other Childs
	{
		menu.status = false;
		check(document.getElementById(idLi).parentNode);
		if (!menu.status)
		{
			menu.status = false;
			removeSubMenu(document.getElementById(idLi).parentNode);
			removeParentMenu(document.getElementById(idLi).parentNode);
		}
	}
	else
	{
		if (document.getElementById(menu.strDivId+idLiParent).isOver || document.getElementById(menu.strIFrameId+idLiParent).isOver)
		{
			removeSubMenu(document.getElementById(idLi).parentNode);
			removeParentMenu(document.getElementById(idLi).parentNode);
		}
	}
}

//######################################################################################
//################################### REMOVE SUBMENUS ##################################
//######################################################################################

function removeParentMenu(ul)
{
	if (ul.parentNode.id == menu.strParentDivId) return;
	if (document.getElementById(menu.strDivId+ul.parentNode.id) == null) return;
	if (document.getElementById(menu.strDivId+ul.parentNode.id).isOver) return;
	
	document.body.removeChild(document.getElementById(menu.strDivId+ul.parentNode.id));
	removeParentMenu(ul.parentNode.parentNode);
}

function removeSubMenu(ul)
{
	if (ul.childNodes.length==0) return; 
		
	for (var i=0; i<ul.childNodes.length; i++) 
	{
		var itemLI = ul.childNodes[i];
		if (itemLI.nodeName == "LI") 
		{
			if (itemLI.childNodes.length > 1)
			{
				if (document.getElementById(menu.strDivId+itemLI.id) != null)
				{
					document.body.removeChild(document.getElementById(menu.strDivId+itemLI.id));
				}
			}
			for (var j=0; j<itemLI.childNodes.length; j++) 
			{
				var itemUL = itemLI.childNodes[j];
				if (itemUL.nodeName=="UL") 
				{
					removeSubMenu(itemUL);
				}
			}
		}
	}
}

function check(ul)
{
	if (ul.childNodes.length==0);
	
	for (var i=0; i<ul.childNodes.length; i++) 
	{
		var itemLI = ul.childNodes[i];
		if (itemLI.nodeName == "LI") 
		{
			if (itemLI.childNodes.length > 1)
			{
				if (document.getElementById(menu.strDivId+itemLI.id) != null)
				{
					if (document.getElementById(menu.strDivId+itemLI.id).isOver)
					{
						menu.status = true;
						return;
					}
				}
			}
			for (var j=0; j<itemLI.childNodes.length; j++) 
			{
				var itemUL = itemLI.childNodes[j];
				if (itemUL.nodeName=="UL") 
				{
					check(itemUL);
				}
			}
		}
	}
}

//######################################################################################
//################################### BUILD DIV's AND IFRAME's #########################
//######################################################################################

function execSubMenuOver(objLi, objLiParent, numTop)
{
	if (document.getElementById(menu.strDivId+objLi.id) == null)
	{
		removeSubMenu(objLi.parentNode);
		var objDivParent = document.getElementById(menu.strDivId+objLiParent.id);
		var newDiv = document.createElement("DIV");
		newDiv.id = menu.strDivId+objLi.id;
		newDiv.style.position = "absolute";
		newDiv.style.zIndex = 900;
		if (!IE) numTop = (numTop-1)/2;
		newDiv.style.top = (parseInt(objDivParent.offsetTop) + (numTop*menu.numSubMenuHeight)) + "px";
		newDiv.style.left = (parseInt(objDivParent.offsetLeft) + parseInt(objDivParent.clientWidth) - menu.numBorder) + "px";
		newDiv.onmouseover = function () {
			newDiv.isOver = true;
		}
		newDiv.onmouseout = function () {
			newDiv.isOver = false;
		}

		var newIframe = document.createElement("iframe");
		newIframe.id = menu.strIFrameId+objLi.id;
		newIframe.style.width = "1";
		newIframe.style.height = "1";
		newIframe.frameBorder = "0";
		newIframe.src = menu.strSubMenuFile;
		newIframe.liParent = objLi.id;
		newIframe.onmouseout = function () {
			execSubMenuOut(objLi.parentNode.parentNode.id, objLi.id);
		}
	
		newDiv.appendChild(newIframe);
		document.body.appendChild(newDiv);
	}
}

//######################################################################################
//################################### MOUSE OVER EVENTS ################################
//######################################################################################

function execMenuOver(objLi)
{
	if (document.getElementById(menu.strDivId+objLi.id) == null)
	{
		removeSubMenu(objLi.parentNode);
		var newDiv = document.createElement("div");
		newDiv.id = menu.strDivId+objLi.id;
		newDiv.style.position = "absolute";
		newDiv.style.zIndex = 900;
		//newDiv.style.top = objLi.offsetTop+menu.numSubMenuTop;
		var numTop = document.getElementById(menu.strParentUlId).offsetTop + 187;
		var numLeft = document.getElementById(menu.strParentUlId).offsetLeft + Math.floor((document.body.clientWidth-970)/2);
		newDiv.style.top = numTop+menu.numSubMenuTop-1;
		if (!IE) newDiv.style.top = (numTop+menu.numSubMenuTop-1)+"px";
		newDiv.style.left = numLeft+objLi.offsetLeft+10;
		if (!IE) newDiv.style.left = (objLi.offsetLeft+numLeft+11)+"px";
		newDiv.onmouseover = function () {
			newDiv.isOver = true;
		}
		newDiv.onmouseout = function () {
			newDiv.isOver = false;
		}

		var newIframe = document.createElement("iframe");
		newIframe.id = menu.strIFrameId+objLi.id;
		newIframe.style.width = "0";
		newIframe.style.height = "0";
		newIframe.frameBorder = "0";
		newIframe.src = menu.strSubMenuFile;
		newIframe.liParent = objLi.id;
		newIframe.onmouseout = function () {
			execSubMenuOut(objLi.parentNode.parentNode.id, objLi.id);
		}
	
		newDiv.appendChild(newIframe);
		document.body.appendChild(newDiv);
	}
}

//######################################################################################
//#################################### EXEC WHEN CLICK #################################
//######################################################################################

function execMenuClick(url)
{
	alert(url);
}