// VARIABLES
var activeId, closingTime=200, xBody='null', yBody='null', xCoord, yCoord, xScroll=0, yScroll=0, xWin, yWin;
var IE=(navigator.appVersion.indexOf("MSIE")==-1)?false:true;
if(!IE){document.captureEvents(Event.MOUSEMOVE);};

function getBodyOffset(){ //GET PIXEL OFFSET BETWEEN WINDOW & DOCUMENT BODY
	xBody=yBody=0; if(document.body.offsetLeft){xBody=document.body.offsetLeft;}; if(document.body.offsetTop){yBody=document.body.offsetTop;};}

function getMouse(e){ //GET MOUSE POSITION
	if(!e){e=window.event;}; if(e.pageX){xCoord=e.pageX; yCoord=e.pageY;}
	else{xCoord=event.clientX+document.body.scrollLeft+document.documentElement.scrollLeft; yCoord=event.clientY+document.body.scrollTop+document.documentElement.scrollTop;};}

function getScroll(){ //GET SCROLL POSITION
	if(typeof(window.pageYOffset)=='number'){xScroll=window.pageXOffset; yScroll=window.pageYOffset;} //NETSCAPE COMPLIANT
	else if(document.body && (document.body.scrollLeft || document.body.scrollTop)){xScroll=document.body.scrollLeft; yScroll=document.body.scrollTop;} //W3C DOM COMPLIANT
	else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)){ //IE6 STANDARDS COMPLIANT MODE
		xScroll=document.documentElement.scrollLeft; yScroll=document.documentElement.scrollTop;}}

function getWindowSize(){ //GET WINDOW SIZE COORDINATES
	if(window.innerWidth){xWin=window.innerWidth; yWin=window.innerHeight} //NON-IE
	else if(document.documentElement && document.documentElement.clientWidth){xWin=document.documentElement.clientWidth; yWin=document.documentElement.clientHeight} //IE 6+ STANDARDS COMPLIANT MODE
	else if(document.body && document.body.clientWidth){ //IE 4 COMPATIBLE
		xWin=document.body.clientWidth; yWin=document.body.clientHeight};}

function menuIn(lineId){
	if(window.monitorMouse){if(document.onmousemove==monitorMouse){closeBox(currentBoxId);};}; //*** compatibility with infoBox function
	activeId=lineId; document.onmousemove=getMouse;
	//GET CURRENT WINDOW & SCROLL DIMENSIONS
	getWindowSize(); getScroll();
	//EXTRACT MENU AND LINE NUMBERS
	var mIndex=lineId.substring(1,lineId.indexOf('l')); var lIndexArray=lineId.slice(1+lineId.indexOf('l')).split('l');
	var menuVar=window['menu'+mIndex]; for(i in lIndexArray){menuVar=menuVar['item'+ lIndexArray[i]]}; var specsVar=window['menu'+mIndex].specs;
	if(specsVar.layout=='horizontal' && lIndexArray.length==1){var horizontal=true;} else{var horizontal=false;}; //DETERMINE WHETHER PARENT MENU BOX HAS HORIZONTAL LAYOUT
	// HIGHLIGHT MENU LINE
	document.getElementById(lineId).style.backgroundColor=specsVar.color; document.getElementById(lineId).style.color=specsVar.backgroundColor;
	// OBTAIN MENU LINE DIMENSIONS
	var currentRef=document.getElementById(lineId); var x=0; var y=0; var absolutePos=false;
	while(currentRef.offsetParent){x+=currentRef.offsetLeft; y+=currentRef.offsetTop; if(currentRef.style.position=='absolute'){absolutePos=true;}; currentRef=currentRef.offsetParent;};
	if(!absolutePos){x+=xBody; y+=yBody;};
	menuVar.x=x; menuVar.y=y; menuVar.w=document.getElementById(lineId).offsetWidth; menuVar.h=document.getElementById(lineId).offsetHeight;
	//OBTAIN LEFT & RIGHT WINDOW MARGINS
	var lwm=menuVar.x-xScroll-parseInt(specsVar.borderWidth); var rwm=xScroll+xWin-menuVar.x-menuVar.w-parseInt(specsVar.borderWidth);
	//GET MENU BOX DIMENSIONS
	menuVar.wbox=document.getElementById(lineId+'box').offsetWidth; menuVar.hbox=document.getElementById(lineId+'box').offsetHeight;
	//POSITION SUBMENU BOX
	if(horizontal){document.getElementById(lineId+'box').style.top=(menuVar.ybox=menuVar.y+menuVar.h)+'px';} //MENU CASCADES DOWN
	else{document.getElementById(lineId+'box').style.top=(menuVar.ybox=menuVar.y-parseInt(specsVar.borderWidth))+'px';}; //MENU CASCADES RIGHT OR LEFT
	if(horizontal){document.getElementById(lineId+'box').style.left=(menuVar.xbox=menuVar.x-parseInt(specsVar.borderWidth))+'px';} //MENU CASCADES DOWN
	else{
		if(menuVar.wbox>rwm && menuVar.wbox<lwm){document.getElementById(lineId+'box').style.left=(menuVar.xbox=menuVar.x-menuVar.wbox)+'px';}  //MENU CASCADES LEFT
		else{document.getElementById(lineId+'box').style.left=(menuVar.xbox=menuVar.x+menuVar.w)+'px';}; //MENU CASCADES RIGHT
	};
	document.getElementById(lineId+'box').style.visibility='visible';
	//DETERMINE MOUSEOVER REGION ASSOCIATED WITH CURRENT MENU LINE
	if(horizontal){menuVar.xActive=menuVar.x; menuVar.yActive=menuVar.y; menuVar.wActive=menuVar.w; menuVar.hActive=menuVar.h;}
	else{
		if(menuVar.xbox>menuVar.x){menuVar.xActive=menuVar.x; menuVar.yActive=menuVar.y; menuVar.wActive=menuVar.xbox-menuVar.x; menuVar.hActive=menuVar.h;}
		else{menuVar.xActive=menuVar.xbox+menuVar.wbox; menuVar.yActive=menuVar.y; menuVar.wActive=menuVar.x-menuVar.xbox+menuVar.w-menuVar.wbox; menuVar.hActive=menuVar.h;};};
	//ACTIVATE MOUSE MONITORING
	setTimeout('monitorMouseCM(true,"'+lineId+'")',0); //*** setTimeout here allows getMouse() time to capture mouse position -- necessary in some browsers
}

function menuOut(mIndex,lineId){
	//HIDE SUBMENU BOX
	document.getElementById(lineId+'box').style.visibility='hidden';
	//REMOVE MENU LINE HIGHLIGHT
	document.getElementById(lineId).style.backgroundColor=window['menu'+mIndex].specs.backgroundColor; document.getElementById(lineId).style.color=window['menu'+mIndex].specs.color;}

function monitorMouseCM(activeStatus,lineId){ //MONITOR MOUSE MOTION
	var mIndex=lineId.substring(1,lineId.indexOf('l')); var lIndexArray=lineId.slice(1+lineId.indexOf('l')).split('l');
	var menuVar=window['menu'+mIndex]; for(i in lIndexArray){menuVar=menuVar['item'+ lIndexArray[i]]};
	//DETERMINE MOUSEOVER STATUS
	var overRegion=true;
	with(menuVar){
		if((xCoord<xActive || xCoord>xActive+wActive || yCoord>yActive+hActive || yCoord<yActive) && (xCoord<xbox || xCoord>xbox+wbox || yCoord>ybox+hbox || yCoord<ybox)){
			overRegion=false;};};
	if(activeStatus){
		if(lineId==activeId){ //CURRENT LINE INACTIVE, BUT NONE OTHER SELECTED
			if(overRegion){setTimeout('monitorMouseCM(true,"'+lineId+'")',closingTime); return;} //CURRENT LINE REMAINS ACTIVE
			else{activeId='null';};}
		else{ //NEW ACTIVE LINE IS CHILD OF CURRENT LINE
			if(activeId.indexOf(lineId)!=-1){return;};};}
	else{
		if(activeId=='null' && overRegion){ //CURRENT LINE BECOMES ACTIVE
			activeId=lineId; setTimeout('monitorMouseCM(true,"'+lineId+'")',closingTime); return;}
		else{ //NEW ACTIVE LINE IS CHILD OF CURRENT LINE
			if(activeId.indexOf(lineId)!=-1){return;};};};
	//CURRENT LINE CLOSES, ACTION PASSES TO PARENT LINE
	menuOut(mIndex,lineId); lineId=lineId.substring(0,lineId.lastIndexOf('l')); if(lineId.indexOf('l')!=-1){setTimeout('monitorMouseCM(false,"'+lineId+'")',closingTime);};}

function setupMenu(mIndex){ //INITIALIZE MENU BOXES FOR GIVEN MENU
	if(xBody=='null'){getBodyOffset();};
	var menuVarArray=[0];
	var specsVar=window['menu'+mIndex].specs;
	while(true){
		menuVarArray[menuVarArray.length-1]++;
		var menuVar=window['menu'+mIndex]; for(var j=0;j<menuVarArray.length;j++){menuVar=menuVar['item'+menuVarArray[j]];};
		if(!menuVar){ //WRITE MENU BOX FOR CURRENT MENU LEVEL
			var boxId='m'+mIndex;
			if(menuVarArray.length==1 && specsVar.layout=='horizontal'){var horizontal=true;} else{var horizontal=false;}; //DETERMINE WHETHER MENU BOX HAS HORIZONTAL LAYOUT
			var menuVar=window['menu'+mIndex];
			for(var j=0;j<menuVarArray.length-1;j++){
				boxId+='l'+menuVarArray[j];
				menuVar=menuVar['item'+menuVarArray[j]];};
			var lineIdStem=boxId; boxId+='box';			
			var boxHTML='<table id="'+boxId+'" cellspacing="0px" style="';
			if(menuVarArray.length!=1){boxHTML+='visibility:hidden;';};
			if(menuVarArray.length!=1 || specsVar.position=='absolute'){boxHTML+=' position:absolute; left:'+specsVar.left+'px; top:'+specsVar.top+'px;'};
			boxHTML+=' width:auto; opacity:'+specsVar.opacity+'; border-style:outset; border-width:'+specsVar.borderWidth+'px; border-spacing:0px; text-align:center; background-color:'
				+specsVar.backgroundColor+'; border-color:'+specsVar.borderColor+'; ';
			boxHTML+='font-weight:'+specsVar.fontWeight+'; font-size:'+specsVar.fontSize+'; color:'+specsVar.color+';">';
			if(horizontal){boxHTML+='<tr>';}; //TOP MENU LEVEL HAS HORIZONTAL LAYOUT
			var j=0;
			while(menuVar['item'+(++j)]){
				if(!horizontal){boxHTML+='<tr>';}; //MENU LEVEL HAS VERTICAL LAYOUT
				boxHTML+='<td';
				if(!menuVar['item'+j].link){ //MENU LINE HAS SUBMENU
					boxHTML+=' id="'+lineIdStem+'l'+j+'" onmouseover="menuIn(this.id)"';}
				else{ //MENU LINE IS LINK
					boxHTML+=' onmouseover="this.style.color=\''+specsVar.linkColor+'\'" onmouseout="this.style.color=\''+specsVar.color+'\'" onclick="this.style.color=\''
						+specsVar.color+'\'; window.location=\''+menuVar['item'+j].link+'\'"';};
				boxHTML+='style="'
				if(specsVar.width=='auto'){boxHTML+='width:auto;'} else{boxHTML+='width:'+specsVar.width+'px;'};
				boxHTML+=' border-style:solid; border-width:0px;'
				if(menuVar['item'+(j+1)] && !horizontal){ //INCLUDE BOTTOM DIVIDING LINE
					boxHTML+=' border-bottom-width:1px; border-color:'+specsVar.borderColor+';'}
				else if(menuVar['item'+(j+1)] && horizontal){ //INCLUDE RIGHT DIVIDING LINE
					boxHTML+=' border-right-width:1px; border-color:'+specsVar.borderColor+';'};
				boxHTML+=' padding:4px;">'+menuVar['item'+j].name+'</td>';
				if(menuVarArray.length!=1 || specsVar.layout!='horizontal'){boxHTML+='</tr>';}; //MENU LEVEL HAS VERTICAL LAYOUT
			};
			if(menuVarArray.length==1 && specsVar.layout=='horizontal'){boxHTML+='</tr>';}; //TOP MENU LEVEL HAS HORIZONTAL LAYOUT
			boxHTML+='</table>';
			document.write(boxHTML);
			if(menuVarArray.length==1){return;}; //ALL MENU BOXES COMPLETE
			menuVarArray.length=menuVarArray.length-1; continue; //PROCEED TO HIGHER MENU LEVEL
		};
		if(!menuVar['link']){menuVarArray[menuVarArray.length]=0; continue;} //PROCEED TO DEEPER MENU LEVEL
		else{continue;}; //PROCEED AT CURRENT MENU LEVEL
	};}