// NOW WRITTEN TO THE PAGE BY THE CONTROL
//var sMenuMouseOverBGColor = '#0B5173';
//var sMenuMouseOverColor = '#ffe984';
//	var sSubMenuMouseOverBGColor = '#0B5173';
//	var sSubMenuMouseOverColor = '#ffe984';

//var iOffsetY = -3;
//var iOffsetX = 13;
//var sOrientation = 'horizontal OR vertical'; 
// var iMenuItemHeight = 18;
//var iMillisecondsToDisappear = 500;

var iMenuDepth = 10;
iMenuDepth++;

var objLast = null;

var arrPositions = new Array(iMenuDepth);
var arrX = new Array(iMenuDepth);
var arrY = new Array(iMenuDepth);
var iPosition = 0;
var iLevel = -1;
var bMenuVisible = false;
var bSelectsVisible = true;

var ivStartMenu = null;
var ivStartOffMenuTimer = null;
var ivFadeIn = null;
var sFadeID = null;
var sPrevFadeID = null;

var iMinOpacity = 10;
var iMaxOpacity = 100;
var iOpacityIncrement = 10;
var iFadeSpeedMS = 10;
var oLastRollover = null;

for(var i=0; i<iMenuDepth; i++) {
	arrX[i] = 0;
	arrY[i] = 0;
}

preLoadImages();

ivStartMenu = setInterval("InitMenu()", 300);

function rollover(objElement, bOnOrOff) {
	event.cancelBubble = true;

	try {
		// this makes completely solid (non-opaque) the section that contains the row you're hovering over
		objElement.parentElement.parentElement.firstChild.filters.alpha.opacity = iMaxOpacity;
	}
	catch(e) { }
	
	clearTime();
	bMenuVisible = true;
	var id = objElement.id.substring(3);
	var iNewLevel = parseInt(objElement.level) - 1;

	var idLast = -1;
	try{
		idLast = objLast.id.substring(7);
	}
	catch(e) { }

	if(objElement == oLastRollover ) 
		return false;

	oLastRollover = objElement;

	if(iNewLevel == iLevel && iLevel >= 0) {
		sFadeID = null;
	}

	iLevel = iNewLevel;
	
	// CHANGE ROW COLORS
	if(iLevel == 0) {
		colorCell(objElement, sMenuMouseOverBGColor, sMenuMouseOverColor);
	} else {
		colorCell(objElement, sSubMenuMouseOverBGColor, sSubMenuMouseOverColor);
	}

	if(arrPositions[iPosition] != null && iPosition >= iLevel) {
		// CHANGE ROW COLORS BACK
		objElement = document.all['row' + arrPositions[iPosition]];
		if(id != arrPositions[iLevel]) {
			resetCell(objElement);
		}
	}

	var sPositions = "," + arrPositions.join(",") + ",";
	var bNewIndex = (sPositions.indexOf("," + id + ",") == -1);

	if(iPosition > iLevel) {
		// WHEN YOU'RE MOUSING OVER A ROW LOWER THAN THE HIGHEST VISIBLE LAYER.
		for(var i=iPosition; i >= iLevel; i--) {
			// MAKE ALL HIGHER MENUS DISAPPEAR

			var theRow = document.all["row" + arrPositions[i+1]];
			if(theRow != null) {
				resetCell(theRow);
			}

			// don't dissappear the submenu of the item you're on
			if(bNewIndex || i > iLevel) {
				var theObj = document.all['innerMenu' + arrPositions[i]];
				if(theObj != null) {
					theObj.style.display = 'none';
				}
			}
		}

		for(var i= iLevel+1; i<arrPositions.length; i++) {
			arrPositions[i] = null;
		}

		for(var i= iLevel; i<arrX.length; i++) {
			arrX[i] = 0;
			arrY[i] = 0;
		}

		if(id != arrPositions[iLevel]) {
			// IF GOING BACK 1 LEVEL TO THE LEFT, MAKE RIGHT MENU DISAPPEAR
			var obj = document.all["row" + arrPositions[iLevel]];
			resetCell(obj);
		}
	}

	var theObj = document.all["innerMenu" + id];
	if(theObj == null) {
		objLast = objElement;
		
		if(iLevel ==0) {
			showSelects(document);
		}
	}

	arrPositions[iLevel] = id;
	iPosition = iLevel;

	return true;;
}

function showSubMenu(id, objSrc) {
	hideSelects(document);
	//event.cancelBubble = true;

	var theObj = document.all["innerMenu" + id];
	var bRepeatDisplay = (theObj.style.display == 'block');
	
	var iLevel = theObj.level -1 ;
	var bMousingOverHorizItem = (sOrientation == 'Horizontal' && iLevel == 1);
	var id = theObj.id.substring(9);
	objLast = objSrc;
	
	var iWindowHeight = document.body.offsetHeight - 4;
	var iWindowWidth = document.body.offsetWidth - 22;

	// WEIRD BUG OF MOVING STRAIGHT UP FROM ONE SUBMENU TO SELECT ANOTHER
	var counter = 0;

	for(var i = iLevel-1; i<arrPositions.length; i++) {
		// FIX THE X/Y COORDINATES
		arrX[i] = 0;
		arrY[i] = 0;

		if(arrPositions[i] != null) {
			var theRow = document.all["row" + arrPositions[i]];
			if(theRow != null) {
				resetCell(theRow);
			}

			theRow = document.all["innerMenu" + arrPositions[i]];
			if(theRow != null) {
				theRow.style.display = 'none';
			}
		}

		counter++;
	}

	arrPositions[iLevel-1] = id;

	var theRow = document.all["row" + id];
	if(iPosition == 0) {
		colorCell(theRow, sMenuMouseOverBGColor, sMenuMouseOverColor);
	} else {
		colorCell(theRow, sSubMenuMouseOverBGColor, sSubMenuMouseOverColor);
	}

	iPosition = iLevel;

	var x;
	var y;

	
	if(bMousingOverHorizItem == false) {
		x = getPosX(objSrc);
		y = getPosY(objSrc);
	} else {
		x = getPosX(objSrc.parentElement);
		y = getPosY(objSrc.parentElement);
	}

	theObj.style.visibility = 'hidden';
	theObj.style.display = 'block';

	var theRect = theObj.getBoundingClientRect();
	var subMenuHeight = theRect.bottom - theRect.top;
	var subMenuWidth = (theRect.right - theRect.left);
	
	var oldX = x;
	var oldY = y;

	if(bMousingOverHorizItem == true) {
		y += iHorizontalItemOffsetY;
		x += iHorizontalItemOffsetX;
	} else {
		y += iVerticalItemOffsetY;
		x += iVerticalItemOffsetX;
	}
	// slight menu offset

	//if stuff goes off the screen, reposition it
	if(y + subMenuHeight > iWindowHeight) {
		if(bMousingOverHorizItem == true) {
			y = y - theObj.offsetHeight - iMenuItemHeight;
		} else {
			y = y - theObj.offsetHeight + iMenuItemHeight;
		}
	}

	//window.status = (x + subMenuWidth) + ":" + iWindowWidth;
	if(x + subMenuWidth > iWindowWidth) {
		x = x - (subMenuWidth * 2) + 6;
	}
	// end repositioning

	y -= totalY();
	x -= totalX();

	arrX[iLevel-1] = x;
	arrY[iLevel-1] = y;

	theObj.style.left = x;
	theObj.style.top = y;

	if(!bRepeatDisplay)
		fadeIn(theObj.firstChild);

	theObj.style.visibility = 'visible';
	theObj.style.zIndex = 10;
}

function fadeIn(theObj) {
	theObj.filters.alpha.opacity = iMinOpacity;

	try {
		clearInterval(ivFadeIn);
	}
	catch(e) { }	

	sFadeID = theObj.id;
	sPrevFadeID = sFadeID;
	ivFadeIn = setInterval("continueFade('" + sFadeID + "')", iFadeSpeedMS);
}

function continueFade(objID) {
	var theObj = document.all[objID];
	var iCurrentOpacity = parseInt(theObj.filters.alpha.opacity);
	iCurrentOpacity += iOpacityIncrement;
	theObj.filters.alpha.opacity = iCurrentOpacity;
	if(iCurrentOpacity >= iMaxOpacity) {
		clearInterval(ivFadeIn);
	}
}

function preLoadImages() {
	var oImg1 = new Image(6,9);
	oImg1.src = 'images/menuArrow.gif';
	var oImg2 = new Image(1,1);
	oImg2.src = 'images/clear.gif';
}

function InitMenu() {
	try{
		document.body.onmouseover = mouseTravel;
		clearInterval(ivStartMenu);
	}
	catch(e) { }
}

function resetMenu() {
	for(var i=arrPositions.length; i>=0; i--) {
		var theRow = document.all["row" + arrPositions[i]];
		if(theRow != null) {
			resetCell(theRow);
		}

		var theTable = document.all["innerMenu" + arrPositions[i]];
		if(theTable != null) {
			theTable.style.display = 'none';
		}

		arrX[i] = 0;
		arrY[i] = 0;
		arrPositions[i] = null;
	}

	iPosition = 0;
	bMenuVisible = false;
	objLast = null;
	oLastRollover = null;
	showSelects(document);
	sFadeID = null;
	sPrevFadeID = null;
}

function mouseTravel(objID) {
	if(!bMenuVisible) return;

	var objElement = event.toElement;
	var obj = document.all[objID];

	if(obj != null && obj.contains(objElement)) {
		clearTime();
	} else {
		clearTime();

		if(iPosition == 0) {
			resetMenu(); // IF NOT SUBMENUS ARE VISIBLE, HAVE MOUSEOUT HAPPEN IMMDEDIATELY
		} else {
			ivStartOffMenuTimer = setTimeout("resetMenu()", iMillisecondsToDisappear); // OTHERWISE PAUSE FOR THE SPECIFIED INTERVAL
		}
	}
}

function clearTime() {
	try {
		clearTimeout(ivStartOffMenuTimer);
	}
	catch(e) { }
}

function colorCell(objElement, sBackColor, sColor) {
	try{
		for(var i=0; i< objElement.children.length; i++) {
			var ele = objElement.children.item(i);
			ele.style.backgroundColor = sBackColor;
			ele.style.color = sColor;
		}
	}
	catch(e) { //window.status += ":1:" + e;
	}
}

function resetCell(objElement) {
	try {
		for(var i=0; i< objElement.children.length; i++) {
			var ele = objElement.children.item(i);
			ele.style.backgroundColor = '';
			ele.style.color = '';
		}
	}
	catch(e) { //window.status += ":2:" + e;
	}
}

function hideSelects(obj) {
	if(bSelectsVisible == false) return;

	try{
		var oSelect = obj.getElementsByTagName("Select");
		for(var i = 0; i < oSelect.length; i++) {
			var oneSelect = oSelect(i);
			if(oneSelect.style.visibility != 'hidden') {
				oneSelect.style.visibility = 'hidden';
			}
		}

		for(var i = 0; i <obj.frames.length; i++) {
			hideSelects(obj.frames[i].document);
		}

		bSelectsVisible = false;
	}
	catch(e) { alert(e); }
}

function showSelects(obj) {
	if(bSelectsVisible == true) return;

	try{
		var oSelect = obj.getElementsByTagName("Select");
		for(var i = 0; i < oSelect.length; i++) {
			var oneSelect = oSelect(i);
			if(oneSelect.style.visibility != 'visible') {
				oneSelect.style.visibility = 'visible';
			}
		}

		for(var i = 0; i <obj.frames.length; i++) {
			showSelects(obj.frames[i].document);
		}

		bSelectsVisible = true;
	}
	catch(e) { alert("error in showSelects: " + e); }
}

function totalY() {
	var iTotal = 0;

	for(var i=0; i<arrY.length; i++) {
		iTotal += arrY[i];
	}

	return iTotal;
}

function totalX() {
	var iTotal = 0;

	for(var i=0; i<arrX.length; i++) {
		iTotal += arrX[i];
	}

	return iTotal;
}

function pauseThenShowSubMenu(id, objSrc) {
	event.cancelBubble = true;
	
	if(objLast != objSrc) {
		showSubMenu(id, objSrc);
	} 
}

function getPosX(obj) { // CROSS-BROWSER
	var curleft = 0;
	if (document.getElementById || document.all) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (document.layers) {
		curleft += obj.x;
	}
	return curleft;
}

function getPosY(obj) { // CROSS-BROWSER
	var curtop = 0;
	if (document.getElementById || document.all) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (document.layers) {
		curtop += obj.y;
	}
	return curtop;
}
