var Root = new Menu();
var activeMenu = null;
var activeItem = null;
var IE4 = ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4 ) && !(navigator.appVersion.indexOf("5") > -1 ));

function Menu(){
	this.name = ""; //the name of the menu
	this.myHTMLmenu = null; //the HTML element corresponding to this menu
	this.Items = new Array(); //items in this menu
	this.numItems = 0; //counts the number of items in this menu
	this.hasChildren = false; //whether or not the menu has children
	this.isChild = false; //whether or not this menu is a child
	this.parentMenu = null; //points back to parent menu
	this.parentItem = null; //points at parent item in parent menu
	this.activeItem = null; //points at currently active item in this menu
	this.visibleChildMenu = null; //points at the one visible child menu, if any
	this.hasVisChild = false; //tells you if there is a visible child or not
	this.orientation = null; //tells whether the menu is horizontal or vertical
//	this.pixelTop = 50; //integer that represents the x coordinate of the upper-left corner
//	this.pixelLeft = 100; //integer that represents the y coordinate of the upper-left corner
}

function Item(){
	event.cancelBubble = true;
	this.name = ""; //name of this item
	this.myHTMLitem = null; //the HTML element corresponding to this item
	this.itemNum = 0; //number of this item in its menu
	this.hasChild = false; //whether or not this item has a child
	this.childMenu = null; //this item's child menu
	this.myMenu = null; //the menu that this item is in
	this.isActive = false; //whether or not this is the active item in this menu
	this.size = new Size();
	
}

function Location(xCoord, yCoord){
	this.x = xCoord;
	this.y = yCoord;
}

function Size(){
	this.width = 0;
	this.height = 0;
	
}

function showThatMenu(){
//used to test the location of a menu
	eval(menuName.value).className = "visibleMenu";
}

function initMenu(){
//	showMenu.onclick = showThatMenu; //part of a test
//	findAllMenus();
	createRoot();
	activeMenu = null;
	SetMenuPosition(Root);
	var MenuText = writeMenu(Root); //displays a text version of entire menu
//	alert (MenuText);
//	setupRoot();
	
//	var MenuHTML = writeHTML(Root);
//	alert (MenuHTML);
//	RootMenu.closeAll = closeAll;
	//var blah = eval("RootItem2");
//	Root.Items[1].myHTMLitem.style.pixelHeight = 20;
}

function SetMenuPosition(menu){
//receives:  a menu
//action:  sets the position of the menu depending on the orientation of its parent menu
//returns:  nothing
	if (menu.isChild == true){
		menu.myHTMLmenu.style.position = "absolute";
		if (menu.parentMenu.orientation == "vert"){
//			menu.myHTMLmenu.style.pixelLeft = /*menu.parentMenu.myHTMLmenu.style.pixelLeft +*/ menu.parentMenu.myHTMLmenu.style.pixelWidth - 5; //just to the right
			menu.myHTMLmenu.style.pixelLeft =  menu.parentMenu.myHTMLmenu.offsetWidth; //just to the right

		} else if (menu.parentMenu.orientation == "horiz"){
			menu.myHTMLmenu.style.pixelTop = /*menu.parentMenu.myHTMLmenu.style.pixelTop + */menu.parentItem.myHTMLitem.offsetHeight; //just below
			menu.myHTMLmenu.style.pixelLeft = 0;//menu.parentMenu.myHTMLmenu.style.pixelLeft;
		}
	}
	//alert ("I just put the menu at " + menu.myHTMLmenu.style.pixelTop + ", " + menu.myHTMLmenu.style.pixelLeft);
	//drawMenuBox(menu);
	for (var j=0; j<menu.numItems; j++){
		if (menu.Items[j].hasChild == true){
			SetMenuPosition(menu.Items[j].childMenu); //position the child menu
		}
	}
}

function createRoot(){
//receives:  nothing
//action:  creates the root menu and its items (ends up recursively creating all the submenus)
//returns:  nothing
	event.cancelBubble = true;
	var topMenuStuff = RootMenu.children; //an array of HTML tags that makes up the menu
	Root.name = "RootMenu";  //the Root menu always has this name
	Root.myHTMLmenu = eval(Root.name);  //hold the actual actual HTML tag
//	alert (Root.myHTMLmenu.style.pixelTop + ":" + Root.myHTMLmenu.style.pixelLeft);
	Root.myHTMLmenu.style.position = "absolute";
//	Root.myHTMLmenu.style.pixelTop = 100;
//	Root.myHTMLmenu.style.pixelLeft = 100;

	if (Root.myHTMLmenu.className == "Hmenu"){
		Root.orientation = "horiz";
	} else if (Root.myHTMLmenu.className == "Vmenu"){
		Root.orientation = "vert";
	}
	
	//alert(Root.myHTMLmenu.style.zIndex);
	var itemCounter = 0;
	for (var j=0; j<topMenuStuff.length; j++){ //loop through all the children of the Root menu tag
		var itemHolder = topMenuStuff[j];
		//alert (itemHolder.outerHTML);
		if (itemHolder.className == "item"){  //child will be either an item or a label
			itemCounter++;
			(Root.numItems)++;
			Root.Items[itemCounter - 1] = createItem(itemHolder, Root, itemCounter); //create the item
			setItemPosition(Root.Items[itemCounter-1], Root, itemCounter, Root.orientation);
		}
	}
	if (Root.orientation == "vert") {
		Root.myHTMLmenu.style.pixelWidth = findMaxItemWidth(Root);
	} else if (Root.orientation == "horiz") {
		Root.myHTMLmenu.style.pixelHeight = 15;//dsj - setting height of menu
	}

	
/*	for (var j = 0; j<Root.Items.length; j++){
		setItemPosition(Root.Items[j], Root, j+1, "vert");	
	}*/

	//CREATE MENU BOX HERE
//	drawMenuBox(Root);
/*	
	var HTMLmenuBox = eval("menuBox");
	if (Root.orientation == "vert"){
		Root.myHTMLmenu.style.pixelWidth = findMaxItemWidth(Root);  //set the width of this menu
	} else if (Root.orientation == "horiz"){
		var menuWidth = 0;
		var menuHeight = Root.Items[0].myHTMLitem.offsetHeight;
//		Root.myHTMLmenu.style.pixelHeight = 15;//dsj - setting height of menu

		for (var j=0; j<Root.Items.length; j++){
			menuWidth += Root.Items[j].myHTMLitem.offsetWidth;
		}
		HTMLmenuBox.style.position = "absolute";
		HTMLmenuBox.className = "visibleMenu";
		HTMLmenuBox.style.pixelWidth = menuWidth +20;
		HTMLmenuBox.style.pixelHeight = menuHeight +10;
		HTMLmenuBox.style.pixelTop = Root.myHTMLmenu.style.pixelTop -5;
		HTMLmenuBox.style.pixelLeft = Root.myHTMLmenu.style.pixelLeft -2;
	}*/
	Root.myHTMLmenu.className = "visibleMenu";
}

function drawMenuBox(menu) {
	var menuBoxArea = eval("BoxArea");
	var BoxName = "BoxFor" + menu.myHTMLmenu.id;
//	alert (menu.myHTMLmenu.outerHTML);
	var thisBoxHTML = "<div id =\"" + BoxName + "\" class=\"menuBox\"></div>";
	menuBoxArea.innerHTML += thisBoxHTML;
	//menu.myHTMLmenu.innerHTML += thisBoxHTML;
	//alert (menu.myHTMLmenu.outerHTML);
//	alert (menuBoxArea.outerHTML);
	//alert (BoxName + "is a " + eval(BoxName).className);
	var thisBox = eval(BoxName);

	//alert("drawing box");
		
	if (menu.orientation == "vert"){
		menu.myHTMLmenu.style.pixelWidth = findMaxItemWidth(Root);  //set the width of this menu
		var menuWidth = menu.myHTMLmenu.style.pixelWidth;
		var menuHeight = 0;
		for (var j=0; j<menu.Items.length; j++){
			menuHeight += menu.Items[j].myHTMLitem.offsetHeight;
//			alert(menuHeight + " = menuHeight");
		}
		thisBox.style.pixelWidth = menuWidth + 20;
		thisBox.style.pixelHeight = menuHeight + 10;
//		thisBox.style.position = "relative";
		thisBox.style.position = "absolute";
//		alert ("The menu is at " + menu.myHTMLmenu.style.pixelTop + ", " + menu.myHTMLmenu.style.pixelLeft);
		thisBox.style.pixelTop = 0;//menu.myHTMLmenu.style.pixelTop - 5;
		thisBox.style.pixelLeft = 0;//menu.myHTMLmenu.style.pixelLeft - 2;
		
	} else if (menu.orientation == "horiz"){
		var menuWidth = 0;
//		var menuHeight = menu.Items[0].myHTMLitem.offsetHeight;
		var menuHeight = menu.Items[0].myHTMLitem.offsetHeight;

//		Root.myHTMLmenu.style.pixelHeight = 15;//dsj - setting height of menu

		for (var j=0; j<menu.Items.length; j++){
			menuWidth += menu.Items[j].myHTMLitem.offsetWidth;
	//		alert(menuWidth + " = menuWidth");
		}
		menuBox.style.position = "absolute";
//		menuBox.className = "visibleMenu";
//		thisBox.style.position = "relative";
		thisBox.style.pixelWidth = menuWidth + 20;
		thisBox.style.pixelHeight = menuHeight + 10;
//		alert ("The menu is at " + menu.myHTMLmenu.style.pixelTop + ", " + menu.myHTMLmenu.style.pixelLeft);
		thisBox.style.pixelTop = 0;//menu.myHTMLmenu.style.pixelTop - 5;
		thisBox.style.pixelLeft = 0;//menu.myHTMLmenu.style.pixelLeft - 2;
	}

/*	
	if (menu.isChild == true){
		if (menu.parentMenu.orientation == "vert"){
//			thisBox.style.pixelLeft = menu.parentMenu.myHTMLmenu.style.pixelWidth - 5; //just to the right
			thisBox.style.pixelLeft = menu.parentMenu.myHTMLmenu.style.pixelWidth - 5 + menu.parentMenu.myHTMLmenu.style.pixelLeft; //just to the right
			alert(menu.parentMenu.myHTMLmenu.style.pixelWidth);
		} else if (menu.parentMenu.orientation == "horiz"){
			thisBox.style.pixelTop = menu.parentItem.myHTMLitem.offsetHeight + 3; //just below
			thisBox.style.pixelLeft = 0;//menu.parentItem.myHTMLitem.offsetWidth;
			alert(menu.parentMenu.myHTMLmenu.offsetHeight);
		}
				alert(thisBox.style.pixelLeft + " = left for menu: " + menu.name);	
	}	
*/
}


function setItemPosition(itemToSet, thisMenu, itemNumber, orient) {
	var refX = thisMenu.myHTMLmenu.style.pixelLeft;
	var refY = thisMenu.myHTMLmenu.style.pixelTop;
	var totalVertOffset=0;
	var totalHorizOffset=0;
	//alert ("ref: " + refX + " x " + refY);
	
	if (orient == "horiz") {
		for (var j=1; j<itemNumber; j++) {
			totalHorizOffset += thisMenu.Items[j-1].myHTMLitem.offsetWidth;
			totalVertOffset -= thisMenu.Items[j-1].myHTMLitem.offsetHeight;
		}
		itemToSet.myHTMLitem.style.position = "relative";
	    itemToSet.myHTMLitem.style.pixelLeft = totalHorizOffset;	
		itemToSet.myHTMLitem.style.pixelTop = totalVertOffset;
//		alert("item: " + itemToSet.myHTMLitem.style.pixelLeft + " x " +itemToSet.myHTMLitem.style.pixelTop);
	} else if (orient == "vert") {
		for (var j=1; j<itemNumber; j++) {
			totalVertOffset += thisMenu.Items[j-1].myHTMLitem.offsetHeight;
//			alert ("height = " + thisMenu.Items[j].myHTMLitem.offsetHeight);
		}
//		itemToSet.myHTMLitem.style.position = "relative";
	    itemToSet.myHTMLitem.style.pixelTop = totalVertOffset;	
		itemToSet.myHTMLitem.style.pixelLeft = totalHorizOffset;
//		alert("item: " + itemToSet.myHTMLitem.style.pixelLeft + " x " +itemToSet.myHTMLitem.style.pixelTop);
	}
	
}

function findMaxItemWidth(menu){
//receives:  a menu (with items)
//action:  determines width of widest item
//returns:  an integer value equal to its widest item
	var maxWidth = 0;
	for (var j = 0; j<menu.Items.length; j++){
		var itemWidth = menu.Items[j].myHTMLitem.offsetWidth;
		if (menu.Items[j].hasChild == true){
			itemWidth += 20;  //add 20 pixels for the "more arrow"
		}
//		var itemSections = menu.Items[j].children;
//		if (itemSections != null){
//			for (var k = 0; itemSections.length; k++){
//				itemWidth += itemSections[k].offsetWidth;
//			}
//		}
//		alert (itemWidth);
		if (itemWidth > maxWidth){
			maxWidth = itemWidth;
		}
	}
	return maxWidth;
}

function createItem(it, menu, counter){
//receives:  HTML tag containing an item, the menu in which that item appears, its position in that menu
//action:  creates an Item; if it has a child menu, it hangs that menu (recursively generates all menus)
//returns:  the Item
	event.cancelBubble = true;
	var itemStuff = it.children;
	var thisItem = new Item();
//	var thisItem = eval(it.id);
	thisItem.myMenu = menu;
	thisItem.name = it.id;
	thisItem.myHTMLitem = eval(thisItem.name);
//	thisItem.myHTMLitem.style.pixelHeight = thisItem.myHTMLitem.offsetHeight + 15;
	thisItem.itemNum = counter;
	if (modDiv(counter, 2) == 1){
//		if (thisItem.myMenu.name == "RootMenu"){alert ("green");}
		thisItem.myHTMLitem.className = "itemGreen";
	} else if (modDiv(counter, 2) == 0){
//	if (thisItem.myMenu.name == "RootMenu"){alert ("red");}
		thisItem.myHTMLitem.className = "itemRed";
	}
	
//	thisItem.myMenu.myHTMLmenu.style.pixelWidth = Math.max(thisItem.myMenu.myHTMLmenu.offsetWidth, thisItem.myHTMLitem.offsetWidth);//apj
//	alert (thisItem.name + " width: " + thisItem.myHTMLitem.offsetWidth + " x " + thisItem.myHTMLitem.offsetHeight);
	for (var j = 0; j<itemStuff.length; j++){
		var oneTag = itemStuff[j];
		if (oneTag.className == "label"){
			//alert ("Item name: " + oneTag.innerText);
//			alert (thisItem.name);
		}
		if ((oneTag.className == "Hmenu") || (oneTag.className == "Vmenu")){
			//alert ("Menu tag: " + oneTag.outerHTML);
//			alert(oneTag.className);
			thisItem.hasChild = true;
			AddMoreArrow(thisItem.myHTMLitem);
//			alert (thisItem.innerHTML);
			thisItem.childMenu = hangMenu(eval(oneTag.id), thisItem);
		}
//		alert (itemStuff[j].outerHTML);
		
	}
//	thisItem.myHTMLitem.activate = activate;
//	thisItem.myHTMLitem.onmouseover = thisItem.myHTMLitem.activate;
	thisItem.myHTMLitem.onmouseover = activate;
//	thisItem.myHTMLitem.deactivate = deactivate;
//	thisItem.myHTMLitem.onmouseout = thisItem.myHTMLitem.deactivate;
//	alert(it.id + " is this tall:  " + it.offsetHeight);	
	
	return thisItem;
}
function modDiv(num, divisor){
	var quotient = num/divisor;
	var rem = quotient - Math.floor(quotient);
	var answer = Math.round(rem * divisor);
	return answer;
	
}

function activate(){
//receives:  through "this," an item (HTML tag) to activate
//action:  highlights the item and displays its child menu, if any
//returns:  nothing
//	alert (this.id);
	event.cancelBubble = true;
	//alert (this.id);
	var thisItem = FindItem(this.id, Root); //finds the Item associated with the HTML tag
	activeMenu = thisItem.myMenu; //sets the new activemenu
	//alert (thisItem.name);
	if (activeItem == null){ //only immediately after the menu is initialized
		this.className = "itemOver"; //changes attributes of item
	//	alert(this.id);
	}else if (activeItem != thisItem){
		if (activeItem != thisItem.myMenu.parentItem) {
			deactivate(activeItem);
			if (thisItem.myMenu != activeItem.myMenu){
				var findThisMenu = activeItem.myMenu.parentMenu;
				var findThisParentItem = activeItem.myMenu.parentItem;
				while (findThisMenu != thisItem.myMenu) {
					findThisParentItem = findThisMenu.parentItem;
					findThisMenu = findThisMenu.parentMenu;
					//alert ("blah " + findThisParentItem.name + " in " + findThisMenu.name);
				}
				//alert ("parentItem is " + findThisParentItem.name);
				deactivate(findThisParentItem);
			}
		}
		
		this.className = "itemOver";
	}
	activeItem = thisItem;
	if (thisItem.hasChild == true){
		ShowChildMenu(thisItem.childMenu);
		thisItem.myMenu.hasVisChild = true;
		thisItem.myMenu.visibleChildMenu = thisItem.childMenu;
	} else {
		thisItem.myMenu.visibleChildMenu = null;
	}
}

function deactivate(itemToDeact){
	event.cancelBubble = true;
	unhighlightItem(itemToDeact);
	//	itemToDeact.myHTMLitem.className = "item";
	//	if (itemToDeact.myMenu == activeMenu){
		if (itemToDeact.hasChild == true){
			HideChildMenu(itemToDeact.childMenu);
			itemToDeact.myMenu.hasVisChild = false;
		}
	//	}
}

function ShowChildMenu(childToShow){
	childToShow.myHTMLmenu.className = "visibleMenu";
}

function HideChildMenu(childToHide){
	if (childToHide.hasVisChild == true) {
		HideChildMenu(childToHide.visibleChildMenu);
		childToHide.hasVisChild = false;
	}
	for (var j=0; j<childToHide.Items.length; j++){
		unhighlightItem(childToHide.Items[j]);
	}
	childToHide.myHTMLmenu.className = "menu";	
}

//function unhighlightItem(itemToUnh){
//	itemToUnh.myHTMLitem.className = "item";
//}

function unhighlightItem(itemToUnh){
	if (modDiv(itemToUnh.itemNum, 2) == 1){
		itemToUnh.myHTMLitem.className = "itemGreen";
	} else if (modDiv(itemToUnh.itemNum, 2) == 0){
		itemToUnh.myHTMLitem.className = "itemRed";
	}
}



function FindItem(itemName, menu){
	//alert ("finding " + itemName + " looking in menu " + menu.name);
	event.cancelBubble = true;
	var foundItem = null;
	for (var j=0; j<menu.Items.length; j++){
		if (menu.Items[j].name == itemName){
			foundItem = menu.Items[j];
			j=menu.Items.length;
		}else if (menu.Items[j].hasChild == true){
			foundItem = FindItem(itemName, menu.Items[j].childMenu);
			if (foundItem != null){
				j=menu.Items.length;
			}
		}
	}
	return foundItem;
}

function AddMoreArrow(itemWithMenu){
//	alert ("Adding an arrow");
//	alert (itemWithMenu.innerHTML);
	var	childHolder = itemWithMenu.children;
//	alert (childHolder[0].outerHTML);
	childHolder[0].innerHTML += "<span id=\"" + itemWithMenu.id + "_more\" class=\"more\">4</span>";
//	alert (childHolder[0].outerHTML);
//	itemWithMenu.innerHTML += "<span id=\"" + itemWithMenu.id + "_more\" class=\"more\">4</span>";
//alert (itemWithMenu.innerText);
}


function hangMenu(newMenu, hangFromItem){ //receives an HTML menu
	event.cancelBubble = true;
	var newMenuStuff = newMenu.children;
	//alert (newMenu.innerHTML);
//	var thisMenu = eval(newMenu.id);
	var thisMenu = new Menu();
	thisMenu.name = newMenu.id;
	thisMenu.myHTMLmenu = eval(thisMenu.name);		
//	var HMenu = thisMenu.myHTMLmenu;
	//	thisMenu.id = newMenu.id;
//	alert (newMenu.outerHTML);
	thisMenu.parentItem = hangFromItem;
	thisMenu.parentMenu = hangFromItem.myMenu;
	hangFromItem.hasChild = true;
	hangFromItem.childMenu = thisMenu;
	hangFromItem.myMenu.hasChildren = true;
	thisMenu.isChild = true;
	thisMenu.activeItem = null;
	thisMenu.hasChildren = false;

	if (thisMenu.myHTMLmenu.className == "Hmenu"){
		thisMenu.orientation = "horiz";
	} else if (thisMenu.myHTMLmenu.className == "Vmenu"){
		thisMenu.orientation = "vert";
	}
		
	var itemCounter = 0;
	for (var j=0; j<newMenuStuff.length; j++){
		
		var itemHolder = newMenuStuff[j];
		//alert (itemHolder.outerHTML);
		if (itemHolder.className == "item"){
			itemCounter++;
			(thisMenu.numItems)++;
			thisMenu.Items[itemCounter - 1] = createItem(itemHolder, thisMenu, itemCounter);
			setItemPosition(thisMenu.Items[itemCounter-1], thisMenu, itemCounter, thisMenu.orientation);
		}
	}

	if (thisMenu.orientation == "vert") {
		thisMenu.myHTMLmenu.style.pixelWidth = findMaxItemWidth(thisMenu);
	} else if (thisMenu.orientation == "horiz") {
		thisMenu.myHTMLmenu.style.pixelHeight = 15;//dsj - setting height of menu
	}
	
	//DRAW BOX HERE
//	drawMenuBox(thisMenu);
	
	return thisMenu;
	
}

function writeMenu(menuToWrite){
	var testWrite = ""
	for (var j = 0; j<menuToWrite.Items.length; j++){
//	for (var eachItem in menuToWrite.Items){
	//	testWrite += eachItem.name + String.fromCharCode(10);
		testWrite += menuToWrite.Items[j].name + String.fromCharCode(10);
		if (menuToWrite.Items[j].hasChild == true){
			testWrite += writeMenu(menuToWrite.Items[j].childMenu);
		}
	}
//	var i=10;
//	char r = (char)i;
//	testWrite += String.fromCharCode(10);
	return testWrite;
}

/******************************************************
 *  HMenu.js - v. 1.05 000710                         *
 *  Cascading menu creation system                    *
 *  by Aaron Prenot                                   *
 *  Copyright (c) 2000 Aaron Prenot                   *
 *  http://www.redrival.com/aprenot                   *
 *                                                    *
 *  Published and Documented at                       *
 *     o www.redrival.com/aprenot                     *
 *     o www.webdevelopersjournal.com                 *
 *                                                    *
 *  You may use this code on a public web site only   *
 *  if this entire copyright notice appears           *
 *  unchanged. We would also appreciate you       
 *  creating a link to                                *
 *  http://www.webdevelopersjournal.com somwhere on   *
 *  your site.                                        *
 *                                                    *
 *                                                    *
 *     Please send questions and bug reports to:      *
 *             aprenot@hotmail.com                    *
 ******************************************************/
/*
var menus = new Array();
var activeItem = null;
var IE4 = ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4 ) && !(navigator.appVersion.indexOf("5") > -1 ));

function initMenu(){
		// test for IE4+, it won't work otherwise
		if(!document.all) return false;
		
		findMenus();
		menuContainer.activeMenu = null;
		menuContainer.closeAll = closeAll;
		attachMenus();
}

function findMenus(){
		var cTag = menuContainer.children;
		for(var i=0; i < cTag.length; i++){
			tcTag = cTag[i];
			if(tcTag.className == "menu"){
				var tMenu = findSubMenus(tcTag);
				menus[menus.length] = tMenu;
			}
		}
		var tHTML = "<div id=\"menuContainer\">";
		for(var i=0; i < menus.length; i++){
			var tcTag = menus[i]
			tHTML += moveHTML(tcTag);
		}
		tHTML += "</div>";
		menuContainer.outerHTML = "";
		document.body.innerHTML += tHTML;
		for(var i=0; i < menus.length; i++){
			var tcTag = menus[i];
			setupMenu(tcTag);
		}
}

function findSubMenus(menu){
		var cMenu = menu.children;
		var tMenu = new Menu();
		tMenu.id = menu.id;
		for(var i=0; i < cMenu.length; i++){
			var tcMenu = new Item();
			tcMenu.id = cMenu[i].id;
			if(tcMenu.id.indexOf("subMenu") != -1){
				++i;
				var subMenu = cMenu[i];
				tMenu.subMenus[tMenu.subMenus.length] = findSubMenus(subMenu)
				tMenu.subMenus[(tMenu.subMenus.length - 1)].isChild = true;
				tMenu.subMenus[(tMenu.subMenus.length - 1)].parentMenu = tMenu;
				tMenu.subMenus[(tMenu.subMenus.length - 1)].parentItem = tcMenu;				
				tMenu.hasChildren = true;
				tcMenu.hasMenu = true;
				tcMenu.menu = tMenu.subMenus[(tMenu.subMenus.length - 1)];
			}
			tcMenu.parentMenu = tMenu;
			tMenu.items[tMenu.items.length] = tcMenu;
		}
		return tMenu;
}

function moveHTML(menu){
		var tHTML = "";
		if(menu.hasChildren == true){
			for(var i=0; i < menu.subMenus.length; i++){
				tHTML += moveHTML(menu.subMenus[i]);
			}
		}
		var tMenu = eval(menu.id);
		var tMenuHTML = tMenu.outerHTML;
		
		tMenu.outerHTML = "";
		tHTML += tMenuHTML;
		return tHTML;
}


function setupMenu(menu){
		if(menu.hasChildren == true){
			for(var i=0; i < menu.subMenus.length; i++){
				setupMenu(menu.subMenus[i]);
			}
		}
		
		tMenu = eval(menu.id);
		tMenu.noWrap = true;
		tMenu.hasChildren = menu.hasChildren;
		tMenu.hasVisibleChild = false;
		tMenu.visibleChild = null;
		tMenu.isChild = menu.isChild;
		tMenu.onselectstart = returnFalse;
		tMenu.onclick = handleMenuClick;
		tMenu.currWidth = 0;
		tMenu.items = menu.items;
		
		for(var i=0; i < menu.items.length; i++){
			setupItem(menu.items[i]);
		}
		tMenu.style.pixelWidth += 5;

		for(var i=0; i < menu.items.length; i++){
			var tItem = eval(menu.items[i].id);
			if(!IE4){
				tItem.style.width = "100%";
				if(tItem.hasMenu == true) {
					tItem.more.style.position = "absolute";
					tItem.more.style.pixelLeft = (tMenu.style.pixelWidth - 17);
				}
			}
		}
		if(menu.isChild == true){
			tMenu.parentMenu = eval(menu.parentMenu.id);
			tMenu.parentItem = eval(menu.parentItem.id);
		}
}

function setupItem(item){
		tItem = eval(item.id);
		tItem.highlight = highlight;
		tItem.onmouseover = tItem.highlight;
		tItem.parentMenu = eval(item.parentMenu.id);
		tItem.hasMenu = false;
		tItem.menu = null;
		tItem.onclick = handleItemClick;
		tItem.ondragstart = returnFalse;
		tItem.noWrap = true;
		
		if(item.hasMenu == true){
			tItem.innerHTML += "<span id=\"" + item.id + "_more\" class=\"more\">4</span>";
			tItem.more = eval(item.id + "_more");//this line defines a new attirbute of the variable tItem
			tItem.menu = eval(item.menu.id); 
			tItem.hasMenu = true;
		}
		if(!IE4) {
			tItem.parentMenu.style.pixelWidth = Math.max(tItem.parentMenu.currWidth, tItem.offsetWidth + 20);//apj
		}
		
		
}

function highlight(){
		if(activeItem != null){
			if(activeItem != this){
				unhighlight(activeItem);	
			} else {
				return;
			}
		}
		
		event.cancelBubble = true;
	
		this.className = "menuItemOver";
		activeItem = this; //whatever variable ran this function.

		// dont open a menu thats already open
		if((this.hasMenu == true) && (this.parentMenu.hasVisibleChild == true) && (this.parentMenu.visibleChild == this.menu)) return;
		
		// if there is a menu open, close it
		if(this.parentMenu.hasChildVisible == true){
			hideMenu(this.parentMenu.visibleChild);
		}
		
		// if this item has a menu, show it
		if(this.hasMenu){
			showMenu(this.menu);
		}
}

function unhighlight(menu){
		event.cancelBubble = true;
		menu.className = "menuItem";
}

function showMenu(menu, x, y){
		event.cancelBubble = true;
		if(menu){
			if(IE4){
				for(var i=0; i < menu.items.length; i++){
					tItem = eval(menu.items[i].id);
					if(tItem.hasMenu == true) {
						tItem.more.style.pixelLeft = (menu.offsetWidth - tItem.more.offsetLeft - 17);
					}
				}
			}
			if(menu.isChild == true){
				menu.style.pixelTop = menu.parentItem.offsetTop + menu.parentMenu.offsetTop + 4;
				menu.style.pixelLeft = menu.parentMenu.offsetLeft + menu.parentMenu.offsetWidth - 4;
				menu.parentMenu.hasChildVisible = true;
				menu.parentMenu.visibleChild = menu;
				menu.style.zIndex = menu.parentMenu.style.zIndex + 1;
			} else if(x && y){
		  		menu.style.pixelTop = y;
				menu.style.pixelLeft = x;
				menuContainer.activeMenu = menu;
				document.onclick = menuContainer.closeAll;
			} 
		} else {
			menu = eval(this.menu);
			if(IE4){
				for(var i=0; i < menu.items.length; i++){
					tItem = eval(menu.items[i].id);
					if(tItem.hasMenu == true) {
						tItem.more.style.pixelLeft = (menu.offsetWidth - tItem.more.offsetLeft - 17);
					}
				}
			}
			menu.style.pixelTop = event.clientY;
			menu.style.pixelLeft = event.clientX;
			menuContainer.activeMenu = menu;
			document.onclick = menuContainer.closeAll;
		}
		menu.className = "visibleMenu";
		return false;
}

function hideMenu(menu){
		// to handle the careless child menu hiding down below
		if(menu == null) return false;
		event.cancelBubble = true;

		// i do this kind of carelessly.  i was having trouble otherwise
		hideMenu(menu.visibleChild);

		if(menu.isChild == true){
			menu.parentMenu.hasChildVisible = false;
			menu.parentMenu.visibleChild = null;
			menu.className = "menu";//apj
		} else {
//apj		  document.onclick = "";
		  menuContainer.activeMenu = menu;
		}
//apj		menu.className = "menu";
}

function closeAll(){
		hideMenu(menuContainer.activeMenu);
}

// simple function to return false
function returnFalse(){return false;}

// function to be used for later functionality
// for now it just keeps the menu open when it receives a click;
function handleMenuClick(){
		event.cancelBubble = true;
		return false;
		
}

// just like the function above, only it closes the menu
function handleItemClick(){
		event.cancelBubble = true;
		menuContainer.closeAll();
}

// searches the document for elements with a menu paramater
function attachMenus(){
		for(var i in document.all){
			if(document.all[i].menu){
				document.all[i].onclick = showMenu;
			}
		}
}
*/
