/*	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/>
*/
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;
			};
		}
	};
}();

// cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko by Scott Andrew
// changed by Attila Szabo
function addEvent(elm, evType, fn, useCapture){
var wrappedFn = fn;

    var scope = elm;
    var wrappedFn = function(event){
        var e = event || window.event;
        return fn.call(scope, e);
    };

    var result;
    if (elm.addEventListener){
        elm.addEventListener(evType, wrappedFn, useCapture);
        result = true;
    } else if (elm.attachEvent){
        var r = elm.attachEvent('on' + evType, wrappedFn);
        result = r;
    } else {
        elm['on' + evType] = wrappedFn;
        result = true;
    }

    EventCache.add(elm, evType, wrappedFn, useCapture);
    return result;
}

window.onunload = function(){
	EventCache.flush();
};
