//Top Nav Bar I v2.1- By Constantin Kuznetsov Jr.
//Visit http://www.dynamicdrive.com/dynamicindex1/sm/index.htm for this script
//Modified to work on WildlifeCampus

var lastMenu = null;
var n, ie, ns6, fShow, fHide, opr6, iParentCount
var rightX = 0;

if (document.all)    {n = 0; ie = 1; ns6 = 0; fShow = "visible"; fHide = "hidden";}
if (document.getElementById && !document.all)    {n = 0; ie = 0; ns6 = 1; fShow = "visible"; fHide = "hidden";}

if (ie||ns6)
{
	window.onclick = hideAll;
  document.body.onclick = hideAll;
	document.body.onscroll = hideAll;
	document.body.onmousemove = updateIt;
}

opr6 = ie && navigator.userAgent.indexOf("Opera") != -1
window.onerror = new Function("return true")
iParentCount = 0

function Menu()
{	
	this.addItem    = addItem;
	this.addSubItem = addSubItem;
	this.showMenu   = showMenu;
	rightY = 0;
	leftY = 0;
	leftX = 0;

	HTMLstr = "";
	HTMLstr += "<!-- MENU PANE DECLARATION BEGINS -->\n";
	HTMLstr += "\n";

	if (ie||ns6) {HTMLstr += "<div id='MenuMainTable' width='370'>\n";}

	HTMLstr += "<table width='100%' border='0'>\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 += "<table border='0' width='100%'>"
	HTMLstr += "<!-- SUB MENU STARTS -->\n";
	HTMLstr += "<!-- SUB_MENU -->\n";
	HTMLstr += "<!-- SUB MENU ENDS -->\n";
	HTMLstr += "</table>"
	HTMLstr += "\n";

	if (ie||ns6) {HTMLstr+= "</div>\n";}

	HTMLstr += "<!-- MENU PANE DECLARATION ENDS -->\n";
}

function addItem(idItem, text, hint, location, altLocation)
{
	iParentCount ++
	var Lookup = "<!-- ITEM "+idItem+" -->";

	if (HTMLstr.indexOf(Lookup) != -1)
	{
		alert(idParent + " already exist");
		return;
	}

	var MENUitem = "<!-- ITEM "+idItem+" -->\n";	
	
	if (iParentCount == 1)	{MENUitem += "<td>&nbsp;</td>"}
	else {MENUitem += "<td>|</td>";}

	MENUitem += "<td align='center'>";
	MENUitem += "<div id='"+idItem+"'>\n";
	MENUitem += "<a class='clsMenu'";

	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=\"displaySubMenu('"+idItem+"')\"";
			MENUitem += " onclick='return false;'"
		}

	MENUitem += ">" + text + "</a>\n";
	MENUitem += "</div>\n";
	MENUitem += "</td>\n";
	MENUitem += "<!-- END OF ITEM " + idItem + " -->\n";
	MENUitem += "<!-- MAIN_MENU -->\n";
	HTMLstr = HTMLstr.replace("<!-- MAIN_MENU -->\n", MENUitem);
}

function addSubItem(idParent, text, hint, location, linktarget)
{
	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)
	{
		MENUitem += "\n";
		MENUitem += "<div id='" + idParent + "submenu' onmouseout=operahide() class='submenu'>\n";
		MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
		MENUitem += "</div>\n";	
		MENUitem += "<!-- SUB_MENU -->\n";
		HTMLstr = HTMLstr.replace("<!-- SUB_MENU -->\n", MENUitem);	
	}

	Lookup = "<!-- NEXT ITEM OF SUB MENU " + idParent + " -->\n";
	MENUitem = "<li><a class='clsMenuItem' title=\"" + hint + "\" href=\"" + location + "\""

	if (linktarget != "") {MENUitem += " target='" + linktarget + "'"}

	MENUitem += ">" + text + "</a></li>\n"
	MENUitem += Lookup;		
	HTMLstr = HTMLstr.replace(Lookup, MENUitem);
}

function showMenu()
{		
	document.writeln(HTMLstr)
}

function displaySubMenu(idMainMenu)
{
	var menu;
	var submenu;
	var subWidth;
	
	menu = ie ? eval(idMainMenu) : document.getElementById(idMainMenu);
	submenu = ie ? eval(idMainMenu + "submenu.style") : document.getElementById(idMainMenu + "submenu").style;
	subWidth = ie ? eval(idMainMenu + "submenu") : document.getElementById(idMainMenu + "submenu");
	submenu.left = calculateSumOffset(menu, 'offsetLeft') + menu.offsetWidth - subWidth.offsetWidth
	submenu.top  = menu.style.top + 30;
	submenu.visibility = fShow;

	if (lastMenu != null && lastMenu != submenu) {hideAll();}

	leftX  = ie ? document.all[idMainMenu + "submenu"].style.posLeft : parseInt(document.getElementById(idMainMenu + "submenu").style.left);
	rightX = ie ? leftX + document.all[idMainMenu + "submenu"].offsetWidth : leftX + parseInt(document.getElementById(idMainMenu + "submenu").offsetWidth);
	leftY  = ie ? document.all[idMainMenu + "submenu"].style.posTop + document.all[idMainMenu + "submenu"].offsetHeight : parseInt(document.getElementById(idMainMenu + "submenu").style.top) + parseInt(document.getElementById(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 && !opr6)
	{
		var x = window.event.clientX;					//Mouse x coordinate
		var y = window.event.clientY;					//Mouse y coordinate
	}

	if (n||ns6)
	{
		var x = e.pageX;	//Mouse x coordinate
		var y = e.pageY;	//Mouse y coordinate
	}

	if (x > rightX || x < leftX) {hideAll();}
	else if (y > rightY) {hideAll();}
}

function operahide()
{
	if (opr6)
		{
		 if (event.toElement != null)
			{
				if (!MenuMainTable.contains(event.toElement)) {hideAll()}
			}
		}
}
