// define package for abc stuff if it doesn't already exist
if (typeof console == "object") {
	// do nothing
} else {
	console = { debug: function() {}, info: function() {}, warn: function() {},
	 		    error: function() {}, log: function() {}, trace: function() {},
				dir: function() {}, assert: function() {}, group: function() {},
				groupEnd: function() {}, dirxml: function() {}, count: function() {} 
	};
}

Object.extend(Event, {
	customEvents: [],
	broadcast: function(broadcaster, method, params) {
		method = method.toLowerCase();
		//console.info(method);
		var e = { target: broadcaster, method: method, params: params };
		for (var i=0; i<Event.customEvents.length; i++) {
			if (Event.customEvents[i].method == method &&
				Event.customEvents[i].broadcaster == broadcaster) {
				Event.customEvents[i].action(e);
			}
		}
	},
	addListener: function(broadcaster, method, action) {
		if (typeof(action) == 'function' && broadcaster && method) {
			method = method.toLowerCase();
			var customEvent = { broadcaster: broadcaster, method: method, action: action };

			var found = false;
		
			for (var i=0; i<this.customEvents.length; i++) {
				if (this.customEvents[i].method == method &&
					this.customEvents[i].broadcaster == broadcaster &&
					this.customEvents[i].action == action) {
					found = true;
					break;
				}
			}

			if (!found) this.customEvents.push(customEvent);
		
			return customEvent;
		} else { 
			return false;
		}
	},
	removeListener: function(listener) {
		var index = -1;
		for (var i=0; i<this.customEvents.length; i++) {
			if (this.customEvents[i].method == listener.method &&
				this.customEvents[i].broadcaster == listener.broadcaster &&
				this.customEvents[i].action == listener.action) {
				this.customEvents.splice(i,1);
				break;
			}
		}
	}
});

/* -- DOM READY http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype  -- */
try {
    Object.extend(Event, {
        _domReady : function() {
            if (arguments.callee.done) return;
            arguments.callee.done = true;
            if (this._timer) clearInterval(this._timer);
            this._readyCallbacks.each(function(f) { f() });
            this._readyCallbacks = null;
        },
        onDOMReady : function(f) {
            if (!this._readyCallbacks) {
                var domReady = this._domReady.bind(this);
                if (document.addEventListener) document.addEventListener("DOMContentLoaded", domReady, false);
                /*@cc_on @*/
                /*@if (@_win32)
		    var curProtocol = location.protocol == "https:" ? "https://":"";
                    document.write("<script id=__ie_onload defer src='"+curProtocol+" +javascript:void(0)'><\/script>");
                    document.getElementById("__ie_onload").onreadystatechange = function() {
                        if (this.readyState == "complete") domReady();
                    };
                /*@end @*/
                if (/WebKit/i.test(navigator.userAgent)) {
                    this._timer = setInterval(function() {
                        if (/loaded|complete/.test(document.readyState)) domReady();
                    }, 10);
                }
                Event.observe(window, 'load', domReady);
                Event._readyCallbacks =  [];
            }
            Event._readyCallbacks.push(f);
        }
    });
} catch(e) {
}
