
var aAllMenus			= new Array();
var bReleaseMenus		= false;
var bMenuAutoAssign	= false;
var iMenuCloseDelay	= 0;
var oMenuCloseTimer	= null;
var iMenuPageStartX	= 0;
var iMenuPageStartY	= 0;
var iMenuContentX		= 0;
var iMenuContentY		= 0;

window.onload	= _unlockLayerMenus;
window.onresize	= _recalculateMenus;


// --- -----------------------------------------------------------------------
// --- User functions
// --- -----------------------------------------------------------------------

function setMenuDelay(iDelay) {
	// User-definable delay before closing menus.
	//
	// iDelay == 0 : Close menus immediately.
	// iDelay >= 1 : Wait specified number of milliseconds before closing menus

	iMenuCloseDelay = iDelay;
}


function setContentDimensions(iX, iY) {
	// If specified, the popup menus will be offset to adjust for variations
	// in screen width and height. The offset will be added to any specified
	// x/y coordinates on the showLayerMenu calls.

	// Specify zero (0) if the contents have no specific width or height.
	
	iMenuContentX = iX;
	iMenuContentY = iY;
}


function setMenuAutoAssign(bMode) {
	// If specified, all registered menus will be parsed and have events
	// attached to cells. Cells must be of type DIV and have
	// the "lmcell" attribute != null.
	
	bMenuAutoAssign = bMode;
}


function showLayerMenu(sName) {
	// Show menu, referenced by its name. Additional arguments dictates:
	// - location on screen, (x-coordinate first, then y)
	// - popup relative to (0: nothing, 1: pointer, 2: trigger)
	// - popup on pointer (0: offset SE, 1: offset NW, 2: offset NE, 3: offset SW, 4: center).
	// - popup transition (0: none, 1: reveal)
	// Example: showLayerMenu('divMenu1', 100, 50);
	// Example: showLayerMenu('divMenu1', 8, 16, 2, 2);

	if (!bReleaseMenus)
		return;
	
	_closeMenus();

	if ((oStyle = getStyle(sName)) != null) {
		var iPageOffsetX = iMenuPageStartX;
		var iPageOffsetY = iMenuPageStartY;
		oStyle.visibility = (isNetscape4())?"show":"visible";

		if (arguments.length > 1)
			iPageOffsetX = iPageOffsetX + arguments[1];

		if (arguments.length > 2)
			iPageOffsetY = iPageOffsetY + arguments[2];

		if (arguments.length > 3) {
			switch (arguments[3]) {
				case 1: // Offset to pointer	
					iPageOffsetX = arguments[1] + getMouseX();
					iPageOffsetY = arguments[2] + getMouseY();
				break;

				case 2: // Offset to trigger object
					if (isNetscape4()) {
						iPageOffsetX = arguments[1] + getMouseX();
						iPageOffsetY = arguments[2] + getMouseY();
					} else {
						iPageOffsetX = arguments[1] + getAbsoluteLeft(event.srcElement);
						iPageOffsetY = arguments[2] + getAbsoluteTop(event.srcElement);
					}
				break;

				default: // No relative offset

			}
		}

		if (arguments.length > 4) {
			var iPX, iPY, iMHeight, iMWidth = 0;
			if ((oLayer = getElementByIdNew(sName)) != null) {
				iMHeight = getElementHeight(oLayer);
				iMWidth = getElementWidth(oLayer);
			}

			switch (arguments[4]) {
				case 1: // offset NW
					iPageOffsetX = iPageOffsetX + (iMWidth * -1);
					iPageOffsetY = iPageOffsetY + (iMHeight * -1);
				break;

				case 2: // offset NE
					iPageOffsetY = iPageOffsetY + (iMHeight * -1);
				break;

				case 3: // offset SW
					iPageOffsetX = iPageOffsetX + (iMWidth * -1);
				break;

				case 4: // Center
					iPageOffsetX = iPageOffsetX + (Math.round(iMWidth / 2) * -1);
					iPageOffsetY = iPageOffsetY + (Math.round(iMHeight / 2) * -1);
				break;

				default: // offset SE
			}

		}

		if (arguments.length > 5) {
			oMam = getElementByIdNew(sName);
			oMam.style.overflow = 'hidden';
			oMam.style.height = 5;
		}

		oStyle.left = iPageOffsetX;
		oStyle.top = iPageOffsetY;
	}
}


function holdLayerMenus() {
	_cancelMenuTimeout();
}


function hideLayerMenus() {
	if (!bReleaseMenus)
		return;

	if (iMenuCloseDelay > 0) {
		_cancelMenuTimeout();
		oMenuCloseTimer = setTimeout("_closeMenus();", iMenuCloseDelay);
	} else {
		_closeMenus();
	}
}


function registerLayerMenu(sName) {
	// Register a layer menu by its name/id
	var i = 0;

	while (aAllMenus[i] != null)
		i++;

	aAllMenus[i] = sName;
}



// --- -----------------------------------------------------------------------
// --- Internal functions
// --- -----------------------------------------------------------------------

function _closeMenus() {
	// Immediately closes all open layer menus

	_cancelMenuTimeout()

	for (var i = 0; i < aAllMenus.length; i++) {
		if ((oStyle = getStyle(aAllMenus[i])) != null) {
			oStyle.visibility = (isNetscape4())?"hide":"hidden";
		}
	}
}


function _cancelMenuTimeout() {
	// Cancels a timed request to close any open layer menus, effectively
	// holding them open indefinitely.

	if (oMenuCloseTimer != null) {
		clearTimeout(oMenuCloseTimer);
		oMenuCloseTimer = null;
	}
}


function _unlockLayerMenus() {
	// Unlock the layer menus
	var oTemp;
	bReleaseMenus = true;

	for (var i = 0; i < aAllMenus.length; i++) {
		if ((oTemp = getElementByIdNew(aAllMenus[i])) != null) {
			oTemp.onmouseover = _cancelMenuTimeout;
			oTemp.onmouseout = hideLayerMenus;

			if (bMenuAutoAssign) {

				if (!isExplorer4() && !isNetscape()) {
					for (var j = 0; j < oTemp.all.length; j++) {
						if ((oTemp.all[j].tagName == "DIV") && (oTemp.all[j].LMCELL != null))
							oTemp.all[j].onmouseover = _lmOnMouseOver;
							oTemp.all[j].onmouseout = _lmOnMouseOut;
					}
				} else if (!isExplorer4() && !isNetscape4()) {
					var oTableRows = oTemp.childNodes[0].rows;
					for (var j = 0; j < oTableRows.length; j++) {
						var oFindDiv = oTableRows[j].childNodes[0];
						if (oFindDiv.childNodes[0] != null)
							if ((oFindDiv = oFindDiv.childNodes[0]) != null)
								if ((oFindDiv.tagName == "DIV") && (oFindDiv.getAttribute("lmcell").length > 0)) {
	
									oFindDiv.onmouseover = _lmOnMouseOver;
									oFindDiv.onmouseout = _lmOnMouseOut;

								}

					}
				}
				
			} // END bMenuAutoAssign
		}
	}

	_closeMenus();
	_recalculateMenus();
}


function _recalculateMenus() {
	var iExcessWidth	= (iMenuContentX >= getDocumentWidth())? 0 : getDocumentWidth() - iMenuContentX;
	var iExcessHeight	= (iMenuContentY >= getDocumentHeight())? 0 : getDocumentHeight() - iMenuContentY;

	// Values are subtracted by 0.25, to ensure correct rounding
	if (iMenuContentX > 0) {
		iMenuPageStartX = (iExcessWidth == 0) ? 0 : Math.round((iExcessWidth / 2) - 0.25);
	}

	if (iMenuContentY > 0) {
		iMenuPageStartY = (iExcessHeight == 0) ? 0 : Math.round((iExcessHeight / 2) - 0.25);
	}
}


function _lmOnMouseOver() {
	if (isNetscape4())
		return;

	if (this._rollover3 == null) {
		var oTemp = this.parentNode;
		while ((oTemp != null) && (oTemp.tagName != "DIV")) {
			oTemp = oTemp.parentNode;
		}

		if (oTemp != null) {
			this._rollover3 = oTemp.getAttribute("autorollover").split(";")[0];
			this._rollover4 = oTemp.getAttribute("autorollover").split(";")[1];
		}
	}


	if (this._rollover1 == null) {
		this._rollover1 = this.style.backgroundColor;
		this._rollover2 = this.style.color;
	}

	this.style.backgroundColor = this._rollover3;
	this.style.color = this._rollover4;
}


function _lmOnMouseOut() {
	if (isNetscape4())
		return;
		
	this.style.backgroundColor = this._rollover1;
	this.style.color = this._rollover2;
}
