// Specific issues with IE and Netscape
var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) 
			&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) 
			&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_nav4up = (is_nav && (is_major >= 4));
var is_nav6 = (is_nav && (is_major == 5));
if ( agt.indexOf('opera') != -1 ) is_nav6 = 1;
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie4up  = (is_ie  && (is_major >= 4));

if (is_ie4up)    {n=0;ie=1;fShow="visible";fHide="hidden";}
if (is_nav4up) {n=1;ie=0;fShow="show";fHide="hide";}
if (is_nav6) {n=1;ie=0;fShow="visible";fHide="hidden";}

// Prep some vars
lastMenu = null;
window.onerror=new Function("return true")
rightX = leftX = topY = bottomY = 0;
isShowing = false;


// Build the menu
function Menu() {
	this.bgColor = "#0000CC";
	this.subMenuBGColor = "#0000CC";
	
	if (is_nav6) this.bgColor = "#0000CC";
	if (is_nav6) this.subMenuBGColor = "#FFFFFF";

	if (is_ie4up) this.menuFont = "bold small Arial";
	if (is_nav6) this.menuFont = "bold Arial";
	else this.menuFont = "bold small Arial";
	
	this.fontColor = "#FFFFFF";
	if (is_nav6) this.fontColor = "#0000CC";

	this.addItem    = addItem;
	this.addSubItem = addSubItem;
	this.showMenu   = showMenu;
	this.mainOuterPaneCellpadding = 0;
	this.mainOuterPaneCellspacing = 0;
	this.mainOuterPaneBorder = 0;
	this.mainOuterPaneBGColor = "#999999";
	this.mainInnerPaneCellpadding = 2;
	this.mainInnerPaneCellspacing = 1;
	this.mainInnerPaneBorder = 0;
	this.subMenuOuterPaneCellpadding = 0;
	this.subMenuInnerPaneCellpadding = 2;
	this.subMenuOuterPaneCellspacing = 0;
	this.subMenuInnerPaneCellspacing = 1;
	this.subMenuOuterPaneBGColor = "#999999";
	this.subMenuOuterPaneBorder = 0;
	this.subMenuInnerPaneBorder = 0;
	this.subMenuPaneWidth = 230;
	
	HTMLstr = "";
	if (is_ie4up) HTMLstr += "<DIV id='MainTable' style='position:relative'>\n";
	HTMLstr += '<TABLE border="'+this.mainOuterPaneBorder+'" width="100%" cellpadding="'+this.mainOuterPaneCellpadding+'" cellspacing="'+this.mainOuterPaneCellspacing+'" bgcolor="'+this.mainOuterPaneBGColor+'"><TR><TD>\n';
	HTMLstr += '<TABLE border="'+this.mainInnerPaneBorder+'" width="100%" cellpadding="'+this.mainInnerPaneCellpadding+'" cellspacing="'+this.mainInnerPaneCellspacing+'" bgcolor="'+this.mainOuterPaneBGColor+'">\n';
	HTMLstr += '<TR bgcolor="'+this.bgColor+'">';
	HTMLstr += "<TD>&nbsp;\n";
	HTMLstr += "<!-- MAIN_MENU -->\n";
	HTMLstr += "</TD></TR>\n";
	HTMLstr += "</TABLE></TD></TR>\n";
	HTMLstr += "</TABLE>\n";
	HTMLstr += "<!-- SUB_MENU -->\n";
	if (is_ie4up) HTMLstr+= "</DIV>\n";
}

function addItem(idItem, text, lastitem, location) {
	var Lookup = "<!-- ITEM "+idItem+" -->";
	if (HTMLstr.indexOf(Lookup) != -1) {
		alert(idParent + " already exist");
		return;
	}
	var MENUitem = "";
	MENUitem += "\n<!-- ITEM "+idItem+" -->\n";
	if (is_nav4up && !is_nav6) {
		MENUitem += '<ILAYER name="'+idItem+'">';
		MENUitem += '<A href="." class="clsMenuItemNS" onmouseover="displaySubMenu(\''+idItem+'\')" onclick="return false;">';
		MENUitem += text;
		MENUitem += "</A>\n";
		MENUitem += "</ILAYER>\n";
	}
	if (is_ie4up || is_nav6) {
		if ( location == null ) location = ".";
		MENUitem += '<SPAN id="'+idItem+'">\n';
		MENUitem += '<A class="clsMenuItemIE" href="'+location+'" onmouseover="displaySubMenu(\''+idItem+'\')" onclick="return false;">';
		MENUitem += text;
		MENUitem += "</A>\n";
		MENUitem += "</SPAN>\n";
	}
	if (!lastitem) MENUitem += "&nbsp;|&nbsp;";
	MENUitem += "<!-- END OF ITEM "+idItem+" -->\n\n";
	MENUitem += "<!-- MAIN_MENU -->\n";

	HTMLstr = HTMLstr.replace("<!-- MAIN_MENU -->\n", MENUitem);
}

function addSubItem(idParent, text, hint, location)
{
	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 (is_nav4up && !is_nav6)
		{
			MENUitem += "\n";
			MENUitem += '<LAYER id="'+idParent+'submenu" visibility="hide" z-index="1" bgcolor="'+this.subMenuBGColor+'">\n';
			MENUitem += '<TABLE border="'+this.subMenuOuterPaneBorder+'" bgcolor="'+this.subMenuOuterPaneBGColor+'" cellpadding="'+this.subMenuOuterPaneCellpadding+'" cellspacing="'+this.subMenuOuterPaneCellspacing+'" width="'+this.subMenuPaneWidth+'"><TR><TD>\n';
			MENUitem += '<TABLE border="'+this.subMenuInnerPaneBorder+'" bgcolor="'+this.subMenuOuterPaneBGColor+'" cellpadding="'+this.subMenuInnerPaneCellpadding+'" cellspacing="'+this.subMenuInnerPaneCellspacing+'" width="'+this.subMenuPaneWidth+'">\n';
			MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
			MENUitem += "</TABLE></TD></TR></TABLE>\n";
			MENUitem += "</LAYER>\n";
			MENUitem += "\n";
		}
		if (is_ie4up || is_nav6)
		{
			MENUitem += "\n";
			MENUitem += "<DIV id='"+idParent+"submenu' style='position:absolute; visibility: hidden; text-decoration: none; width: "+this.subMenuPaneWidth+"; font: "+this.menuFont+"; top: 23;'>\n";
			MENUitem += '<TABLE border="'+this.subMenuOuterPaneBorder+'" bgcolor="'+this.subMenuOuterPaneBGColor+'" cellpadding="'+this.subMenuOuterPaneCellpadding+'" cellspacing="'+this.subMenuOuterPaneCellspacing+'" width="'+this.subMenuPaneWidth+'"><TR><TD>\n';
			MENUitem += '<TABLE border="'+this.subMenuInnerPaneBorder+'" bgcolor="'+this.subMenuOuterPaneBGColor+'" cellpadding="'+this.subMenuInnerPaneCellpadding+'" cellspacing="'+this.subMenuInnerPaneCellspacing+'" width="'+this.subMenuPaneWidth+'">\n';
			MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
			MENUitem += "</TABLE></TD></TR></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 (is_nav4up)  MENUitem = '<TR bgcolor="'+this.subMenuBGColor+'" onmouseover="this.style.backgroundColor=\'#c0c0c0\'" onmouseout="this.style.backgroundColor=\'#f6f6f6\'"><TD>&nbsp;<A class="clsMenuItemNS" title="'+hint+'" href="'+location+'"><FONT color="'+this.fontColor+'" style="font:'+this.menuFont+'; text-decoration: none">'+text+'</FONT></A><BR></TD></TR>\n';
	if (is_ie4up) MENUitem = '<TR bgcolor="'+this.subMenuBGColor+'" onmouseover="this.className=\'clsMenuItemIEOver\';" onmouseout="this.className=\'clsMenuItemIE\'"><TD><A class="clsMenuItemIE" title="'+hint+'" href="'+location+'"><FONT color="'+this.fontColor+'">'+text+'</FONT></A><BR></TD></TR>\n';
	MENUitem += Lookup;
	HTMLstr = HTMLstr.replace(Lookup, MENUitem);

}

function showMenu()
{
	document.writeln(HTMLstr);
}

////////////////////////////////////////////////////////////////////////////
// Private declaration
function displaySubMenu(idMainMenu)
{
	var menu;
	var submenu;
	if ( is_nav6 )
	{
		submenu = document.getElementById(idMainMenu+"submenu");
		if (lastMenu != null && lastMenu != submenu) hideAll();
		
		submenu.style.left = document.getElementById(idMainMenu).offsetLeft; //pageX - 6;
		
		submenu.style.top  = document.getElementById(idMainMenu).offsetTop + 21;
		submenu.style.visibility = fShow;
	
		leftX  = parseInt(document.getElementById(idMainMenu+"submenu").style.left);
		rightX = leftX + parseInt(document.getElementById(idMainMenu+"submenu").style.width);
		topY = document.getElementById(idMainMenu).offsetTop;
		bottomY  = parseInt(document.getElementById(idMainMenu+"submenu").style.top)+
			parseInt(document.getElementById(idMainMenu+"submenu").offsetHeight);
	} else if (is_nav4up && !is_nav6) {
		submenu = document.layers[idMainMenu+"submenu"];
		if (lastMenu != null && lastMenu != submenu) hideAll();
		submenu.left = document.layers[idMainMenu].pageX - 6;
		submenu.top  = document.layers[idMainMenu].pageY + 21;
		submenu.visibility = fShow;

		leftX  = document.layers[idMainMenu+"submenu"].left;
		rightX = leftX + document.layers[idMainMenu+"submenu"].clip.width;
		topY = document.layers[idMainMenu].pageY;
		bottomY  = document.layers[idMainMenu+"submenu"].top+document.layers[idMainMenu+"submenu"].clip.height;
	} else if (is_ie4up ) {
		menu = eval(idMainMenu);
		submenu = eval(idMainMenu+"submenu.style");
		submenu.left = menu.offsetLeft-9;
		submenu.top  = 24;
		submenu.visibility = fShow;
		if (lastMenu != null && lastMenu != submenu) hideAll();

		leftX  = document.all[idMainMenu+"submenu"].style.posLeft;
		rightX = leftX + document.all[idMainMenu+"submenu"].offsetWidth;
		topY = document.all[idMainMenu+"submenu"].offsetParent.offsetTop;
		bottomY  = document.all[idMainMenu+"submenu"].offsetParent.offsetTop + document.all[idMainMenu+"submenu"].style.posTop+
			document.all[idMainMenu+"submenu"].offsetHeight;
	}
	lastMenu = submenu;
	//alert(lastMenu);
	isShowing = true;
}

function hideAll()
{ 	//alert(lastMenu);
	if (lastMenu != null) {
		if ( is_nav6 ) {
			//alert(lastmenu);
			lastMenu.style.visibility = "hidden";
		}	
		else lastMenu.visibility = fHide;
		
		lastMenu.left = 0;
		isShowing = false;
	}
}

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)
{ 
	e = e || window.Event || window.event;
	var x = e.pageX || e.clientX;
	var y = e.pageY || e.clientY;
	
	//window.status = "MouseX:"+x+" MouseY:"+y+" MinX:"+leftX+" MaxX:"+rightX+" MinY:"+topY+" MaxY:"+bottomY;
	if (x > rightX || x < leftX) {
		hideAll();
	} else if (y > bottomY || y < topY) {
		hideAll();
	}
}

if (is_ie4up)
{
	//document.body.onclick=hideAll;
	document.body.onscroll=hideAll;
	document.body.onmousemove=updateIt;
}
if (is_nav4up)
{
	//document.onmousedown=hideAll;
	window.captureEvents(Event.MOUSEMOVE);
	window.onmousemove=updateIt;
}
