// MENU.JS

var sMenuHTML;

var sColor;
var sBkColor;
var sSepColor;
var sTextColor;
var sTableWidth;

var nLeftMenu = 0;
var nTopMenu = 0;
var nRightMenu = 0;
var nBottomMenu = 0;

var oLastMenu = null;

function setTableWidth(sNew)
{
    sTableWidth = sNew;
}

function setColor(sNew)
{
    sColor = sNew;
}

function setBkColor(sNew)
{
    sBkColor = sNew;
}

function setSepColor(sNew)
{
    sSepColor = sNew;
}

function setTextColor(sNew)
{
    sTextColor = sNew;
}

function initMenu()
{
    sMenuHTML = "<TABLE WIDTH=" + sTableWidth + " CELLSPACING=0 CELLPADDING=0 BGCOLOR=#" + sBkColor + " BORDER=0><TR><!-- MENU_CORE --><TD STYLE=\"BORDER-RIGHT: #" + sColor + " 1px solid; BORDER-TOP: #" + sColor + " 1px solid; BORDER-BOTTOM: #" + sColor + " 1px solid\">&nbsp;</TD></TR></TABLE><!-- SUB_CORE -->";
}

function addItem(sID, nWidth, sText, sTitle, sLoc, sAltLoc, fFirst)
{
	var sItem = "<!-- ITEM_" + sID + " -->";

    if (isIE || isNS6)
	{
		sItem += "<TD ID='" + sID + "'WIDTH=" + nWidth + " STYLE=\"PADDING-LEFT: 4px; PADDING-RIGHT: 4px; PADDING-TOP: 2px; PADDING-BOTTOM: 2px; ";

        if (fFirst)
            sItem += "BORDER-LEFT: #" + sColor + " 1px solid; ";
            
        sItem += "BORDER-RIGHT: #" + sSepColor + " 1px solid; BORDER-TOP: #" + sColor + " 1px solid; BORDER-BOTTOM: #" + sColor + " 1px solid\" onmouseover=\"";

        if (sLoc)
			sItem += "hideAllMenus()\" ";
		else
			sItem += "showSubMenu('" + sID + "')\" ";
        
        sItem += "><A TARGET='_top' STYLE=\"text-decoration: none; color: #" + sTextColor + "; cursor: hand; z-index:100\" ";
		
        if (sLoc)
			sItem += "HREF='" + sLoc + "' ";
		else if (sAltLoc)
			sItem += "HREF='" + sAltLoc + "' ";

        if (sTitle)
			sItem += "TITLE='" + sTitle + "' ";
		
		sItem += ">" + sText + "</A></TD>";
	}
	else if (isNS)
	{
		sItem += "<ILAYER name=" + sID + "><A TARGET='_top' HREF='.' STYLE='text-decoration: none; color: #" + sTextColor + "; cursor: hand; z-index:100' onmouseover=\"showSubMenu('" + sID + "')\" onclick=\"return false;\">&nbsp;";
		sItem += sText;
		sItem += "&nbsp;&nbsp;|</A></ILAYER>";
	}

	sItem += "<!-- MENU_CORE -->";

	sMenuHTML = sMenuHTML.replace("<!-- MENU_CORE -->", sItem);
}

function addSubItem(sID, sText, sTitle, sLoc, nState)
{
	var sItem = "";

	sLookup = "<!-- ITEM_NEXT_" + sID + " -->";

    if (isIE || isNS6)
	{
    	if (sMenuHTML.indexOf(sLookup) == -1)
        {
		    sItem += "<DIV ID='" + sID + "submenu' onmouseout=operaHide() STYLE='position:absolute; visibility: hidden; z-index:100'>";
		    sItem += "<TABLE BORDER='0' BGCOLOR=#" + sBkColor + " WIDTH=200 CELLSPACING=0 CELLPADDING=0>";
            sItem += sLookup;
		    sItem += "</TABLE></DIV><!-- SUB_CORE -->";
    		
            sMenuHTML = sMenuHTML.replace("<!-- SUB_CORE -->", sItem);
        }

        sItem = "<TR><TD STYLE=\"PADDING-LEFT: 4px; PADDING-RIGHT: 4px; PADDING-TOP: 2px; PADDING-BOTTOM: 2px; BORDER-LEFT: #" + sColor + " 1px solid; BORDER-RIGHT: #" + sColor + " 1px solid; ";

        if (nState == -1)
            sItem += "BORDER-BOTTOM: #" + sColor + " 1px solid; ";
        else if (nState == 1)
            sItem += "BORDER-BOTTOM: #" + sSepColor + " 1px solid; BORDER-TOP: #" + sColor + " 1px solid; ";
        else
            sItem += "BORDER-BOTTOM: #" + sSepColor + " 1px solid; ";

        sItem += "\"><A TARGET='_top' STYLE='text-decoration: none; color: #" + sTextColor + "; cursor: hand; z-index:100' TITLE='" + sTitle + "' HREF='" + sLoc + "'>" + sText + "</A></TD></TR>";
	    sItem += sLookup;
	}
    else if (isNS)
	{
    	if (sMenuHTML.indexOf(sLookup) == -1)
        {
		    sItem += "<LAYER ID='" + sID + "submenu' visibility=hide BGCOLOR=#" + sBkColor + ">";
		    sItem += "<TABLE BORDER='0' BGCOLOR=#" + sBkColor + " WIDTH=200>";
		    sItem += sLookup;
            sItem += "</TABLE></LAYER><!-- SUB_CORE -->";

		    sMenuHTML = sMenuHTML.replace("<!-- SUB_CORE -->", sItem);
        }

        sItem = "<TR><TD><A TARGET='_top' STYLE='text-decoration: none; color: #" + sTextColor + "; cursor: hand; z-index:100' TITLE='" + sTitle + "' HREF='" + sLoc + "'>" + sText + "</A></TD></TR>";
	    sItem += sLookup;
    }

	sMenuHTML = sMenuHTML.replace(sLookup, sItem);
}

function showMenu()
{
	document.writeln(sMenuHTML);
}

function showSubMenu(sID)
{
	var oSrc;
	var oDrop;
    var oParent;
	
    if (isIE)
    {
		oSrc = document.all[sID];
        oDrop = document.all[sID + "submenu"];

        var x = oSrc.offsetLeft;
        var y = oSrc.offsetTop;

        oParent = oSrc.offsetParent;
        y += oParent.offsetHeight;

        do {
            x += oParent.offsetLeft;
            y += oParent.offsetTop;

            oParent = oParent.offsetParent;
        } while (oParent);

		oDrop.style.left = x;
		oDrop.style.top = y + 1;
		oDrop.style.visibility = "visible";
		
        if (oLastMenu && oLastMenu != oDrop)
            hideAllMenus();

		nLeftMenu = oDrop.style.posLeft;
		nRightMenu = nLeftMenu + oDrop.offsetWidth;

		nTopMenu = oDrop.style.posTop + oDrop.offsetHeight;
		nBottomMenu = nTopMenu;
	}
    else if (isNS6)
    {
		oSrc = document.getElementById(sID);
        oDrop = document.getElementById(sID + "submenu");

        var x = oSrc.offsetLeft;
        var y = oSrc.offsetTop;

        oParent = oSrc.offsetParent;
        y += oParent.offsetHeight;

        do {
            x += oParent.offsetLeft;
            y += oParent.offsetTop;

            oParent = oParent.offsetParent;
        } while (oParent);

		oDrop.style.left = x;
		oDrop.style.top = y + 1;
		oDrop.style.visibility = "visible";

        if (oLastMenu && oLastMenu != oDrop)
            hideAllMenus();

		nLeftMenu = parseInt(oDrop.style.left);
		nRightMenu = nLeftMenu + parseInt(oDrop.offsetWidth);

		nTopMenu = parseInt(oDrop.style.top) + parseInt(oDrop.offsetHeight);
		nBottomMenu = nTopMenu;
	}
    else if (isNS)
	{
		oDrop = document.layers[sID + "submenu"];
		if (oLastMenu && oLastMenu != oDrop)
            hideAllMenus();
		
        oDrop.left = document.layers[sID].pageX;
		oDrop.top  = document.layers[sID].pageY + 21;
	    oDrop.visibility = isNS6 ? "show" : "visible";

		nLeftMenu  = document.layers[sID + "submenu"].left;
		nRightMenu = nLeftMenu + document.layers[sID + "submenu"].clip.WIDTH;
		nTopMenu  = document.layers[sID + "submenu"].top + document.layers[sID + "submenu"].clip.height;
		nBottomMenu = nTopMenu;
	}

	oLastMenu = oDrop;
}

function hideAllMenus()
{
    if (oLastMenu)
        oLastMenu.style.visibility = isIE || isNS6 ? "hidden" : "hide";
}

function updateMenu(oElement)
{
	if (isIE && !isO6)
	{
		var x = window.event.clientX;
		var y = window.event.clientY;

		if (x > nRightMenu || x < nLeftMenu || y > nBottomMenu)
            hideAllMenus();
	}
    else if (isNS || isNS6)
	{
		var x = oElement.pageX;
		var y = oElement.pageY;

		if (x > nRightMenu || x < nLeftMenu || y > nBottomMenu)
            hideAllMenus();
	}
}

function operaHide()
{
    if (isO6 && !MainTable.contains(event.toElement))
        hideAllMenus();
}

if (isIE || isNS6)
{
	document.body.onclick = hideAllMenus;
	document.body.onscroll = hideAllMenus;
	document.body.onmousemove = updateMenu;
}

if (document.layers)
{
	window.captureEvents(Event.MOUSEMOVE);
	window.captureEvents(Event.CLICK);
	window.onmousemove = updateMenu;
	window.onclick = hideAllMenus;
}
