var shiftX =  10;
var shiftY = -30;

var popupTimerID = -1;
var currentWnd = null;
var thisWnd = null;
var currentElem = null;
	
	
//this is common
function zsProcessMouseMove(event)
{
	var targetElement = null;	
	if(event.srcElement)
		targetElement = event.srcElement;
	else if(event.target)
		targetElement = event.target;		
		
	if (currentWnd != null)
	{
		if (currentWnd.IsVisible)
		{
			if (currentElem != targetElement)
			{
				currentWnd.Hide();
			}
		}
	}
}


//this is common	
function zsPopupByElement(url, windowName, elem, width, height, status, delay, centered, isRollOver)
{
	var oManager = GetRadWindowManager();
	thisWnd = oManager.GetWindowByName(windowName);
	if (isRollOver)
	{
		currentWnd = thisWnd;
	}

	//if you click on the link when the window is open, then close it
	if (!isRollOver && thisWnd.IsVisible())
	{
		thisWnd.Close();
		return;
	}
	thisWnd.SetSize(width, height);
	if (url!='')
	{
		//currentWnd.SetUrl(url);
	}
	thisWnd.SetUrl(url);
	thisWnd.SetStatus(status);
	currentElem = elem;
	
	var oElem = elem;
	
	if (isRollOver)
	{
		currentElem = elem;
	}

	if (centered)
	{
		popupTimerID = window.setTimeout("zsPopupShowCentered(thisWnd)",delay);
		
	}else
	{
		popupTimerID = window.setTimeout("zsPopupShowRelative(thisWnd, " + width + "," + height + ")",delay);
	}
}

function zsPopupShowCentered(thisWnd)
{
		thisWnd.Show();
}

//function zsPopupShowRelative(currentWnd, oElem, W, H)
function zsPopupShowRelative(thisWnd, W, H)
{
	thisWnd.Show();
	var oElem = currentElem;
	var x;
	var y;
	//currentWnd.MoveTo(GetElementXPos(oElem) + GetElementWidth(oElem), GetElementYPos(oElem));
	
	//get the space in the right and see if there is room to show the popup
	var rSpace = (GetClientWidth() + GetScrollXOffset()) - (GetElementXPos(oElem) + GetElementWidth(oElem));
	
	//true means theres no room to show the popup	
	if(rSpace <= (W + shiftX))
	{
		x = GetElementXPos(oElem) - W - shiftX;
	}
	else
	{
		//x = GetMouseX(oElem) + 10 ;
		x = GetElementXPos(oElem) + GetElementWidth(oElem) + shiftX;
	};

	if(x < 0) x = 0;

	//get the bottom space and see if there is room to show the popup
	var bSpace = (GetClientHeight() + GetScrollYOffset()) - (GetElementYPos(oElem) + GetElementHeight(oElem));

	//true means there is no room
	if(bSpace <= (H + shiftY))
	{
		//we'll show above the element
		//y = GetElementYPos(oElem) - GetElementHeight(oElem) - H;
		y = GetClientHeight() + GetScrollYOffset() - H;
	}
	else
	{
		y = GetElementYPos(oElem) + GetElementHeight(oElem) + shiftY;
	}

	if(y < 0) y = 0;	

	thisWnd.MoveTo(x, y);
}




function zsPopupHide(windowName)
{
	var oManager = GetRadWindowManager();
	//oManager.GetActiveWindow().Hide();
	currentWnd = oManager.GetWindowByName(windowName);
	//currentWnd.Close();
	currentWnd.Hide();
	
}


//retrieves the left position of the given element, works with IE5+, NS6+ and Firefox1+
function GetElementXPos(obj)
{
	var xPos = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			xPos += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		xPos += obj.x;
	return xPos;
}

//retrieves the top position of the given element, works with IE5+, NS6+ and Firefox1+
function GetElementYPos(obj)
{
	var yPos = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			yPos += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		yPos += obj.y;
	return yPos;
}

//retrieves the width of the given element, works with IE5+, NS6+ and Firefox1+
//in NS and firefox, it may only return the visible width of the element
function GetElementWidth(obj)
{
    var width = 0;

    if(obj.scrollWidth)
	    width =  obj.scrollWidth;
    else if(obj.scrollOffsetWidth)
	    width =  obj.scrollOffsetWidth;
    else if(obj.offsetWidth)
	    width =  obj.offsetWidth;
    else if(obj.width)
	    width =  obj.width;

	return width;
}

//retrieves the height of the given element, works with IE5+, NS6+ and Firefox1+
//in NS and firefox, it may only return the visible height of the element
function GetElementHeight(obj)
{
    var height = 0;

    if(obj.scrollHeight)
	    height = obj.scrollHeight;
    else if(obj.scrollOffsetHeight)
	    height = obj.scrollOffsetHeight;
    else if(obj.offsetHeight)
	    height = obj.offsetHeight;
    else if(obj.height)
	    height = obj.height;

	return height;
}

//retrieves the verticle scrolling position, from the top of the document, works with IE5+, NS6+ and Firefox1+
function GetScrollYOffset()
{
    if (document.compatMode=='CSS1Compat') return document.documentElement.scrollTop; 
	if(document.body.scrollTop) return document.body.scrollTop;
	if(window.pageYOffset) return window.pageYOffset;
	
	return 0;
}

//retrieves the horizontal scrolling position, from the left side of the document, works with IE5+, NS6+ and Firefox1+
function GetScrollXOffset()
{
    if (document.compatMode=='CSS1Compat') return document.documentElement.scrollLeft; 
	if(document.body.scrollLeft) return document.body.scrollLeft;
	if(window.pageXOffset) return window.pageXOffset;
	
	return 0;
}	


//retrieves the client width of the browser, works with IE5+, NS6+ and Firefox1+
function GetClientWidth()
{
    if (window.innerWidth!=window.undefined) return window.innerWidth; 
    if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
    if (document.body) return document.body.clientWidth; 
    
	return screen.x;
}		

//retrieves the client height of the browser, works with IE5+, NS6+ and Firefox1+
function GetClientHeight()
{
    if (window.innerHeight!=window.undefined) return window.innerHeight;
    if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
    if (document.body) return document.body.clientHeight; 
    
	return screen.y;
}	

function GetMouseX(parentObj)
{
    // Detect if the browser is IE or not.
    // If it is not IE, we assume that the browser is NS.
    var IE = document.all?true:false
    // Temporary variable to hold mouse x pos.
    var tempX = 0

	/*
    if (IE)
    {   // grab the x pos if browser is IE
        tempX = event.clientX + document.body.scrollLeft;
    }
    else
    {   // grab the x pos.s if browser is NS
        tempX = GetElementXPos(parentObj) + 20;
    } */
    tempX = GetElementXPos(parentObj) + 20;
    // catch possible negative values in NS4
    if (tempX < 0) tempX = 0
    
    return tempX
}

function GetMouseY(parentObj)
{
	// Detect if the browser is IE or not.
    // If it is not IE, we assume that the browser is NS.
    var IE = document.all?true:false
    // Temporary variable to hold mouse y pos.
    var tempY = 0

	/*
    if (IE)
    {   // grab the y pos if browser is IE
        tempY = event.clientY + document.body.scrollTop;
    }
    else
    {   // grab the y pos.s if browser is NS
        tempY = GetElementYPos(parentObj);
    }*/ 
    
    tempY = GetElementYPos(parentObj);
    // catch possible negative values in NS4
    if (tempY < 0) tempY = 0
    
    return tempY
}

