/*
	domready.js
	
	v1.01, 2007-09-09, Christian Augustin

*/
/*
	Simple window.onDomReady(init) solution that should be compatible
	with MooTools window.onDomReady(init).
	
	Changes
	2007-09-09, cma: Finaly using defered script method for MSIE ...
	2007-03-11, cma: Better fall-back strategy, declared 1.0!
	2007-02-28, cma: MSIE 5.0/Win bugfix to avoid crash on load.
	2007-02-27, cma: Harmonizing MSIE and Safari technique.
	2007-02-20, cma: Changed MSIE technique -- is it realy that simple?
	2007-02-06, cma: Initial version, borrowing from other sources.
	
	Technical Remarks:
	With MSIE, document.body.readyState is 'complete' before document.readyState,
	but WebKit/KHTML knows only document.readyState.
	With MSIE, document.readyState is "interactive" most of the time,
	and than changes to "complete" imediately;
	document.body.readyState is "loading" and at last "complete" -- but
	this isn't reliable.
	
	DOMReady.observer is defined by intention, because MSIE 5.0/Win crashes
	when using an anonymous function defined in DOMReady.interval.
	
*/

if (typeof window.onDomReady == 'undefined') {

	var DOMReady = {
		inits: [],
		append: function(init) {
			DOMReady.inits[DOMReady.inits.length] = init;
		},
		execute: function() {
			if (DOMReady.active) return;
			DOMReady.active = true;
			if (DOMReady.interval) window.clearInterval(DOMReady.interval);
			for (var i = 0, f = DOMReady.inits; i < f.length; i++) f[i]();
			DOMReady.complete = true;
		}
	};

	if (typeof document.addEventListener !='undefined') {
		// Gecko, Opera 9, Safari/KTHML fallback, and others ...
		document.addEventListener("DOMContentLoaded", DOMReady.execute, false);
		document.addEventListener("load", DOMReady.execute, false);
	} else if (typeof document.attachEvent != 'undefined') {
		// MSIE
		document.attachEvent("onload",  DOMReady.execute);
	} else {
		// As a last resort ...
		DOMReady.oldOnLoad = window.onload || function(){};
		window.onload = function() {
			DOMReady.oldOnLoad();
			DOMReady.execute();
		};
	};
	
	if (typeof document.readyState != 'undefined') {
		if (document.all && !window.opera && document.write) {
			// MSIE, document.write enabled ...
			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") {
					DOMReady.execute(); // call the onload handler
				}
			};
		} else {
		// WebKit, KHTML and browsers with similar functionality
			DOMReady.observer = function() {
				var d = document;
				if (/loaded|complete/.test((d.body&&d.body.readyState)||d.readyState)) {
					DOMReady.execute();
				};
			};
			DOMReady.interval = window.setInterval(DOMReady.observer, 10);
		};
	};
	
	window.onDomReady = DOMReady.append;

};


