/**
 * addEvent & removeEvent -- unified cross-browser event handling
 * Mack Pexton 2/18/2008 <mack at acmebase.org>
 * Based on work by Dao Gottwald, Dean Edwards, and Diego Perini.
 *
 * This implements parts of the W3C DOM Level 2 Event interface
 *  Methods:	event.stopPropagation(), event.preventDefault()
 *  Attributes:	event.currentTarget, event.target, event.relatedTarget (MouseEvent),
 * 		event.eventPhase
 *  Constants:	Event.AT_TARGET, Event.BUBBLING_PHASE
 * Supports W3C handleEvent.
 * "this" in listener function refers to object listener was attached to.
 * Listeners are called if FIFO order.
 * Removes all listeners on page unload to prevent memory leaking.
 * Implements addEvent(document.'load',fn) which fires when document is loaded but
 *  before images are loaded. 
 *
 * Distributed under GNU Lesser General Public License 2.1
 */

function addEvent(o, type, fn) {
	if (o == document && type == 'load') return document._onload.addListener(fn);
	o.addEventListener(type, fn, false);
}
function removeEvent(o, type, fn) {
	if (o == document && type == 'load') return document._onload.removeListener(fn);
	o.removeEventListener(type, fn, false);
}

// Support for addEvent(document,'load',fn)
document._onload = {
	done: false,
	queue: [],
	addListener: function(fn) {
		this.queue.push(fn);
	},
	removeListener: function(fn) {
		for (var i=0,l=this.queue.length; i<l; i++) {
			if (this.queue[i] == fn) {
				this.queue[i] = function(){};
				return;
			}
		}
	},
	callListeners: function(e){ 
		if (!this.done) {
			for (var i=0,l=this.queue.length; i<l; i++) {
				this.queue[i](e);
			}
			this.done = true;
		}
	},
	event: function() {
		var e = document.createEvent ? document.createEvent('load') : {type:'load'};
		e.target = e.currentTarget = document;
		return e;
	}
};

/*@cc_on
// from Dao Gottwald http://en.design-noir.de/webdev/JS/addEvent/
if (!window.addEventListener) {
	var addEvent = function (o, type, fn) {
		if (!o._events) o._events = {};
		var queue = o._events[type];
		if (!queue) {
			o._events[type] = [fn];
			if (!o._events._callback) o._events._callback = function (e) { Event._callListeners(e, o); };
			if (!(o === document && type == 'load')) o.attachEvent("on" + type, o._events._callback);
		}
		else if (Event._fnIndex(o, type, fn) == -1) {
			queue.push(fn);
		}
		else {
			return;
		}
		Event._mem.push([o, type, fn]);
	};
	var removeEvent = function (o, type, fn) {
		var i = Event._fnIndex(o, type, fn);
		if (i < 0) return;
		var queue = o._events[type];
		if (queue.calling) {
			delete queue[i];
			if (queue.removeListeners) { queue.removeListeners.push(i); }
			else { queue.removeListeners = [i]; }
		}
		else {
			if (queue.length == 1) { Event._detach(o, type); }
			else { queue.splice(i, 1); }
		}
	};
	var Event = {
		AT_TARGET: 2,
		BUBBLING_PHASE: 3,
		stopPropagation: function () { this.cancelBubble = true },
		preventDefault: function () { this.returnValue = false },
		_mem: [],
		_callListeners: function (e, o) {
			e.stopPropagation = this.stopPropagation;
			e.preventDefault = this.preventDefault;
			e.currentTarget = o;
			e.target = e.srcElement;
			e.eventPhase = e.currentTarget == e.target ? this.AT_TARGET : this.BUBBLING_PHASE;
			switch (e.type) {
				case "mouseover":
					e.relatedTarget = e.fromElement;
					break;
				case "mouseout":
					e.relatedTarget = e.toElement;
			}
			var queue = o._events[e.type];
			queue.calling = true;
			for (var i = 0, l = queue.length; i < l; i++)
				if (queue[i])
					if ("handleEvent" in queue[i])
						queue[i].handleEvent(e);
					else
						queue[i].call(o,e);
			queue.calling = null;
			if (!queue.removeListeners)
				return;
			if (queue.length == queue.removeListeners.length) {
				this._detach(o, e.type);
				return;
			}
			queue.removeListeners = queue.removeListeners.sort(function(a,b){return a-b});
			var i = queue.removeListeners.length;
			while (i--)
				queue.splice(queue.removeListeners[i], 1);
			if (queue.length == 0)
				this._detach(o, e.type);
			else
				queue.removeListeners = null;
		},
		_detach: function (o, type) {
			if (!(o === document && type == 'load')) o.detachEvent("on" + type, o._events._callback);
			delete o._events[type];
		},
		_fnIndex: function (o, type, fn) {
			var queue = o._events[type];
			if (queue)
				for (var i = 0, l = queue.length; i < l; i++)
					if (queue[i] == fn)
						return i;
			return -1;
		},
		_cleanup: function () {
			for (var m, i = 0; m = this._mem[i]; i++)
				if (m[1] != "unload" || m[2] == this._cleanup)
					removeEvent(m[0], m[1], m[2]);
		}
	};
	addEvent(document, "load", function(e){document._onload.callListeners(e)});
	addEvent(window, "unload", function(){Event._cleanup()});
}
@*/

// Setup event to fire when document loaded but before images and flash are loaded.
// from http://dean.edwards.name/weblog/2005/02/order-of-events
if (/WebKit/i.test(navigator.userAgent)) {	// Safari (ala jQuery)
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			clearInterval(_timer);
			document._onload.callListeners(document._onload.event()); 
		}
	}, 50);
}
else if (document.addEventListener) {		// MOZ, Opera
	document.addEventListener('DOMContentLoaded', function(e){document._onload.callListeners(e)}, false);
}
/*@cc_on
// from Diego Perini http://javascript.nwbox.com/IEContentLoaded/index.html
else if (document.all && !window.opera) {	// IE
	// polling for no errors
	(function() {
		try {
			// throws errors until after ondocumentready
			document.documentElement.doScroll('left');
		} catch (e) {
			setTimeout(arguments.callee, 50);
			return;
		}
		// no errors, fire
		document._events._callback(document._onload.event());
	})();
}
@*/
addEvent(window,'load',function(e){document._onload.callListeners(e)});
