// ***********************************************
// global variables


//Global variable to count number of menus created
var menuNum = 0;

//Global array to hold all the names menus created
var menuArray = new Array();

//Timer to show & hide in seconds
var timer = .8;

//Timer ID to clear timed hide of main menus
var timerID = null;


// ***********************************************
// menu constructors


// Function to build general menu object
// and write style statement defining the DIV style
function Menu(name, style){
	this.menuitemarray = new Array();
	
	this.name = name;
	this.style = style;
	
	//increments the number of menus
	menuNum++;
	menuArray[menuNum] = this.name;

	this.additem = additem;
	this.writeMenu = writeMenu;
	this.show = show;
	this.hide = hide;
	
	this.styleStr = "#"+this.name+ " { position: absolute; visibility: hidden;  z-index: "+eval(menuNum + 100)+";}";
	
	//write the style for the menus at the beginning of the document
	document.write('<style type="text/css">');
	document.write(this.styleStr);
	document.write('</style>');
}


//add a link to the menu
function additem(menuItem){
	this.menuitemarray[this.menuitemarray.length] = menuItem;
	menuItem.addParent(this);
}


function writeMenu() {
	var output = "";
	output += "<div class=\"" + this.style + "\" id=\"" + this.name + "\">\n";
	for (this.i = 0; this.i < this.menuitemarray.length; this.i++) {
		output += this.menuitemarray[this.i].writeItem(); 
	}
	output += "</div>\n";
	return (output);
}


// ***********************************************
// showing and hiding menu

//Function to show menu
function show(){
	//alert (this.name);
	if(timerID){
		clearTimeout(timerID);
	}
	hideall();	
	menushow(this);
}


// Function to actually show menu
// Tests for the right way to address the object
function menushow(menu) {
	//alert(menu.name);	
	if (document.getElementById){
		document.getElementById(menu.name).style.visibility = "visible";
	} else {
		document[menu.name].visibility = "visible";
	}
	if (menu.parent != null) {
		menushow(menu.parent);
	}
}

// Function to call menuhide with timer
// called from onmouseout
function hide(){ 
	timerID = setTimeout("hideall();",timer*1000);
}


//Function that hides all main & secondary menus
function hideall(){
	var mainObj;
		for(var i = 1; i<=menuNum; i++){
			mainObj = menuArray[i];
			menuhide(mainObj);
		}
}

//Function to actually hide menu
//first test if the div actually exists before making it invisible
function menuhide(menu){
	if (document.getElementById) {
		if (document.getElementById(menu)) {
			document.getElementById(menu).style.visibility = "hidden";
		}
	}

	else {
		if (document[menu]) {
			document[menu].visibility = "hidden";
		}
	}
}


// BASIC CLASS TO CREATE MENU ITEMS
// THAT ARE ADDED TO MENUS USING ADDMENU

/* class MenuItem */
function MenuItem(name, label, url, style){
	this.name = name;
	this.label = label;
	this.url = url;
	this.addMenu = addMenu;
	this.writeItem = writeItem;
	this.show = show;
	this.hide = hide;
	this.addParent = addParent;
	this.style = style;
}

function addMenu (menu) {
	this.menu = menu;
	menu.parent = this.parent;
}

function addParent (menu) {
	this.parent = menu;
	if (this.menu != null) 
		this.menu.parent = menu;
}

function writeItem() {
	//alert(this.name);
	var output;
	output = "<A href=\"" + this.url + "\" ";
	
	if (this.menu != null) {
		output += "onmouseout=\"" + this.parent.name + ".hide();\" onmouseover=\"" + this.menu.name + ".show(); window.status=\'"+ this.label +"\'; return true;\"";

	} else {
	output += "onmouseout=\"" + this.parent.name + ".hide();\" onmouseover=\"" + this.parent.name + ".show();\ window.status=\'"+ this.label +"\'; return true;\"";
	}
	output += " class=\"" + this.style + "\">"+ this.label + "</A>\n";
	if (this.menu != null) {
		output += this.menu.writeMenu();
		}
	return (output);
}



