/*

===================================================
XHTML/CSS/DHTML Semantically correct drop down menu 
===================================================
Altered by: Kelvin Tse

Author: Sam Hampton-Smith
Site: http://www.hampton-smith.com

Description:	This script takes a nested set of <ul>s
		and turns it into a fully functional
		DHTML menu. All that is required is 
		the correct use of class names, and
		the application of some CSS.
		
Use:		Please leave this information at the
		top of this file, and it would be nice
		if you credited me/dropped me an email
		to let me know you have used the menu.
		sam AT hampton-smith.com


---------------------------------------------------
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/

*/


var currentMenu = null;
var currentStarter = null;
var mytimer = null;
var timerOn = false;
var opera = window.opera ? true : false;
var menuLeftMax = 0;
var menuRightMax = 960;

if (!document.getElementById)
	document.getElementById = function() { return null; }

function initialiseMenu(menu, starter, root) {
	var leftstarter = false;

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

	starter.onmouseover = function() {
		if (currentMenu) {
			if (this.parentNode.parentNode!=currentMenu) {
				currentMenu.style.visibility = "hidden";
				currentStarter.className = "";
			}
			if (this.parentNode.parentNode==root) {
				tempCurrentMenu = currentMenu
				while (tempCurrentMenu.parentNode.parentNode!=root) {
					tempCurrentMenu.parentNode.parentNode.style.visibility = "hidden";
					tempCurrentMenu = tempCurrentMenu.parentNode.parentNode;
				}
			}
			currentMenu = null;
			this.showMenu();
		}
	}

	menu.onmouseover = function() {
	/*
		if (currentMenu) {
			currentMenu = null;
			this.showMenu();
		}
	*/
	}	

	starter.showMenu = function() {
		var leftoff = 0;
		if (!opera) {
			if (this.parentNode.parentNode==root) {
				menuLeft = this.parentNode.offsetLeft + leftoff;
				menuTop = this.offsetTop + this.offsetHeight;
			}
			else {
				menuLeft = this.parentNode.offsetLeft + leftoff + this.offsetWidth;
				menuTop = this.offsetTop;
			}
		}
		else {
			if (this.parentNode.parentNode==root) {
				menuLeft = this.offsetLeft + leftoff;
				menuTop = this.offsetHeight;
			}
			else {
				menuLeft = this.offsetWidth;
				menuTop = this.offsetTop ;
			}

		}
		menuWidth = getMenuWidth(menu);
		menu.style.width = (menuWidth + 1) + "px";
		menuLeft = menuLeft - (menuWidth/2) + (getWidth(this)/2);
		if (menuLeft < menuLeftMax) { menuLeft = menuLeftMax; }
		else if ((menuLeft + menuWidth) > menuRightMax) { menuLeft = menuRightMax - menuWidth; }
		menu.style.left = menuLeft + "px";
		menu.style.top = menuTop + "px";
		menu.style.visibility = "visible";
		currentMenu = menu;
		currentStarter = this;
		currentStarter.className = "hoverStarter";
	}

	starter.onfocus	 = function() {
		starter.onmouseover();
	}

	menu.onfocus	 = function() {
	}

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

	menu.hideMenu = function()  {
		if (!timerOn) {
			mytimer = setInterval("killMenu('" + this.id + "', '" + root.id + "');", 400);
			timerOn = true;
			for (var x=0;x<menu.childNodes.length;x++) {
				if (menu.childNodes[x].nodeName=="LI") {
					if (menu.childNodes[x].getElementsByTagName("UL").length>0) {
						menuItem = menu.childNodes[x].getElementsByTagName("UL").item(0);
						menuItem.style.visibility = "hidden";
					}
				}
			}
		}
	}

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

	starter.onmouseout = function() {
		for (var x=0;x<menu.childNodes.length;x++) {
			if (menu.childNodes[x].nodeName=="LI") {
				if (menu.childNodes[x].getElementsByTagName("UL").length>0) {
					menuItem = menu.childNodes[x].getElementsByTagName("UL").item(0);
					menuItem.style.visibility = "hidden";
				}
			}
		}
		if (starter.className == "starter") {
			menu.hideMenu();
		} else {
			menu.style.visibility = "hidden";
		}
	}
	*/
}
function killMenu(menu, root) {
	var menu = document.getElementById(menu);
	var root = document.getElementById(root);
	menu.style.visibility = "hidden";
	for (var x=0;x<menu.childNodes.length;x++) {
		if (menu.childNodes[x].nodeName=="LI") {
			if (menu.childNodes[x].getElementsByTagName("UL").length>0) {
				menuItem = menu.childNodes[x].getElementsByTagName("UL").item(0);
				menuItem.style.visibility = "hidden";
			}
		}
	}
	while (menu.parentNode.parentNode!=root) {
		menu.parentNode.parentNode.style.visibility = "hidden";
		menu = menu.parentNode.parentNode;
	}
	stopTime();
}
function stopTime() {
	if (mytimer) {
		 clearInterval(mytimer);
		 mytimer = null;
		 timerOn = false;
	}
} 

function hmenu_init() { 
	var root = document.getElementById("hnavbar_menu");
	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=="LI") {
			if (elementItem.childNodes[x].getElementsByTagName("UL").length>0) {
				menuStarter = elementItem.childNodes[x].getElementsByTagName("A").item(0);
				menuItem = elementItem.childNodes[x].getElementsByTagName("UL").item(0);
				getMenus(menuItem, root);
				initialiseMenu(menuItem, menuStarter, root);
			}
		}
	}
}

function getMenuWidth(menu) {
	var menuItem = menu.getElementsByTagName("li");
	var total = 0;
	for (var x=0;x<menuItem.length;x++) {
		total += getWidth(menuItem[x]);
	}
	return total;
}
