/** 
 * @fileoverview Event-related functionality for the JSPOOP library
 * @author James Palmer james@phoenixlondon.co.uk
 * @version 0.1 
 */

/**
 * Construct the Phoenix.Util.Event object.
 * @class Base class for Event-related functionality in the Phoenix library.
 * @constructor
 * @requires Phoenix
 */
Phoenix.Util.Event = function()
{
};
//registerNamespace("Phoenix.Util.Event");

/**
 * Browser-agnostic function to retrieve the element which <strong>originally</strong> fired the passed event
 *  
 * @param 	{object}		event			An event object
 *  
 * @return												A reference to the element which originally fired the event (or null, in case of failure)
 * 
 * @see #getCurrentTarget
*/
Phoenix.Util.Event.getTarget = function (event)
{
	event = event || window.event;
	var target = null;

	if(event.target)
	  target = event.target;
	else if(event.srcElement)
	  target = event.srcElement;
	else
		return(null);

	if(target.nodeType == 3) // defeat Safari bug
	  target = target.parentNode;

	return(target);
};


/**
 * Function to retrieve the which element is <strong>currently</strong> handling the event (may be originating element, or may be a parent of the originating element as the event bubbles up the DOM tree)
 *  
 * @param		{string}		event			An event object
 * 
 * @return												A reference to the element which originally fired the event (or null, in case of failure)
 * 
 * @see #getTarget
*/
Phoenix.Util.Event.getCurrentTarget = function (event)
{
	event = event || window.event;
	var target = null;

	if(event.currentTarget)
	  target = event.currentTarget;
	else
		throw new Phoenix.Exception.ReallyBadIdeaException("This functionality is unavailable on Internet Explorer 7 and below.  IEv<=7 does not support event.currentTarget (or any synonym), so it is effectively impossible to discover which element is handling the event as it bubbles up the DOM tree.  Hence it's a Really Bad Idea to call this function in IE.", "Phoenix.Util.Event.getCurrentTarget");
		
	if(target.nodeType == 3) // defeat Safari bug
	  target = target.parentNode;

	return(target);
};




/**
 * Browser-agnostic function to attach an event handler to an element.
 * 
 * @param			{string}		type					Event type ("mouseover", "click", etc).
 * @param 		{object}		element				Element to attach the event handler to.
 * @param 		{function}	eventhandler	Reference to the event-handling function
 * @param 		{boolean}		usecapture		Initiate event-capture (true), or use event bubbling as normal (false)
 * 
 * @return															False if no mechanism was found to attach an event.  Otherwise true.
*/
Phoenix.Util.Event.addEventHandler = function (type, element, eventhandler, usecapture)
{
	usecapture = usecapture || false;
	if(element.addEventListener)
		element.addEventListener(type, eventhandler, usecapture);
	else if(element.attachEvent)
		element.attachEvent("on"+type, eventhandler);
	else
		return(false);
		
	return(true);
};


/**
 * Browser-agnostic function to artificially fire an event from an element - KNOWN BUGS.
 * 
 * <br /><br />KNOWN BUGS IN SOME BROWSERS: Some browsers will only allow certain types of event to be fired from certain types of element. For this reason ensure you carefully test any code relying on this functionality in all browsers to ensure the event is definitely fired correctly and without errors.
 * 
 *  
 * 
 * @param 		{object}		element				Element to attach the event handler to.
 * @param			{string}		eventtype			Event type ("mouseover", "click", etc).
 * @param 		{boolean}		bubbles				Can the event bubble up the DOM tree?
 * @param 		{boolean}		cancelable		Can the event be cancelled?
 * 
 * @return															False if error encountered, otherwise true.
*/
Phoenix.Util.Event.fireEvent = function(element, eventtype, bubbles, cancelable)
{
	if(!element || !eventtype)
		return(false);
		
	bubbles = bubbles || true;
	cancelable = cancelable || true;
		
	// And finally fire change event to notify any attached event handlers that the value's changed
	if(document.createEvent)
	{
		var event = document.createEvent("HTMLEvents");
	  event.initEvent(eventtype, bubbles, cancelable);
	  element.dispatchEvent(event);
	}
	else if(thumb.fireEvent)
	{
		element.fireEvent("on" + eventtype);
	}
	
	return(true);
};