registerNamespace("Phoenix.Util.Mouse");


/*
	getAbsolutePosition()

	Retrieve the absolute 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});
};


/*
	getRelativePosition()

	Retrieve the coordinates of an event (eg, a mouse click) relative to the top-left corner of the supplied element (event does not have to take place within the element's boundaries)

	@param event
	An event object
	
	@param 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");
	}
};

