
var subsStack = Array();
var timer;

function getAbsoluteLeft(objectId) {
	// Get an object left position from the upper left viewport corner
	// Tested with relative and nested objects
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft            // 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 // Add parent left position
		o = oParent
	}
	// Return left postion
	return oLeft
}

function getAbsoluteTop(objectId) {
	// Get an object top position from the upper left viewport corner
	// Tested with relative and nested objects
	o = document.getElementById(objectId)
	oTop = o.offsetTop            // 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 // Add parent top position
		o = oParent
	}
	// Return top position
	return oTop
}

function popup(parent, child)
{
	clearTimeout(timer);

	// hide other popups.
	for (var p in subsStack)
	{
		document.getElementById(subsStack[p]).style.visibility = 'hidden';
	}
	
	
	var popup = document.getElementById(child);
		
	var pheight = popup.style.height;
	var left = getAbsoluteLeft(parent.id) + 0;
	var top = getAbsoluteTop(parent.id) - (popup.offsetHeight)/2 + 10;
	popup.style.left = left+'px';
	popup.style.top = top+'px';
	popup.style.visibility = 'visible';
	
	popup.onmouseout = function()
	{
		restPopups();
	}

	// add popup to array.
	if (subsStack.toString().indexOf(popup.id)==-1)	subsStack.push(popup.id);
	
}

function restPopups(delay)
{
	var delay = (delay) ? delay : 200;
	timer = setTimeout(killPopups, delay);
}

function killPopups(id)
{
	for (var p in subsStack)
	{
		document.getElementById(subsStack[p]).style.visibility = 'hidden';
	}
}
