/** 
 * @fileoverview Mouse-related functionality for the JSPOOP library
 * @author James Palmer james@phoenixlondon.co.uk
 * @version 0.1 
 */

/**
 * Construct the Phoenix.Util.Mouse object.
 * @class Base class for Mouse-related functionality in the Phoenix library.
 * @constructor
 * @requires Phoenix
 */
Phoenix.Util.Mouse = function()
{
};
//registerNamespace("Phoenix.Util.Mouse");


/**
 * Retrieve the absolute (document-relative) coordinates of an event (eg, a mouse click)
 * 
 * @param event An event object
 *  
 * @return A hash containing keys 'x' and 'y'.
 */
Phoenix.Util.Mouse.getAbsolutePosition = function (event)
{
	event = event || window.event;	// Allow for IE-type deviations
  var posx = 0;
  var posy = 0;
  var IEsBrokenClientAreaOffset = 2;	// In IE, co-ordinates relative to the page's "client area" are offset from co-ordinates relative to the *document* by two pixels.  This happens in *both* IE 6 and 7, and will probably continue in later versions.
  
  // Check either x or y values are nonzero for IE and W3C-style co-ordinates.  If both these conditionals fall-through then no matter which property-pair were correct we know the co-ordinates are (0,0)
  
	if(event.pageX || event.pageY)	// W3C
	{
		posx = event.pageX;
		posy = event.pageY;
	}
	else if(event.clientX || event.clientY)	// IE
	{
		posx = (event.clientX-IEsBrokenClientAreaOffset) + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = (event.clientY-IEsBrokenClientAreaOffset) + document.body.scrollTop + document.documentElement.scrollTop;
	}
	
	return({x:posx, y:posy});
};


/**
 * Retrieve the coordinates of an event relative to the top-left corner of the supplied element.  Note: the passed event does not have to take place within the element's boundaries.
 * 
 * @param		{object}	event			An event object
 * @param 	(object)	element		An HTML element
 * 
 * @return											A hash containing keys 'x' and 'y'.
*/
Phoenix.Util.Mouse.getRelativePosition = function (event, element)
{
	if(Phoenix && Phoenix.DOM)
	{
		event = event || window.event;	// Allow for IE-type deviations
		
		var mousecoords = this.getAbsolutePosition(event);
		var elementcoords = Phoenix.DOM.getAbsolutePosition(element);
		var posx, posy;

		posx = mousecoords.x - elementcoords.x;
		posy = mousecoords.y - elementcoords.y;
		
		return({x:posx, y:posy});
	}
	else
	{
		throw new Phoenix.Exception.MissingLibraryException("Phoenix.DOM");
	}
};

