var timerMenuID=null; // holds the timer object
var timerMenuRunning=false; // bolean indicating timer is running
var timerMenuChildID=null; // holds the timer object
var timerMenuChildRunning=false; // bolean indicating timer is running
var timerMenuChildClearID=null; // holds the timer object

function menuItem(name,child,label,menuURL,submenus,descr) // function creates the menu object
{
	// make variables accessible to other functions
	this.name = name; // make the name variable available to other scripts
	this.child = child; // make the child name avialable to other scripts
	this.label = label; // make the label avialable to other scripts
	this.menuURL = menuURL; // make the menuURL available to other scripts
	this.submenus = submenus; // make the number of submenus available to other scripts
	this.descr = descr; // the description describing the menu choice
	this.timer = null; // needed for the interval routine
	this.timerMenu = null; // needed for the interval routine 

	// make general methods accessible to other functions
	this.show = show; // function which shows the layer
	this.hide = hide; // function which hides the layer
	this.isVisible = isVisible; // function checks to make sure the layer is visible
	this.left = left; // function returns the left position of the layer
	this.top = top; // function returns the right position of the layer

	// make custom methods accessible to other functions
	this.Highlight = Hightlight; // function highlights the selection
	this.Unhighlight = Unhightlight; // function highlights the selection
	this.container = container; // function finds the container of the object
	this.dispChild = dispChild; // function displays the child container
	this.Timeout = Timeout; // after a set time close the menu
	this.TimeoutChild = TimeoutChild; // after a set time close the child window
	this.Erase = Erase; // this deals with the closing of a menu item
	this.EraseChild = EraseChild; // erase the child window
	this.closeMenus = closeMenus; // recursevly close all of the menus
	this.lyrRef = lyrRef; // get the reference to the layer of interest
	this.element = element; // get the reference to current layer
	this.elementStyle = elementStyle // get the style reference for the current layer
	this.childElement = childElement; // get the reference to the child laer
	this.childElementStyle = childElementStyle; // get the style reference for the child layer
	this.multiHght = multiHght; // find the position of the table of the current layer
	this.findHiclr = findHiclr; // function finds the highlight color of the current level
	this.findBgclr = findBgclr; // find the background color of the child
	
	// custom methods used to create the menus
	this.createHref = createHref; // function which calls functions to create menus
	
	// methods to clear any left over menu items
	this.clearMenus = clearMenus; // clears off any left over menus
}

function Hightlight() // function highlights the selection
{
	inmenu=true; // set the boolean to true since the mouse is over a menu
	lastmenu=this.container(); // set the pointer to the current container
	
	this.elementStyle().bgColor=highLightClr[this.findHiclr()]
	
	chgBkground(this.elementStyle(),highLightClr[this.findHiclr()]); // change the background color of the current menu

	dispInfo(this.descr); // show the info box

	if(this.child != 0){ // if there is a child then display it
		this.dispChild();
	}
}
	
function Unhightlight() // function unhighlights the selection
{
	delInfo(); // hide the info box
	
	if(this.child != 0) this.TimeoutChild(); // if there is a child hide the child layer

	this.Timeout(); // close the menu item

	chgBkground(this.elementStyle(),backGroundClr[this.findHiclr()]); // change the background color of the current menu

	currObject=txtreplace(this.name,'.','_')
	window.setTimeout(currObject+".clearMenus()",(numSec*1000)); // clear any menu objects left on the screen
}

function clearMenus()
{
	// get the root of the current container
	rootCurrent=this.container().substring(0,this.container().indexOf('-'));
	rootCurrent=rootCurrent.concat('-0');
	
	if(inmenu && (rootCurrent == 'item1-0')) return; // if we are looking at the first menu layer then return
	
	// else make sure all of the menus are hidden
	
	for(menuChild=0;menuChild<addedChildMenuRefs.length;menuChild++){
		if(isVisible(addedChildMenuRefs[menuChild])) hide(addedChildMenuRefs[menuChild]);
	}
}		

function dispChild() // display the layer that holds the child
{
	// find out the container layer that contains the current layer for positioning purpoes
	// the container layer similar to the current layer except that it has '-' instead of '.'
	// and always ends in '-1'

	containerLyr=this.lyrRef(this.container()); // get the pointer to the current container

	// the first menu is displayed below the top item
	if((this.findBgclr() == 1) && (isHorizontal == false))
	{
		this.childElementStyle().left=containerLyr.offsetLeft + 15; // set the left position child layer
		this.childElementStyle().top=containerLyr.offsetTop+containerLyr.offsetHeight-5; // set the top position of the child layer
	}
	else
	{
		this.childElementStyle().left=containerLyr.offsetLeft + containerLyr.offsetWidth-2; // set the left position child layer
		this.childElementStyle().top=containerLyr.offsetTop+((this.multiHght()-1)*layerHeight)+15; // set the top position of the child layer
	}
	
	chgBkground(this.childElementStyle(),backGroundClr[this.findBgclr()]); // set the background of the child layer

	this.show(this.childElement()); // show the child layer
}


function Timeout() // close the required menu item
{
	inmenu=false; // the pointer is no longer over the menu item
	
	newtxt=txtreplace(this.name,'.','_')
	// if the timer is running then close the menu
	timerMenuID=window.setTimeout(newtxt+".Erase()",(numSec*1000));
}

function Erase() // this deals with the closing of a menu item
{
	if(inmenu) return;

	var testLyr=GetObject(this.container()); // get the reference to the container layer

//	alert(isVisible(testLyr));

	rootContainer=root(this.container()); // get the root of the current container
	rootlastmenu=root(lastmenu); // get the root of the last container

	// if layer is already hidden then nothing needs to be done
	if(!isVisible(testLyr) || this.container() == rootContainer){
		window.clearTimeout(timerMenuID); // everything is closed therefore the counter can stop
		timerMenuID=null; // blank the timerMenuID
		return;
	}
	
	// check to make sure that the mouse is not over a menu and
	// make sure that the mouse is not over the current menu
	// or that this layer is not the parent of a displayed child menu

	
	// erase the parent level if the mouse is not over that level
	if(this.container() != rootContainer)
	{
		this.closeMenus(this.container());
	}
}

function closeMenus(name) // recursevly close all of the menus
{
	// get the root of the current container
	rootCurrent=name.substring(0,name.indexOf('-'));
	rootCurrent=rootCurrent.concat('-0');
	
	if(name == -1) return; // if name is -1, then break the recursively called function

   	if(name != root(this.container())){
		this.hide(this.lyrRef(name));
		// remove the '-1' off the layer
		name=name.substring(0,name.lastIndexOf('-'));
		// remove the index to jump up one layer and add '-1' to point to container
		name=name.substring(0,name.lastIndexOf('-'));
		name=name.concat('-1');
		
		this.closeMenus(name); // recursevily call this function
	}
}

function TimeoutChild() // after a set time close the child window
{
	newtxt=txtreplace(this.name,'.','_')
	timerMenuChildID=window.setTimeout(newtxt+".EraseChild()",(0.3*1000))  // close the menu after a delay
}

function EraseChild() // erase the child window
{
	// if child layer is already hidden then nothing needs to be done
	if(!this.isVisible(this.childElement())) return;

	// if the mouse is off the child delete the child
	if(lastmenu != this.child){
		this.hide(this.childElement());
	}

	window.clearTimeout(timerMenuChildID);
	timerMenuChildID=null; // blank the timerMenuChild
	
	return;
}


function container() // function finds the current container of the object
{
	toplevel=this.name.charAt(this.name.length-1);

	text=this.name.substring(0,this.name.lastIndexOf('.')); // remove the last number from the layer

	if(toplevel == 0) // we are at top leve and therefore can append '-0' to the string
	{
		return(text.concat('-0'));
	}
	// if there is a '.' then we are at a sublevel and we have to replace '.' with '-' to get the layer
	// then we add '-1' to denote the container that that the layer is in
	else
	{	
		// if there is no '.' it is second level so just append '-1'
		if(text.lastIndexOf('.') == -1){
			text=text.concat('-1');
		}
		// if there is '.' then replace '.' with '-' and append '-1' and return result
		else
		{
			newtxt=txtreplace(text,'.','-'); // initialize a temporary string
			text=newtxt; // set text string with '-'
			text=text.concat('-1'); // concat '-1' as the end of the string
		}
		return(text);
	}
}

function root(name) // finds the name of the root menu
{
	rootxt=name.substring(0,name.indexOf('-'));
	return(rootxt.concat('-0'));
}


function findBgclr() // find the background color of the child
{
	count=0; // initialize the counter
	
	// look through the string and count the number of '-'
	for(i=0;i<this.child.length;i++) if(this.child.charAt(i) == '-') count++;

	return(count);
}

function findHiclr() // find the Highlight color of the current level
{
	count=0; // initialize the counter
	
	// look through the string and count the number of '.'
	for(i=0;i<this.name.length;i++) if(this.name.charAt(i) == '.') count++;
	
	if(this.name.charAt(this.name.length-1) == 0) return(0); // if one fines .0, we are at the top level
	else return(count); // else the number '.' represents the number of layers
}

function multiHght() // find the position in the able of the current layer
{
	// if top layer then there is no height offset
	if(this.name.charAt(this.name.length-1) == 0) return(1);
	// find height within the table of the layer for positioning purposes
	else return(this.name.substring(this.name.lastIndexOf('.')+1,this.name.length));
}


