addLoadEvent(menuLoad);
var menu;

var menusets = new Object();
var menus = new Object();
var menuheads = new Object();

var currentopenmenu = -1;

// initialization
function menuLoad(){
	menu = $('ddnav');
	var alldivs = menu.getElementsByTagName("div");
	// populate the menus submenus arrays
	for(var i=0; i<alldivs.length; i++){
		if(alldivs[i].className == "menuset"){
			addMenuSet(alldivs[i],i);
		}
	}
}
// functions to populate menu arrays and add event handlers
// invoked by menuLoad
function addMenuSet(elem,index){
	menusets[index] = elem;
	elem.index = index;
	elem.onmouseover = hideCurrentOpenMenu;
	var children = elem.getElementsByTagName("*");
	for(var i=0; i<children.length; i++){
		if(children[i].className == "menuhead"){
			addMenuHead(children[i],index);
		} else if(children[i].className == "menu"){
			addMenu(children[i],index);
			elem.hovering = false;
			elem.onmouseover = showMenu;
			elem.onmouseout = hideMenu;
		}
	}
}
function addMenuHead(elem,index){
	menuheads[index] = elem;
	elem.index = index;
}
function addMenu(elem,index){
	menus[index] = elem;
	elem.index = index;
}

// show a menu implicitly
function showMenu(evt){
	if(!this.hovering){
		if(this.timeout){
			clearTimeout(this.timeout);
		}
		hideCurrentOpenMenu();
		
		show(menus[this.index]);
		menuheads[this.index].className += " active";
		
		this.hovering = true;
		currentopenmenu = this.index;
		
		if(
			document.all && document.getElementById &&
			(
				this == $('dropdown1') || this == $('dropdown2') || this == $('dropdown3')
			)
		){
			hideSelectMenus();
		}
	}
}
function hideCurrentOpenMenu(){
	if(currentopenmenu != -1){
		menusets[currentopenmenu].hovering = false;
		instantHideMenu(currentopenmenu);
		currentopenmenu = -1;
	}
}
// hide a menu implicitly
function hideMenu(evt){
	this.hovering = false;
	this.timeout = setTimeout("instantHideMenu(" + this.index + ")",1000);
}
// explicitly hide a menu
function instantHideMenu(menunum){
	// check if they reactivated the menu while the timeout was running
	// don't hide the menu if they did
	if(!menusets[menunum].hovering){
		if(menus[menunum]){
			menuheads[menunum].className = menuheads[menunum].className.replace(" active","");
			hide(menus[menunum]);
		}
		if(currentopenmenu == menunum){
			currentopenmenu = -1;
		}
		
		if(
			document.all && document.getElementById &&
			(
				this == $('dropdown1') || this == $('dropdown2') || this == $('dropdown3')
			)
		){
			showSelectMenus();
		}
	}
}

// functions to hide quick search selects on home page
function hideSelectMenus() {
	for (var i=0; i<document.forms.length; i++) {
		var f=document.forms[i];
		for (var j=0; j<f.length; j++) {
			var t=f[j].type.toLowerCase();
			if (t.indexOf("select") >= 0) {
				f[j].style.visibility="hidden";
			}
		}
	}
}

function showSelectMenus() {
	for (var i=0; i<document.forms.length; i++) {
		var f=document.forms[i];
		for (var j=0; j<f.length; j++) {
			var t=f[j].type.toLowerCase();
			if (t.indexOf("select") >= 0) {
				f[j].style.visibility="visible";
			}
		}
	}
}
