var PopupLock = false;
var LastPopupName = '';
var DefaultTimeout = 2500;
var DefaultMenuTimeout = 1000;
var DefaultImageTimeout = 500;
var DefaultLongTimeout = 10000;
var DefaultExtraLongTimeout = 15000;
var TimerId = 0;

function OpenPopup(PopupItem, MaxParentLevel)
{
	ShowPopup(null, PopupItem, 0, 0, MaxParentLevel);
}

function OpenPopupWithTimer(PopupItem, Timeout, MaxParentLevel)
{
	ShowPopup(null, PopupItem, 0, 0, MaxParentLevel);
	StartPopupTimer(Timeout);
}

function OpenPopupOverParent(Parent, PopupItem, X, Y, MaxParentLevel)
{
	ShowPopup(Parent, PopupItem, X, Y, MaxParentLevel);
}

function OpenPopupOverParentWithTimer(Parent, PopupItem, X, Y, Timeout, MaxParentLevel)
{
	ShowPopup(Parent, PopupItem, X, Y, MaxParentLevel);
	StartPopupTimer(Timeout);
}

function StartPopupTimer(Timeout)
{
	StopPopupTimeout();

	if (Timeout == 0)
	{
		Timeout = DefaultTimeout;
	}

	TimerId = setTimeout('HidePopup()', Timeout);
}

function StopPopupTimeout()
{
	if (TimerId != 0)
	{
		clearTimeout(TimerId);
		TimerId = 0;
	}
}

function ShowPopup(Parent, PopupItem, X, Y, MaxParentLevel)
{
	StopPopupTimeout();
	// Check if there is already a popup other than this one showing already
	if ((LastPopupName != '') && (LastPopupName != PopupItem))
	{
		// Hide any window that is already showing
		HidePopup(LastPopupName);
	}
	PopupLock = true;
	LastPopupName = PopupItem;

	// Set popup to visible
	var Element = document.getElementById(PopupItem);
	if (!Element)
	{
		return 0;
	}
	Element.style.visibility = "Visible";

	if (Parent)
	{
		var OffsetLeft = 0;
		var OffsetTop = 0;
		var ParentLevel = 0;
		
		// OffsetTop += Parent.offsetHeight;
		
 		if (Parent.offsetParent) 
		{
 			LoopParent = Parent;
			do 
			{
				if ((MaxParentLevel != null) && (ParentLevel >= MaxParentLevel))
				{
					break;
				}
				OffsetLeft += LoopParent.offsetLeft;
				OffsetTop += LoopParent.offsetTop;				
				ParentLevel++;
			} while (LoopParent = LoopParent.offsetParent);
		}
 		
//alert(OffsetTop + "," + Parent.offsetTop);
		
		Element.style.left = OffsetLeft + X + "px";
		Element.style.top = OffsetTop + Y + "px";

		// Set position of hover-over popup
// Element.style.left = Parent.offsetLeft + X + "px";
// Element.style.top = Parent.offsetTop + Y + "px";
	}
	
/*
 	if (Parent)
	{
		Element.style.top = Parent.Height + "px";
	}
*/
}

function OverPopup()
{
	PopupLock = false;
	StopPopupTimeout();
}

function ClosePopup(Event)
{
	// Check if the popup is locked open because it has not been hovered over yet
	if ((PopupLock) ||(LastPopupName == ''))
	{
		return 0;
	}

	StopPopupTimeout();

	// Check that this has not been triggered by a child of the popup
	var Target = '';
	if (window.event)
	{
		Event = window.event;
		Target = Event.srcElement;
	}
	else
	{
		Target = Event.target;;
	}

	if (!Target)
	{
		return 0;
	}

	var RelatedTarget = Event.relatedTarget;
	if (!RelatedTarget)
	{
		RelatedTarget = Event.toElement;
	}

	if (!RelatedTarget)
	{
		return 0;
	}

	if (Target.nodeName != 'DIV') 
	{
		return 0;
	}

	while (RelatedTarget != Target && RelatedTarget.nodeName != 'BODY')
	{
		RelatedTarget= RelatedTarget.parentNode
	}

	if (RelatedTarget== Target) 
	{
		return 0;	
	}

	HidePopup();
}

function HidePopupByName(PopupName)
{
	Element = document.getElementById(PopupName);

	if (!Element)
	{
		return 0;
	}

	StopPopupTimeout();
	Element.style.visibility = "Hidden";
}

function HidePopup()
{
	StopPopupTimeout();
	HidePopupByName(LastPopupName);
	LastPopupName = '';
}