/* Load event handling code // This defines the following:
  addEvent(targetObj,event,handlerFn,captureBool)
  the last add event should always be: addEvent(window, "unload",EventCache.flush,false);
*/



/*	EventCache Version 1.0
	Copyright 2005 Mark Wubben

	Provides a way for automagically removing events from nodes and thus preventing memory leakage.
	See <http://novemberborn.net/javascript/event-cache> for more information.
	
	This software is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
*/

/*	Implement array.push for browsers which don't support it natively.
	Please remove this if it's already in other code */
if(Array.prototype.push == null){
	Array.prototype.push = function(){
		for(var i = 0; i < arguments.length; i++){
			this[this.length] = arguments[i];
		};
		return this.length;
	};
};

/*	Event Cache uses an anonymous function to create a hidden scope chain.
	This is to prevent scoping issues. */
var EventCache = function(){
	var listEvents = [];
	
	return {
		listEvents : listEvents,
	
		add : function(node, sEventName, fHandler, bCapture){
			listEvents.push(arguments);
		},
	
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				
				/* From this point on we need the event names to be prefixed with 'on" */
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				
				item[0][item[1]] = null;
			};
		}
	};
}();

/* 
Modified addEvent script - changes compiled by Jared Noble 10/6/2005

originally written by Scott Andrew - http://www.scottandrew.com/weblog/articles/cbs-events
This function supports event registration for both DOM2 and IE event models.

This version has been modified to automatically add registered events to the event cache.
modified version from http://novemberborn.net/javascript/event-cache/follow-up

It also includes a third branch for DOM 0 interface (for IE/Mac)  this code from
Simon Willison (http://simon.incutio.com/archive/2004/05/26/addLoadEvent)
*/
function addEvent(obj, evType, fn, useCapture){
// supports event registration via DOM2 (preferred) and IE event models
    var result;
    if (obj.addEventListener){ // DOM 2 interface
        obj.addEventListener(evType, fn, useCapture);
        result = true;
    } else if (obj.attachEvent){ // IE5 Win interface
        result = obj.attachEvent("on"+evType, fn);
    } else {  // DOM 0 interface
        var oOldEvent = obj["on" + evType];
        if (typeof oOldEvent != "function") {
          obj["on"+evType] = fn;
        } else {
          obj["on"+evType] = function(e) {
            oOldEvent(e);
            fn(e);
          }
        }
        result=false;
    }
    // Only write DOM2/IE event handlers to the Event Cache
    if(result=true){
      EventCache.add(obj, evType, fn, useCapture);
    }
    return result;
}


