/**
 * General JS functions to show hidden elements, and other useful stuff.
 */

/* extract parameters from URL. From http://www.netlobo.com/url_query_string_javascript.html */
function getUrlParam( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
	

/**
* Display the given div assumed to be hidden at the current mouse pointer location
*/
function showDivAtCursor(e, div) {
	showDiv(e, div, 0, 0);
}

function showDiv(e, div, xOffset, yOffset) {
	var element = document.getElementById(div);
	var xPos = 400;
	var yPos = 400;
	var windowWidth = 0;
	var windowHeight = 0;
	var windowTop = document.documentElement.scrollTop;

	if (window.innerWidth || window.innerHeight){
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	} else if (document.documentElement.clientHeight || document.documentElement.clientWidth) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body.clientWidth || document.body.clientHeight){
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	if (e.pageX || e.pageY) {
		xPos = e.pageX;
		yPos = e.pageY;
	} else {
		xPos = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		yPos = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}

	element.style.position = "absolute";
	element.style.display="";
	if(xPos+element.offsetWidth > windowWidth){xPos = windowWidth-element.offsetWidth-10}
	if(yPos+element.offsetHeight > (windowTop + windowHeight)){yPos = (windowTop + windowHeight)-element.offsetHeight-10}
	if(yPos < windowTop){yPos = windowTop + 10}
	xPos += xOffset;
	yPos += yOffset;
	element.style.left=xPos + "px";
	element.style.top=yPos + "px";
	element.style.zIndex = 100;
}

function showHelp(box,e) {
	// using the lovely new jQuery UI widgets
	$('#'+box).dialog('open');
}

