﻿var activePopupMenu = null;
var popupClosingTimeout = null;

function showPopupMenu(linkReference) {
	var childMenus = linkReference.parentNode.getElementsByTagName("DIV");

	/* hide the currently displaying menu, if any */
	if (activePopupMenu != null) {
		activePopupMenu.style.display = "none";
		cancelPopupClosingTimer()
	}

	if (childMenus.length == 1) {
		/* the menu draws at the same X position as the referencing A tag, and Y is that position plus an arbitrary number of pixels.*/
		var pos = getElementPosition(linkReference);

		activePopupMenu = childMenus[0];
		activePopupMenu.style.display = "block";

		activePopupMenu.style.top = (pos["y"] + 19).toString() + "px";
		activePopupMenu.style.left = (pos["x"] - 39).toString() + "px";
	}
}

function getElementPosition(obj) {
	/* derived from http://quirksmode.org/js/findpos.html */
	var originalObj = obj;

	var coords = new Object();
	coords["x"] = 0;
	coords["y"] = 0;

	if (obj.offsetParent) {
		do {
			coords["x"] += obj.offsetLeft;
			coords["y"] += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}

	return coords;
}

function startPopupClosingTimer(divReference) {
	popupClosingTimeout = setTimeout("activePopupMenu.style.display = \"none\";", 500);
}

function cancelPopupClosingTimer() {
	if (popupClosingTimeout != null) {
		clearTimeout(popupClosingTimeout);
		popupClosingTimeout = null;
	}
}
