function JSMenu(label){
	//Variables
	this.label = label;
	if(!window.jsMenus){ window.jsMenus = new Array(); }
	window.jsMenus[this.label] = this;
	window.jsMenus[window.jsMenus.length] = this;
	if(!window.jsMenusActive){ window.jsMenusActive = Array(); }
	if(!window.jsMenusParents){ window.jsMenusParents = Array(); }
	if(!window.currentZIndex){ window.currentZIndex = 1; }
	
	this.width = 0;
//	this.menuID = "menu" + Math.random();
	this.menuID = this.label;
	this.containerStyle = "JSMenu";
	this.menuItemStyle = "JSMenuItem";
	this.menuItemStyleOver = "JSMenuItemOver";
	this.levelMarker = "";
	this.menuItems = new Array();
	this.whereToPosition = "SW";
	this.topPadding = 0;
	this.leftPadding = 0;
	this.hideTimer = 1000;
	this.timerCatcher = null;
	this.width = 0;
	
	//Function call definitions
	this.addItem = addItem;
	this.addMenu = addMenu;
	this.printMenu = printMenu;
	this.findPos = findPos;
	this.showMenu = showMenu;
	this.hideMenu = hideMenu;
	this.hideMenus = hideMenus;
	this.stopHideMenu = stopHideMenu;
	this.checkForParent = checkForParent;
	this.parent = null;
	
	function addItem(mValue,mLink){
		this.menuItems.push(new Array("menuItem" + Math.random(),mValue,mLink));
	}
	
	function addMenu(mValue,mLink,mMenu){
		mMenu.parent = this;
		this.menuItems.push(new Array("menuItem" + Math.random(),mValue,mLink,mMenu));
	}
	
	function printMenu(){
		var currentZIndex = 1;
		if(arguments[0]){ currentZIndex = arguments[0]; }
		
		var objectsToBePrinted = new Array();
		document.write("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"" + this.containerStyle + "\" id=\"" + this.menuID + "\"");
		document.write(" style=\"z-index: " + currentZIndex + "; position: absolute; left: -1000px;\"");
		if(this.width > 0){
			document.write(" width=\"" + this.width + "\"");
		}
		document.write(">\r\n");
		for(var i = 0; i < this.menuItems.length; ++i){
			document.write("<tr><td id=\"" + this.menuItems[i][0] + "\" class=\"" + this.menuItemStyle + "\"");
			document.write(" onMouseOut=\"javascript:window.jsMenus['" + this.label + "'].hideMenu(); this.className = '" + this.menuItemStyle + "';");
			if(this.menuItems[i][3]){
				document.write("window.jsMenus['" + this.menuItems[i][3].label + "'].hideMenu();");
			}
			document.write("\" onMouseOver=\"javascript:window.jsMenus['" + this.label + "'].stopHideMenu(); this.className = '" + this.menuItemStyleOver + "';");
			//Add the hide menus command to hide all other menus except this tree
			document.write("window.jsMenus['" + this.label + "'].hideMenus(window.jsMenus['" + this.label + "']);");
			//See if there is a sub menu
			if(this.menuItems[i][3]){
				document.write("window.jsMenus['" + this.menuItems[i][3].label + "'].showMenu('" + this.menuItems[i][0] + "');");
			}
			document.write("\"");
			document.write(">");

			document.write("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">");
			document.write("<tr><td>");
			if(this.menuItems[i][2] != ""){ document.write("<a href=\"" + this.menuItems[i][2] + "\">"); }
			document.write(this.menuItems[i][1]);
			if(this.menuItems[i][2] != ""){ document.write("</a>"); }
			document.write("</td>");
			if(this.menuItems[i][3] && this.levelMarker != ""){
				document.write("<td align=\"right\">" + this.levelMarker + "</td>");
			}
			document.write("</tr></table>");
			document.write("</td>");
			document.write("</tr>\r\n");
			if(this.menuItems[i][3]){
				//Now do this menu
				objectsToBePrinted.push(this.menuItems[i][3]);
			}
		}
		document.write("</table>\r\n");
		for(var i = 0; i < objectsToBePrinted.length; ++i){
			objectsToBePrinted[i].printMenu(currentZIndex + 1);
		}
	}

	function showMenu(parentItem){
		//Start by hiding all the other menus
		this.hideMenus(this);
		var obj = document.getElementById(parentItem);
		//Make this menu available
		var datum = this.findPos(obj);
		var menuObj = document.getElementById(this.menuID);
		if(this.whereToPosition == "NE"){
			menuObj.style.top = "" + (datum[1] + this.topPadding) + "px";
			menuObj.style.left = "" + (datum[0] + obj.offsetWidth + this.leftPadding) + "px";
		}
		if(this.whereToPosition == "SE"){
			menuObj.style.top = "" + (datum[1] + obj.offsetHeight + this.topPadding) + "px";
			menuObj.style.left = "" + (datum[0] + obj.offsetWidth + this.leftPadding) + "px";
		}
		if(this.whereToPosition == "SW"){
			menuObj.style.top = "" + (datum[1] + obj.offsetHeight + this.topPadding) + "px";
			menuObj.style.left = "" + (datum[0] + this.leftPadding) + "px";
		}
		if(this.whereToPosition == "NW"){
			menuObj.style.top = "" + (datum[1] + this.topPadding) + "px";
			menuObj.style.left = "" + (datum[0] + this.leftPadding) + "px";
		}
		
		//Now check for off screen positioning
//		alert("Window Width = " + document.body.offsetWidth + "\nWindow Height = " + document.body.offsetHeight+ "\nMenu Width = " + menuObj.offsetWidth + "\nMenu left = " + menuObj.offsetLeft);
		
		//Find the farthest left position of the menu
		var flp = menuObj.offsetWidth + menuObj.offsetLeft;
		//Compare this to the right border of the window
		if(flp > document.body.offsetWidth){
			flp = document.body.offsetWidth - menuObj.offsetWidth;
			if(flp < 0){ flp = 0; }
			menuObj.style.left = "" + flp + "px";
		}
		
		//Set the zIndex
		window.currentZIndex += 1;
		menuObj.style.zIndex = window.currentZIndex;
		//Add this menu into the parents array
		window.jsMenusParents[window.jsMenusParents.length] = this;
	}
	
	function stopHideMenu(){
		var obj = null;
		for(var i = 0; i < window.jsMenus.length; ++i){
			window.jsMenus[i].timerCatcher = window.clearTimeout(window.jsMenus[i].timerCatcher);
		}
	}
	function hideMenu(){
		//This function will start the timer to hide this menu
		this.timerCatcher = window.setTimeout("window.jsMenus['" + this.label + "'].hideMenus()",this.hideTimer);
	}
	
	function hideMenus(item){
		this.stopHideMenu();
		var obj = null;
		var closeWindow = true;
		for(var i = 0; i < window.jsMenus.length; ++i){
			closeWindow = true;
			//Now don't close the parents
			//IF item.parent is set, then don't close that parent window
			obj = document.getElementById(window.jsMenus[i].menuID);
			if(item){
				if(item.menuID && item.menuID == window.jsMenus[i].menuID){
					closeWindow = false;
				}
				if(item.parent){
					//Check for this parent
					if(this.checkForParent(window.jsMenus[i],item.parent) == true){
						closeWindow = false;
					}
				}
			}
			if(closeWindow == true){
				obj.style.left = "-1000px";
			}
		}
		if(window.jsMenus.length <= 1){
			window.currentZIndex = 1;
		}
	}
	
	function checkForParent(obj,parentToCheck){
		if(obj.menuID == parentToCheck.menuID){
			return(true);
		}
		if(parentToCheck.parent && parentToCheck.parent != null){
			return(this.checkForParent(obj,parentToCheck.parent));
		}
/**
		if(obj.parent && obj.parent != null && parentToCheck.parent && parentToCheck.parent != null){
			return(this.checkForParent(obj.parent,parentToCheck.parent));
		}
**/
		return(false);
	}
	
	function findPos(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curleft,curtop];
	}
}
