var timeOutMS = 750;
var horizLevelSpace = -1;

var menuTimer = null;
var menuTimerExec = "";
var isMenuActive = 0;
function showMenu(divTagId, aTagId, menuArray, horizExpansion, level, aTopTagId, imgTopTagId)
{			
	if (aTopTagId == null)
	{
		aTopTagId = aTagId;
	}
				
	var aTag = document.getElementById(aTagId);
	var xPos = aTag.offsetLeft;
	var yPos = aTag.offsetTop;
	
	if (horizExpansion)
	{
		xPos += aTag.offsetWidth;
		xPos += horizLevelSpace;
	}
	else
	{
		yPos += aTag.offsetHeight;
	}
	
	var parentTag = aTag.offsetParent;			
	while (parentTag != null)
	{
		xPos += parentTag.offsetLeft;
		yPos += parentTag.offsetTop;
		parentTag = parentTag.offsetParent
	}

	var menuHtml = "";					
	var onMouseOverValue = "";
	var onMouseOutValue = "";
	var curStyle, tableStyle, curItemClass;
	var tdId = "";
	var tableName = "MenuTable" + level;
	eval("tableStyle = " + divTagId + "_TableStyle");
	
	if (menuArray != null)
	{
		menuHtml += " <table class='" + tableStyle + "' cellspacing='0' cellpadding='0' id='" + tableName + "'>";					
		
		for (var i=0; i<menuArray.length; i++)
		{
			tdId = aTagId + "_" + i;
			eval("curStyle = " + divTagId + "_Style[" + menuArray[i].style + "];");
			
			onMouseOverValue = "inactivateLast(" + level + ");activateMenu();hideMenu(\"" + divTagId + "\", " + (level + 1) + ");";
			if (menuArray[i].children != null)					
			{
				onMouseOverValue += "changeClass(this.id, \"" + curStyle.activeExpClass + "\"); showMenu(\"" + divTagId + "\", \"" + tdId + "\", " + menuArray[i].children + ", true, " + (level+1) + ", \"" + aTopTagId + "\");";
				curItemClass = curStyle.itemExpClass;
			}
			else
			{
				onMouseOverValue += "changeClass(this.id, \"" + curStyle.activeClass + "\");";
				curItemClass = curStyle.itemClass;
			}			

			if (imgTopTagId != null)
				onMouseOutValue = "deactivateMenu(\"" + divTagId + "\", \"" + aTopTagId + "\", \"\", \"" + imgTopTagId + "\"); setLast(this.id, \"" + curItemClass + "\", " + level + ");";
			else
				onMouseOutValue = "deactivateMenu(\"" + divTagId + "\", \"" + aTopTagId + "\", \"\"); setLast(this.id, \"" + curItemClass + "\", " + level + ");";
																		
			menuHtml += "<tr><td id='" + tdId + "' onclick='window.location=\"" + menuArray[i].url + "\"' class='" + curItemClass + "' onmouseover='" + onMouseOverValue + "' onmouseout='" + onMouseOutValue +"'>" + menuArray[i].text + "</td></tr>";
		}
		menuHtml += " </table>";
	}
	
	var divTag = document.getElementById(divTagId + level);
	
	divTag.style.zIndex = 2;
	divTag.style.left = xPos;
	divTag.style.top = yPos;
	divTag.style.position = "absolute";
	divTag.onmouseover = "activateMenu();";
	
	if (imgTopTagId != null)
		divTag.onmouseout = "deactivateMenu('" + divTagId + "', '" + aTopTagId + "', '', '" + imgTopTagId + "');";
	else
		divTag.onmouseout = "deactivateMenu('" + divTagId + "', '" + aTopTagId + "', '');";
	
	divTag.innerHTML = menuHtml;

	if (navigator.appVersion.indexOf("MSIE") != -1 && 
		navigator.userAgent.indexOf("Opera") == -1 &&
		navigator.appVersion.indexOf("Mac") == -1)
	{
		var menuTable = document.getElementById(tableName);
		showIframeLayer(divTagId, level, menuTable, xPos, yPos);
	}
}

function showIframeLayer(divTagId, level, menuTable, xPos, yPos)
{
	var frameTag = document.getElementById(divTagId + level + "Frame");
	frameTag.style.zIndex = 1;
	frameTag.style.left = xPos;
	frameTag.style.top = yPos;
	frameTag.style.position = "absolute";
	frameTag.innerHTML = "<iframe width='" + menuTable.clientWidth + "' height='" + menuTable.clientHeight + "' frameborder='0' scrolling='no' src='about:blank'>";
}

function hideMenu(divTagId, startLevel)
{
	if (startLevel == null)
		startLevel = 1;

	var level = startLevel;
	var divTag = document.getElementById(divTagId + level);
	while (divTag != null)
	{
		divTag.style.zIndex = 2;
		if (divTag.innerHTML != "")
			divTag.innerHTML = "";
		level++;
		divTag = document.getElementById(divTagId + level); 
	}			
	
	if (navigator.appVersion.indexOf("MSIE") != -1 && 
		navigator.userAgent.indexOf("Opera") == -1 &&
		navigator.appVersion.indexOf("Mac") == -1)
	{
		level = startLevel;
		var frameTag = document.getElementById(divTagId + level + "Frame");
		while (frameTag != null)
		{
			frameTag.style.zIndex = 1;			
			if (frameTag.innerHTML != "")
				frameTag.innerHTML = "";
			level++;
			frameTag = document.getElementById(divTagId + level + "Frame"); 
		}			
	}
}
		
function activateMenu()
{
	clearTimeout(menuTimer);
	isMenuActive = 1;
}

function deactivateMenu(divTagId, curTopLevelTagId, newClassName, curTopLevelImgTagId)
{
	isMenuActive = 0;
	menuTimerExec = "hideMenu('" + divTagId + "');changeClass('" + curTopLevelTagId + "', '" + newClassName + "');"; 
	if (curTopLevelImgTagId != null)
		menuTimerExec += "changeImage('" + curTopLevelImgTagId + "', false);";
	menuTimer = setTimeout(menuTimerExec, timeOutMS);
}

function resetMenu()
{
	if (menuTimerExec)
	{
		eval(menuTimerExec);
		menuTimerExec="";
	}
}

function changeClass(curTagId, newClassName)
{
	var curTag = document.getElementById(curTagId);
	if (curTag != null)
	{
		if (newClassName != null && newClassName != '')
			curTag.className = newClassName;
		else
		{
			if (curTag.getAttribute("origclass") != null)
				curTag.className = curTag.getAttribute("origclass");
		}
	}
	return false;
}

function changeImage(curTagId, isMouseOver)
{
	var curTag = document.getElementById(curTagId);
	if (curTag != null)
	{
		if (isMouseOver)
		{
			if (curTag.getAttribute("mouseoverimg") != null)
			{
				curTag.src = curTag.getAttribute("mouseoverimg");
			}
		}
		else
		{
			if (curTag.getAttribute("normalimg") != null)
				curTag.src = curTag.getAttribute("normalimg");
		}			
	}
}

var lastActiveTagId = new Array();
var lastActiveNewClassName = new Array();

function setLast(curTagId, newClassName, atLevel)
{
	lastActiveTagId[atLevel] = curTagId;
	lastActiveNewClassName[atLevel] = newClassName;
	return false;
}

function inactivateLast(newAtLevel)
{
	if (lastActiveTagId[newAtLevel] != null)
	{
		changeClass(lastActiveTagId[newAtLevel], lastActiveNewClassName[newAtLevel]);
	}
}
