var NS4_MT = ((document.layers) || (browser().indexOf("ie") < 0)) ? true : false;
var IE4_MT = !NS4_MT && ((document.getElementById) ? true : false);

var style_MT = true; //((NS4_MT && document.test) || IE4_MT) ? 1 : 0;
var padding_MT = 0; // < 4 recommended
var borWid_MT = null; // for no border, assign null
var borCol_MT = "white";
var borSty_MT = "solid";
var nTimout_MT = 0; // seconds
var bottomConstraint = 440;
var itemHeight = 27;

var str = "<style TYPE='text/css'>";
str += ".menutip {";
str += "position: absolute;";
str += "visibility: hidden;";
str += "left: 0; top: 0;";
if (borWid_MT > 0)
{ // if a border is specified
	str += "border-width: " + borWid_MT + ";";
	str += "border-color: " + borCol_MT + ";";
	str += "border-style: " + borSty_MT + ";";
}
if (NS4_MT)
{
	if (borWid_MT > 0 && padding_MT <= 3)
	{
		str += "padding: 0;";
	}
	else if (borWid_MT > 0 && padding_MT > 3)
	{
		str += "padding: " + (padding_MT - 3) + ";";
	}
	else if (borWid_MT == 0)
	{
		str += "padding: " + padding_MT + ";";
	}
}
else
{
	str += "padding: " + padding_MT + ";";
}
str += "}";
str += "</style>";

if (style_MT)
{
	document.write(str);
	if (NS4_MT)
		window.onload = init_MT;
}

function init_MT()
{
	setTimeout("window.onresize = redo_MT", 1000);
}

function redo_MT()
{
	window.location.reload();
}

function oMenu(strID, parentID, nX, nY, bPersistent, numItems)
{
	this.strID = strID; // id of menu
	this.parentID = parentID; // parent id
	this.nX = nX; // x location (0 uses mouse position)
	this.nY = nY; // y location (0 uses mouse position)
	this.bPersistent = bPersistent; // keep it up or have it go away
	this.numItems = numItems;
	this.bActive = false; // active ?
	this.nTimeout = 0; // timeout delay
}
var aMenus = new Array();

function SetTimeout(id, nTimeoutVal)
{
	var oTimoutObj = FindMenuObject(id);
	if (oTimoutObj)
		oTimoutObj.nTimeout = nTimeoutVal;
}

function SetPersistent(id, bPersistentVal)
{
	var oPersistentObj = FindMenuObject(id);
	if (oPersistentObj)
		oPersistentObj.bPersistent = bPersistentVal;
}

function FindMenuObject(strID)
{
	if (String(strID) == "undefined")
		return null;

	for (var iMenu = 0; iMenu < aMenus.length; ++iMenu)
	{
//		alert(aMenus[iMenu].strID + " : " + strID);
		if (aMenus[iMenu].strID == strID)
			return aMenus[iMenu];
	}
	return null;
}

function makeMenu(id, parentID, code, x, y, bPersistent, level, numItems)
{
	if (!style_MT)
		return;
	if (!level) level = 0;
	// force it to boolean
	bPersistent = bPersistent ? true : false;

	aMenus[aMenus.length] = new oMenu(id, parentID, x, y, bPersistent, numItems);
//alert(id+","+parentID+","+code+","+x+","+y+","+bPersistent+","+level)
		
	var str = "<style TYPE='text/css'>";
	str += "#" + id + " {";
	str += "}";
	str += "</style>";
	str += "<DIV CLASS='menutip' ID='" + id + "' align='left' style='z-index: " + (level + 10) + ";'>" + 
		unescape(code) + "</DIV>";
	document.write(str);
//alert(str)
}

function activateMenu(id, event) 
{
//alert("-- " + NS4_MT + " : " + IE4_MT + " --")
	if (!event) // ie compatibility
		event = window.event;
//alert(event)

	var oActiveMenu = FindMenuObject(id);
//alert(id + " : " + oActiveMenu)

	// Clear any existing...
//	clearMenu();

	if (!oActiveMenu)
		return;
	if (!(NS4_MT || IE4_MT))
		return;
	if (!style_MT)
		return;
	if (!document.getElementById(oActiveMenu.strID))
		return;

	if (!oActiveMenu.bActive)
	{
//alert("test1")
		oActiveMenu.bActive = true;
		oActiveMenu.nTimeout = nTimout_MT;
	
		var whichMenu = document.getElementById(oActiveMenu.strID).style;
		if (whichMenu)
		{
//alert("test2")
			if (event & (!oActiveMenu.nX || (oActiveMenu.nX == 0))) // cursor location
				whichMenu.left = (NS4_MT) ? event.pageX : event.clientX + document.body.scrollLeft;
			else
				whichMenu.left = oActiveMenu.nX;
//alert(whichMenu.left)
				
			var newTop;
//alert(event)	
			if (event && (!oActiveMenu.nY || (oActiveMenu.nY == 0))) // cursor location
				newTop = (NS4_MT) ? event.pageY - 10: event.clientY + document.body.scrollTop - 10;
			else
				newTop = oActiveMenu.nY;
//alert(newTop)	
			if ((newTop + (oActiveMenu.numItems * 27)) > bottomConstraint)
				newTop = bottomConstraint - (oActiveMenu.numItems * 27);
			whichMenu.top = newTop;
			whichMenu.visibility = "visible";
		}
	}
}

function clearMenu(idToClear, aoIdsNotToClear) 
{
	var oClearMenu = idToClear ? FindMenuObject(idToClear) : null;

	var aoNotClearMenus = null;
	if (aoIdsNotToClear)
	{
		aoNotClearMenus = new Array();
		for (var iNotClearItems = 0; iNotClearItems < aoIdsNotToClear.length; ++iNotClearItems)
		{
			var oTmp = FindMenuObject(aoIdsNotToClear[iNotClearItems]);
			if (oTmp)
				aoNotClearMenus[aoNotClearMenus.length] = oTmp;
		}
	}		

	if (!style_MT) // ???
		return;

	// ID valid on page?	
	if (oClearMenu && !document.getElementById(oClearMenu.strID))
		return;
	// todo: check for valid 'not clears'

	// ignore if a menu was stated and it is not active
	if (oClearMenu && !oClearMenu.bActive)
		return;
	// todo: check for non-active 'not clears'

	if (oClearMenu && oClearMenu.bActive)
	{
			var whichMenu = document.getElementById(oClearMenu.strID).style;
			if (whichMenu)
				whichMenu.visibility = (NS4_MT) ? "hide" : "hidden";
			oClearMenu.bActive = false;
	}

	// Add parent heirarchy to array of 'not clears'.
	if (aoNotClearMenus)
	{
		var oTmp = aoNotClearMenus[0];
		while (oTmp != null) 
		{
//alert(oTmp.strID + "," + oTmp.parentID)
			oTmp = FindMenuObject(oTmp.parentID);
			if (oTmp)
				aoNotClearMenus[aoNotClearMenus.length] = oTmp;
		}
//DisplayObjectElements(aoNotClearMenus, true)
	}

	if (!oClearMenu)
	{
		// Go through all menu items and adjust based on properties.
		for (var iMenu = 0; iMenu < aMenus.length; ++iMenu)
		{
			// skip menu(s) set not to clear
			if (aoNotClearMenus)
			{
				var bFound = false;
				for (var iItem = 0; iItem < aoNotClearMenus.length; ++iItem)
				{
//alert(aMenus[iMenu].strID + "==" + aoNotClearMenus[iItem].strID)
					if(aMenus[iMenu].strID == aoNotClearMenus[iItem].strID)
					{
						bFound = true;
						break;
					}
				}
				if (bFound)
					continue; // found in not to clear list
			}
				
			// clear the menu
			aMenus[iMenu].bActive = false;
			var whichMenu = document.getElementById(aMenus[iMenu].strID).style;
//if (NS4_MT) 
//	alert("-- " + document[aMenus[iMenu].strID] + " --")
			if (whichMenu)
				whichMenu.visibility = "hidden";
		}
	}
}

function CheckToClear()
{
// TODO: This needs to drill to bottom levels first...

	for (var iMenu = 0; iMenu < aMenus.length; ++iMenu)
	{
		if (aMenus[iMenu].bActive && !aMenus[iMenu].bPersistent)
		{
			if (aMenus[iMenu].nTimeout > 0)
				--aMenus[iMenu].nTimeout;
			else
				clearMenu(aMenus[iMenu].strID);
		}
	}
}
setInterval("CheckToClear()", 3000);