/**
 * Dustin Diaz's "Rock Solid addEvent." Attach script events to DOM nodes in a
 * safe and uniform way; remove them automatically when page is unloaded to
 * prevent memory leaks.
 *
 * @author Dustin Diaz (www.dustindiaz.com)
 * @author Mark Wubben (www.novemberborn.net), for EventCache
 * @copyright 2005 by Dustin Diaz and Mark Wubben
 * @license CC-GNU LGPL (http://creativecommons.org/licenses/LGPL/2.1/)
 * @param {Object} obj The object to which the event is attached (e.g. window or document).
 * @param {String} type The event we're on the lookout for (e.g. onload or onclick).
 * @param {Function} fn The function to execute when the event happens to the object.
 */
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

// EventCache by Mark Wubben (www.novemberborn.net/javascript/event-cache)
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			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]);
				};
				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;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);