/**
 * Popup functions
 */
var hideTimer;
var visiblePopup;
var popupmap = new Array();
var popupSrcId = 0;

function popup(clickSrc, event) {
	var tableCell;
	if (typeof(clickSrc) == "string")
		tableCell = document.getElementById(clickSrc);
	else
		tableCell = clickSrc.parentNode;
	var popup;

	// The popup DIV will be moved so keep track of which TD opens which popup.
	if (!clickSrc.id) {
		clickSrc.id = "popupOpenSource_" + popupSrcId;
		popupSrcId++;
	}

	if (popupmap[clickSrc.id])
		popup = popupmap[clickSrc.id];
	else {
		popup = getElementsByClass("Popup", tableCell)[0];
		popupmap[clickSrc.id] = popup;
	}

	if (popup == null)
		return;

	// Hide the currently visible popup.
	if (visiblePopup && visiblePopup != popup)
		visiblePopup.style.display = "none";

	// Make the div a child of the body tag.
	if (popup.parentNode != document.body) {
		popup.parentNode.removeChild(popup);
		document.body.appendChild(popup);
	}

	// If the popup is already visible, hide it.
	if (popup.style.display == "block") {
		popup.style.display = "none";
		return;
	}

	// Show the popup, otherwise the height will be 0.
	popup.style.display = "block";
	popup.style.position = "absolute";

	var x = event.clientX - 5;
	var height;
	var width;

	// IE and FF measurements
	if (window.innerHeight) {
		height = window.innerHeight;
		width = window.innerWidth;
	} else {
		height = document.body.clientHeight;
		width = document.body.clientWidth;
	}

	// Don't let the popup get out of the screen on the X-axis
	if (x + popup.offsetWidth + 5 > width)
		popup.style.left = (width - popup.offsetWidth) + 'px';
	else
		popup.style.left = (x + 5) + 'px';

	// Don't let the popup get out of the screen on the Y-axis
	if ((event.clientY + popup.offsetHeight) > height)
		popup.style.top = (height - popup.offsetHeight - 5) + 'px';
	else
		popup.style.top = event.clientY + 'px';

	popup.style.zIndex = 10001;

	if (popup != null)
		visiblePopup = popup;
	if (hasClass(popup, "donthide")) {
		clearTimeout(hideTimer);
		return;
	}
	
	popup.onmouseover = hideOnLeave;

	var childTags = popup.getElementsByTagName("*");
	for ( var i = 0; i < childTags.length; i++) {
		childTags[i].onfocus = hideOnLeave;
	}
}

function hideOnLeave() {
	this.onmouseout = popupHide;
	clearTimeout(hideTimer);
}

function popupHide() {
	hideTimer = setTimeout("doHidePopup();", 250);
}

function doHidePopup() {
	if (visiblePopup && visiblePopup.style && visiblePopup.style.display && !hasClass(visiblePopup, "donthide"))
		visiblePopup.style.display = 'none';
//	visiblePopup = null;
}
