var navlinksmenuarray = new Array()
navlinksmenuarray[0] = ["navlinksmenu", "first"];
navlinksmenuarray[1] = ["navlinksmenu", "second"];
navlinksmenuarray[2] = ["navlinksmenu", "third"];
navlinksmenuarray[3] = ["navlinksmenu", "fourth"];
navlinksmenuarray[4] = ["navlinksmenu", "fifth"];
navlinksmenuarray[5] = ["navlinksmenu", "sixth"];
navlinksmenuarray[6] = ["navlinksmenu", "seventh"];
navlinksmenuarray[7] = ["navlinksmenu", "eighth"];
navlinksmenuarray[8] = ["navlinksmenu", "nineth"];

var menusOpen = new Array()
var theTime

function menuObj(id) {
	this.layerobj = document.getElementById(id + "menu")
	this.layerobj.name = id + "menu"
	this.layerobj.kidarray = eval(id + "menuarray")
	this.layerobj.css = this.layerobj.style
	this.layerobj.visible = this.layerobj.css.visibility
	this.layerobj.myparent = document.getElementById(id + "link")
	this.layerobj.show = showMe;
	this.layerobj.hide = hideMe
	this.layerobj.makechildren = makeMenuObjects
	this.layerobj.makechildren(this.layerobj.kidarray)
}

function makeMenuObjects() {
	for (this.i = 0; this.i < this.kidarray.length; this.i++) {
		this.kidarray[this.i] = new menuItemObj(this.kidarray[this.i][1], this.kidarray[this.i][0])
	}
}

function menuItemObj(id, par) {
	this.layerobj = document.getElementById(id + "link")
	this.layerobj.name = id + "link"
	this.layerobj.myparent = document.getElementById(par)

	if (document.getElementById(id + "menu")) {
		new menuObj(id)
		this.layerobj.kids = document.getElementById(id + "menu")
	}
	this.layerobj.css = this.layerobj.style
	this.layerobj.onmouseover = itemOver
	this.layerobj.onmouseout = itemOut
	this.layerobj.cleanMenus = cleanUp
}

function itemOver() {
	myclass = (this.className=='submenuitem') ? 'submenuitemover' : 'submenuitemover'
	if (this.myparent.name != "navlinksmenu") {
		this.className = this.className + 'over';
	}
	else
	{
		this.className = this.className + 'over';
	}

	clearTimeout(theTime)
	if (menusOpen.length > 0) {
		lastOne = menusOpen[menusOpen.length - 1]
		if (lastOne != this.myparent && lastOne != this.kids) {
			for (p=0; p < menusOpen.length; p++) {
				if (menusOpen[p] == this.myparent) {
					this.cleanMenus()
					break
				}
			}
			menusOpen[menusOpen.length] = this.myparent
		}
	}
	else  {
		menusOpen[menusOpen.length] = this.myparent
	}

	this.cleanMenus()
	if (this.kids) this.kids.show()
}

function itemOut() {
	myclass = (this.className=='submenuitemover') ? 'submenuitem' : 'submenuitem'
	if (this.myparent.name != "navlinksmenu") {
	    myclass = this.className;
		this.className = myclass.substr(0, myclass.length - 4);
	}
	else
	{
	    myclass = this.className;
		this.className = myclass.substr(0, myclass.length - 4);
	}
	setTimer()
}

function cleanUp() {
	for (i = menusOpen.length-1; i >= 0; i--) {
		for (j = 0; j < menusOpen[i].kidarray.length; j++) {
			if (menusOpen[i].kidarray[j].layerobj.kids) menusOpen[i].kidarray[j].layerobj.kids.hide()
		}
		if (menusOpen[i] == this.myparent) break
		menusOpen[i].hide()
		menusOpen[i] = null
		menusOpen.length = menusOpen.length - 1
	}
}

function showMe() {
	/*
		if the menu has a submenu, the myparent refers to the individual menu item and not the menu as a whole. 
		that is why you see this.myparent.myparent reference to get the offsetTop and offsetLeft
	*/
	if (this.myparent.myparent.id == 'navlinksmenu')
	{
		var outerTable = document.getElementById('tblMain');
		var menuLeft = parseInt(outerTable.offsetWidth) + parseInt(outerTable.offsetLeft);
		var totalWidth = this.myparent.offsetLeft + outerTable.offsetLeft + this.offsetWidth;
		this.style.top = this.myparent.offsetTop + 133;
		
		if (totalWidth > menuLeft)
		{
			this.style.left  = ((this.myparent.offsetLeft + outerTable.offsetLeft) - (totalWidth - menuLeft) + 1) + "px";
		}
		else
		{
		    this.style.left = (this.myparent.offsetLeft + outerTable.offsetLeft + 4) + "px";
		}
		
	}
	else
	{
		this.style.top = (this.myparent.offsetTop + 133) + "px";
		this.style.left = (this.myparent.myparent.offsetLeft + this.myparent.offsetWidth - 1) + "px";
	}
	this.visible = "visible"
	this.css.visibility = this.visible
}

function hideMe() {
	if (this.name != "navlinksmenu") {
		this.visible = "hidden"
		this.css.visibility = this.visible
	}
}

function setTimer() {
	theTime = setTimeout("cleanUp()",250)
}

new menuObj("navlinks")

