/*==================================================================
	popups.js
	
	Author: James Edgeworth (2009) (unless otherwise credited)
	
	
	Various functions handling various types of popups:
	
	drop-down - an element used as a drop-down list for custom Ajax-based
	search-while-you-type, etc.
	
	top-right - an element that appears in the top right corner of the page to
	notify of Ajax-based functionality taking place.
	
	info hover popup - an element that appears next to the mouse cursor when
	hovering over a triggering element.

==================================================================*/

var dropDownDiv;
var topRightDiv;


// Very very old, un-used function awaiting removal.
function OpenPopup(url, windowName, params)
{

	var url = (url == null) ? "" : url;
	var windowName = (windowName == null) ? "Something Important" : windowName;
	var params = (params == null) ? "HEIGHT=225,resizable=yes,WIDTH=400" : params;
	
	 window.open(url, windowName, params);

}






/*
	resultsPopup()
	
	Pops up a div container underneath an input element.
	The intended use is to call a script with the value
	of the inputElement, and any results will be shown
	in a div element underneath the inputElement.

*/

var g_type = '';

function searchDropDown(inputElement, script, type)
{

	g_type = type;

	// Only bother if there's data worthwhile in the input. Saves a few queries.
	if ( inputElement.value.length >= 1 )
	{

		// Check if dropDownDiv has been created, and create if it needs be.
		if ( !dropDownDiv )
		{
		
			dropDownDiv = document.createElement('div');
			dropDownDiv.className = 'dropDownPopup';
			dropDownDiv.id = 'dropDownId';
			
			//dropDownDiv.style.width = inputElement.offsetWidth + "px";
			
			document.body.appendChild(dropDownDiv);
			
		}
		
		var offset = elementPosition(inputElement);
			
			dropDownDiv.style.top 	= offset[0] + "px";
			dropDownDiv.style.left 	= offset[1] + inputElement.offsetWidth + 10 + "px";
		
		dropDownDiv.style.visibility = 'visible';
		
		var getVarName = inputElement.name;
		
		sendAndCall(dropDownPostProcess, 'dropDownId', script, getVarName + '=' + inputElement.value);
		
	}
	else
	{
		dropDownDiv.style.visibility = 'hidden';
	}
	
}

function closeSearchDropDown()
{
	dropDownDiv.style.visibility = 'hidden';
}

function openSearchDropDown()
{
	dropDownDiv.style.visibility = 'visible';
}


/*
	dropDownPostProcess()
	
	Target of sendAndCall() used in searchDropDown(). Handles
	the data recieved.
*/

function dropDownPostProcess()
{

	//dropDownDiv.style.visibility = 'visible';
	
	document.getElementById('dropDownId').innerHTML = '<b>Existing ' + g_type + ':</b><br />';
	document.getElementById('dropDownId').innerHTML += responseContent;
	
}




/*
	topRightPopup()
	
	Creates a div element which is perfectly at the top right
	corner of the page. It is used to display a status message
	during Ajax calls.
	
*/
function topRightPopup()
{

	if ( !topRightDiv )
	{
	
		topRightDiv = document.createElement('div');
		topRightDiv.className = 'topRightPopup';
		topRightDiv.id = 'topRightId';
	
		topRightDiv.style.left = getPageWidth() - 210 + 'px';
		
		topRightDiv.style.visibility = 'hidden';
		
		document.body.appendChild(topRightDiv);
		
	}

}


function setTopRightPopupText(text)
{

	topRightDiv.style.visibility = 'visible';
	
	topRightDiv.innerHTML = text;
	
}


function setTopRightPopupHide(time)
{
	setTimeout("topRightDiv.style.visibility = 'hidden'", time);
}




/*
	hoverPageStart()
	
	Call this function when page is loading up. Without it, 
	we cannot get the mouse position.
*/

function hoverPageStart()
{

	if ( window.addEventListener == null )
	{
		
		document.attachEvent('onmousemove', getMouseXY);
	
	}
	else
	{
	
		window.addEventListener('mousemove', getMouseXY, true);
	
	}
	
}


/*
	openInfoHoverPopup()
	
	Displays a given div and it's contents next to the cursor when the
	cursor is hovering over a triggerable element (one which simply calls
	this function with mouseOver).

*/
function openInfoHoverPopup(triggerElement, hoverDivId)
{

	hoverDiv = document.getElementById(hoverDivId);
	
	hoverDiv.style.top = mouseY + "px";
	hoverDiv.style.left = mouseX + 20 + "px";
	hoverDiv.style.zIndex = "1";
	
	hoverDiv.style.visibility = "Visible";
	
}


/*
	closeInfoHoverPopup()
	
	Closes an opened infoHoverPopup.

*/
function closeInfoHoverPopup(hoverDivId)
{

	hoverDiv = document.getElementById(hoverDivId);
	
	hoverDiv.style.visibility = 'Hidden';
	
}




hoverPageStart();
