var oldBrowser = false;
var isNS4 = false;
var isNS6 = false;
var isIE4 = false;
var isIE5 = false;
var xPosition = 0;

if ( parseInt( navigator.appVersion ) < 4 ) {
	oldBrowser = true
} else {	
	isNS4 = (document.layers) ? true : false;
	isIE4 = (document.all && !document.getElementById) ? true : false;
	isIE5 = (document.all && document.getElementById) ? true : false;
	isNS6 = (!document.all && document.getElementById) ? true : false;
}
 
function Menu(label, mw, mh, fnt, fs, fclr, fhclr, bg, bgh,imgon, imgoff, imgid) {
	this.version = "990702 [Menu; menu.js]";
	this.type = "Menu"; // yes, we're a menu
	this.menuWidth = mw;
	this.menuItemHeight = mh;
	this.fontSize = fs||8.5;
	this.fontWeight = "normal"; 
	this.fontFamily = fnt||"Verdana, Arial, Helvetica, sans-serif";
	this.fontColor = fclr||"#000000";
	this.fontColorHilite = fhclr||"#000000";
	this.bgColor = "whitesmoke";
	
	this.menuBorder = 1;
	this.menuItemBorder = 0;
	this.menuItemBgColor = bg||"whitesmoke"; 
	this.menuLiteBgColor = "#FFFFFF"; 
	this.menuBorderBgColor = "#b82619"; 
	this.menuHiliteBgColor = bgh||"#FFFFFF"; 
	this.menuContainerBgColor = "#b82619";
	
	this.imgon = imgon;
	this.imgoff = imgoff;
	this.imgid = imgid;
	
	this.childMenuIcon = "/_inc/images/nav_arrow.gif";
	
	this.items = new Array();
	this.actions = new Array();
	this.childMenus = new Array();

	
	this.hideOnMouseOut = true;

	
	this.addMenuItem = addMenuItem;
	this.addMenuSeparator = addMenuSeparator;
	this.writeMenus = writeMenus;
	this.FW_showMenu = FW_showMenu;
	this.onMenuItemOver = onMenuItemOver;
	this.onMenuItemAction = onMenuItemAction;
	this.hideMenu = hideMenu;
	this.hideChildMenu = hideChildMenu;

	
	if (!window.menus) window.menus = new Array();

	this.label = label || "menuLabel" + window.menus.length;

	window.menus[this.label] = this;
	window.menus[window.menus.length] = this;
	
	if (!window.activeMenus) window.activeMenus = new Array();
}


function addMenuItem(label, action) {
	
	this.items[this.items.length] = label;
	this.actions[this.actions.length] = action;
}

function addMenuSeparator() {
	
	this.items[this.items.length] = "separator";
	this.actions[this.actions.length] = "";
	
	this.menuItemBorder = 0;
}


function FIND(item) {

	if (document.all) return(document.all[item]);
	
	if (document.getElementById) return(document.getElementById(item));
	
	return(false);
}

function writeMenus(container) {
	
	if (window.triedToWriteMenus) return;

	
	if (!container && document.layers) {
		
	} else if (document.all || document.hasChildNodes) {
		
		document.writeln('<SPAN ID="menuContainer"></SPAN>');
		
		container = FIND("menuContainer");
	}

	
	window.fwHideMenuTimer = null;
	
	if (!container) return;	
	
	window.triedToWriteMenus = true; 
	
	container.isContainer = true;
	container.menus = new Array();
	for (var i=0; i<window.menus.length; i++) 
		container.menus[i] = window.menus[i];
	
	window.menus.length = 0;
	var countMenus = 0;
	var countItems = 0;
	var top = 0;
	var content = ''; 
	var lrs = false;
	var theStat = "";
	var tsc = 0;
	if (document.layers) lrs = true; 
	
	for (var i=0; i<container.menus.length; i++, countMenus++) {
		
		var menu = container.menus[i];
		
		if (menu.bgImageUp) {
			
			menu.menuBorder = 0;
			menu.menuItemBorder = 0;
		}
		
		if (lrs) {
			
		} else {
			
			content += ''+
			
			'<DIV ID="menuLayer'+ countMenus +'" STYLE="position:absolute;z-index:1;left:10;top:'+ (i * 100) +';visibility:hidden;">\n'+
			
			'  <DIV ID="menuLite'+ countMenus +'" STYLE="position:absolute;z-index:1;left:'+ menu.menuBorder +';top:'+ menu.menuBorder +';visibility:hide;" onMouseOut="mouseoutMenu();">\n'+
			
			'	 <DIV ID="menuFg'+ countMenus +'" STYLE="position:absolute;left:'+ menu.menuBorder +';top:'+ menu.menuBorder +';visibility:hide;">\n'+
			'';
		}
		var x=i;
		
		for (var i=0; i<menu.items.length; i++) {
			
			var item = menu.items[i];
			var childMenu = false;	
			var defaultHeight = menu.fontSize+6;	
			var defaultIndent = menu.fontSize;	
			if (item.label) {
				
				item = item.label;
				childMenu = true;
			}
			
			menu.menuItemHeight = menu.menuItemHeight || defaultHeight;
			menu.menuItemIndent = menu.menuItemIndent || defaultIndent;
			
			var itemProps = 'font-family:' + menu.fontFamily +';font-weight:' + menu.fontWeight + ';fontSize:' + menu.fontSize + ';';
			
			if (menu.fontStyle) itemProps += 'font-style:' + menu.fontStyle + ';';
			if (document.all) 
				
				itemProps += 'font-size:' + menu.fontSize + ';" onMouseOver="onMenuItemOver(null,this);" onClick="onMenuItemAction(null,this);';
			else if (!document.layers) {
				
				itemProps += 'font-size:' + menu.fontSize + 'pt;'; // zilla wants 12px.
			}
			var l;
			if (lrs) {
				
			}
			
			var dTag	= '<DIV ID="menuItem'+ countItems +'" STYLE="position:absolute;left:0;top:'+ (i * menu.menuItemHeight) +';'+ itemProps +'">';
			
			var dClose = '</DIV>'
			
			if (menu.bgImageUp) {
				
				menu.menuBorder = 0;
				menu.menuItemBorder = 0;
				
				dTag	= '<DIV ID="menuItem'+ countItems +'" STYLE="background:url('+menu.bgImageUp+');position:absolute;left:0;top:'+ (i * menu.menuItemHeight) +';'+ itemProps +'">';
				
				if (document.layers) { // same as if(lrs) {
					
				}
			}
			
			var textProps = 'position:absolute;left:' + menu.menuItemIndent + ';top:1;';
			
			if (lrs) {
				
			}

			
			var dText	= '<DIV ID="menuItemText'+ countItems +'" STYLE="' + textProps + 'color:'+ menu.fontColor +';">'+ item +'&nbsp</DIV>\n<DIV ID="menuItemHilite'+ countItems +'" STYLE="' + textProps + 'top:1;color:'+ menu.fontColorHilite +';visibility:hidden;">'+ item +'&nbsp</DIV>';
			
			if (item == "separator") {
				
				content += ( dTag + '<DIV ID="menuSeparator'+ countItems +'" STYLE="position:absolute;left:1;top:2;"></DIV>\n<DIV ID="menuSeparatorLite'+ countItems +'" STYLE="position:absolute;left:1;top:2;"></DIV>\n' + dClose);
			} else if (childMenu) {
				
				content += ( dTag + dText + '<DIV ID="childMenu'+ countItems +'" STYLE="position:absolute;left:0;top:3;"><IMG SRC="'+ menu.childMenuIcon +'"></DIV>\n' + dClose);
			} else {
				
				content += ( dTag + dText + dClose);
			}
			
			if (lrs) {
				
			}
			
			countItems++;  
		}
		if (lrs) {
			
		} else {
			
		  content += '	  <DIV ID="focusItem'+ countMenus +'" STYLE="position:absolute;left:0;top:0;visibility:hide;" onClick="onMenuItemAction(null,this);">&nbsp;</DIV>\n';
		  content += '   </DIV>\n  </DIV>\n</DIV>\n';
		}
		
		i=x;
	}

	
	if (document.layers) {		
		
	} else {
	
		if ((!document.all) && (container.hasChildNodes)) {
			container.innerHTML=content;
		} else {
			container.document.open("text/html");
			container.document.writeln(content);
			container.document.close();	
		}
	
		if (!FIND("menuLayer0")) return;
		var menuCount = 0;
	
		for (var x=0; x<container.menus.length; x++) {
	
			var menuLayer = FIND("menuLayer" + x);
			container.menus[x].menuLayer = "menuLayer" + x;
			menuLayer.Menu = container.menus[x];
			menuLayer.Menu.container = "menuLayer" + x;
			menuLayer.style.zIndex = 5; 
		    var s = menuLayer.style; 
			s.top = s.pixelTop = -300;
			s.left = s.pixelLeft = -300;

			
			var menu = container.menus[x];
			menu.menuItemWidth = menu.menuWidth || menu.menuIEWidth || 140; 
			menuLayer.style.backgroundColor = menu.menuBorderBgColor; 
			var top = 0;
			
			for (var i=0; i<container.menus[x].items.length; i++) {
			
				var l = FIND("menuItem" + menuCount);
				l.Menu = container.menus[x]; // set the menu item's menu
				if (l.addEventListener) { // ns6
					l.style.width = menu.menuItemWidth;	
					l.style.height = menu.menuItemHeight;
					l.style.top = top; // move the menu item
					l.addEventListener("mouseover", onMenuItemOver, false);
					l.addEventListener("click", onMenuItemAction, false);
					l.addEventListener("mouseout", mouseoutMenu, false);
				} else { //ie
					l.style.pixelWidth = menu.menuItemWidth;	
					l.style.pixelHeight = menu.menuItemHeight;
					l.style.pixelTop = top; // move the menu item
				}
				// move the top down the height of a menu item
				top = top + menu.menuItemHeight+menu.menuItemBorder;
				l.style.fontSize = menu.fontSize; // set the menu item's font size...
				l.style.backgroundColor = menu.menuItemBgColor; //...background color...
				l.style.visibility = "inherit"; // ..inherit the visibility from the parent...
				l.saveColor = menu.menuItemBgColor; // save off the background color
				l.menuHiliteBgColor = menu.menuHiliteBgColor; // set the hilite background color
				l.action = container.menus[x].actions[i]; // set the action for the menu item
				l.hilite = FIND("menuItemHilite" + menuCount); // set the hilite element item
				l.focusItem = FIND("focusItem" + x); // set the focus item
				l.focusItem.style.pixelTop = l.focusItem.style.top = -30;
				var childItem = FIND("childMenu" + menuCount); // attempt to find a submenu in this menu item
				// If we have a submenu...
				if (childItem) {
					// add a property for the child menu to the menu item layer
					l.childMenu = container.menus[x].items[i].menuLayer;
					// create a little space for that nifty little submenu arrow
					childItem.style.pixelLeft = childItem.style.left = menu.menuItemWidth -11;
					childItem.style.pixelTop = childItem.style.top =(menu.menuItemHeight /2) -8;

					l.Menu.childMenus[l.Menu.childMenus.length] = l.childMenu;
				}
				var sep = FIND("menuSeparator" + menuCount); // try to find a menu separator
				if (sep) {
					sep.style.clip = "rect(0 " + (menu.menuItemWidth - 3) + " 1 0)";
					sep.style.width = sep.style.pixelWidth = menu.menuItemWidth; // set the width
					sep.style.backgroundColor = menu.bgColor; // set the background color
					sep = FIND("menuSeparatorLite" + menuCount); // grab the seperator lite "layer"
					sep.style.clip = "rect(1 " + (menu.menuItemWidth - 3) + " 2 0)";
					sep.style.width = sep.style.pixelWidth = menu.menuItemWidth; // set the width
					sep.style.backgroundColor = menu.menuLiteBgColor; // set the background color
					l.style.height = l.style.pixelHeight = menu.menuItemHeight/2; // cut the height in half
					l.isSeparator = true // mark the layer as a separator
					top -= (menu.menuItemHeight - l.style.pixelHeight) // move up the top some (we only took up half as much space)
				} else {
					l.style.cursor = "pointer"
				}
				menuCount++;
			}// end for each menu item
			menu.menuHeight = top-1; // top is where the next menu item should  begin.  But since there's not any more,
									// its the height of the menu
			var lite = FIND("menuLite" + x); // get the 'lite' layer of the menu
			var s = lite.style; // get a local reference to the 'lite' layer's style
			// set 'lite' layer's height and width
			s.height = s.pixelHeight = menu.menuHeight +(menu.menuBorder * 2);
			s.width = s.pixelWidth = menu.menuItemWidth + (menu.menuBorder * 2);
			s.backgroundColor = menu.menuLiteBgColor;

			var body = FIND("menuFg" + x);
			s = body.style;
			s.height = s.pixelHeight = menu.menuHeight + menu.menuBorder;
			s.width = s.pixelWidth = menu.menuItemWidth + menu.menuBorder;
			s.backgroundColor = menu.bgColor;  // set the background color

			s = menuLayer.style; // make a local reference to the layer's style
			s.width = s.pixelWidth  = menu.menuItemWidth + (menu.menuBorder * 4); // set its width
			s.height = s.pixelHeight  = menu.menuHeight+(menu.menuBorder*4); // set its height
		}
	}
	if (document.captureEvents) {	
		document.captureEvents(Event.MOUSEUP);
	}
	if (document.addEventListener) {	
		document.addEventListener("mouseup", onMenuItemOver, false);
	}
	if (document.layers && window.innerWidth) {
		
	}
	document.onmouseup = mouseupMenu;
	window.fwWroteMenu = true;
	status = ""; // and reset the status
}

function NS4resize() {
	
}

function onMenuItemOver(e, l) {
	FW_clearTimeout(); // since we just moused over, clear the autohide timer
	l = l || this; // if we didn't get it from the parameter
	a = window.ActiveMenuItem; // grab the active menu item
	
	if (document.layers) {
		
	} else if (l.style && l.Menu) {
		if (a) {
			a.style.backgroundColor = a.saveColor;
			if (a.hilite) a.hilite.style.visibility = "hidden"; // hide its hilite layer
			if (a.Menu.bgImageUp) {
				a.style.background = "url(" + a.Menu.bgImageUp +")";;
			}
		} 
		if (l.isSeparator) return;
		l.style.backgroundColor = l.menuHiliteBgColor;
		l.zIndex = 5;  // magic IE 4.5 mac happy doohicky.	jba
		if (l.Menu.bgImageOver) {
			l.style.background = "url(" + l.Menu.bgImageOver +")";
		}
		if (l.hilite) {
			// set the background color and visibility
			l.style.backgroundColor = l.menuHiliteBgColor;
			l.hilite.style.visibility = "inherit";
		}
		l.focusItem.style.top = l.focusItem.style.pixelTop = l.style.pixelTop;
		l.focusItem.style.zIndex = l.zIndex +1; // set its z-index to be above the menu item
		l.Menu.hideChildMenu(l); // This shows the child menu, if it has one 
	} else {
		return; // not a menu - magic IE 4.5 mac happy doohicky.  jba
	}
	window.ActiveMenuItem = l;
}

function onMenuItemAction(e, l) {
	l = window.ActiveMenuItem; // just use the ActiveMenuItem property instead of the parameter
	if (!l) return;
	hideActiveMenus();
	if (l.action) {
		eval("" + l.action);
	}
	window.ActiveMenuItem = 0;
}

function FW_clearTimeout()
{
	if (fwHideMenuTimer) clearTimeout(fwHideMenuTimer);
	fwHideMenuTimer = null;
	fwDHFlag = false;
}
function FW_startTimeout()
{
	fwStart = new Date();
	fwDHFlag = true;
	fwHideMenuTimer = setTimeout("fwDoHide()", 1000);
}

function fwDoHide()
{
	if (!fwDHFlag) return;
	var elapsed = new Date() - fwStart;
	if (elapsed < 1000) {
		fwHideMenuTimer = setTimeout("fwDoHide()", 1100-elapsed);
		return;
	}
	fwDHFlag = false;
	hideActiveMenus();
	window.ActiveMenuItem = 0;
}

function FW_showMenu(menu, x, y, child) {

	x = GetImageXPos(menu.imgid);	//NEW WAY OF DOING THINGS
	
	if (!window.fwWroteMenu) return;
	FW_clearTimeout();
	if (document.layers) {
		
	} else if (FIND("menuItem0")) {
		var l = menu.menuLayer || menu;	
		hideActiveMenus(); // hide all of the currently active menus
		if (typeof(l) == "string") {
			// ...so go find the actual menu element
			l = FIND(l);
		}
		window.ActiveMenu = l;
		var s = l.style; // make a reference to the menu element's style
		s.visibility = "inherit";// inherit our visibility
		if (x != "relative") 
			s.left = s.pixelLeft = x || (window.pageX + document.body.scrollLeft) || 0;
		if (y != "relative") 
			s.top = s.pixelTop = y || (window.pageY + document.body.scrollTop) || 0;
		l.Menu.xOffset = document.body.scrollLeft; // this is so we appear in the right place even if the user has scrolled
		l.Menu.yOffset = document.body.scrollTop;
	}
	if (menu) {
		window.activeMenus[window.activeMenus.length] = l;
	}
	
	/*   ADDED FOR IMAGE TOGGLES */
	if(menu.imgid != "" && menu.imgon != null){
		
		if( isIE5 || isNS6 ){
			document.getElementById(menu.imgid).src = menu.imgon.src;
			
		} else if(isNS4){
			
		} else {
			document.all[menu.imgid].src = menu.imgon.src;
		}
	}
}

function onMenuItemDown(e, l) {
	var a = window.ActiveMenuItem;
	if (document.layers) {    }
}

function mouseupMenu(e)
{
	hideMenu(true, e); // true means "hide happened on a mouse up"
	hideActiveMenus();
	return true;
}

function getExplorerVersion()
{
	var ieVers = parseFloat(navigator.appVersion);
	if( navigator.appName != 'Microsoft Internet Explorer' )
		return ieVers;
	var tempVers = navigator.appVersion;
	var i = tempVers.indexOf( 'MSIE ' );
	if( i >= 0 ) {
		tempVers = tempVers.substring( i+5 );
		ieVers = parseFloat( tempVers ); 
	}
	return ieVers;
}

function mouseoutMenu()
{

	if ((navigator.appName == "Microsoft Internet Explorer")
		&& (getExplorerVersion() < 4.5))
		return true;
	hideMenu(false, false); // false means we're not hiding on a mouse up.

	return true;
}

function hideMenu(mouseup, e) {
	var a = window.ActiveMenuItem; //nab the currently active menu item

	if (a && document.layers) {
		
	} else if (window.ActiveMenu && FIND("menuItem0")) {
		if (a) { // if there's an active menu item
			a.style.backgroundColor = a.saveColor;
			if (a.hilite) a.hilite.style.visibility = "hidden"; // if we have a hilite layer, hide it
			if (a.Menu.bgImageUp) {
				a.style.background = "url(" + a.Menu.bgImageUp +")";;
			}
		}
	}
	if (!mouseup && window.ActiveMenu) {
		if (window.ActiveMenu.Menu) {
			if (window.ActiveMenu.Menu.hideOnMouseOut) {
				FW_startTimeout();
			}
			return(true);
		}
	}
	return(true);
}

function PxToNum(pxStr){ 
	if (pxStr.length > 2) {
		n = Number(pxStr.substr(0, pxStr.length-2));
		return(n);
	}
	return(0);
}

function hideChildMenu(hcmLayer) {
	FW_clearTimeout(); // clear the time out 'cause we're about to show a submenu
	var l = hcmLayer; // make a reference to the submenu layer
	for (var i=0; i < l.Menu.childMenus.length; i++) {
		var theLayer = l.Menu.childMenus[i]; // make a local reference
		if (document.layers) { // for netscape 4.x
			
		} else { // for IE & Mozilla
			theLayer = FIND(theLayer); // get the real layer element (given the id)
			theLayer.style.visibility = "hidden";//mark it has hidden
		}
		theLayer.Menu.hideChildMenu(theLayer);
	}
	
	if (l.childMenu) {
		var childMenu = l.childMenu; 
		if (document.layers) { 
			
		} else if (FIND("menuItem0")) { 
			childMenu = FIND(l.childMenu); 
			var menuLayer = FIND(l.Menu.menuLayer); 
			var s = childMenu.style; 
			s.zIndex = menuLayer.style.zIndex+1; 

			if (document.all) { // ie case.
				s.pixelTop = l.style.pixelTop + menuLayer.style.pixelTop + l.Menu.menuItemHeight/3;
				s.left = s.pixelLeft = (menuLayer.style.pixelWidth) + menuLayer.style.pixelLeft -5;
			} else { // zilla case
				var top = PxToNum(l.style.top) + PxToNum(menuLayer.style.top) + l.Menu.menuItemHeight/3;
				var left = (PxToNum(menuLayer.style.width)) + PxToNum(menuLayer.style.left) -5;
				s.top = top;
				s.left = left;
			}
			childMenu.style.visibility = "inherit"; // inherit our visiblity
		} else {
			return;
		}
		window.activeMenus[window.activeMenus.length] = childMenu;
	}
}

function hideActiveMenus() {
	if (!window.activeMenus) return;
	for (var i=0; i < window.activeMenus.length; i++) {
		if (!activeMenus[i]) continue; // just a safety check
		if (activeMenus[i].visibility && activeMenus[i].Menu) { // for Netscape 4.x
			activeMenus[i].visibility = "hidden"; // mark as hidden
			activeMenus[i].Menu.container.visibility = "hidden"; //mark container as hidden as well
			activeMenus[i].Menu.container.clip.left = 0; // move the left of the container to zero 
		} else if (activeMenus[i].style) { // for IE and Mozilla
			var s = activeMenus[i].style;
			s.visibility = "hidden"; //set to hidden
			s.left = -200; 
			s.top = -200;
		}
		
		
		/*   ADDED FOR IMAGE TOGGLES */
		if(activeMenus[i].Menu.imgid != "" && activeMenus[i].Menu.imgon != null){
			
			if( isIE5 || isNS6 ){
				document.getElementById(activeMenus[i].Menu.imgid).src = activeMenus[i].Menu.imgoff.src;
				
			} else if(isNS4){
				
			} else {
				document.all[document, activeMenus[i].Menu.imgid].src = activeMenus[i].Menu.imgoff.src
			}
			
		}
		
	}
	// if we have an active menu
	if (window.ActiveMenuItem) {
		hideMenu(false, false); // set a timer to hide the menu
	}
	// clear out the active menus list
	window.activeMenus.length = 0;
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function GetImageXPos(imgId){
	
	if( isIE5 || isNS6 ){
		
		xPosition = findPosX( document.getElementById(imgId) );
	} else if(isNS4){
		
	} else {
		xPosition = findPosX( document.all[imgId] )
	}
	
	return (xPosition);
}

