var ie=0;
var n=0;
var ns6=0;
var lastMenu;
var submenu;
var leftX;
var rightX;
var leftY;
var rightY;

if (document.all)    {n=0;ie=1;fShow="visible";fHide="hidden";}
if (document.layers) {n=1;ie=0;fShow="show";   fHide="hide";}
if (navigator.userAgent.indexOf("Gecko") > 0) {ns6=1;ie=0;n=0;fShow="visible";fHide="hidden";}

window.onerror=new Function("return true")
////////////////////////////////////////////////////////////////////////////
// Function Menu()                                                        //
////////////////////////////////////////////////////////////////////////////
rightX = 0;
function Menu(id,bgcolor)
{
	this.bgColor = bgcolor;
	if (ie) this.menuFont = "bold xx-small Verdana";
	if (n || ns6)  this.menuFont = "bold x-small Verdana";

	this.addItem    = addItem;
	this.addSubItem = addSubItem;
	this.showMenu   = showMenu;
	this.mainPaneBorder = 0;
	this.subMenuPaneBorder = 0;

	this.subMenuPaneWidth = 130;

	lastMenu = null;
	
	rightY = 0;
	leftY = 0;
	leftX = 0;

	HTMLstr = "";
	HTMLstr += "<!-- MENU PANE DECLARATION BEGINS -->\n";
	HTMLstr += "\n";
	if (ie || ns6) HTMLstr += "<div id='" + id + "' style='position:realtive;'>\n";
//	if (n)  HTMLstr += "<layer name='MainTable'>\n";
	HTMLstr += "<table bgcolor='"+this.bgColor+"' border='"+this.mainPaneBorder+"'>\n";
	HTMLstr += "<tr>";
	if (n) HTMLstr += "<td>";
	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 || ns6) HTMLstr+= "</div>\n";
//	if (n)  HTMLstr+= "</layer>\n";
	HTMLstr += "<!-- MENU PANE DECALARATION ENDS -->\n";
}


function addItem(idItem, text, hint, location, altLocation, isEnd)
{
	var Lookup = "<!-- ITEM "+idItem+" -->";
	if (HTMLstr.indexOf(Lookup) != -1)
	{
		alert(idParent + " already exist");
		return;
	}
	var MENUitem = "";
	MENUitem += "<!-- ITEM "+idItem+" -->";
	if (n)
	{
		MENUitem += "<ilayer name="+idItem+">";
			MENUitem += "<span class='clsMenuItemNS'>";
		if (hint == '-') {
			MENUitem += "<span class='clsMenuItemNSgrey'>";
			MENUitem += text;
			MENUitem += "</span>";
		}
		else {
			MENUitem += "<a class=clsMenuItemNS ";
			if (location != null) {
				if (hint=="*"){
					MENUitem += "href='"+location+"' ";
					MENUitem += "onmouseover=\"hideAll(s)\">";
				}else{
//					MENUitem += "href='" +location+"' onMouseOver=\"hideAll();\">";
					MENUitem += "href='" +location+ "' onmouseover=\"displaySubMenu('"+idItem+"')\">";
				}
			}
			else {
				MENUitem += "href='.' onmouseover=\"displaySubMenu('"+idItem+"')\" onclick=\"return false;\">";
			}
			MENUitem += text;
			MENUitem += "</a>";
		}
		if ( !isEnd )
			MENUitem += "&nbsp;|&nbsp;";
		MENUitem += "</span>";
		MENUitem += "</ilayer>";
	}
	if (ie || ns6)
	{
		MENUitem += "<td nowrap>\n";
		MENUitem += "<div id='"+idItem+"' style='position:relative; font: "+this.menuFont+";'>\n";
		MENUitem += "<span id='M_" + idItem + "'>"
		if (hint == '-') {
			MENUitem += "<span class='clsMenuItemIEgrey'>";
			MENUitem += text;
			MENUitem += "</span>";
		}
		else {
			MENUitem += "<a ";
			if (hint != null)
				MENUitem += "title='"+hint+"' ";
			if (location != null)
			{
				if (hint=="*"){
					MENUitem += "href='"+location+"' ";
					MENUitem += "class=clsMenuItemIETop ";
					MENUitem += "onmouseover=\"hideAll()\">";
				}else{
//					MENUitem += "class=clsMenuItemIE ";
					MENUitem += "href='"+location+"' ";
					MENUitem += "class=clsMenuItemIETop ";
					MENUitem += "onmouseover=\"displaySubMenu('"+idItem+"')\" ";
					MENUitem += "onmouseout=\"mouseMenu('out','M_"+ idItem+"');\">";
//					MENUitem += "onmouseover=\"mouseMenu('over','M_"+ idItem+ "');hideAll()\"";
//					MENUitem += "onmouseout=\"mouseMenu('out','M_"+ idItem+ "');\">";
				}
			}
			else
			{
				if (altLocation != null)
					MENUitem += "href='"+altLocation+"' ";
				else
					MENUitem += "href='.' ";
				MENUitem += "class=clsMenuItemIETop ";
				MENUitem += "onmouseover=\"displaySubMenu('"+idItem+"')\" ";
				MENUitem += "onmouseout=\"mouseMenu('out','M_"+ idItem+"');\" ";
				MENUitem += "onclick=\"return false;\">"
			}
			MENUitem += text;
			MENUitem += "</a>";
		}
		MENUitem += "</span>\n";
//        MENUitem += "<span class=clsMenuItemIE>&nbsp;|&nbsp;</span>\n";
		if ( !isEnd )
        	MENUitem += "<span class=clsMenuItemIE style='font-size: 10px;'>|</span>\n";
        MENUitem += "</div>\n";
		MENUitem += "</td>\n";
	}
	MENUitem += "<!-- END OF ITEM "+idItem+" -->";
	MENUitem += "<!-- MAIN_MENU -->";

	HTMLstr = HTMLstr.replace("<!-- MAIN_MENU -->", MENUitem);
}

function addSubMenuLine(idParent)
{
	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)
	{
		if (n)
		{
			MENUitem += "\n";
			MENUitem += "<layer id='"+idParent+"submenu' visibility=hide bgcolor='"+this.bgColor+"'>\n";
			MENUitem += "<table border='"+this.subMenuPaneBorder+"' bgcolor='"+this.bgColor+"' width="+this.subMenuPaneWidth+">\n";
			MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
			MENUitem += "</table>\n";
			MENUitem += "</layer>\n";
			MENUitem += "\n";
		}
		if (ie || ns6)
		{
			MENUitem += "\n";
			MENUitem += "<div id='"+idParent+"submenu' style='position:absolute; visibility: hidden; width: "+this.subMenuPaneWidth+"; font: "+this.menuFont+"; top: 0;'>\n";
			MENUitem += "<table border='"+this.subMenuPaneBorder+"' bgcolor='"+this.bgColor+"' width="+this.subMenuPaneWidth+">\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><hr class=clsMenuItemNS size='1' width='100%'></td></tr>\n";
	if (ie) MENUitem = "<tr><td><hr class=clsMenuItemIE size='1' width='100%'></td></tr>\n";
	MENUitem += Lookup;
	HTMLstr = HTMLstr.replace(Lookup, MENUitem);
}

var globalSubID = 0

function mouseMenu(action, MenuIDStr) 
{
	// alert(action + " " + MenuIDStr)	
	if (action == "out")
	{
		eval(MenuIDStr).className = "clsMenuItemIECell"
	}		
	else if (action == "over")
		eval(MenuIDStr).className = "clsMenuItemIECellOver"
}

function addSubItem(idParent, text, hint, location)
{	
	var MenuIDStr = ""
	var MENUitem = "";
	var char34 = String.fromCharCode(34);
	
	MenuIDStr = idParent + globalSubID.toString();
	globalSubID++;
	
	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)
	{
		if (n)
		{
			MENUitem += "\n";
			MENUitem += "<layer id='"+idParent+"submenu' visibility=hide bgcolor='"+this.bgColor+"'>";
			MENUitem += "<img src=img/transpix.gif width=1 height=1 border=0><br>";
			MENUitem += "<table border='"+this.subMenuPaneBorder+"' bgcolor='"+this.bgColor+"' width="+this.subMenuPaneWidth+">\n";
			MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
			MENUitem += "</table>\n";
			MENUitem += "</layer>\n";
			MENUitem += "\n";
		}
		if (ie || ns6)
		{
			MENUitem += "\n";
			MENUitem += "<div id='"+idParent+"submenu' style='position:absolute; background-color: "+this.bgColor+"; visibility: hidden; width: "+this.subMenuPaneWidth+"; font: "+this.menuFont+"; top: 0;'>\n";
			MENUitem += "<table border='"+this.subMenuPaneBorder+"' bgcolor='"+this.bgColor+"' width="+this.subMenuPaneWidth+">\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)
		if (hint == "-")
			MENUitem = "<tr><td><span class='clsMenuItemNSgrey'>"+text+"</span></td></tr>\n";
		else
			MENUitem = "<tr><td><a href='"+location+"' class='clsMenuItemNS' title='"+hint+"'>"+text+"</a></td></tr>\n";
	if (ie || ns6) 
		if (hint == "-")
			MENUitem = "<tr><td nowrap><span class='clsMenuItemIEgrey'>"+text+"</span></td></tr>\n";
		else
			MENUitem = "<tr><td nowrap " + " onmouseover=" + char34 + "mouseMenu('over','" + MenuIDStr + "');" + char34 + 
				" onmouseout=" + char34 + "mouseMenu('out' ,'" + MenuIDStr + "');" + char34 +
				" onmouseout=" + char34 + "mouseMenu('out' ,'" + MenuIDStr + "');" + char34 +
				" onmousedown=" + char34 + "goToURL('" + location + "');" + char34 +
				" id='" + MenuIDStr + "' class='clsMenuItemIECell'>" + 
				" <a href='"+location+"' class='clsMenuItemIE' title='"+hint+"'" +  
				" onmouseout=" + char34 + "mouseMenu('out' ,'" + MenuIDStr + "');" + char34 +
				" onmouseover=" + char34 + "mouseMenu('over','" + MenuIDStr + "');" + char34 + ">"+text+"</a></td></tr>\n";
	MENUitem += Lookup;
	HTMLstr = HTMLstr.replace(Lookup, MENUitem);
}

function goToURL(url)
{
	window.location = url;
}

function showMenu()
{
//	document.test.test.value = HTMLstr
	document.writeln(HTMLstr);
	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 + 19;
		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 (ns6) {
		menu = document.getElementById(idMainMenu);
		submenu = document.getElementById(idMainMenu+"submenu").style;
		submenu.left = calculateSumOffset(menu, 'offsetLeft') -3;
		submenu.top = calculateSumOffset(menu, 'offsetTop') +18;
		submenu.visibility = fShow;
		if (lastMenu != null && lastMenu != submenu) hideAll();

		leftX  = parseInt(document.getElementById(idMainMenu+"submenu").style.left);
		rightX = leftX + document.getElementById(idMainMenu+"submenu").offsetWidth;

		leftY  = document.getElementById(idMainMenu+"submenu").offsetTop;
		leftY  += document.getElementById(idMainMenu+"submenu").offsetHeight;
		rightY = leftY;
	} else if (ie) {
		menu = eval(idMainMenu);
		submenu = eval(idMainMenu+"submenu.style");
		submenu.left = calculateSumOffset(menu, 'offsetLeft')-3;
		submenu.top = calculateSumOffset(menu, 'offsetTop') +18;
//		if (navigator.appVersion.indexOf('MSIE 5.5',0)>0) submenu.top = 63; // IE5.5 bugfix
		submenu.visibility = fShow;
		if (lastMenu != null && lastMenu != submenu) hideAll();

		leftX  = document.all[idMainMenu+"submenu"].style.posLeft;
		rightX = leftX + document.all[idMainMenu+"submenu"].offsetWidth;

		leftY  = document.all[idMainMenu+"submenu"].style.posTop+
			document.all[idMainMenu+"submenu"].offsetHeight;
		rightY = leftY;
	}
	lastMenu = submenu;
}

function hideAll()
{
	if (lastMenu != null){
		lastMenu.visibility = fHide;
		lastMenu.left = 0;
	}
}

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;

		if (x > rightX || x < leftX) hideAll();
		else if (y > rightY) hideAll();
	}
	if (n)
	{
		var x = e.pageX;
		var y = e.pageY;

		if (x > rightX || x < leftX) hideAll();
		else if (y > rightY) hideAll();
	}
	if (document.all)
	{
		document.body.onclick=hideAll;
		document.body.onscroll=hideAll;
		document.body.onmousemove=updateIt;
	}
}

function updateItNs6(e)
{
	if (ns6)
	{
		var x = e.pageX;
		var y = e.pageY;

		if (x > rightX || x < leftX){
			 hideAll();
		}
		else if (y > rightY) hideAll();
	}
}


if (document.layers)
{
	document.onmousedown=hideAll;
	window.captureEvents(Event.MOUSEMOVE);
	window.onmousemove=updateIt;
}

if (ns6){
//	document.onmousedown=hideAll;
	window.captureEvents(Event.MOUSEMOVE);
	document.onmousemove=updateItNs6;
}
