/***********************************************************************
 *  Filename: mod25_events.js                                          *
 *  Date Created: 10MAY06                                              *
 *  Date Last Modified: 16JUL06                                        *
 *  Package: mod25                                                     *
 *  Type: Class definition                                             *
 *  Copyright © 2006 Mod25 Solutions, LLC. All Rights Reserved         *
 *  Notes: This class contains functions for working with events, such *
 *	as adding, removing, and pre-processing.  This class is a          *
 *	static class and does not require and instance to be created.      *
 *	All methods are static.                                            *
 **********************************************************************/


/***********************************************************************
 * Requirements:                                                       *
 *      None                                                           *
 **********************************************************************/


// Define the class object
var mod25_events = new Object();

// Process an event so that it will act equally on both Mozilla and IE
// Arguments: e - The event object passed to the event handler
mod25_events.processEvent = function(e) {
	// Get the event object
	e = e? e : window.event;
	
	// Get the element that caused the event
	if (!e.target) e.target = e.srcElement;
	
	// Make sure the event can be kept from calling the default action
	if (!e.preventDefault) e.preventDefault = function() { return false; };
	
	// Make sure the event can be kept from bubbling up the DOM tree
	if (!e.stopPropagation) e.stopPropagation = function() { if (window.event) window.event.cancelBubble = true; };
	
	return e;
};

// Add an event handler to an element
// Arguments: obj - The object reference to which the event will be added; event - Event that the handler is for; func - Function that will handle the event;
//	capture - Set to true for user capture
mod25_events.addEvent = function(obj, event, func, capture) {
	// Check if capture is defined or set it to false
	capture = capture || false;
	
	// Check if this is a custom event
	if (event.indexOf('on_') != -1) {
		obj['_' + event] = func;
		return;
	}
	
	// Add the event handler
	if (obj.addEventListener) obj.addEventListener(event, func, capture);
    else if (obj.attachEvent) obj.attachEvent("on" + event, func);
	
};

// Remove an event handler from an element
// Arguments: obj - The object reference the event handler will be removed from; event - The event handler to remove; func - The function the event handler calls
//	capture - Set to true for user capture
mod25_events.removeEvent = function(obj, event, func, capture) {
	// Check if capture is defined or set it to false
	capture = capture || false;
	
	// Check if this is a custom event
	if (event.indexOf('on_') != -1) {
		delete obj['_' + event];
		return;
	}
	
	// Remove the event handler
	if (obj.removeEventListener) obj.removeEventListener(event, func, capture);
    else if (obj.detachEvent) obj.detachEvent("on" + event, func);
	
};