addLoadEvent(menuLoad);
var menu;

var menusets = new Object();
var menus = new Object();
var menubgs = 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;
	
	// add menu bg element so you can use opacity
	// sort of a hack way of doing it - can't find a better way to have 100% opacity on the text but not on the background.
	// simply copies the menu and changes the copy's class name to 'menubg' instead of 'menu' to apply different styling to
	var menubg = document.createElement("div");
	menubg.className = "menubg";
	menubg.innerHTML = elem.innerHTML;
	menubg.index = index;
	var elemparent = menusets[index];
	elemparent.appendChild(menubg);
	
	menubgs[index] = menubg;
}

// show a menu implicitly
function showMenu(evt){
	if(!this.hovering){
		if(this.timeout){
			clearTimeout(this.timeout);
		}
		hideCurrentOpenMenu();
		
		show(menus[this.index]);
		show(menubgs[this.index]);
		menuheads[this.index].className += " active";
		
		this.hovering = true;
		currentopenmenu = this.index;
		if(currentopenmenu == 0 || currentopenmenu == 3){
			hideSelectMenus();
		}
	}
}
// hides whatever the currently open menu is immediately
function hideCurrentOpenMenu(){
	if(currentopenmenu != -1){
		menusets[currentopenmenu].hovering = false;
		instantHideMenu(currentopenmenu);
		currentopenmenu = -1;
	}
}
// hide a menu implicitly after a duration
function hideMenu(evt){
	this.hovering = false;
	this.timeout = setTimeout("instantHideMenu(" + this.index + ")",1000);
}
// hide a menu explicitly right now
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]);
			hide(menubgs[menunum]);
		}
		if(currentopenmenu == menunum){
			currentopenmenu = -1;
		}
		if(currentopenmenu != 0 && currentopenmenu != 3){
			showSelectMenus();
		}
	}
}
// functions to hide quick search selects on home page
function hideSelectMenus() {
	if(document.getElementById && document.all){
		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() {
	if(document.getElementById && document.all){
		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";
				}
			}
		}
	}
}
