var timers=new Array();
var menus, menu_links, id_i;
// attach the mouse events to the menus when they appear on the DOM
function fastInitialise(t) {
	menus = getElementsByClassName('menu',document,'ul');
	if($('topNavNoScript'))
		$('topNavNoScript').id = 'topNav';
	for(var i=0;i<menus.length;i++) {
		menu_links = getElementsByClassName('menuLink', menus[i], 'LI');
		for(var j=0; j<menu_links.length; j++) {
			if(menu_links[j].id == '') {
				if (document.addEventListener) {
					menu_links[j].addEventListener('mouseover',showmenu,false);
					menu_links[j].addEventListener('mouseout',hidemenu,false);
				}
				else if (document.attachEvent) {
					menu_links[j].attachEvent('onmouseenter',showmenu,false);
					menu_links[j].attachEvent('onmouseleave',hidemenu,false);
				}
				menu_links[j].id = 'link_'+i+'-'+j;
				do_hide(menu_links[j].id);
			}
		}
	}
	if(t) timers['loadMenu'] = setTimeout('fastInitialise(true)',1);
}
// start initialising the menus even before the window has finished loading
fastInitialise(true);

document.write('<link type="text/css" media="screen" href="/styles/jsmenuhide.css" rel="stylesheet" />');

// when the window has loaded, stop the initialisation.
window.onload = function() {
	clearTimeout(timers['loadMenu']);
	//make sure the menu has loaded
	fastInitialise(false);
}

// simplify getElementById
function $(e) {
	return document.getElementById(e);
}

// return an array of elements with particular class.
function getElementsByClassName(searchClass, node, tag) {
	var classElements = new Array();
	if (node == null)
		node = document;
	if (tag == null)
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if (pattern.test(els[i].className)) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// show the sub menu of a menu item.
function showmenu(e) {
	var li;
	// make sure we are capturing the right event (ie. the mouse is not already inside the element)
	if(e.relatedTarget) {
		var cNode = e.relatedTarget;
		while (cNode != this && cNode.parentNode)
			cNode=cNode.parentNode;
		if (cNode==this) return;
	}

	//find the li element we are dealing with
	if(e.childNodes)
		li = e;
	else if(this.childNodes)
		li = this;
	else if(e.srcElement.childNodes)
		li = e.srcElement;
	
	// clear any delayed hiding
	clearTimeout(timers[li.id]);
	
	// find child UL elements and show them.
	if(li.childNodes)
		for(var i=0; i<li.childNodes.length; i++) {
			if(li.childNodes[i] && li.childNodes[i].nodeName=='UL') {
				//set location of menu depending on whether is a top level menu or not.
				if(li.parentNode.className.indexOf('sub') > -1) {
					//set top of menu to equal parent menu
					li.childNodes[i].style.top = li.offsetTop +'px';
					//set which side the menu appears according to screen space.
					if((li.parentNode.offsetLeft + li.offsetWidth + li.childNodes[i].offsetWidth) < (document.documentElement.clientWidth-40) || ((li.offsetParent.offsetLeft - li.childNodes[i].offsetWidth) < 0 && (li.parentNode.offsetLeft + li.offsetWidth) < (document.documentElement.clientWidth-20)))
						li.childNodes[i].style.left = li.offsetLeft + li.offsetWidth +'px';
					else
						//if((li.offsetParent.offsetLeft - li.childNodes[i].offsetWidth) > 0)
							li.childNodes[i].style.left = li.offsetLeft - li.childNodes[i].offsetWidth +'px';
						//else
						//	li.childNodes[i].style.left =  '50px';
				}
				else {
					li.childNodes[i].style.top  = li.offsetHeight +'px';
					if((li.offsetLeft + li.childNodes[i].offsetWidth) < (document.documentElement.clientWidth) || (document.documentElement.clientWidth - li.childNodes[i].offsetWidth)<0)
						li.childNodes[i].style.left  = li.offsetLeft +'px';
					else
						li.childNodes[i].style.left  = (document.documentElement.clientWidth - li.childNodes[i].offsetWidth)+'px';
				}
				//make sure the menu is on top of other menus and set to visible
				li.className = li.className + ' active';
				li.childNodes[i].style.zIndex = '1';
				li.childNodes[i].style.visibility = 'visible';
			}
		}
}

// hide the sub menu of a menu item.
function hidemenu(e) {
	var li;
	// make sure we are capturing the right event (ie. the mouse is not still inside the element)
	if(e.relatedTarget) {
		var cNode = e.relatedTarget;
		while (cNode != this && cNode.parentNode)
			cNode=cNode.parentNode;
		if (cNode==this) return;
	}
	//find the element we are dealing with
	if(e.childNodes)
		li = e;
	else if(this.childNodes)
		li = this;
	else if(e.srcElement.childNodes)
		li = e.srcElement;

	//find the sub menu to hide
	if(li.childNodes)
		for(var i=0; i<li.childNodes.length; i++)
			if(li.childNodes[i] && li.childNodes[i].nodeName=='UL') {
				li.childNodes[i].style.zIndex = '0';
				li.className = li.className.replace(' active','');
				//hide sub menus faster than top level menus
				if(li.parentNode.className.indexOf('sub') > -1) {
					timers[li.id] = setTimeout("do_hide('"+li.id+"');",10);
				}
				else {
					timers[li.id] = setTimeout("do_hide('"+li.id+"');",300);
				}
			}
}

// actually perform the hiding of the menu (could be after timeout)
function do_hide(id) {
	var li;
	li = $(id);
	if(li.childNodes)
		for(var i=0; i<li.childNodes.length; i++)
			if(li.childNodes[i] && li.childNodes[i].nodeName=='UL') {
				li.childNodes[i].style.visibility = 'hidden';
			}
}

