/*-----------------------------------------------------------
http://www.quirksmode.org/js/findpos.html

This script finds the real position, so if you resize the page 
and run the script again, it points to the correct new position 
of the element.
-----------------------------------------------------------*/

var tmp= new Array(2);
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
//alert(curleft+"   "+curtop);
	return [curleft,curtop];
	}
}
/*
=================================================================================================
>> EASYnavmenu - XHTML/CSS/DHTML Semantically correct drop down navmenu 
=================================================================================================
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 navmenu. All that is required is the correct use of class names, and
		the application of some CSS. For the latest version of this script, go to
		http://www.easynavmenu.co.uk
		
Use:		Use of this code is free for all non-commercial applications. If you would like to 
		use the code on a commercial website, a link back to http://www.easynavmenu.co.uk is 
		required. You are free to make alterations to the code as required.
		

Copyright:	This script is copyright 2004-2005 hampton-smith limited. All rights reserved.


--------------------------------------------------------------------------------------------------
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
--------------------------------------------------------------------------------------------------
Credits: 	Inspiration/Code borrowed from Dave Lindquist (http://www.gazingus.org)
		navmenu hide functionality was aided by some code I found on http://www.jessett.com/
		Konqueror functionality was aided by some code Scott Wehrenberg emailed over
--------------------------------------------------------------------------------------------------
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
--------------------------------------------------------------------------------------------------

==================================================================================================
>> REVISION HISTORY
==================================================================================================
Date		Version		Changes/Bug Fixes/New functionality added
--------------------------------------------------------------------------------------------------
10/01/2005	1.01		Fixed known issue where hovering over a sub-navmenu, then returning to 
				its parent navmenu not via the "starter" item on that navmenu, where the 
				new item moused over is a "starter" caused the two sub-navmenus 
				to overlay each other until a further movement of the mouse. 
				Fixed known issue where returning to parent navmenu via a non-"starter"
				navmenu item on the parent navmenu would not hide the child navmenu
08/01/2005	1.00		First full release of easynavmenu.
				Stylesheets now loaded in through JavaScript to improve accessibility.
01/12/2004	0.9		Base code inherited from client project
				Added suggestion from Scott Wehrenberg enabling Konqueror functionality
--------------------------------------------------------------------------------------------------

*/
	// Load up the stylesheet for the navmenus. This code is here rather than in the html
	// document to allow for non-javascript browsers to render a seperately styled non-
	// interactive list rather than the DHTML navmenu

/*
 Navigation style sheet no longer used.
 New styles incorporated into layout CSS by jnuwer.com
	
document.write("<link rel='stylesheet' href='./navigate.css' type='text/css' />");
*/

var currentnavmenu = null;
var lastnavmenuStarter = null;
var mytimer = null;
var timerOn = false;
var opera = window.opera ? true : false;

if (!document.getElementById)
	document.getElementById = function() { return null; }

function initialiseDummy(dummy, root) {
	dummy.onmouseover = function() {
		containingnavmenu = this.parentNode.parentNode;
		for (var x=0;x<containingnavmenu.childNodes.length;x++) {
			if (containingnavmenu.childNodes[x].nodeName.toUpperCase()=="LI") {
				if (containingnavmenu.childNodes[x].getElementsByTagName("ul").length>0) {
					containingnavmenu.childNodes[x].getElementsByTagName("UL").item(0).style.visibility = 'hidden';
				}
			}
		}
	}
	dummy.onfocus = function() {
		dummy.onmouseover();
	}
}

function initialisenavmenu(navmenu, starter, root) {
	var leftstarter = false;

	if (navmenu == null || starter == null) return;
		currentnavmenu = navmenu;

	starter.onmouseover = function() {
//alert("starter.onmouseover");
		if (currentnavmenu) {
			if (this.parentNode.parentNode!=currentnavmenu) {
				currentnavmenu.style.visibility = "hidden";
				hideAllnavmenus(currentnavmenu, root);

			}
			if (this.parentNode.parentNode==root) {
				while (currentnavmenu.parentNode.parentNode!=root) {
					currentnavmenu.parentNode.parentNode.style.visibility = "hidden";
					currentnavmenu = currentnavmenu.parentNode.parentNode;
				}
			}
			currentnavmenu = null;
			this.shownavmenu();
        	}
	}

	navmenu.onmouseover = function() {
		if (currentnavmenu) {
			currentnavmenu = null;
			this.shownavmenu();
        	}
	}	

	starter.shownavmenu = function() {

		if (!opera) {
			if (this.parentNode.parentNode==root) {
/*-----------------------------------------------------------
This section revised due to quirk in calculating
offsetLeft and offsetTop in IE.  jnuwer.com
				navmenu.style.left = this.offsetLeft + "px";
				navmenu.style.top = this.offsetTop + this.offsetHeight + "px";
-----------------------------------------------------------*/
tmp=findPos(this);
//alert(tmp[0]+"   "+tmp[1])
navmenu.style.left = tmp[0] + "px";
navmenu.style.top = tmp[1] + "px";
/*-----------------------------------------------------------*/

				if (navmenu.offsetWidth < this.offsetWidth) navmenu.style.width = this.offsetWidth;
//alert(navmenu.style.left+"   "+navmenu.style.top)
			}
			else {
			 	navmenu.style.left = this.offsetLeft + this.offsetWidth + "px";
			 	navmenu.style.top = this.offsetTop + "px";
			}
		}
		else {
			var rootOffset = root.offsetLeft;
			if (this.parentNode.parentNode==root) {
				navmenu.style.left = this.offsetLeft - rootOffset + "px";
				navmenu.style.width = this.offsetWidth;
				navmenu.style.top = this.offsetHeight + "px";
			}
			else {
			 	navmenu.style.left = this.offsetWidth - rootOffset + "px";
			 	navmenu.style.top = this.offsetTop + "px"; //navmenu.style.top - navmenu.style.offsetHeight + "px";
			}

		}
		navmenu.style.visibility = "visible";
		currentnavmenu = navmenu;
	}

	starter.onfocus	 = function() {
		starter.onmouseover();
	}

	navmenu.onfocus	 = function() {
//			currentnavmenu.style.visibility="hidden";
	}

	navmenu.shownavmenu = function() {
		navmenu.style.visibility = "visible";
		currentnavmenu = navmenu;
		stopTime();
	}

	navmenu.hidenavmenu = function()  {
		if (!timerOn) {
			mytimer = setInterval("killnavmenu('" + this.id + "', '" + root.id + "');", 300);
			timerOn = true;
			for (var x=0;x<navmenu.childNodes.length;x++) {
				if (navmenu.childNodes[x].nodeName=="LI") {
					if (navmenu.childNodes[x].getElementsByTagName("UL").length>0) {
						navmenuItem = navmenu.childNodes[x].getElementsByTagName("UL").item(0);
						navmenuItem.style.visibility = "hidden";
					}
				}
			}
		}
	}

	navmenu.onmouseout = function(event) {
		this.hidenavmenu();
	}
	navmenu.onblur = function() {
		this.hidenavmenu();
	}
	starter.onmouseout = function() {
		for (var x=0;x<navmenu.childNodes.length;x++) {
			if (navmenu.childNodes[x].nodeName=="LI") {
				if (navmenu.childNodes[x].getElementsByTagName("UL").length>0) {
					navmenuItem = navmenu.childNodes[x].getElementsByTagName("UL").item(0);
					navmenuItem.style.visibility = "hidden";
				}
			}
		}
		navmenu.style.visibility = "hidden";
	}
}
function killnavmenu(navmenu, root) {
	var navmenu = document.getElementById(navmenu);
	var root = document.getElementById(root);
	navmenu.style.visibility = "hidden";
	for (var x=0;x<navmenu.childNodes.length;x++) {
		if (navmenu.childNodes[x].nodeName=="LI") {
			if (navmenu.childNodes[x].getElementsByTagName("UL").length>0) {
				navmenuItem = navmenu.childNodes[x].getElementsByTagName("UL").item(0);
				navmenuItem.style.visibility = "hidden";
			}
		}
	}
	while (navmenu.parentNode.parentNode!=root) {
		navmenu.parentNode.parentNode.style.visibility = "hidden";
		navmenu = navmenu.parentNode.parentNode;
	}
	stopTime();
}
function stopTime() {
	if (mytimer) {
	 	 clearInterval(mytimer);
		 mytimer = null;
		 timerOn = false;
	}
} 
function load_getElementById() { 
	var root = document.getElementById("navmenuList");
	var rootOffset = root.offsetLeft;
	getnavmenus(root, root);
}

addDOMLoadEvent(load_getElementById);

function getnavmenus(elementItem, root) {
	var selectedItem;
	var navmenuStarter;
	var navmenuItem;
	for (var x=0;x<elementItem.childNodes.length;x++) {
		if (elementItem.childNodes[x].nodeName.toUpperCase()=="LI") {
			if (elementItem.childNodes[x].getElementsByTagName("ul").length>0) {
				navmenuStarter = elementItem.childNodes[x].getElementsByTagName("A").item(0);
				navmenuItem = elementItem.childNodes[x].getElementsByTagName("UL").item(0);
				getnavmenus(navmenuItem, root);
				initialisenavmenu(navmenuItem, navmenuStarter, root);
			}
			else {
				initialiseDummy(elementItem.childNodes[x].getElementsByTagName("A").item(0), root);
			}
		}
	}
}
function hideAllnavmenus(elementItem, root) {
	for (var x=0;x<elementItem.childNodes.length;x++) {
		if (elementItem.childNodes[x].nodeName.toUpperCase()=="LI") {
			if (elementItem.childNodes[x].getElementsByTagName("ul").length>0) {
				elementItem.childNodes[x].getElementsByTagName("UL").item(0).style.visibility = 'hidden';
				hideAllnavmenus(elementItem.childNodes[x].getElementsByTagName("UL").item(0), root);
			}
		}
	}
}

