var bDyn = (document.all || document.layers || document.getElementById) && (screen.height > 480) && (screen.width > 640);

// Netscape 4.04 check...
if (navigator.appName=="Netscape") {
	var iVersion = parseFloat(navigator.appVersion.substr(0, 4));
	if (iVersion < 4.08) {
		bDyn = false;
	}
}

if (bDyn) {
	var gT, gHLT;
	var oCM = null;
	var oHM = null;
	var NS6 = (navigator.vendor == ("Netscape6") || navigator.product == ("Gecko"));
	var amIMac = navigator.platform && (navigator.platform.indexOf('Mac') > -1);
	var IEMac = (document.all && amIMac);
	var IE5Mac = IEMac && navigator.appVersion.indexOf('MSIE 5') == -1;
	var oLLM = null;
	var oHLM = null;
	var oAn = null;
	var gImUp, gImOv;
}

bDyn = bDyn && !(document.layers && amIMac) && (!IE5Mac);
if (!bDyn) var event = null;
	
function pLI() {
  var d = document;
	if (d.images) {
		if (!d.MM_p) d.MM_p = new Array();
		if (gImUp != "") {
			d.MM_p[d.MM_p.length] = new Image;
			d.MM_p[d.MM_p.length-1].src = gImUp;
		}
		if (gImOv != "") {
			d.MM_p[d.MM_p.length] = new Image;
			d.MM_p[d.MM_p.length-1].src = gImOv;
		}
	}
}

function fObj(n, d) {
  var p,i,x;  
	if(!d) d=document; 
	if ((p=n.indexOf("?") ) > 0 && parent.frames.length) {
   	d = parent.frames[n.substring(p+1)].document; 
		n=n.substring(0,p);
	}
  if (!(x=d[n]) && d.all) x = d.all[n];
	for (i=0; !x && i < d.forms.length; i++) x = d.forms[i][n];
	for (i=0; !x && d.layers && i < d.layers.length; i++) x = fObj(n, d.layers[i].document);
	if (!x && d.getElementById) x = d.getElementById(n);
	return x;
}

function gX(e) {
	return (e.x || e.x==0) ? e.x : gAP(e, "Left");
}
function gY(e) { 
	return (e.y || e.y==0) ? e.y : gAP(e, "Top"); 
}

function gAP(e, which) {
	iPos = 0;
	while (e != null) {
		iPos += e["offset" + which];
		e = e.offsetParent;
	}	
	return iPos;
}

function setP(div, sParam, sValue) {
	if (document.all)
		div.style[sParam] = sValue;
	else if (document.getElementById)
		div.style[sParam] = sValue;
	else if (document.layers) {
		div[sParam] = sValue;
	}
}

function initMenuEvents() {
	if (bDyn && document.layers) {
		for (var i = 0; i < window.menus.length; i++) {
			var oL = fObj('divNav' + window.menus[i].mN);
			setE(oL, i);
			for (var j=0; j < oL.layers.length; j++) {
				setE(oL.layers[j], i, true, Math.round(j/2 - .01));
			}
		}
  }
}

function setE(oL, iC, bMenu, iI) {
	oL.menuID = iC;
	if (bMenu) {
		oL.bAnchor = true;
		oL.itemID = iI;
	}
	oL.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
	oL.onMouseOut = nsMenuEvent;
	oL.onMouseOver = nsMenuEvent;
}

function nsMenuEvent(e) {
	if (document.layers) {
		if (e.type == 'mouseover') {
			if (e.target.itemID || e.target.itemID==0) {
				sC(e.target.menuID, e.target.itemID);
				iSM(e.target.menuID, e);
			} else if (e.target.menuID && ((!oCM) || e.target.menuID != oCM.mN)) {
				iSM(e.target.menuID, e);
			}
    }
    else if (e.type == 'mouseout') {
			if (e.target.bAnchor) {
				rC(e.target.menuID, e.target.itemID);
				iHM(e.target.menuID);
			} else if (e.target.menuID) {
				iHM(e.target.menuID);
			}
    }
  }
}

function hideAllMenus() {
	for (var i=0; bDyn && i < window.menus.length; i++) {
		if (window.menus[i] != oCM) {
			hMI(window.menus[i]);
		}
	}
}

function hideMenu(oM) {
	if (bDyn && oM) {
		oHM = oM;
		oCM = null;
		gT = setTimeout('hMT()', (gTimeOut ? gTimeOut : 500));
	}
}

function hMT(oM){
	if ((oHM != null) && ((oM == null) || (oHM != oCM))) {
		clearTimeout(gT);
		hMI(oHM);
		oHM = null;
	}
}

function hMI(oM){
	if (oM) {
		var dN = fObj('divNav' + oM.mN);
		var bVis = false;
		if (document.layers){
			bVis = dN['visibility'] == 'show';
		} else {
			bVis = dN.style.visibility == 'visible';
		}
		if (dN && bVis) {
			setP(dN, 'visibility', 'hidden');
			setP(dN, 'left', '-100');
			setP(dN, 'top', '-100');
			if (document.layers) {
				dN.left = -1000;
				dN.visibility = "hide";
			}
		}
	}
}

function Menu() {
	this.type = "Menu";
	this.items = new Array();
	this.actions = new Array();
	this.childMenus = new Array();
	this.addMenuItem = aMI;
	if (!window.menus)
		window.menus = new Array();
	this.mN = window.menus.length;
	this.label = "menuLabel" + window.menus.length;
	window.menus[this.label] = this;
	window.menus[window.menus.length] = this;
}

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

function writeMenus() {
	var sOP = "";
	if (bDyn) {
		var sDiv;
		var i, j, iMenu;
		var rI, rL;
		gImUp = (typeof(bgImageUp)=="string" ? bgImageUp : "");
		gImOv = (typeof(bgImageOver)=="string" ? bgImageOver : "");
		pLI();
		sOP = '<div bgcolor="green" id="divNSError" style="position:absolute; width:0px; height:0px; left: 0px; top: 0px; z-index:1; visibility: visible"></div>';
		for (var i=0; i < window.menus.length; i++) {
			var oM = window.menus[i];
			sDiv = '<div id="divNav' + oM.mN + '" style="position:absolute; top: 0px; left: 0px; z-index:' + (i+1) + '; visibility: hidden;">\n';
			sDiv += '	<table onMouseOver="iSM(' + i + ', event)" border="0" width="' + iMenuWidth + '" cellspacing="0" cellpadding="0"">\n';
			rI = oM.items;
			rL = oM.actions;
			for (j=0; j < rI.length; j++) {
				sDiv += '		<tr>\n'
				if (document.layers) {
					var sL = '			<a href="' + rL[j] + '" class="' + sMenuClassLo + '" onMouseOut="rC('+i+', '+j+');" onMouseOver="sC('+i+', '+j+', event);">';
					var sLT = '			<a href="' + rL[j] + '" class="' + sMenuClassLo + '">&nbsp;' + rI[j] + '<img src="images/spacer.gif" height="1" width="1" border="0"></a>';
					var sLHi = '			<a href="' + rL[j] + '" class="' + sMenuClassHi + '">'
					var sLTHi = '			<a href="' + rL[j] + '" class="' + sMenuClassHi + '">&nbsp;' + rI[j] + '<img src="images/spacer.gif" height="1" width="1" border="0"></a>';
					sDiv += '			<td>';
					var dText	= sL + '<layer itemID="' + j + '" ID="' + oM.label + j +'" bgColor="' + sBGColour + '" height="' + iMenuHeight + '" width="' + iMenuWidth + '" top="' + j * iMenuHeight + '" background="' + gImUp + '">'+ sLT +'</layer></a>\n';
					dText += 	  sLHi + '<layer itemID="' + j + '" ID="'+ oM.label + 'Hi' + j +'" bgColor="' + sBGHiLiteColour + '" visibility="hidden" background="' + gImOv + '" height="' + iMenuHeight + '" width="' + iMenuWidth + '" top="' + j * iMenuHeight + '">'+ sLTHi +'</layer></a>';
					sDiv += dText;
					sDiv += '</td>\n';
				} else {
					var sLT = '<a style="height: ' + iMenuHeight + '; width: ' + iMenuWidth + ';" id="anc'+ oM.label + j + '" onMouseOut="rC('+i+', '+j+', this); iHM('+ i +');" class="' + sMenuClassLo + '" href="' + rL[j] + '">&nbsp;' + rI[j] + '</a>';
					var sLTHi = '<a style="height: ' + iMenuHeight + '; width: ' + iMenuWidth + ';" id="anc'+ oM.label + 'Hi' + j + '" onMouseOut="rC('+i+', '+j+', this); iHM('+ i +');" class="' + sMenuClassHi + '" href="' + rL[j] + '">&nbsp;' + "HiLite Crunt" + '</a>';
					sDiv += '			<td>';
					var dText	= '<div onMouseOver="iSM('+ i +', event); sC('+i+', '+j+', event);" onMouseOut="rC('+i+', '+j+'); iHM('+ i +');" class="' + sMenuClassLo + '" menuID="'+i+'" itemID="'+j+'" id="' + oM.label + j +'" style="height: ' + iMenuHeight + '; width: ' + iMenuWidth + '; background: ' + sBGColour + '; background-image: url(' + gImUp + ')"> ' + sLT + '</div>\n';
					dText	+= '<div onMouseOver="iSM('+ i +', event); sC('+i+', '+j+', event);" onMouseOut="rC('+i+', '+j+'); iHM('+ i +');" class="' + sMenuClassHi + '" menuID="'+i+'" itemID="'+j+'" id="' + oM.label + 'Hi' + j +'" style="display: none; height: ' + iMenuHeight + '; width: ' + iMenuWidth + '; background: ' + sBGHiLiteColour + '; background-image: url(' + gImOv + ')"> ' + sLTHi + '</div>\n';
					sDiv += dText;
					sDiv += '</td>\n';
				}
				sDiv += '		</tr>\n';
			}
			sDiv += '	</table>\n';
			sDiv += '</div>\n';
			sOP += sDiv;
		}
	}
	return sOP;
}

function iHM(iMenu) {
	hideMenu(window.menus[iMenu]);
}

function iSM(iMenu, e) {
	displayMenu(window.menus[iMenu], e);
}

function displayMenu(oM, oNav, e) {
     if (bDyn) {
		if (oM == oHM) {
			clearTimeout(gT);
			oHM = null;
			oCM = oM;	
		}
		if (oCM != oM) {
			oCM = oM;
			hideAllMenus();
			var iImageHeight = 34;
			var iX, iY = 0;
			if (NS6) {
				iX = e.target.offsetLeft + iImageWidth;
				iY = e.target.offsetTop;
			} else if (IE5Mac) {
				iX = window.event.clientX - window.event.offsetX + iImageWidth;
				iY = window.event.clientY - window.event.offsetY;
			} else {
				if (oNav) {
					iX = gX(oNav) + iImageWidth;
					iY = gY(oNav);
				} else {
					iX = oM.lastX;
					iY = oM.lastY;
				}
			}
			iY += 1;
			var dN = fObj('divNav' + oM.mN);
			if (dN) {
				oM.lastX = iX;
				oM.lastY = iY;
				setP(dN, 'left', iX);
				setP(dN, 'top', iY);
				setP(dN, 'visibility', 'visible');
			}
		}
	}
}

function dHL(iMenu, iL, bShow) {
	var oM = window.menus[iMenu];
	if (oM) {
		var dH = fObj(oM.label + 'Hi' + iL);
		var dL = fObj(oM.label + iL);
		var dA = fObj('anc' + oM.label + iL);
		if (bShow) {
			clearTimeout(gHLT);
			if (dH != oHLM) {
				hHL();
				if (IEMac) {
					// nothing
					dL.style.background = sBGHiLiteColour;
					dL.style.backgroundImage = "url(" + gImOv + ")";
				} else if (NS6 || document.all) {
					dL.className = sMenuClassHi;
					dL.style.background = sBGHiLiteColour;
					dL.style.backgroundImage = "url(" + gImOv + ")";
					if (dA) dA.className = sMenuClassHi;
				}	else if (document.layers) {
					if (amIMac) {
						dL.background.src = gImOv;
						dL.bgColor = sBGHiLiteColour;
					} else {
						dH.visibility = "show";
						dL.visibility = "hide";
					}
				} 
			}
			oLLM = null;
			oHLM = null;
			oAn = null;
		} else {
			oHLM = dH;
			oLLM = dL;
			oAn = dA;
			gHLT = setTimeout('hHL()', 100);
		}
	}
}

function hHL() {
	if (oHLM && oLLM) {
		if (IEMac) {
			// nothing
			oLLM.style.background = sBGColour;
			oLLM.style.backgroundImage = "url(" + gImUp + ")";
		} else if (NS6 || document.all) {
			oLLM.className = sMenuClassLo;
			oLLM.style.background = sBGColour;
			oLLM.style.backgroundImage = "url(" + gImUp + ")";
			if (oAn) oAn.className = sMenuClassLo;
		} else if (document.layers) {
			if (amIMac) {
				oLLM.background.src = gImUp;
				oLLM.bgColor = sBGColour;
			} else {
				oLLM.visibility = "show";
				oHLM.visibility = "hide";
			}
		}
		oHLM = null;
		oLLM = null;
	}
}

function sC(iMenu, iL) {
	dHL(iMenu, iL, true);
}

function rC(iMenu, iL, el) {
	dHL(iMenu, iL, false);
}

