/*
Based on the excellent work of Dean Edwards (and others):
http://dean.edwards.name/weblog/2005/09/busted/
*/

unFocus.Utilities.QuickLoader = (function() {
	var _eventMgr = new unFocus.Utilities.EventManager("_quickLoad", "_quickLoadPriority");
	
	function _quickLoad() {
		// cancel the standard load to prevent double call
		if (document.removeEventListener)
			document.removeEventListener("load", _load, false);
		// prevents double load and cleans up possible memory leak issues in IE
		else if (window.detachEvent) window.detachEvent("onload", _load);
		// call loading script
		_load();
	}
	function _load(e) {
		// trigger listeners
		_eventMgr.notifyListeners("_quickLoadPriority");
		_eventMgr.notifyListeners("_quickLoad"); // :TODO: send the event object
	}
	
	// the following lines all attempt to set up the early load events.
	if (document.addEventListener) {
		// add the quick load for Mozilla (and Opera 9+)
    	document.addEventListener("DOMContentLoaded", _quickLoad, false);
		// fail safe load method (in case the quicker load method fails)
		document.addEventListener("load", _load, false);
	} else if (window.attachEvent)
		window.attachEvent("onload", _load);
	
	// For Safari
	// this doesn't seem to work if the document is small enough
	/*if (/WebKit/i.test(navigator.userAgent)) { // sniff
		var _timer = setInterval(function() {
			if (/loaded|complete/.test(document.readyState)) {
				clearInterval(_timer);
				delete _timer;
				_quickLoad(); // call the onload handler
			}
		}, 10);
	}*/
	
	// For IE (conditional comments would have been better, but they break compression techniques)
	if (window.ActiveXObject && window.print && !window.opera) {
		document.write("<script id=__"+"ie_onload defer src=javascript:void(0)><\/script>");
		var _script = document.getElementById("__"+"ie_onload");
		_script.onreadystatechange = function() {
			if (this.readyState == "complete")
				_quickLoad();
		};
	}
	
	// return an object with public interface
	var _QuickLoader = {
		addListener: function($method, $priority) {
			var $type = (!$priority)?"_quickLoad":"_quickLoadPriority";
			_eventMgr.addEventListener($type, $method);
		},
		removeListener: function($method, $priority) {
			var $type = (!$priority)?"_quickLoad":"_quickLoadPriority";
			_eventMgr.removeEventListener($type, $method);
		}
	};
	return _QuickLoader;
})();