// PHP Layers Menu 2.3.5 (C) 2001-2003 Marco Pratesi (marco at telug dot it)

loaded = 0;
layersMoved = 0;

menuLeftShift = 6;
menuRightShift = 10;

layerPoppedUp = "";

currentY = 0;
function grabMouse(e) {	// for NS4
	currentY = e.pageY;
}
if (NS4) {
	document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE);
	document.onmousemove = grabMouse;
}

function shutdown() {
	for (i=1; i<=numl; i++) {
		LMPopUpL(listl[i], false);
	}
	layerPoppedUp = "";
}
if (NS4) {
	document.onmousedown = shutdown;
} else {
	document.onclick = shutdown;
//	document.onmouseout = setTimeout("shutdown",1000);
}

function moveLayerX(menuName) {
	if (!loaded || (isVisible(menuName) && menuName != layerPoppedUp)) {
		return;
	}
	if (father[menuName] != "") {
		if (!Opera5 && !IE4) {
			width0 = lwidth[father[menuName]];
			width1 = lwidth[menuName];
		} else if (Opera5) {
			// Opera 5 stupidly and exaggeratedly overestimates layers widths
			// hence we consider a default value equal to $abscissaStep
			width0 = abscissaStep;
			width1 = abscissaStep;
		} else if (IE4) {
			width0 = getOffsetWidth(father[menuName]);
			width1 = getOffsetWidth(menuName);
		}
		onLeft = getOffsetLeft(father[menuName]) - width1 + menuLeftShift;
		onRight = getOffsetLeft(father[menuName]) + width0 - menuRightShift;
		windowWidth = getWindowWidth();
		windowXOffset = getWindowXOffset();
//		if (NS4 && !DOM) {
//			windowXOffset = 0;
//		}
		if (onLeft < windowXOffset && onRight + width1 > windowWidth + windowXOffset) {
			if (onRight + width1 - windowWidth - windowXOffset > windowXOffset - onLeft) {
				onLeft = windowXOffset;
			} else {
				onRight = windowWidth + windowXOffset - width1;
			}
		}
		if (back[father[menuName]]) {
			if (onLeft < windowXOffset) {
				back[menuName] = 0;
			} else {
				back[menuName] = 1;
			}
		} else {
//alert(onRight + " - " + width1 + " - " +  windowWidth + " - " + windowXOffset);
			if (onRight + width1 > windowWidth + windowXOffset) {
				back[menuName] = 1;
			} else {
				back[menuName] = 0;
			}
		}
		if (back[menuName]) {
			setLeft(menuName, onLeft);
		} else {
			setLeft(menuName, onRight);
		}
	}
	moveLayerY(menuName, 0);	// workaround needed for Mozilla for MS Windows
}

function moveLayerY(menuName, ordinateMargin) {
	if (!loaded || (isVisible(menuName) && menuName != layerPoppedUp)) {
		return;
	}
	if (!layersMoved) {
		moveLayers();
		layersMoved = 1;
	}
	if (!NS4) {
		newY = getOffsetTop("ref" + menuName);
	} else {
		newY = currentY;
	}
	newY -= ordinateMargin;
	layerHeight = getOffsetHeight(menuName);
	windowHeight = getWindowHeight();
	windowYOffset = getWindowYOffset();
	if (newY + layerHeight > windowHeight + windowYOffset) {
		if (layerHeight > windowHeight) {
			newY = windowYOffset;
		} else {
			newY = windowHeight + windowYOffset - layerHeight;
		}
	}
	if (Math.abs(getOffsetTop(menuName) - newY) > thresholdY) {
		setTop(menuName, newY);
	}
}

function LMPopUp(menuName, isCurrent) {
	if (!loaded || menuName == layerPoppedUp || (isVisible(menuName) && !isCurrent)) {
		return;
	}
	if (menuName == father[layerPoppedUp]) {
		LMPopUpL(layerPoppedUp, false);
	} else if (father[menuName] == layerPoppedUp) {
		LMPopUpL(menuName, true);
	} else {
		shutdown();
		foobar = menuName;
		do {
			LMPopUpL(foobar, true);
			foobar = father[foobar];
		} while (foobar != "")
	}
	layerPoppedUp = menuName;
}

function LMPopUpL(menuName, on) {
	if (!loaded) {
		return;
	}
	if (!layersMoved) {
		moveLayers();
		layersMoved = 1;
	}
	setVisibility(menuName, on);
}

function resizeHandler() {
	if (NS4) {
		window.location.reload();
	}
	shutdown();
	for (i=1; i<=numl; i++) {
		setLeft(listl[i], 0);
		setTop(listl[i], 0);
	}
//	moveLayers();
	layersMoved = 0;
}
window.onresize = resizeHandler;

function yaresizeHandler() {
	if (window.innerWidth != origWidth || window.innerHeight != origHeight) {
		if (Konqueror2 || Opera5) {
			window.location.reload();	// Opera 5 often fails this
		}
		origWidth  = window.innerWidth;
		origHeight = window.innerHeight;
		resizeHandler();
	}
	setTimeout('yaresizeHandler()', 500);
}
function loadHandler() {
	if (Konqueror2 || Opera56) {
		origWidth  = window.innerWidth;
		origHeight = window.innerHeight;
		yaresizeHandler();
	}
}
window.onload = loadHandler;

function fixieflm(menuName) {
	if (DOM) {
		setWidth(menuName, "100%");
	} else {	// IE4 IS SIMPLY A BASTARD !!!
		document.write("</div>");
		document.write("<div id=\"IE4" + menuName + "\" style=\"position: relative; width: 100%; visibility: visible;\">");
	}
}

