﻿// Kudos to http://www.haveamint.com/forum/viewtopic.php?pid=8153
// JG modified to generalize to multiple eventHandlers

function addEvent(func,funcName) {
	// Applies to document (window) level only
	if(typeof window.addEventListener != 'undefined') {
		//.. gecko, safari, konqueror and standard
		window.addEventListener(funcName, func, false);
	}
    else if(typeof document.addEventListener != 'undefined') {
		//.. opera 7
		document.addEventListener(funcName, func, false);
    }
	else if(typeof window.attachEvent != 'undefined') {
         //.. win/ie
         window.attachEvent('on'+funcName, func);
    }
    //** remove this condition to degrade older browsers
    else {
         //.. mac/ie5 and anything else that gets this far

         //if there's an existing function
         if(typeof eval('window.on'+funcName) == 'function') {
             //store it
             var existing = eval('window.on'+funcName);

             //add new handler
             eval('window.on'+funcName) = function() {
                 //call existing function
                 existing();

                 //call new function
                 func;
             };
         }
         else {
             //setup new function
             eval('window.on'+funcName) = func;
         };
     };
};

function addObjEvent(obj,type,fn) {
	// Used to attach events to in-page objects
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj.attachEvent( "on"+type, function() { obj["e"+type+fn](); } );
	};
};

function removeObjEvent(obj,type,fn) {
	// Used to remove events from in-page objects
	if (obj.removeEventListener) {
		obj.removeEventListener( type, fn, false );
	}
	else if (obj.detachEvent) {
		obj.detachEvent( "on"+type, obj["e"+type+fn] );
		obj["e"+type+fn] = null;
	};
};
