/**
 * Modeling Java Utilities Classes, since we don't have a private keyword in JS
 * just throw an exception when someone tries to use it. Unlike java you can just 
 * throw anything, so lets throw a string. This code was adapted from Todd 
 * Ditchendorf's blog http://www.ditchnet.org/wp/2005/04/02/6/
*/
function EventUtils() {
	throw 'RuntimeException: EventUtils is a static utility class ' +
    	' and may not be instantiated';
}

/**
 * Static utility method for adding event listeners in a browser neutral way.
 * EOMB (Every other modern browser) besides IE implements the event module
 * of the DOM 2 spec. IE may adapt this in some future release (IE 6 was
 * being used at the time of this writing) but until then ....
*/
EventUtils.addEventListener = function (target,type,callback,captures) {
	if (target.addEventListener) {
    	// EOMB
	    target.addEventListener(type,callback,captures);
	} else if (target.attachEvent) {
	    // IE
	    target.attachEvent('on'+type,callback,captures);
	} else {
		// IE 5 Mac and some others
	    target['on'+type] = callback;
	}
}

/**
 * Static utility method to get the tag name of the DOM node that caused the event. This method works for IE and DOM 2
 * compliant browswers (EOMB every other major browser) 
*
*/
EventUtils.getSourceEventTagName = function (evt) {
	if (evt && evt.target){
	    // EOMB
	    return evt.target.tagName;	
	}
	else if (window.event && window.event.srcElement) {
    	// IE
	   	return window.event.srcElement.tagName;
	} 
}

/**
 * Cancel the event by cancelling the default action and stopping the event propogation. Often times you don't need
 * to do both. In those cases use EventUtils.stopEventPropogation and EventUtils.preventEventDefault individually.
*/
EventUtils.cancelEvent = function(evt) {
	EventUtils.preventEventDefault(evt);		
	EventUtils.stopEventPropogation(evt);
}

/**
 * Stops the event propagation. This will stop the propogation in either the capture phase (DOM2 EOMB) or the bubbling
 * phase (EOMB and IE). In the capture phase the event works it's way from the top of the tree to the bottom. In the 
 * bubbling phase the event bubbles up from the targeted entity to the document. IE only bubbles. EOMB implements
 * the DOM2 standard and does both.
*/ 
EventUtils.stopEventPropogation = function(evt) {
	if (evt && evt.stopPropogation){
		//EOMB
		evt.stopPropogation();
	}
	else if (window.event){
		//IE
		window.event.cancelBubble = true;
	}
}

/**
 * This cancles the default action of the target entity. For example the default action of a click event on an URL is to
 * navigate to the URL. The event default is NOT specified by the DOM spec so this may behave differently amongst different
 * browsers.
*/
EventUtils.preventEventDefault = function(evt) {
	if (evt && evt.preventDefault){
		//EOMB
		evt.preventDefault();
	}
	else if (window.event){
		//IE
		window.event.returnValue = false;
	}
}

/**
  * Returns true if the alt key was pressed, false otherwise
 */  
EventUtils.altKey = function(evt) {
	if (evt){
		//EOMB
		return evt.altKey;
	}
	else if (window.event){
		//IE
		return window.event.altKey ;
	}
}

/**
  * Returns true if the ctrl key was pressed, false otherwise
 */  
EventUtils.ctrlKey = function(evt) {
	if (evt){
		//EOMB
		return evt.ctrlKey;
	}
	else if (window.event){
		//IE
		return window.event.ctrlKey;
	}
}

/**
  * Returns the 0 for the left mouse button, 1 for the middle button, and 2 for the right mouse button
  */
EventUtils.mouseButton = function(evt) {
	if (evt){
		//EOMB
		return evt.button;
	}
	else if (window.event){
		//IE
		return window.event.button;
	}
}
