//  _                     _ _      _____                 _          _     
// | |__   __ _ _ __   __| | | ___| ____|_   _____ _ __ | |_ ___   (_)___ 
// | '_ \ / _` | '_ \ / _` | |/ _ \  _| \ \ / / _ \ '_ \| __/ __|  | / __|
// | | | | (_| | | | | (_| | |  __/ |___ \ V /  __/ | | | |_\__ \_ | \__ \
// |_| |_|\__,_|_| |_|\__,_|_|\___|_____| \_/ \___|_| |_|\__|___(_)/ |___/
//                                                               |__/     
//
//This file declares a global object "handleEvents" that holds various methods
//to use while working with events.

var handleEvents = new Object;

//getMousePosition()
//Returns the mouse position relative to the event's target.
handleEvents.getMousePosition = function(event, element)
{
	//Is the element given or do we have to find it?
	if (element == null) element = this.getEventTarget(event);
	
	//Get the absolute position of the element.
	var mouse = this.getElementPosition(element);
	
	//Subtract the absolute position of the mouse.
	if (event.pageX || event.pageY)
	{
		mouse.x = event.pageX - mouse.x;
		mouse.y = event.pageY - mouse.y;
	}
	else if (e.clientX || e.clientY) 
	{
		mouse.x = (e.clientX + document.body.scrollLeft
					  + document.documentElement.scrollLeft) - mouse.x;
		mouse.y = (e.clientY + document.body.scrollTop
					  + document.documentElement.scrollTop) - mouse.y;
	}
			
	return mouse;
}
		
//getElementPosition()
//Returns the position of the given element relative to the page.
handleEvents.getElementPosition = function(element)
{
	//Create a object to return position.
	var pos = new Object();
	pos.x = 0;
	pos.y = 0;
	
	//Loop through all parent Nodes.
	while (typeof(element) == "object")
	{
		pos.x += element.offsetLeft;
		pos.y += element.offsetTop;
		
		if (element.tagName.toUpperCase() == "BODY")
			element = 0;
		else
			element = element.offsetParent;
	}
	
	return pos;
}

//getEventTarget()
//Returns the target of the given element.
handleEvents.getEventTarget = function(event)
{
	//This function returns the correct target of the given event.
	// (Thanks: http://www.quirksmode.org)
	var target;

	//Does the browser know the target-property?
	if (event.target)
		target = event.target;
		
	//Does the browser know the srcElement-property?
	else if (event.srcElement)
		target = event.srcElement;
	
	//Defeat a bug in Safari.
	if (target.nodeType == 3)
		target = target.parentNode;
		
	return target;
}
