// init variables
var menuTimer = null;
var tDelay = 750;
var popDelay = 500;
var overItem = 0; // bug fix (mouseover for next object happens before moueout of current object)
var overMenu = 0; // bug fix (mouseover for next object happens before moueout of current object)
var winX = window.outerWidth;
var winY = window.outerHeight;
var ActiveMenus = new Array();
var gBrowser = "";
var debug = 0;


// init DOM refrence
if(is_nav4)
{
	doc = "document.layers";
	sty = "";
	
	// event propagation for netscape
	document.captureEvents(Event.MOUSEOVER|Event.MOUSEOUT);
	document.onmouseover=checkEvent;
	document.onmouseout=checkEvent;
	
	window.captureEvents(Event.RESIZE);
	window.onresize=checkEvent;
	
	gBrowser = "ns";
}
if(is_gecko)
{
	doc = "document.getElementById";
	sty = ".style";
	gBrowser = "nsGecko";
}
else if(is_ie5up)
{
	doc = "document.all";
	sty = ".style";
	gBrowser = "ie";
}

function browserDetect()
{
	if(navigator.appVersion.charAt(0) >= 4)
	{
		return true;
	}
	else
	{
		return false;
	}
}


// folder handling functions
function xDash(pString)
{
	var gotDash = 1;
	while(gotDash)
	{
		dashIndex = pString.indexOf("-");
		if(dashIndex != -1)
		{
			pString = pString.substring(0,dashIndex) + pString.substring(dashIndex+1,pString.length);
		} else {
			gotDash = 0;
		}
	}
	return pString;
}

function getParentID(itemID)
{
	var parentID = 0;
	
	for(i=0;i<allFolders["id"].length;i++)
	{
		if("i" + xDash(allFolders["id"][i]) == itemID)
		{
			parentID = xDash(allFolders["parentid"][i]);
			break;
		}
	}
	return parentID;
}

//function addToArray(itemID,menuID,parentID,hOffset,vOffset,mouseOut,mouseOver,mouseStatus,isImg,pRow,pArrow)
function addToArray(itemID,menuID,parentID,hOffset,vOffset,mouseOut,mouseOver,mouseStatus,isImg)
{
	// set newIndex
	if(ActiveMenus.length == 0)
	{
		var newIndex = 0;
	} else {
		var newIndex = ActiveMenus.length;
	}
	
	ActiveMenus[newIndex] = new Object();
	ActiveMenus[newIndex].itemID = itemID;
	ActiveMenus[newIndex].menuID = menuID;
	ActiveMenus[newIndex].parentID = parentID;
	ActiveMenus[newIndex].hOffset = hOffset;
	ActiveMenus[newIndex].vOffset = vOffset;
	ActiveMenus[newIndex].mouseOut = mouseOut;
	ActiveMenus[newIndex].mouseOver = mouseOver;
	ActiveMenus[newIndex].mouseStatus = mouseStatus;
	ActiveMenus[newIndex].isImg = isImg;
	
	//ActiveMenus[newIndex].pRow = pRow;
	//ActiveMenus[newIndex].pArrow = pArrow;
}



// event functions
/***************************************************************************************/
/*** pRow and pArrow are custom to Pacific Bearing because Gary had to get all fancy ***/
/***************************************************************************************/
//function onItemOver(itemID,menuID,hOffset,vOffset,mouseOut,mouseOver,mouseStatus,pRow,pArrow)
function onItemOver(itemID,menuID,hOffset,vOffset,mouseOut,mouseOver,mouseStatus)
{
	if(browserDetect())
	{
		stopTimer()
		overItem = 1;
		var thisParentID = getParentID(itemID);
		
		// hide all apropriate menus
		if(ActiveMenus.length != 0)
		{
			var i;
			for(i=0;i<ActiveMenus.length;i++)
			{
				if(thisParentID == 0)
				{
					hideAllMenus()
					break;
				}
				
				if(ActiveMenus[i].parentID == thisParentID)
				{
					var loopStart = ActiveMenus.length - 1;
					for(x=loopStart;x>=i;x--)
					{
						hideMenu(ActiveMenus[x].menuID)
					}
					break;
				}
			}
		}
		
		// if this menu is NOT in array
		var showFlag = 1;
		for(i=0;i<ActiveMenus.length;i++)
		{
			if(ActiveMenus[i].menuID == menuID)
			{
				var showFlag = 0;
				break;
			}
		}
		//if(showFlag) showMenu(itemID,menuID,thisParentID,hOffset,vOffset,mouseOut,mouseOver,mouseStatus,pRow,pArrow);
		if(showFlag) showMenu(itemID,menuID,thisParentID,hOffset,vOffset,mouseOut,mouseOver,mouseStatus);
	}
	else // for old browsers
	{
		currentMenuID = itemID;
		currentSubMenuID = menuID;
		currentMouseOut = mouseOut;
		if((mouseOut != "") && (mouseOver != ""))
		{
			currentItemType = "image";
		} else {
			currentItemType = "text";
		}
		if(currentItemType == "image") imgSwap(currentMenuID,mouseOver,mouseStatus);
	}
}

function onItemOut(itemID)
{
	if((overMenu != 1) && (browserDetect()))
	{
		startTimer()
	}
	overItem = 0;
	
	// for old browsers
	if(!browserDetect())
	{
		if(currentItemType == "image") imgSwap(itemID,currentMouseOut,"");
	}
}

function onMenuOver()
{
	if(browserDetect())
	{
		overMenu = 1;
		stopTimer()
	}
}

function onMenuOut()
{
	if((overItem != 1) && (browserDetect()))
	{
		startTimer()
	}
	overMenu = 0;
}

function checkEvent(e)
//netscape only
{
	if(e.target.name)
	{
		for(i=0;i<ActiveMenus.length;i++)
		{
			if(e.target.name == ActiveMenus[i].menuID)
			{
				if(e.type == "mouseover")
				{
					onMenuOver()
				}
				
				else if((e.type == "mouseout"))
				{
					onMenuOut()
				}
				break;
			}
		}
	}
	
	if(e.type == "resize")
	{
		if((winX != window.outerWidth)||(winY != window.outerHeight))
		{
			//alert(winX + ", " + window.outerWidth + "\n" + winY + ", " + window.outerHeight)
			document.location.reload();
		}
	}
	routeEvent(e);
	return true;
}



// layer functions
/***************************************************************************************/
/*** pRow and pArrow are custom to Pacific Bearing because Gary had to get all fancy ***/
/***************************************************************************************/
//function showMenu(itemID,menuID,parentID,hOffset,vOffset,mouseOut,mouseOver,mouseStatus,pRow,pArrow)
function showMenu(itemID,menuID,parentID,hOffset,vOffset,mouseOut,mouseOver,mouseStatus)
{
	// set isImg
	var isImg = 1;
	if((mouseOut == "") && (mouseOver == "")) isImg = 0;
	
	// image swap
	if(isImg) rootImgSwap(itemID,mouseOver);
	menuWinStat(mouseStatus);
	
	var objRef = getObjRef(menuID);
	objRef.top = getItemOffset(itemID,isImg,menuID,"Top") + vOffset;
	objRef.left = getItemOffset(itemID,isImg,menuID,"Left") + hOffset;
	
	addToArray(itemID,menuID,parentID,hOffset,vOffset,mouseOut,mouseOver,mouseStatus,isImg)
	
	objRef.zIndex = ActiveMenus.length;  //This controls the z-index of the subMenu Div that we want to show.  ActiveMenus.length should always be 1 as there will only be 1 subMenu showing at a time.
	objRef.visibility = "visible";
	
	//customOver(pRow,pArrow);
	
	//addToArray(itemID,menuID,parentID,hOffset,vOffset,mouseOut,mouseOver,mouseStatus,isImg,pRow,pArrow);
}

function hideMenu(menuID)
{
	if(ActiveMenus.length != 0)
	{
		for(i=0;i<ActiveMenus.length;i++)
		{
			if(ActiveMenus[i].menuID == menuID)
			{
				var isImg = ActiveMenus[i].isImg;
				var mouseOut = ActiveMenus[i].mouseOut;
				var itemID = ActiveMenus[i].itemID;
				if(isImg) rootImgSwap(itemID,mouseOut);
				menuWinStat("");
				
				//var pRow = ActiveMenus[i].pRow;
				//var pArrow = ActiveMenus[i].pArrow;
				//customOut(pRow,pArrow);
				
				break;
			}
		}
		
		var objRef = getObjRef(menuID);
		objRef.visibility = "hidden";
		ActiveMenus.length = ActiveMenus.length - 1;
	}
}

function hideAllMenus()
{
	if(ActiveMenus.length != 0)
	{
		for(i=ActiveMenus.length-1;i>=0;i--)
		{
			var isImg = ActiveMenus[i].isImg;
			var mouseOut = ActiveMenus[i].mouseOut;
			var itemID = ActiveMenus[i].itemID;
			if(isImg) rootImgSwap(itemID,mouseOut);
			menuWinStat("");
				
			//var pRow = ActiveMenus[i].pRow;
			//var pArrow = ActiveMenus[i].pArrow;
			//customOut(pRow,pArrow);
			
			var objRef = getObjRef(ActiveMenus[i].menuID);
			objRef.visibility = "hidden";
			
			ActiveMenus.length = ActiveMenus.length - 1;
		}
	}
}


// DOM functions
function getObjRef(objRef)
{
	if(browserDetect())
	{
		if(gBrowser == "nsGecko")
		{
			return eval(doc + "('" + objRef + "')" + sty);
		} else {
			return eval(doc + "." + objRef + sty);
		}
	}
}

function getItemOffset(itemID,isImg,menuID,side)
{
	var objRef = 0;
	var layerOffset = 0;
	var thisParent = getParentID(itemID);
	var calcVal = 0;
	
	// set item refrence
	if(isImg)
	{
		//item is an image
		if(thisParent == 0)
		{
			objRef = "document." + itemID;
		} else {
			objRef = doc + ".m" + thisParent + ".document." + itemID;
		}
	} else {
		//item is a text link
		if(gBrowser == "ie")
		{
			for(i=0;i<document.anchors.length;i++)
			{
				if(document.anchors[i].name == itemID)
				{
					objRef = "document.anchors[" + i + "]";
					break;
				}
			}
		} else if(gBrowser == "ns") {
			
			if(thisParent == 0)
			{
				objRef = "document.anchors[\"" + itemID + "\"]";
			} else {
				objRef = doc + ".m" + thisParent + ".document.anchors[\"" + itemID + "\"]";
			}
		} else if(gBrowser == "nsGecko") {
			objRef = "document.anchors[\"" + itemID + "\"]";
		}
	}
	
	// get location (offset) of item
	if(gBrowser == "ie")
	{
		if((side == "Top") || (thisParent == 0))
		{
			calcVal = offsetLoop(objRef,side);
		
		// get right side of items parent menu
		// (if the item is in a menu we want to line up off the side of the menu not the item)
		} else if((side == "Left") && (thisParent != 0)) {
			var parentDivLeft = eval(doc + ".m" + thisParent + ".style.left");
			var parentDivWidth = eval(doc + ".m" + thisParent + ".offsetWidth");
			calcVal = parseInt(parentDivLeft) + parentDivWidth;
		}
	}
	else if(gBrowser == "ns")
	{
		if(thisParent != 0)
		{
			// get right side of items parent menu
			// (if the item is in a menu we want to line up off the side of the menu not the item)
			if(side == "Top") layerOffset = eval(doc + ".m" + thisParent + ".top");
			if(side == "Left") layerOffset = eval(doc + ".m" + thisParent + ".left");
			if(side == "Left") layerOffset += eval(doc + ".m" + thisParent + ".clip.width");
		}
		
		if(isImg)
		{
			if(side == "Top") calcVal = eval(objRef + ".y") + layerOffset;
			if(side == "Left") calcVal = eval(objRef + ".x") + layerOffset;
		} else {
			if(side == "Top") calcVal = eval(objRef + ".y") + layerOffset;
			if((side == "Left") && (thisParent == 0)) calcVal = eval(objRef + ".x");
			if((side == "Left") && (thisParent != 0)) calcVal = layerOffset;
		}
	}
	else if(gBrowser == "nsGecko")
	{
		if(thisParent != 0)
		{
			// get right/top side of items parent menu
			myParentID = "m" + thisParent;
			var myObjRef = document.getElementById(myParentID);
			if(side == "Top") layerOffset = myObjRef.offsetTop;
			if(side == "Left") layerOffset = myObjRef.offsetLeft + myObjRef.offsetWidth;
		}
		
		if(isImg)
		{
			calcVal = offsetLoop(objRef,side);			
		} else {
			if(side == "Top") calcVal = offsetLoop(objRef,side);
			if((side == "Left") && (thisParent == 0)) calcVal = offsetLoop(objRef,side);
			if((side == "Left") && (thisParent != 0)) calcVal = layerOffset;
		}
	}
	
	return calcVal;
}

function offsetLoop(pObjRef,pSide)
{
	var theTagName = "";
	var parentRef = "";
	var returnVal = 0;
	var counter = 0;
	
	while(theTagName != "BODY")
	{
		returnVal += eval(pObjRef + parentRef + ".offset" + pSide);
		parentRef += ".offsetParent";
		theTagName = eval(pObjRef + parentRef + ".tagName");
		
		counter++
		if(counter == 2000)
		{
			alert("DOM traversing error.");
			break;
		}
	}
	
	return returnVal;
}


// timer functions
function stopTimer()
{
	clearTimeout(menuTimer)
}

function startTimer()
{
	stopTimer()
	menuTimer = setTimeout("hideAllMenus()",tDelay)
}


// user feedback functions
function rootImgSwap(imgName,newImg)
{
	if(document.images) document.images[imgName].src = eval(newImg + ".src");
}

function menuImgSwap(imgName,newImg,winStat,layerName)
{
	if(gBrowser == "ie" || gBrowser == "nsGecko")
	{
		document.images[imgName].src = eval(newImg + ".src");
	}
	else if(gBrowser == "ns")
	{
		layerRef = eval("document." + layerName);
		layerRef.document.images[imgName].src = eval(newImg + ".src");
	}
	
	window.status = winStat;
}

function menuWinStat(winStat)
{
	window.status = winStat;
}


// debug functions
if(debug)
{
	var debugLevels = 5;
	
	debugWin = window.open("","debugWin","toolbar=0,menubar=0,scrollbars=1,status=1,directories=0,resizable=1,width=400,height=550");
	
	debugWin.document.clear();
	debugWin.document.write("<div align='center'><form action='' name='debugForm'>overItem: <input type='text' name='overItem' size='1'><br>overMenu: <input type='text' name='overMenu' size='1'><br>");
	for(i=0;i<debugLevels;i++)
	{
		debugWin.document.write("<hr> &nbsp; &nbsp; Menu ID " + eval(i+1) + " : <input type='text' name='menuID_" + i + "'><br>Parent ID " + eval(i+1) + " : <input type='text' name='parentID_" + i + "'><br>Menu Label " + eval(i+1) + " : <input type='text' name='mouseStatus_" + i + "'><br>");
	}
	debugWin.document.write("<hr>Trace<br><textarea cols=40 rows=20 name='trace'></textarea><br><input type='button' value=' clear ' onClick=\"document.debugForm.trace.value=''\"");
	debugWin.document.write("<hr></form></div>");
	debugWin.document.close();
	
	setInterval("popDebug()",250)
}

function popDebug()
{
	var formRef = debugWin.document.debugForm;
	
	for(i=0;i<debugLevels;i++)
	{
		menuIDRef = eval("formRef.menuID_" + i);
		parentIDRef = eval("formRef.parentID_" + i);
		mouseStatusRef = eval("formRef.mouseStatus_" + i);
		
		if(ActiveMenus.length >= i + 1)
		{
			menuIDRef.value = ActiveMenus[i].menuID;
			parentIDRef.value = ActiveMenus[i].parentID;
			mouseStatusRef.value = ActiveMenus[i].mouseStatus;
		} else {
			menuIDRef.value = "";
			parentIDRef.value = "";
			mouseStatusRef.value = "";
		}
	}
	
	formRef.overMenu.value = overMenu;
	formRef.overItem.value = overItem;
}

function trace(pOutput)
{
	if(debug)
	{
		var traceRef = debugWin.document.debugForm.trace;
		traceRef.value += pOutput + "\n";
	}
}