// JavaScript Document

var NUMTOPSUBMENUS = 7;

function TopMenu() {
	// construct a new menu object;
	this.type="top";
	this.delay = 750;
	
	this.defaultSubMenuId = null;
	
	this.overSubMenu = mouseOver;	
	this.overMainMenu = mouseOver;
		
	this.outMainMenu = returnToDefault;
	this.outSubMenu = returnToDefault;
	

	this.mouseOver = mouseOver;
	this.showSubMenu = showSubMenu;
	this.returnToDefault = returnToDefault;
	this.hideSubMenu = hideSubMenu;
	this.hideAll = hideAll;
	this.setDefaultMenu = setDefaultTopMenu;
	
	this.returnToDefaultTimer = returnToDefaultTimer;
	this.cancelReturnToDefaultTimer = cancelReturnToDefaultTimer;
	
}



function setDefaultTopMenu(id) {
	this.defaultSubMenuId=id;
	this.returnToDefault(null,true);
}


function mouseOver(id) {
	this.hideAll();
	this.showSubMenu(id);
	this.cancelReturnToDefaultTimer();
}

function showSubMenu(id) {

	document.getElementById("topMenuOption" + id).style.backgroundColor="#cf84b4";	
	document.getElementById("topSubMenu" + id).style.display="block";
}

function cancelReturnToDefaultTimer() {
	if (this.returnToDefaultTimer()) {
		window.clearTimeout(this.returnToDefaultTimer());
		this.timerId=null;
	}
}

function returnToDefaultTimer() {
	if (!arguments[0]) {
		return this.timerId || null;
	} else {
		this.timerId = arguments[0];	
	}
}

function returnToDefault(menuid, rightNow) {
	var Tmp = this;
	this.doStuff = function () {
		Tmp.hideAll();
		if (Tmp.defaultSubMenuId) {
			//alert("defaulting to" + this.defaultSubMenuId); 
			Tmp.showSubMenu(Tmp.defaultSubMenuId);
		}
	}
	this.returnToDefaultTimer(setTimeout(this.doStuff, (rightNow ? 0 : this.delay)));
}

function hideSubMenu(id) {
		document.getElementById("topMenuOption" + id).style.backgroundColor="";	
		document.getElementById("topSubMenu" + id).style.display="none";
}

function hideAll() {
	for (var i=0; i<NUMTOPSUBMENUS; i++) {
		hideSubMenu(i+1);
	}
}
