/*
Credits: 	Inspiration/Code borrowed from Dave Lindquist (http://www.gazingus.org)
		Menu hide functionality was aided by some code I found on http://www.jessett.com/
		Based on the work of sam AT hampton-smith.com

*/


var currentMenu = null;
var mytimer = null;
var timerOn = false;
var opera = window.opera ? true : false;

if (!document.getElementById)
    document.getElementById = function() { return null; }
	
function initialiseMenu(menu, starter, root) {
    var leftstarter = false;

    if (menu == null || starter == null) return;
    currentMenu = menu;

    if(!opera)
    {
	menu.style.left = starter.offsetLeft + starter.offsetWidth + "px";
	menu.style.top = starter.offsetTop + "px";
    }

    starter.onmouseover = function() {
	this.style.background = "rgb(146,159,165)";
	if (currentMenu) {
	    if (this.parentNode.parentNode!=currentMenu) {
		currentMenu.style.visibility = "hidden";
	    }
	    currentMenu = null;
	    this.showMenu();
	}
    }

    menu.onmouseover = function() {
	if (currentMenu) {
	    currentMenu = null;
	    this.showMenu();
	}
    }	
	
    starter.showMenu = function() {
	
	menu.style.visibility = "visible";
	currentMenu = menu;
	stopTime();
    }

    starter.onfocus	 = function() {
	starter.onmouseover();
    }
	
    menu.showMenu = function() {
	menu.style.visibility = "visible";
	currentMenu = menu;
	stopTime();
    }

    menu.hideMenu = function()  {
	if (!timerOn) {
	    mytimer = setInterval("killMenu('" + this.id + "', '" + root.id + "');", 500);
	    timerOn = true;
	}
    }

    menu.onmouseout = function(event) {
	this.hideMenu();
    }

    starter.onmouseout = function() {
	this.style.background = "rgb(99,118,126)";
	if(!timerOn) {
	    mytimer = setInterval("killMenu('" + menu.id + "', '" + root.id + "');", 500);
	    timerOn = true;
	}
    }

}

function killMenu(menu, root) {
    var menu = document.getElementById(menu);
    var root = document.getElementById(root);
    menu.style.visibility = "hidden";
    stopTime();
}

function stopTime() {
    if (mytimer) {
	clearInterval(mytimer);
	mytimer = null;
	timerOn = false;
    }
} 

window.onload = function() {
    var root = document.getElementById("menuList");
    getMenus(root, root);
}

function getMenus(elementItem, root) {
	var selectedItem;
	var menuStarter;
	var menuItem;
	for (var x=0;x<elementItem.childNodes.length;x++) {
		if (elementItem.childNodes[x].nodeName.toUpperCase()=="LI") {
			if (elementItem.childNodes[x].getElementsByTagName("ul").length>0) {
				menuStarter = elementItem.childNodes[x];
				menuItem = elementItem.childNodes[x].getElementsByTagName("ul").item(0);
				initialiseMenu(menuItem, menuStarter, root);
			}
			else
			{
			    menuStarter = elementItem.childNodes[x];
			    menuStarter.onmouseover = function() { 
				currentMenu.style.visibility = "hidden"; 
				this.style.background = "rgb(146,159,165)";
			    }
			    menuStarter.onmouseout = function() { 
				this.style.background = "rgb(99,118,126)";
			    }	
			}
		}
	}
}

