
//global variables
var gHoverDestPopup = new Object();
gHoverDestPopup.status = 0; // 0=not in use, 1=open
gHoverDestPopup.el = null;


// hand over popups data
var overDestPopup = false;
var destLocator = new Object();

/*
* createDestLocator() to create div
*/
function createDestLocator()
{
	destLocator.contents = document.createElement("DIV");
	destLocator.contents.id = "Destinationlocator";
}

/*
* popup() to list the data
*/
function destPopup(me, c, halign, valign, popFrame)
{
	var popupDiv = document.getElementById("destpopup");
	if(popupDiv.firstChild)
		popupDiv.removeChild(popupDiv.firstChild);
	else if(c == "locator")
	{  
		popupDiv.appendChild(destLocator.contents);
		currentLocator = me;		
	}
	popupDiv.halign = halign?halign:0;
	popupDiv.valign = valign?valign:0;
	popupDiv.fixed = (c=="locator"?true:false);
	popupDiv.popFrame = popFrame;
	popupDiv.calledby = me;	
	if (popupDiv.className.indexOf(c) == -1)
	{
		popupDiv.className = "popup "+c;
	}
	positiondestpopup(popupDiv);
	setDestPopupCover();

}


/*****************************
*  event popups
*****************************/

function cancelHoverPopup()
{
	if (gHoverDestPopup.status == 1)
	{
		// !!! remove
		assert( (gHoverDestPopup.el == null), "cancelHoverPopup err2" );

		destPopdown(true);

		gHoverDestPopup.status = 0;
		gHoverDestPopup.type = null;
		gHoverDestPopup.el = null;
		gHoverDestPopup.timeout = null;
	}
}

/*
* popdown() to hide the list
*/
function destPopdown(force)
{
	if(force || !overDestPopup)
	{
		var popupDiv = document.getElementById("destpopup");	
		popupDiv.className = "popup";
		popupDiv.style.height = "";
		if(popupDiv.firstChild)
			popupDiv.removeChild(popupDiv.firstChild);
		popupDiv.calledby = null;
		currentLocator = null;

		var cov = document.getElementById("destpopupcover");	
		cov.style.visibility = "hidden";
		cov.style.display = "none";
	}
}

/*
*positionpopup() to list according to position of text
*/
function positiondestpopup(p)
{
	p = document.getElementById("destpopup");
	var popupDiv = p?p:document.getElementById("destpopup");
	popupDiv.style.visibility = "hidden";
	popupDiv.style.position = "absolute";	
	popupDiv.style.display = "block";
	if(popupDiv.calledby)
	{
		var cx = findPosX(popupDiv.calledby);
		var cy = findPosY(popupDiv.calledby);
		var x = align(popupDiv.halign,cx,popupDiv.calledby.offsetWidth, popupDiv.offsetWidth);
		var y = align(popupDiv.valign,cy,popupDiv.calledby.offsetHeight,popupDiv.offsetHeight);

		if(!popupDiv.fixed)
		{
			if (p.popFrame && p.popFrame != '' && document.getElementById(p.popFrame))
				var r = elementRect(document.getElementById(p.popFrame));
			else
				var r = clientRect();

			var c = contained({'l':x,'t':y,'r':x+popupDiv.offsetWidth,'b':y+popupDiv.offsetHeight},r);
			
			if(c.axis == 'y') //x okay
			{
				//flip or nudge
				if(popupDiv.halign == 0 || popupDiv.halign == 3)//flip up/down			
					y = align(c.diff>0?1:2,cy,popupDiv.calledby.offsetHeight, popupDiv.offsetHeight);
				else y += c.diff			
			}
			else if(c.axis == 'x') //y okay
			{
				//flip or nudge
				if(popupDiv.valign == 0 || popupDiv.valign == 3)//flip to side
					x = align(c.diff>0?1:2,cx,popupDiv.calledby.offsetWidth, popupDiv.offsetWidth);
				else x += c.diff;
			}	
		}		
		popupDiv.style.left = x + 10 + "px";
		popupDiv.style.top = y + "px";
	}

	popupDiv.style.visibility = "visible";
}
/*
* horzPosPopup() to adjust the horizantal position
*/
function horzPosDestPopup()
{
	var popupDiv = document.getElementById("destpopup");
	var cx = findPosX(popupDiv.calledby);
	var x = align(popupDiv.halign,cx,popupDiv.calledby.offsetWidth, popupDiv.offsetWidth);

		if(!popupDiv.fixed)
		{
			var r = clientRect();
			var y = 0;
			var c = contained({'l':x,'t':y,'r':x+popupDiv.offsetWidth,'b':y+popupDiv.offsetHeight},r);
			
			if(c.axis == 'x') 
			{
				//flip or nudge
				if(popupDiv.valign == 0 || popupDiv.valign == 3)//flip to side
					x = align(c.diff>0?1:2,cx,popupDiv.calledby.offsetWidth, popupDiv.offsetWidth);
				else x += c.diff;
			}	
		}		
		popupDiv.style.left = x + "px";
}
/*
*	setDestPopupCover() calling from destinationList.js
*/
function setDestPopupCover()
{
	if(navigator.appName != "Microsoft Internet Explorer")
		return;
	var popupDiv = document.getElementById("destpopup");
	var cov = document.getElementById("destpopupcover");
	cov.style.display = "block";
	cov.style.left = popupDiv.style.left;
	cov.style.top = popupDiv.style.top;
	cov.style.width = popupDiv.offsetWidth;
	cov.style.height = popupDiv.offsetHeight;
	cov.style.visibility = "visible";
}

function overDestnPopup()
{
	overDestPopup = true;
}
function leaveDestPopup()
{
	overDestPopup = false;
}

function clickDestPopup()
{
	var popupDiv = document.getElementById("destpopup");
}
