
var menuTimers = new Array();

window.onload = function() {

	/* Track mouse location & when to close menus */
	subs = getElementsByClassName('submenu');
	for(ii=0; ii<subs.length; ii++) {
		subs[ii].onmouseover = menuMouseover;
		subs[ii].onmouseout  = menuMouseout;
	}

	/* This should open the first-level menus... */
	subs = getElementsByClassName('entry');
	for(ii=0; ii<subs.length; ii++) {
		subs[ii].onmouseover = menuTopLevelOpen; //menuOpenSubs;
		subs[ii].onmouseout = menuTopLevelClose; //menuCloseSubs;
	}

	/* Automatically assign IDs to submenus for use by other functions */
	subs = getElementsByClassName('submenu');
	menuidx = 0;
	for(ii=0; ii<subs.length; ii++) {
		subs[ii].id = "submenu_" + menuidx;
		menuidx++;
	}
	
	/* Javascript-based hover-class on LI tags */
	subs = document.getElementById('nav').getElementsByTagName('LI');
	for(ii=0; ii<subs.length; ii++) {
		subs[ii].onmouseover = menuEntryHoverOn;
		subs[ii].onmouseout  = menuEntryHoverOut;
		
		subs[ii].onclick = menuEntryClick; /* IE6: clicking on LI fires A tag */
	}
	
}


function menuMouseover() { openMenu(this.id); return false; }
function menuMouseout()  { closeMenu(this.id); return false; }



function menuTopLevelOpen() {
	var img = this.getElementsByTagName("IMG");
	var now_src = img[0].src;

	now_src = now_src.replace(/-over\.png/g, ".png");
	now_src = now_src.replace(/\.png/g, "-over.png");
	
	img[0].src = now_src;	
	menuOpenSubs(this);
}
function menuTopLevelClose() {
	var img = this.getElementsByTagName("IMG");
	var now_src = img[0].src;
	now_src = now_src.replace(/-over\.png/g, ".png");
	img[0].src = now_src;	
	menuCloseSubs(this);
}


/* Applies "hover" class to A tags under the LI (easier to directy target A tags than children of LIs without the child selector) */
function menuEntryHoverOn() { addClass(this, 'hover'); addClass(this.childNodes[0], 'hover'); if(hasClass(this,'hassub')) menuOpenSubs(this); }
function menuEntryHoverOut() { removeClass(this, 'hover'); removeClass(this.childNodes[0], 'hover'); if(hasClass(this,'hassub')) menuCloseSubs(this); }

function menuEntryClick() {
	if(this.childNodes[0].tagName == 'A' && this.childNodes[0].click )
		this.childNodes[0].click();
}

function openMenu( id ) {
	clearTimeout(menuTimers[id]); menuTimers[id] = 0;
	var menu = document.getElementById(id);
	menu.style.display = "block";
}

function closeMenu_now( id ) {
	var menu = document.getElementById(id);
	menu.style.display = "none";
	menuTimers[id] = 0;
}

function closeMenu(id) {
	if( typeof(menuTimers[id]) == 'undefined' || menuTimers[id] == 0 ) {
		menuTimers[id] = setTimeout( 'closeMenu_now(\''+id+'\')', 10);
	}
}

function menuOpenSubs(ob) {
	if( typeof(ob)=='undefined' || typeof(ob.childNodes)=='undefined' ) ob = this; /* Allows function to be called directly by node or targeted to specific node */
	for(ii=0;ii<ob.childNodes.length;ii++) {
		if(ob.childNodes[ii].tagName == "UL") {
			openMenu( ob.childNodes[ii].id );
			break;
		}
	}
	return false;
}

function menuCloseSubs(ob) {
	if( typeof(ob)=='undefined' || typeof(ob.childNodes)=='undefined' ) ob = this; /* Allows function to be called directly by node or targeted to specific node */
	for(ii=0;ii<ob.childNodes.length;ii++) {
		if(ob.childNodes[ii].tagName == "UL") {
			closeMenu( ob.childNodes[ii].id );
			break;
		}
	}
	return false;
}
