// LinkPopup.js

// Used to associate an html link with a popup div.
// When the link is clicked, the div is popped up.
// Click anywhere on the screen to close the popup.


var LinkPopup_ns6Browser = document.getElementById && !document.all;
var LinkPopup_currentPopup = null;
var LinkPopup_currentPopupLink = null;


// Get an object left position from the upper left viewport corner
function LinkPopup_getAbsoluteLeft(o)
{
	oLeft = o.offsetLeft - o.scrollLeft; // Get left position from the parent object

	while(o.offsetParent!=null) {		// Parse the parent hierarchy up to the document element
		oParent = o.offsetParent;		// Get parent object reference
		oLeft += oParent.offsetLeft - oParent.scrollLeft; // Add parent left position
		o = oParent;
	}

	// Return left postion
	return oLeft;
}


// Get an object top position from the upper left viewport corner
function LinkPopup_getAbsoluteTop(o)
{
	oTop = o.offsetTop + o.scrollTop; // Get top position from the parent object
	while(o.offsetParent!=null) {		// Parse the parent hierarchy up to the document element
		oParent = o.offsetParent;		// Get parent object reference
		oTop += oParent.offsetTop + oParent.scrollTop; // Add parent top position
		o = oParent;
	}

	// Return top position
	return oTop;
}


// Hide the currently showing popup
function LinkPopup_hidePopup()
{
	if(LinkPopup_currentPopup) {
		LinkPopup_currentPopup.style.display = "none";
		LinkPopup_currentPopup = null;
		return false;
	}
	else {
		return true;
	}
}


// If popup is currently showing, clicking anywhere in document will hide it.
function LinkPopup_handleDocumentClick(e)
{
	// If clicked right edge of browser, dont process click.
	// This so that clicking a scrollbar will not hide popup.
	// I dont know how to detect scrollbar click so instead I detect click at right edge of browser.
	var clickXPos = LinkPopup_ns6Browser ? e.clientX : event.clientX;
	if(clickXPos > document.body.offsetWidth - 40) return true;
	
	// Same as above but for bottom edge:
	var clickYPos = LinkPopup_ns6Browser ? e.clientY : event.clientY;
	if(clickYPos > document.body.offsetHeight - 40) return true;
	
	// If clicked in the link that opened the current popup, return and let link click handler hide the popup
	var obj = LinkPopup_ns6Browser ? e.target : event.srcElement;
	if(obj == LinkPopup_currentPopupLink) return true;

	return LinkPopup_hidePopup();
}


// Handle a link click by popping up its associated div popup,
// or by closing the popup if its already open.
// This function should be the 'onclick' parameter of an html link.
function LinkPopup_handleLinkClick(linkId, divId)
{
	// If a popup is currently showing, hide it & return
	if(LinkPopup_currentPopup) {
		return LinkPopup_hidePopup();
	}

	var link = document.getElementById(linkId);

	var left;
	if(LinkPopup_ns6Browser) {
		left = LinkPopup_getAbsoluteLeft(link.offsetParent);
	}
	else {
		left = LinkPopup_getAbsoluteLeft(link.offsetParent.offsetParent);
	}

	var top = LinkPopup_getAbsoluteTop(link) + link.offsetHeight;

	LinkPopup_currentPopupLink = link;
	LinkPopup_currentPopup = document.getElementById(divId);

	if(LinkPopup_ns6Browser) {
		document.getElementById(divId).style.left = left;
		document.getElementById(divId).style.top = top;
	}
	else {
		document.getElementById(divId).style.pixelLeft = left;
		document.getElementById(divId).style.pixelTop = top;
	}

	// Unhide the popup div
	document.getElementById(divId).style.display = "";

	return false;
}


// Add an initially hidden div popup. Associates an id with this popup
// so that LinkPopup_handleLinkClick knows which popup to show.
function LinkPopup_addPopup(id, borderColor, text)
{
	document.write('<div onclick="LinkPopup_consumeEvent(event)" id="' + id + '" style="position:absolute; overflow:auto; width:610px; display:none; z-index:10000;">');

	var borderWidth = 2;
	document.write('<table cellspacing=' + borderWidth + ' cellpadding=0 bgcolor=' + borderColor + '><tr><td bgcolor=white>');

	document.write(text);
	document.write('</td></tr></table></div>');
}

document.onclick = LinkPopup_handleDocumentClick;

function LinkPopup_consumeEvent(e) 
{
	var event = e || window.event;
	if (event.stopPropagation) {
		event.stopPropagation();
	} else {
		event.cancelBubble = true;
	}
}
