/*

===================================================
XHTML/CSS/DHTML Semantically correct drop down menu 
===================================================
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/
---------------------------------------------------
Modifications:	Location of drop-down menus were causing
		a horizontal scroll-bar in IE. Ugly. This
		was due to the default menu positioning and
		disappeared as soon as the menu was actually
		positioned and displayed.  This positioning
		was occuring in the starter.showMenu function;
		I moved this code into a separate function
		and now call it in the getMenus function which
		is called when the window is loaded.  Voila,
		no more ugly horizontal scroll bar.  Happiness.
		11/3/2004 TJC.

*/
var currentMenu = null;
var mytimer = null;
var timerOn = false;
var opera = window.opera ? true : false;

var browser = navigator.appName;
var version = navigator.appVersion;
//alert("browserString=" + browser + "=");
//alert("versionString=" + version);
version = version.toLowerCase();
browser = browser.toLowerCase();
browser = browser.indexOf("internet explorer") >= 0 ? "isIE" : "isNotIE";

// IE 8.0 masquerades as IE 7.0, but not exactly...
if( browser == "isIE" ){
	if( version.indexOf("msie 8") >= 0 ){
	    version = "8";
	}
}
//alert("browser=" + browser + "=");
//alert("version=" + version);

if (!document.getElementById)
	document.getElementById = function() { return null; }
	
function initialiseMenu(menu, starter, root) {
	
	if (menu == null || starter == null)
		return;

	currentMenu = menu;

	starter.onmouseover = function() {
		if (currentMenu) {
			//alert("initialiseMenu currentMenu: " + this.parentNode.parentNode.id + ' ' + currentMenu.id);
			if (this.parentNode.parentNode!=currentMenu) {
				currentMenu.style.visibility = "hidden";
			}
			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() {
		menu.style.visibility = "visible";
		//menu.style.z-index = 100;
		currentMenu = menu;
	}

	starter.onfocus	 = function() {
		starter.onmouseover();
	}

	menu.showMenu = function() {
		menu.style.visibility = "visible";
		//menu.style.z-index = 100;
		currentMenu = menu;
		stopTime();
	}

	menu.hideMenu = function()  {
		if (!timerOn) {
			//alert("initiliseMenu: hideMenu timer ON " + this.id);
			mytimer = setInterval("killMenu('" + this.id + "', '" + root.id + "');", 1000);
			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";
				}
			}
		}
		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 setMenuPos( menu, starter, root ){
	//alert("getMenus: Set menu position");
	if (!opera) {
		if (starter.parentNode.parentNode==root) {
			menu.style.left = starter.offsetLeft + "px";
			if( browser == "isIE" && version < 8 ){
			    menu.style.top = starter.offsetTop + starter.offsetHeight + "px";
			} else {
			    //
			    //  TJC 1/14/2008: Complete and utter hack...
			    //  forced to use graphics for top level menu instead of text,
			    //  and for the life of me can't get non-ie browser to eliminate
			    //  extra padding in that element.  Gack.  So this kludge takes care of it.
			    //  This code is ONLY used on the one page, so it won't hose up anythng else...
			    //
			    menu.style.top = ((starter.offsetTop + starter.offsetHeight) - 4)  + "px";
			}
		} else {
			menu.style.left = starter.offsetLeft + starter.offsetWidth + "px";
			menu.style.top = starter.offsetTop + "px";
		}
	} else {
		if (starter.parentNode.parentNode==root) {
			menu.style.left = starter.offsetLeft + "px";
			menu.style.top = starter.offsetHeight + "px";
		} else {
			menu.style.left = starter.offsetWidth + "px";
			menu.style.top = starter.offsetTop + "px";
		}

	}
}

function getMenus(elementItem, root) {
	var menuStarter;
	var menuItem;
	//alert("getMenus: length=" + elementItem.childNodes.length);
	for (var x=0;x<elementItem.childNodes.length;x++) {
		//alert("getMenus: nodeName=" + elementItem.childNodes[x].nodeName);
		if (elementItem.childNodes[x].nodeName=="LI") {
			//alert("getMenus: LI element");
			if (elementItem.childNodes[x].getElementsByTagName("UL").length>0) {
				//alert("getMenus: UL element - do setup");
				menuStarter = elementItem.childNodes[x].getElementsByTagName("A").item(0);
				menuItem = elementItem.childNodes[x].getElementsByTagName("UL").item(0);
				setMenuPos( menuItem, menuStarter, root);
				getMenus(menuItem, root);
				initialiseMenu(menuItem, menuStarter, root);
			}
		}
	}
}

/*
window.onload = function() {
	var root = document.getElementById("menuList");
	getMenus(root, root);
}
*/
