/**
* Provides interface for binding functions to an event of 'DOMContentLoaded'
*  which fires as soon as the DOM has been parsed, (possibly before images/etcetera 
*  have been fully downloaded) This results in much faster js based 
*  presentation/decoration (e.g. eliminates the 'flicker' effect) 
* @uses mootools.js
* @author plawley
* Based on works by Tanny O'Haley, http://tanny.ica.com; Dean Edwards, http://dean.edwards.name/weblog/2005/10/add-event/
*
**/

// Array of DOMContentLoaded event handlers.
window.onDOMLoadEvents = new Array();
window.DOMContentLoadedInitDone = false;

/** addDOMLoadEvent(listener)
 Adds DOMContentLoaded listeners to the array.
**/
function addDOMLoadEvent(listener) 
{
	// If the DOMContentLoaded event has happened, run the function.
	if(window.DOMContentLoadedInitDone)
	{
		listener();
		return;
	}
	window.onDOMLoadEvents.push(listener);
}


// Function to process the DOMContentLoaded events array.
function DOMContentLoadedInit() 
{
	// quit if this function has already been called
	if (window.DOMContentLoadedInitDone) return;

	// flag this function so we don't do the same thing twice
	window.DOMContentLoadedInitDone = true;

	// iterates through array of registered functions 
	for (var i=0; i<window.onDOMLoadEvents.length; i++) 
	{
		window.onDOMLoadEvents[i]();
	}
}

function DOMContentLoadedScheduler() 
{
	// quit if the init function has already been called
	if (window.DOMContentLoadedInitDone) return true;
	
	// First, check for Safari or KHTML.
	// Second, check for IE.
	//if DOM methods are supported, and the body element exists
	//(using a double-check including document.body, for the benefit of older moz builds [eg ns7.1] 
	//in which getElementsByTagName('body')[0] is undefined, unless this script is in the body section)
	if(/KHTML|WebKit/i.test(navigator.userAgent)) {
		if(/loaded|complete/.test(document.readyState)) {
			DOMContentLoadedInit();
		} else {
			// Not ready yet, wait a little more.
			setTimeout("DOMContentLoadedScheduler()", 250);
		}
	} else if(document.getElementById("__ie_onload")) {
		return true;
	}

	return true;
}


// Schedule to run the init function.
setTimeout("DOMContentLoadedScheduler()", 250);

// Just in case window.onload happens first, add it there too.
window.addEvent( 'load', DOMContentLoadedInit);

// If addEventListener supports the DOMContentLoaded event.
if(document.addEventListener)
	document.addEventListener("DOMContentLoaded", DOMContentLoadedInit, false);

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	document.write("<script id=\"__ie_onload\" defer=\"defer\" src=\"//:\"><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			DOMContentLoadedInit(); // call the onload handler
		}
	};
/*@end @*/

