var menuway					= 0;			// 0 = right, 1 = left
var currentSpanElement		= "";			// Needed to track current span element
var menuArray				= new Array();	// Tracks what divs are showing so it knows what to hide when a new span is clicked
var onTextColor				= "#000000";    // Text color with mouseover
var offTextColor			= "#000000";    // Text color with NO mouseover
var onCellColor				= "#EEEEFF";	// Span color with mouseover
var offCellColor			= "#FFFFFF";    // Span color with nomouseover
var offsetMenuX				= 255;			// X Distance new menu will be from parent menu
var offsetMenuY				= 0;			// Y Distance increase of new menu compared to parent span
var startDistanceX			= -10;			// Sets how far off onMouseOver start element we should go
var startDistanceY			= 5;			// Sets how far off onMouseOver start element we should go
var menuOn					= false;		// Only one set of menus can be displayed at once
var started					= false;		// Used to track if we just started the menus
var clickStart				= true;			// Allow an onClick event to start the menu or not here
var clickX					= 0;			// Location X of event to start menu
var clickY					= 0;			// Location Y of event to start menu
var selectCount				= 0;
var select					= "";
var appletCount				= 0;			// If we have applets, track the # so we can temporarily hide them
var applets					= "";			// Tracks the document.all.tags("applet") collection so we can reference it once rather than twice
var md						= 1000;			// Tempo para desaparecer o menu
var ti						= -1;
var xtraSpace				= 7;

function startIt(menu,thisItem,level,evt) { //menu = menu to display,thisItem=coordinates of item to use,level=current depth of menus
	clearTimeout(ti);
	var e = (window.event) ? window.event : evt;
	
	menuway=0;
	Begin(menu,thisItem,level,e.clientX,e.clientY,evt)
}

function startItXY(menu,thisItem,level,PosX,PosY,evt) {	//menu = menu to display,thisItem=coordinates of item to use,level=current depth of menus
	menuway=0;
	Begin(menu,thisItem,level,PosX,PosY,evt)
}

function Begin(menu,thisItem,level,PosX,PosY,evt) { //menu = menu to display,thisItem=coordinates of item to use,level=current depth of menus
	clearTimeout(ti);
	var e = (window.event) ? window.event : evt;
	
	if (menuOn == true) {
		e.cancelBubble = true;
		hideAllDivs();
		//return; //Only allow one menu to be activated at a time
	} //else {
		select = document.getElementsByTagName("select");
		selectCount = select.length;
		if (selectCount > 0) {
			for (i=0;i<selectCount;i++) {
				select.item(i).style.visibility = "hidden";
			}
		}
		applets = document.getElementsByTagName("applet");
		appletCount = applets.length;
		if (appletCount > 0) {
			for (i=0;i<appletCount;i++) {
				applets.item(i).style.visibility = "hidden";
			}
		}
		menuOn = true;			
		started = true; //Lets us know we're coming in for the 1st time

		clickX = PosX;
		clickY = PosY;
		
		if (clickStart) e.cancelBubble = true;		
		stateChange(menu,thisItem,level);
	//}	
}

function stateChange(menu,thisItem,level) { //menu = menu to display,thisItem=name of span item to use,level=current depth of menus

	menuway=0;
	if (currentSpanElement != thisItem.id && started != true) { //Only hit this if they changed span elements	
		if (currentSpanElement == "") currentSpanElement = thisItem.id; //Used 1st time through only	

		eItemOld = eval("document.getElementById('" + currentSpanElement + "')");
		eItemNew = eval("document.getElementById('" + thisItem.id + "')");
		eParent = eItemNew.parentNode;
		eParent.style.background = offCellColor; //Must set DIV background color or it will be transparent by default	
		
		//Turn off whatever span was turned on
		eItemOld.style.background = offCellColor;
		eItemOld.style.color = offTextColor;
		
		//Turn on new span
		eItemNew.style.background = onCellColor;
		eItemNew.style.color = onTextColor;

		currentSpanElement = thisItem.id; //Track where the last mouseover came from
	}
	
	if (menu != "") {
	
		eMenu = eval("document.getElementById('" + menu + "')");			
		eItem = eval("document.getElementById('" + thisItem.id + "')"); //Used for x,y coordinates
		hideDiv(level);
		menuArray[menuArray.length] = menu; //Tracks open menus	

		var positionX;
		var positionY;
		
		if (started) {
			positionX = 0;
			positionY = 0;

			if(thisItem.id.indexOf("Menu")!=-1)
				positionX =	eItem.parentNode.offsetLeft + startDistanceX - (document.body.scrollLeft * 0.2);	
			else
				positionX =	clickX + startDistanceX	+ document.body.scrollLeft;	//eItem.offsetLeft + startDistanceX;
			
			// Verifica se o objeto é um Calendário
			var sObject = new String(menu);
			var sEval = eval("document.getElementById('" + sObject + "_CalendarAddHoc')");			
			if(sEval!=null)	{
				positionY = clickY - 115;
			}
			else { 
				sEval = eval("document.getElementById('" + sObject + "_LinkPopupItem0')");			
				if(sEval!=null)
					positionY = 100;
				else
					positionY =	clickY + startDistanceY	+ document.body.scrollTop; //eItem.offsetTop + startDistanceY
			}
		}
		else
		{
			// Se estiver abrindo para DIREITA soma, senao diminui
			if(menuway==0)
				//positionX = eItem.parentNode.offsetLeft + offsetMenuX;
				positionX = findPosX(eItem.parentNode) + offsetMenuX;
			else
				//positionX = eItem.parentNode.offsetLeft - offsetMenuX;
				positionX = findPosX(eItem.parentNode) - offsetMenuX;
			positionX -= xtraSpace;
			
			//positionY = eItem.parentNode.offsetTop + eItem.offsetTop + offsetMenuY + document.body.scrollTop + 4;
			positionY = findPosY(eItem.parentNode) + eItem.offsetTop + offsetMenuY + document.body.scrollTop + 4;
		}

		//If screen isn't wide enough to fit menu, bump menu back to the left some
		if ((positionX + offsetMenuX) >= document.body.clientWidth) {

			if(level==0) {
				positionX -= (offsetMenuX * (level + 1));
				positionX += (document.body.clientWidth - eItem.parentNode.offsetLeft);
				
			} else {
				positionX -= (offsetMenuX * 2);
			}
			positionX += (xtraSpace * 2);
			
			// informa que agora começa a diminuir
			menuway=1;
		}

		//If the menu is too far to the left to display, bump it to the right some
		if(positionX < eMenu.offsetWidth) {

			if(level==0) 
				positionX += (offsetMenuX * (level + 1));
			else
				positionX += (offsetMenuX * level);
			
			positionX -= xtraSpace;
			
			// informa que agora começa a aumentar
			menuway=0;

		}

		//If the menu is too far down, bump the menu up to the bottom equals the body clientHeight property
		if ((positionY + eMenu.offsetHeight) >= document.body.clientHeight) {
			if (started != true) positionY = document.body.clientHeight - eMenu.offsetHeight;
		}
	
		eMenu.style.left = positionX;
		eMenu.style.top  = ( level == 0 ? findPosY(thisItem) + 18 : positionY);

		//
		/*
		var sCloser = new String(menu);
		sCloser += "_closer";
		
		document.getElementById(sCloser).style.display = 'block';
		document.getElementById(sCloser).style.left    = findPosX(thisItem) - 18;
		document.getElementById(sCloser).style.top     = findPosY(thisItem) - 2;
		*/
		//eMenu.style.z-index = level;									//Only use this if we don't reverse the arrays in the ASP/XML Script
		eMenu.style.display="block";
	}
	
	started = false;												//After 1st menu, turn of started variable
}

function findPosX(obj) {
	var curleft = 0;
	if(obj.offsetParent)
		while(1) {
  			curleft += obj.offsetLeft;
  			if(!obj.offsetParent)
  				break;
			obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if(obj.offsetParent)
		while(1) {
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

function iniciaHide() { ti = setTimeout('hideAllDivs()',md); }
function finalizaHide() { clearTimeout(ti); }

function hideDiv(currentLevel) {
		for (var i=currentLevel;i<menuArray.length;i++) {
			var arrayString = new String(menuArray[i]);
			if (arrayString == "undefined") continue;

			eval("document.getElementById('" + menuArray[i] + "').style.display='none'");
		}
		menuArray.length = currentLevel;
}

function hideAllDivs() {
	if (menuOn == true) { //Don't loop through document elements if they clicked a hyperlink since it wastes time
		for (var i=0;i<menuArray.length;i++) {
			var arrayString = new String(menuArray[i]);
			if (arrayString == "undefined") continue;

			document.getElementById(menuArray[i]).style.display = "none";
			//document.all(menuArray[i]).style.left = 0;
			//document.all(menuArray[i]).style.top = 0;
			
			// Desliga o objeto que esta em baixo do menu
			//if(menuArray[i].indexOf("Menu")!=-1) {
				//eval("document.getElementById('" + menuArray[i] + "_closer').style.display='none'");
			//}
		}
		if (currentSpanElement != "") { //No currentSpanElement if they haven't mousedOver any span element
			eItem = eval("document.getElementById('" + currentSpanElement + "')");
			eItem.style.background = offCellColor; //Ensure current span's color is changed back to "off" color
			eItem.style.color = offTextColor; //Ensure current span's text color is changed back to "off" color
			menuArray = new Array();	
			currentSpanElement = "";		
		}
		if (selectCount > 0) {
			for (i=0;i<selectCount;i++) {
				select.item(i).style.visibility = "visible";
			}
			selectCount = 0;
			select = "";
		}
		if (appletCount > 0) {
			for (i=0;i<appletCount;i++) {
				applets.item(i).style.visibility = "visible";
			}
			appletCount = 0;
			applets = "";
		}
	}
	menuOn = false; //Menus off, so set this to false
}

document.onclick = hideAllDivs;