/**
 * Add and Remove events from objects
 */
PopJavaScriptFramework.v1B1.event = {
	/**
	 * Adds an event to an object
	 * @param Object obj The object to attach an event to
	 * @param String type [load | blur | focus | etc]
	 * @param Function fn The function to call when the even fires
	 */
	add: function( obj, type, fn ) {
		if (obj.addEventListener) {
			obj.addEventListener( type, fn, false );
		} 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] );
		}		
	},
	/**
	 * The exact same as event.add, but removing instead of adding
	 */
	remove: function( obj, type, fn ) {
		if (obj.removeEventListener) {
			obj.removeEventListener( type, fn, false );
		} else if (obj.detachEvent) {
			obj.detachEvent( "on"+type, obj[type+fn] );
			obj[type+fn] = null;
			obj["e"+type+fn] = null;
		}
	},
	/**
	 * Stop an event from firing
	 * This does no work in Safari prior to 2.0.? (find webkit version)
	 */
	stop: function(e) {
		if (e) { // event object
			if (e.preventDefault) {	// W3C
				e.preventDefault();
				e.stopPropagation();
			} else {				// IE
				e.returnValue = false;
				e.cancelBubble = true;
			}
		} else {
			dbug.log("onSubmit: try to cancel submit");
		}
		return false;
	},
	/**
	 * Allows for the implementation of custom events on a per Object level
	 */
	CustomEvents: function() {
		/**
		 * Container object for custom events.
		 */
		this.events = {};
		/**
		 * Create a custom event
		 * @param String sEventName The name of your custom event
		 */
		this.create = function(sEventName) {
			this.events[sEventName] = [];
		}
		/**
		 * Fire a custom event
		 * @param String sEventName The name of the event to fire
		 */
		this.fire = function(sEventName) {
			if (typeof this.events[sEventName] == 'undefined') { return; }

			for (var i=0; i<this.events[sEventName].length; i++) {
				if (typeof this.events[sEventName][i] == 'undefined') { continue; }

				var id =		this.events[sEventName][i][0];
				var scope =		this.events[sEventName][i][1];
				var method =	this.events[sEventName][i][2];
				var arg =		this.events[sEventName][i][3] || [];
				scope[method].apply(scope, arg);

				if (dbug.customEventLogging === true) {
					dbug.log(id, sEventName, method);
				}
			}
			if (dbug.customEventLogging === true && this.events[sEventName].length == 0) {
				dbug.log(sEventName);
			}
		}
		/**
		 * Add a method to call when the event is fired
		 * @param String sEventName The name of the event to listen for
		 * @param Object oScope Optional, scope to call the method in. Default is Window
		 * @param String sMethod The method to call in the given scope
		 */
		this.addListener = function(sEventName, oListenScope, sListenMethod, aListenArguments) {
			var oListenScope=oListenScope, sListenMethod=sListenMethod, aListenArguments=aListenArguments;
			var sListenID = sEventName + '_' + Math.random().toString().split('.')[1];
			if (typeof sListenMethod == 'undefined') {
				sListenMethod = oListenScope;
				oListenScope = window;
			}
			if (typeof this.events[sEventName] != 'undefined') {
				this.events[sEventName].push([sListenID, oListenScope,sListenMethod,aListenArguments]);
				return sListenID;
			} else {
				return false;
			}
		}
		this.removeListener = function(sListenID) {
			var event_name = sListenID.split('_')[0];
			var ev = this.events[event_name];
			for (var i=0; i<ev.length; i++) {
				if (ev[i][0] == sListenID) {
					delete this.events[event_name][i];
					break;
				}
			}
		}
	}
}
$event = PopJavaScriptFramework.v1B1.event;