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;

isIE = (isIE4 || isIE5) ? true : false;
isNS = (isNS4 || isNS6) ? true : false;

// topMenus is used to keep track of the top level menus
var preloadMenuImages
preloadMenuImages();
var topMenus = new Array();

var preloadMenuFlag = false;

// Keeps track of the pointer image last changed.
var pointerImage = null;

// used to change the href of a link if the browser is current
function changeLinkIfShould(l, url) {
//	if ( isNS6 || (isIE5 && (navigator.platform).indexOf("Mac") == -1) ) {
	if ( (isNS6 || (isIE5 && (navigator.platform).indexOf("Mac") == -1)) && (navigator.userAgent).indexOf("Opera") == -1 ) {
		l.href = url;
	}
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages(img, imgSrc) {
	if (document[img].src != eval(imgSrc).src)
		{
			document[img].src = eval(imgSrc).src;
		}
}

function preloadMenuImages() {
	if (document.images) {
		subMIndicator_over = newImage("images/4.gif");
		subMIndicator_overL = newImage("images/4L.gif");
		subMIndicator = newImage("images/5.gif");
		subMIndicatorL = newImage("images/5L.gif");
		preloadMenuFlag = true;
	}
}


// Selects the target and sends the browser to the url.
function gotoPage(target, url, level) {
	if (target != "") {
		changeItemSelected( target, eval("a" + menuAtLevels[level-1]), "L", 1 );
	}
	
	top.location = url;
}

// This funciton recreates the contents for the layer of the level being opened and closes everything above it.
function showLayer(target, menuOpen, level, posX, posY)
{
	// the cooridates Netscape and IE get are different so some messy code had to be written in to make sure they end up in the same spots
	if (isNS && level > 1)
		posY += 20;
	
	
	// Hides all menus above and including the level being opened.
	for (i = level; i <= maxMenuLevels; i++) {
		if (document.getElementById("menLay" + i))
			layer = document.getElementById("menLay" + i);
			layer.innerHTML = "";
			layer.style.visibility = "hidden";
			layer.style.top = -300;
			layer.style.left = -300;
	}
	
	if ( menuAtLevels[level-1] != menuOpen ) {
		a = eval("a" + menuOpen);
		
		text = "";
		for (i = 0; i < a.length; i++) {
			if (i != 0)
				text += '<img src="images/8.gif" width=207 height=1 vspace=2>';
			text += addItm( a[i], level );
		}
		
		layer = document.getElementById("menLay" + level);
		layer.innerHTML = text;
		layer.style.top = posY-8;
		layer.style.left = posX-1;
		
		if (target != "") {
			changeItemSelected( target, eval("a" + menuAtLevels[level-2]), "", 1 );
		}
		
		layer.style.visibility = "visible";
		
		// keeps track of what menu is open at what level
		menuAtLevels[level-1] = menuOpen;
		
		// Changes the pointer images
		if ( pointerImage != null ) {
			pointerImage.src = "/images/menu/pointer.gif";
		}
		if ( document.getElementById(menuOpen + "_pointer") ) {
			pointerImage = document.getElementById(menuOpen + "_pointer");
			pointerImage.src = "/images/menu/pointer2.gif";
		}
	} else {
		// keeps track of what menu is open at what level
		menuAtLevels[level-1] = "";
		
		if (target != "") {
			changeItemSelected( target, eval("a" + menuAtLevels[level-2]), "", 0 );
		}
		
		// Changes the pointer images
		if ( pointerImage != null ) {
			pointerImage.src = "/images/menu/pointer.gif";
		}
//		if ( document.getElementById(menuOpen + "_pointer") ) {
//			pointerImage = document.getElementById(menuOpen + "_pointer");
//			pointerImage.src = "/images/menu/pointer2.gif";
//		}
	}
}
function addItm(item, level)
{
	var imgInd = "";
	myArray = item.split(",");
	itemText = myArray[0];
	itemLinkURL = myArray[1];
	myArray2 = myArray[2].split(">");
	if (myArray2.length > 1) {
		itemName = myArray2[0];
		itemLinkMenu = myArray2[1];
	} else {
		itemName = myArray[2];
		itemLinkMenu = "";
		imgInd = "L";
	}
	
//' ' + itemLinkURL + ' ' + itemName + ' ' + itemLinkMenu +

	text ='<div id="'+itemName+'_T" class="menuBox" style=" width=210">'
		+'<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>'
		+'<td align="left" width="208">';
	if ( imgInd == "L" ) {
		re = /\'/g;
		temp = itemLinkURL.replace(re, "\\'");
		text += '<a href="javascript:gotoPage(\'' + itemName + '\', \'' + temp +'\', ' + level + ');" class="menu" id="'+itemName+'_L">'+ itemText + '</a></td>';
	} else {
		text += '<a href="javascript:showLayer(\'' + itemName + '\', \'' + itemLinkMenu +'\', ' + (level+1) + ', getLeft(document.getElementById(\'' + itemName + '_T\'))+50, getTop(document.getElementById(\'' + itemName + '_T\')));" class="menu" id="'+itemName+'_L">'+ itemText + '</a></td>';
	}
	text +='<td align="right">'
		+'<img name="i'+itemName+'_I" src="images/5'+imgInd+'.gif" width=9 height=8 border=0 hspace=4>'
		+'</td></tr></table></div>';

	return text;
}

function changeItemSelected(target, array, thisImgInd, ifSelect) {
	if (array) {
		for (i = 0; i < array.length; i++) {
			// gets the item name
			var imgInd = "";
			myArray = array[i].split(",");
			myArray2 = myArray[2].split(">");
			if (myArray2.length > 1) {
				itemName = myArray2[0];
				itemLinkMenu = myArray2[1];
			} else {
				itemName = myArray[2];
				itemLinkMenu = "";
				imgInd = "L";
			}
			
			// makes it not selected
			document.getElementById(itemName +"_T").style.backgroundColor='#FFFFFF';
			if (document["i" + itemName + "_I"].src != eval("subMIndicator" + imgInd).src) {
				document["i" + itemName + "_I"].src = eval("subMIndicator" + imgInd).src;
			}
			document.getElementById(itemName +"_L").style.color='#333333';
		}
	
		if (ifSelect == 1) {
			// changes the color of the item selected
			document.getElementById(target +"_T").style.backgroundColor='#666666';
			document["i" + target + "_I"].src = eval("subMIndicator_over" + thisImgInd).src;
			document.getElementById(target +"_L").style.color='#FFFFFF';
		}
	}
}


function getTop(item) {
	var y = calculateSumOffset(item, 'offsetTop');
	if (isIE == true) {
		y += 20;
	} else{ 
		y += 1;
	}
	return y;
}
function getLeft(item) {
	return calculateSumOffset(item, 'offsetLeft')
}
function calculateSumOffset(idItem, offsetName) {
    var totalOffset = 0;
    var item = eval('idItem');
    do
    {
        totalOffset += eval('item.'+offsetName);
        item = eval('item.offsetParent');
    } while (item != null);
    return totalOffset;
}
