/*-----------------------------------------------------------------------------
 * Event Handle Manager.  This simply allows multiple objects to be registered 
 * to handler certain events.  For example:
 *   window.addEventListener("load", alert('hi'), false);
 *   window.addEventListener("load", alert('there'), false);
 * results in only the first alert box showing.  The code that creates the 
 * manager should also check for any existing handles any existing
 * calls then calls the newly registered items. 
 * 
 * See http://therealcrisp.xs4all.nl/upload/addEvent_dean.html for a discussion
 * on advanced addEvent methods.
 *
 * Modifications:
 * Date        By          Description
 * ----------  ----------  -------------------------------------------------
 * 11/10/2006  Garth       Initial split from general.js file.
 *---------------------------------------------------------------------------*/


// Constructor.  This object provides the ability to handle one or more events.  
// This is used by a page to register multiple on load events, window scrolled 
// events, window resized etc.
// Arguments: 
//   - browserObjectToAttach = Browser Object to have the manager attached to 
//     (typcailly document or window).
//   - eventTypeToAttach = String value of the type of event to attach to.  This
//     value is like "load", "scroll" etc.
// Returns: An event manager that is basically an array of registered objects to
//   be called when the event is fired.  The constructor attaches itself to the 
//   object's event then when actived loops through the array.
//
// NOTE: did NOT use prototype functions because there won't be many of these
// objects and tried prototype with the handleEvent method having problems with
// the this.functionsToCall array.  Plus didn't really want that array to be 
// public!!!
function EventManager(browserObjectToAttach, eventTypeToAttach)
{
	if(!eventTypeToAttach) throw("EventManager.constructor - eventTypeToAttach can NOT be null."); 

	// private variable - container event object
	var _functionsToCall = new Array();	// private array of handlers to call.


	// Adds the function to the array of functions to call when this event is fired.
	// Arguments:
	//   - functionToAdd = Function that is either a "function" or a string to be
	//       evaluated, or an object in which case the code will try to call a
	//       method named ".handleEvent(e)".
	// Returns: nothing.
	this.add = function(functionToAdd)
	{
		_functionsToCall[_functionsToCall.length] = functionToAdd;
	} //-------------------------- End of Function --------------------------


	// Function which is retsitered with the container object to process/handle
	// the event.  This function loops through all the items in the array (added)
	// and if of type function calls, if type of string then it evaluates, else
	// if object then it calls a method called ".handleEvent(e)".
	// Arguments:
	//   - evt = Event object passed to this handler by the container object.
	// Returns: nothing.
	this.handleEvent = function(evt)
	{
		for(var i in _functionsToCall)
		{
			if(typeof(_functionsToCall[i]) == "function")
				_functionsToCall[i](evt);
			else if(typeof(_functionsToCall[i]) == "string")
				eval(_functionsToCall[i]);
			else if(typeof(_functionsToCall[i]) == "object")
			{
				// for this to work, function has to be a prototype func and named: eventHandler
				if(_functionsToCall[i].eventHandler) _functionsToCall[i].eventHandler(evt);
			}
		}
	} //-------------------------- End of Function --------------------------


	// Register the event listener.  
	// IMPORTANT NOTE: This registration code MUST reside after the function
	// declarations so that the ADD EVENT code being attached to the browser 
	// object exists - so it will interp. correctly.  There is not any way to 
	// find what is already registered.  Level 3 has added eventListenerList but 
	// as of 11/2006, no browser supports it.
	var eventType = eventTypeToAttach.toLowerCase();
	if(!browserObjectToAttach) browserObjectToAttach = window;
	if(typeof(browserObjectToAttach.addEventListener) != "undefined")
		browserObjectToAttach.addEventListener(eventType, this.handleEvent, false);
	else if(typeof(browserObjectToAttach.attachEvent) != "undefined") 
		browserObjectToAttach.attachEvent("on"+eventType, this.handleEvent);
} //~~~~~~~~~~~~~~~~~~~~~~~~~~ End of Constructor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/*=============================== End of File ================================*/

