﻿//global variables for tracking the menu and popups - Courtesy Dundas Software Ltd.
var dropMenuParent = null;
var currentDropMenu = null;
var popupTimerID = -1;
var popupTimerTimeout = 700;
var popupLeftPos = null;

// resets the popup, generally called on onmousemove - Courtesy Dundas Software Ltd.
function ResetPopup()
{
	if(currentDropMenu != null)
	{
		if(currentDropMenu.style.visibility == 'hidden')
		{
			window.clearTimeout(popupTimerID);
			popupTimerID = window.setTimeout("DelayedShowPopup()",popupTimerTimeout);
		}
	}
}

// called to show a dynamicallly loaded popup, generally called on onmouseover - Courtesy Dundas Software Ltd.
function ShowDynPopupNow(url,elementID, parentObj)
{
	WriteSyncDynamicData(url, elementID);
	ShowPopupIntern(elementID, parentObj,1);
}
// hides a drop menu or popup - Courtesy Dundas Software Ltd.
function HidePopup()
{
	HideDropMenu();
}
// Courtesy Dundas Software Ltd.
function WriteSyncDynamicData(url, elementID)
{
	DynamicDataElement = document.getElementById(elementID);
	
	if (window.ActiveXObject)
	{
		DynamicDataRequest = new ActiveXObject("Microsoft.XMLHTTP");
		DynamicDataRequest.open("GET", url, false);
		DynamicDataRequest.send();
		if (DynamicDataRequest.status == 200)
			DynamicDataElement.innerHTML = DynamicDataRequest.responseText;
	}
	else if (window.XMLHttpRequest)
	{
		DynamicDataRequest = new XMLHttpRequest();
		DynamicDataRequest.open("GET", url, false);
		DynamicDataRequest.send(null);
		if (DynamicDataRequest.status == 200)
			DynamicDataElement.innerHTML = DynamicDataRequest.responseText;
	}
}
// Courtesy Dundas Software Ltd.
function ShowPopupIntern(elementID, parentObj, timeDelay)
{
	HideDropMenu();
	currentDropMenu = document.getElementById(elementID);
	dropMenuParent = parentObj;
	popupTimerTimeout = timeDelay;

	//move the popup if required
	var rSpace = ( GetClientWidth() + GetScrollXOffset() ) - 
				( GetElementXPos(parentObj) + GetElementWidth(parentObj) );

	if(rSpace <= GetElementWidth(currentDropMenu))
	{
		currentDropMenu.style.left = GetElementXPos(parentObj) - GetElementWidth(currentDropMenu);
	}
	else
	{
		currentDropMenu.style.left = GetMouseX(parentObj) + 10 + "px";
	}

	if(GetElementXPos(currentDropMenu) < 0)
		currentDropMenu.style.left = 0;

	//store the left position for when the popup actually appears
	//then make sure that the popup is positioned off screen
	//so that parts of it will not appear until it is actually visible
	//this is for working around an IE bug
	popupLeftPos = currentDropMenu.style.left;
	currentDropMenu.style.left = -1000;
	
	var bSpace = ( GetClientHeight() + GetScrollYOffset() ) -
				( GetElementYPos(parentObj) + GetElementHeight(parentObj) );
	
	if(bSpace <= GetElementHeight(currentDropMenu))
	{
		currentDropMenu.style.top = GetElementYPos(parentObj) - GetElementHeight(parentObj) - GetElementHeight(currentDropMenu) + "px";
	}
	else
	{
		currentDropMenu.style.top = GetElementYPos(parentObj) + GetElementHeight(parentObj) + "px";
	}

	if(GetElementYPos(currentDropMenu) < 0)
		currentDropMenu.style.top = 0;
		
	//show the menu, use a timer to indirectly call it so Firefox does not flash
	popupTimerID = window.setTimeout("DelayedShowPopup()",popupTimerTimeout);
}
//should be called in the Body tag using onmousemove, this function tracks the state of the menu
//and automatically hides the menu when appropriate
function ProcessMouseOver(event)
{
	//get the source element that initiall fired the event, useful for bubbled events
	var targetElement = null;
	if(event.srcElement)
		targetElement = event.srcElement;
	else if(event.target)
		targetElement = event.target;		

	if(currentDropMenu != null)
	{		
		var hideMenu = true;
		var par = targetElement;
		while(par != null){
							
			if(par == dropMenuParent)
			{
				hideMenu = false;
				break;
			}
			if(par.parentNode != null)
				par = par.parentNode;
			else
				par = par.parentObjment;
		}
		if(hideMenu)
		{
			HideDropMenu();
		}
	}
}

//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;
    } 
    // 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);
    }  
    // catch possible negative values in NS4
    if (tempY < 0) tempY = 0
    
    return tempY
}

//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;
}
// called by the other popup functions - Courtesy Dundas Software Ltd.
function DelayedShowPopup()
{
	if(currentDropMenu != null)
	{
		currentDropMenu.style.left = popupLeftPos;
		currentDropMenu.style.visibility = 'visible';
	}
}
// Courtesy Dundas Software Ltd.
function HideDropMenu()
{	
	if(currentDropMenu == null)
		return;
	
	window.clearTimeout(popupTimerID);
		
	if(currentDropMenu.style.visibility != 'hidden')
	{
		currentDropMenu.style.visibility = 'hidden';
		currentDropMenu.style.left = -1000;
	}
				
	currentDropMenu = null;
	dropMenuParent = null;

}
