var jsAreaShowTime = 0;
var JsAreas = new Object();

function GetArea(id)
{
	return JsAreas[id] ? JsAreas[id] : (JsAreas[id] = new JsArea(id));
}

// A single popup window
function JsArea(id)
{
	this.AreaId = id;
}

// Function to return the DIV Layer
JsArea.prototype.ContentArea= function()
{
	var divArea = document.getElementById(this.AreaId);
	if (divArea != null)
	{
		return divArea;
	}
	return null;
}

var activeAreaId = null;

// Function to show the DIV Layer
JsArea.prototype.popareaup = function(x, y, max_x)
{
if (activeAreaId != null)	
	jsAreaClose(activeAreaId);
	
	var divLayer = this.ContentArea();
	
	divLayer.style.position = 'absolute';
	divLayer.style.zIndex = 100;
	divLayer.style.display = 'block';
	
	var maxWidth;
	if (max_x) maxWidth = max_x;
	else
		maxWidth = YAHOO.util.Dom.getDocumentWidth () - 50;
	
	
	if (maxWidth < x + divLayer.offsetWidth  )
			x = maxWidth - divLayer.offsetWidth ;
	/*
	var maxHeight = YAHOO.util.Dom.getViewportHeight();
	if (maxHeight < y + divLayer.offsetHeight )
			y = maxHeight - divLayer.offsetHeight - 10;
	*/		
	
	YAHOO.util.Dom.setX(divLayer, x);
	YAHOO.util.Dom.setY(divLayer, y);
	
	
	//divLayer.style.width = "600px;";
	if (jsAreaShowTime > 0){
		divLayer.onmouseover= JsAreaMouseOver;
		divLayer.onmouseout = jsAreaMouseOut;
	}
	activeAreaId = this.AreaId;
	
	return false;
}

// Function to hide the DIV Layer
JsArea.prototype.hide = function()
{
	var divLayer = this.ContentArea();
	if (divLayer != null)
		divLayer.style.display = 'none';
		
	return false;
}

// Function to be called
// by Web forms to show the Popup Window
function PopupArea(e, areaId, linkToButtom)
{	
	var area = GetArea(areaId);		
	var max_x = 0;
	if (linkToButtom){
			var targetBtn = YAHOO.util.Event.getTarget(e);
			var max_x = YAHOO.util.Dom.getX( targetBtn );
			max_x = max_x + targetBtn.offsetWidth;
			max_x = max_x + 10;
			//alert(max_x);
	}
	area.popareaup(YAHOO.util.Event.getPageX(e), YAHOO.util.Event.getPageY(e), max_x );
}

// Function to hide the DIV Layer
function jsAreaClose(areaId)
{
	GetArea(areaId).hide();
	activeAreaId = divHangTimer = null;	
}

var divHangTimer = null;

// Function to keep the Div Layer
// showing for a "period" of time
// after that period, if the mouse
// has been outside the DIV Layer, 
// it will be hidden automatically
function KeepArea(areaId)
{
	if (areaId == activeAreaId && divHangTimer != null)
	{
		clearTimeout(divHangTimer);
		divHangTimer = null;
	}
}

// Function to release the DIV Layer
function RelArea(areaId)
{
	if (jsAreaShowTime > 0 ){
		if (areaId == activeAreaId && divHangTimer == null)
			divHangTimer = setTimeout('jsAreaClose(\'' + areaId + '\')', jsAreaShowTime);
	}
}

// Function fired when mouse is over the 
// DIV Layer, used to keep the layer showing
function JsAreaMouseOver(e)
{
	if (!e) 
		var e = window.event;
	var targ = e.target ? e.target : e.srcElement;
	KeepArea(activeAreaId);
}

// Function that fires when mouse is out of
// the scope of the DIV Layer
function jsAreaMouseOut(e)
{
	if (!e) 
		var e = window.event;
	var targ = e.relatedTarget ? e.relatedTarget : e.toElement;
	var activeAreaView = document.getElementById(activeAreaId);
	if (activeAreaView != null && !jsAreaContains(activeAreaView, targ)) {
		RelArea(activeAreaId);
	}
}
function jsAreaContains(parent, child)
{
	while(child)
		if (parent == child) return true;
		else {
			try {
				child = child.parentNode;
			}
			catch(err){
				return false;
			}
		}
		return false;
}
