
//====================
//	getPositionOfEvent
//
//	Taken from http://www.quirksmode.org/js/events_properties.html
//
//	Get the X and Y coordinates of an event. This could be the X and Y coords for
//	the mouse click on an onclick event. The coordinates are the "good" coordinates,
//	relative to the document.
//====================

function getPositionOfEvent( e )
{
	var loCoords = new Object();

	if ( !e ) var e = window.event;

	if ( e.pageX || e.pageY )
	{
		loCoords.x = e.pageX;
		loCoords.y = e.pageY;
	}
	else if ( e.clientX || e.clientY )
	{
		loCoords.x = e.clientX + ( document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft );
		loCoords.y = e.clientY + ( document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop );
	}

	return loCoords;
}

//====================
//	getPositionOfLayer
//
//	Get the X and Y coordinates of a layer relative to the document.
//====================

function getPositionOfLayer(obj)
{
	var loCoords = new Object();
	loCoords.x = 0;
	loCoords.y = 0;

	if ( obj.offsetParent )
	{
		while ( obj.offsetParent )
		{
			loCoords.x += obj.offsetLeft;
			loCoords.y += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if ( obj.x )
	{
		loCoords.x = obj.x;
		loCoords.y = obj.y;
	}

	return loCoords;
}

//====================
//	moveToMouse
//
//	Combine several helper functions
//====================

function moveToMouse( pEvent, poTargetLayer )
{
	//	The target layer is positioned absolutely in regard to the parent, so make sure the parent is
	//	either relatively or absolutely positioned itself.

	if ( poTargetLayer.parentNode.style.position != "relative" && poTargetLayer.parentNode.style.position != "absolute" )
	{
		poTargetLayer.parentNode.style.position = "relative";
	}

	loEventCoords = getPositionOfEvent( pEvent );
	loParentCoords = getPositionOfLayer( poTargetLayer.parentNode );

	poTargetLayer.style.left = loEventCoords.x - loParentCoords.x + "px";
	poTargetLayer.style.top = loEventCoords.y - loParentCoords.y + "px";

	toggleLayer( poTargetLayer, true );

	return true;
}

//====================
//	Taken from http://www.sitepoint.com/blog-post-view.php?id=171578
//====================

function addLoadEvent( newFunction )
{
	var oldonload = window.onload;
	if ( typeof window.onload != "function" )
	{
		window.onload = newFunction;
	}
	else
	{
		window.onload = function()
			{
				oldonload();
				newFunction();
			}
	}
}

//====================
//	getElementsByClassName
//
//	Taken from http://www.snook.ca/archives/000370.php
//====================

function getElementsByClassName( classname )
{
	var rl = new Array();
	var re = new RegExp('(^| )'+classname+'( |$)');
	var ael = document.getElementsByTagName('*');
	var op = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
	if (document.all && !op) ael = document.all;
	for(i=0, j=0 ; i<ael.length ; i++)
	{
		if(re.test(ael[i].className))
		{
			rl[j]=ael[i];
			j++;
		}
	}

	return rl;
}

//====================
//	Change the show/hide state of a layer
//====================

function toggleLayer( poLayer, pbForceState )
{
	if ( typeof pbForceState != "undefined" ) poLayer.style.display = ( pbForceState ? "" : "none" );
	else if ( poLayer.style.display == "none" ) poLayer.style.display = "";
	else poLayer.style.display = "none";

	return true;
}

//====================
//	isEmpty
//====================

function isEmpty( psString )
{
	return psString.match( new RegExp( "^[ \t\n]*$" ) );
}
