/***********************************************************************************
*
* $Workfile: popup.js $ 
* SafeFile : $Archive: /ContentSystem/HelpSystem/Source/HelpSystemWeb/Scripts/popup.js $
* 
* $Revision: 0 $ 
*
* Purpose : 
*
* Modified : $Author: $ $Modtime: $
*
* Initial version by Alexey Kropotin (Hound) on Oct, 2004
* Copyright 2005 Saxo Bank. All rights reserved.
***********************************************************************************/

var currentPopup;
var openPopups = new Array;
		
function ShowPopup(ev)
{
	HidePopup(ev);
	var obj = null;
	if (ev.srcElement)
	{
		obj = ev.srcElement;
	}
	else
	if (ev.target)
	{
		obj = ev.target;
	}
	
	if(null != obj)
	{
	    while(null != obj && null == obj.getAttribute("popup"))
	    {
	        // HS#Prev.ID#2838
	        // obj = obj.parentElement;
	        obj = obj.parentNode;
	        
	        if(obj.nodeName == "#document")
	        {
	            obj = null;
	            break;
	        }
	    }
	}
	if (obj != null && obj.getAttribute("popup") != '' && obj.getAttribute("popup"))
	{
		obj = document.getElementById(obj.getAttribute("popup"));
		if (obj != null)
		{
			obj.style.zIndex = 2 + openPopups.length;
			obj.style.position = "absolute";
			
			obj.style.top = ev.clientY + document.body.scrollTop;
			obj.style.left = ev.clientX + document.body.scrollLeft;
			
			obj.style.display = "block";
			obj.style.backgroundColor = "white";
			obj.style.borderWidth = "1px";
			AddPopup(obj);
			
			// HS#Prev.ID#2790
			// min = obj.offsetWidth;
			// 
			// if (obj.offsetWidth < document.documentElement.offsetWidth/3)
			// 	min = document.documentElement.offsetWidth/3;
			// l = document.documentElement.offsetWidth - (ev.clientX + min);
			// if (l < 0)
			// {
			// 	//obj.style.posLeft = document.documentElement.offsetWidth - min;
			// 	obj.style.posLeft = obj.style.posLeft + l;
			// }
			// h = document.documentElement.offsetHeight - (ev.clientY + obj.offsetHeight);
			// 
			// if (h < 0)
			// {
			// 	//obj.style.posTop = ev.offsetY - obj.offsetHeight;
			// 	if (obj.style.posTop + h >=0)
			// 	{
			// 		obj.style.posTop = obj.style.posTop + h;
			// 	}
			// 	else
			// 	{
			// 		obj.style.posTop = 0;
			// 	}
			// }
			
			min = obj.clientWidth;
            if(obj.clientWidth < document.body.clientWidth/3)
            {
                min = document.body.clientWidth/3;
            }
   
            l = document.body.clientWidth - (ev.clientX + min);
            if (l < 0)
            {
                obj.style.left = parseInt(obj.style.left) + l;
            }
   
            h = document.body.clientHeight - (ev.clientY + obj.clientHeight);
            if (h < 0)
            {
                if ((parseInt(obj.style.top) + h)  >= 0)
                {
                    obj.style.top = parseInt(obj.style.top) + h;
                }
                else
                {
                    obj.style.top = 0;
                }
            }
					
			ev.returnValue = false;
			ev.cancelBubble = true;
			return false;
		}
	}				
}
			
function HidePopup(ev)
{
    if (currentPopup != null)
	{
		var obj = null;
		if (ev.srcElement)
		{	
			obj = GetParentPopup(ev.srcElement);
		}
		else
		{
			obj = GetParentPopup(ev.target);
		}
		if (currentPopup != null)
		{
			Hide(obj);
		}
	}
}

function GetParentPopup(obj)
{
	while(obj != null && !InPopups(obj))
	{
		// HS#Prev.ID#2783
		// obj = obj.parentElement;
		obj = obj.parentNode;
	}
	return obj;
} 

function AddPopup(obj)
{
	openPopups.push(obj);
	currentPopup = obj;
}

function InPopups(obj)
{
	for(i = 0;i < openPopups.length;i++)
	{
		if (openPopups[i] == obj)
			return true;
	}
	return false;
}

function Hide(obj)
{
  if (obj == null || obj!= currentPopup)
  {
	if (currentPopup != null)
	{
		currentPopup.style.display = "none";
		openPopups.pop();
		currentPopup = null;
		if (openPopups.length > 0)
		{
			currentPopup = openPopups[openPopups.length - 1];
			Hide(obj);
		}		
	}
  }
}



			
