if (document.all)    {n=0;ie=1;fShow="visible";fHide="hidden";}
if (document.layers) {n=1;ie=0;fShow="show";   fHide="hide";}

window.onerror=new Function("return true")
////////////////////////////////////////////////////////////////////////////
// function Menu()                                                        //
////////////////////////////////////////////////////////////////////////////
rightX = 0;
var lastMenu = null;
var lastX = 0;
var lastY = 0;

function Menu()
{
	this.bgColor     = "#000033";
	if (ie) this.menuFont = "bolt xx-small Verdana";
	if (n)  this.menuFont = "bolt x-small Verdana";
    //this.fontColor   = "silver";

	this.addItem    = addItem;
	this.addSubItem = addSubItem;
	this.showMenu   = showMenu;
	this.mainPaneBorder = 1;
	this.subMenuPaneBorder = 1;

	this.subMenuPaneWidth = 150;
	this.subMenuPaneWidthList = new Object();

	lastMenu = null;

	rightY = 0;
	leftY = 0;
	leftX = 0;

	HTMLstr = "";
	HTMLstr += "<!-- MENU PANE DECLARATION BEGINS -->\n";
	HTMLstr += "\n";
	if (ie) HTMLstr += "<div id='MainTable' style='position:relative; left:0; top: 3;'>\n";

	if (n)  HTMLstr += "<layer name='MainTable'>\n";
    //IIC: uncommented the above line (why was it commeted out when it handles the case of Netscape?
    //IIC: changed table width from 100% to 97% to accomodate longer submenu than main menu for the last item
	HTMLstr += "<table width='93%' bgcolor='"+this.bgColor+"' border='"+this.mainPaneBorder+"'>\n";

	HTMLstr += "<tr>";
	if (n) HTMLstr += "<td>&nbsp;";
	HTMLstr += "<!-- MAIN MENU STARTS -->\n";
	HTMLstr += "<!-- MAIN_MENU -->\n";
	HTMLstr += "<!-- MAIN MENU ENDS -->\n";
	if (n) HTMLstr += "</td>";
	HTMLstr += "</tr>\n";
	HTMLstr += "</table>\n";
	HTMLstr += "\n";
	HTMLstr += "<!-- SUB MENU STARTS -->\n";
	HTMLstr += "<!-- SUB_MENU -->\n";
	HTMLstr += "<!-- SUB MENU ENDS -->\n";
	HTMLstr += "\n";
	if (ie) HTMLstr+= "</div>\n";

    //IIC: uncommented the next line; it handles the case of Netscape; why was it commented out at all??
	if (n)  HTMLstr+= "</layer>\n";

	HTMLstr += "<!-- MENU PANE DECALARATION ENDS -->\n";
}

function addItem(idItem, text, hint, location, altLocation, iAdjustment, width)
{
	var Lookup = "<!-- ITEM "+idItem+" -->";
	if (HTMLstr.indexOf(Lookup) != -1)
	{
		alert(idParent + " already exist");
		return;
	}
	var MENUitem = "";
	MENUitem += "\n<!-- ITEM "+idItem+" -->\n";
	if (n)
	{
		MENUitem += "<ilayer name="+idItem+">";
		MENUitem += "<a href='.' class=clsMenuItemNS onmouseover=\"displaySubMenu('"+idItem+"')\" onclick=\"return false;\">";
		MENUitem += "|&nbsp;";
		MENUitem += text;
		MENUitem += "</a>";
		MENUitem += "</ilayer>";
	}
	if (ie)
	{
		MENUitem += "<td>\n";

        //IIC: added "left: 5" to move the text to the right of the left cell border
		MENUitem += "<div id='"+idItem+"' style='position:relative; left: 5; font: "+this.menuFont+";'>\n";

		MENUitem += "<a ";
		MENUitem += "class=clsMenuItemIE ";
        //MENUitem += "style='text-decoration: none; font: "+this.menuFont+"; color: "+this.fontColor+"; cursor: hand;' ";
		if (hint != null)
			MENUitem += "title='"+hint+"' ";
		if (location != null)
		{
			MENUitem += "href='"+location+"' ";
			MENUitem += "onmouseover=\"hideAll()\" ";
		}
		else {
			if (altLocation != null)
				MENUitem += "href='"+altLocation+"' ";
			else
				MENUitem += "href='.' ";
			MENUitem += "onMouseOver=\"LogEvent('a over'); displaySubMenu('"+idItem+"'); setDocumentEvents( false );\" ";

            //IIC: added more events
			MENUitem += "onMouseMove=\"LogEvent('a move');\" ";
			MENUitem += "onMouseOut=\"LogEvent('a out'); setDocumentEvents( true );\" ";

			MENUitem += "onclick=\"return false;\" "
		}
		MENUitem += ">";
		MENUitem += "\n";

        //IIC: indent the text with 2 spaces, but commented out because of another change
		MENUitem += /*"&nbsp;&nbsp;" +*/ text;

		MENUitem += "</a>\n";
		MENUitem += "</div>\n";
		MENUitem += "</td>\n";
	}
	MENUitem += "<!-- END OF ITEM "+idItem+" -->\n\n";
	MENUitem += "<!-- MAIN_MENU -->\n";

    this.subMenuPaneWidthList[ idItem ] = width;
    
	HTMLstr = HTMLstr.replace("<!-- MAIN_MENU -->\n", MENUitem);
}

function addSubItem(idParent, text, hint, location, target)
{
	var MENUitem = "";
	Lookup = "<!-- ITEM "+idParent+" -->";
	if (HTMLstr.indexOf(Lookup) == -1)
	{
		alert(idParent + " not found");
		return;
	}
	Lookup = "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->";
	if (HTMLstr.indexOf(Lookup) == -1)
	{
	    width = ( this.subMenuPaneWidthList[ idParent ] != null 
	              ? this.subMenuPaneWidthList[ idParent ]
	              : this.subMenuPaneWidth );
		if (n) {
			MENUitem += "\n";
			MENUitem += "<layer id='"+idParent+"submenu' visibility=hide bgcolor='"+this.bgColor+"'>\n";
			MENUitem += "<table border='"+this.subMenuPaneBorder+"' bgcolor='"+this.bgColor+"' width="+width+">\n";
			MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
			MENUitem += "</table>\n";
			MENUitem += "</layer>\n";
			MENUitem += "\n";
		}
		if (ie) {
			MENUitem += "\n";
			MENUitem += "<div id='"+idParent+"submenu' style='position:absolute; visibility: hidden; width: "+this.subMenuPaneWidth+"; font: "+this.menuFont+"; top: -300;'>\n";
			MENUitem += "<table border='"+this.subMenuPaneBorder+"' bgcolor='"+this.bgColor+"' width="+width+">\n";
			MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
			MENUitem += "</table>\n";
			MENUitem += "</div>\n";
			MENUitem += "\n";
		}
		MENUitem += "<!-- SUB_MENU -->\n";
		HTMLstr = HTMLstr.replace("<!-- SUB_MENU -->\n", MENUitem);
	}

	Lookup = "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
	if (n)  MENUitem = "<tr><td><a class=clsMenuItemNS title='"+hint+"' href='"+location+"'" + (target != null ? " target='" + target + "'" : "") + ">"+text+"</a><br></td></tr>\n";
	if (ie) MENUitem = "<tr><td><a class=clsMenuItemIE title='"+hint+"' href='"+location+"'" + (target != null ? " target='" + target + "'" : "") + ">"+text+"</a><br></td></tr>\n";
	MENUitem += Lookup;
	HTMLstr = HTMLstr.replace(Lookup, MENUitem);

}

function showMenu()
{
	document.writeln(HTMLstr);

// IIC: for debugging (reverse engineering purposes), write the HTML string to another window
    if (false) {
	    var w = window.open("about:blank");
	    w.document.writeln( HTMLstr );
	    w.document.close();
	}
}

////////////////////////////////////////////////////////////////////////////
// Private declaration
function displaySubMenu(idMainMenu)
{
	var menu;
	var submenu;
	if (n)
	{
		submenu = document.layers[idMainMenu+"submenu"];
		if (lastMenu != null && lastMenu != submenu) hideAll();
		submenu.left = document.layers[idMainMenu].pageX;
		submenu.top  = document.layers[idMainMenu].pageY + 25;
		submenu.visibility = fShow;

		leftX  = document.layers[idMainMenu+"submenu"].left;
		rightX = leftX + document.layers[idMainMenu+"submenu"].clip.width;
		leftY  = document.layers[idMainMenu+"submenu"].top+
			document.layers[idMainMenu+"submenu"].clip.height;
		rightY = leftY;
	}
	else if (ie) {
		menu = eval(idMainMenu);
		submenu = eval(idMainMenu+"submenu.style");

        // IIC: added "- 21" to move the submenu to the left
		submenu.left = calculateSumOffset(menu, 'offsetLeft') - 11;
        // IIC: if id = aus (first submenu, move to the right
        //if (idMainMenu = 'aus') {
        //    submenu.left = parseInt( submenu.left ) + 8;
        //}

        // submenu.top  = calculateSumOffset(menu, 'offsetTop') + 30;
		submenu.top  = menu.style.top + 27;
		submenu.visibility = fShow;
		if (lastMenu != null && lastMenu != submenu) hideAll();

		leftX  = document.all[idMainMenu+"submenu"].style.posLeft ;//+ lastX;
		rightX = document.all[idMainMenu+"submenu"].offsetWidth + leftX;
        // IIC: changed leftY to lastY and rightY to the offset expression + lastY
		leftY = lastY;
		rightY  = leftY + document.all[idMainMenu+"submenu"].style.posTop +
			document.all[idMainMenu+"submenu"].offsetHeight;

        LogEvent( "lX:" + leftX + " lY:" + leftY + " rX:" + rightX + " rY:" + rightY + " " );
        LogEvent( "posTop:" + document.all[idMainMenu+"submenu"].style.posTop );
        LogEvent( "offset:" + document.all[idMainMenu+"submenu"].offsetHeight );
        LogEvent( "lastx/y:" + lastX + ',' + lastY );
	}
	lastMenu = submenu;
}

function hideAll()
{
    //IIC: changed "left = 0;" with "left = 8;"
	if (lastMenu != null) {
	    lastMenu.visibility = fHide;
	    lastMenu.left = 8;
	}
}

function calculateSumOffset(idItem, offsetName)
{
	var totalOffset = 0;
	var item = eval('idItem');
	do
	{
		totalOffset += eval('item.'+offsetName);
		item = eval('item.offsetParent');
	} while (item != null);
	return totalOffset;
}

function updateIt(e)
{
	if (ie)
	{
		var x = window.event.clientX;
		var y = window.event.clientY;

        lastX = x;
        lastY = y;

        LogEvent( "x/y:" + x + "," + y );
		if (x < leftX - 10 || x > rightX + 10) {
		    LogEvent( "hide" );
		    hideAll();
		}
		else if (y < leftY - 10 || y > rightY + 10) {
		    //IIC: don't know why the calculations don't work here and we hide it right away
		    hideAll();
		    LogEvent( "hide,region:" + leftX + "," + leftY + ";" + rightX + "," + rightY );
		}
	}
	if (n)
	{
		var x = e.pageX;
		var y = e.pageY;

		if (x > rightX || x < leftX) hideAll();
		else if (y > rightY) hideAll();
	}
}

function setDocumentEvents( bDocMove ) {
    LogEvent( "setev:" + bDocMove );
if (document.all)
{
	document.body.onclick=hideAll;
	document.body.onscroll=hideAll;
	if (bDocMove) {
		document.body.onmousemove=updateIt;
	}
	else {
		document.body.onmousemove = null;
	}
}
if (document.layers)
{
	document.onmousedown=hideAll;
	window.captureEvents(Event.MOUSEMOVE);
	if (bDocMove) {
		window.onmousemove=updateIt;
	}
	else {
		window.onmousemove = null;
	}
}
}

setDocumentEvents( true );

// IIC: the following function helps debug mouse events; comment out "if (false)" to enable event logging
var wEv;

function LogEvent( name ) {
	if (false) {
		if (wEv == null) {
			wEv = window.open( "about:blank" );
		}
		wEv.document.write( "<nobr>" + name + " </nobr>" );
	}
}

