/*******************************************************************************************
 * addLoadEvent
 * Originally written by Simon Willison (http://simonwillison.net/2004/May/26/addLoadEvent/)
 * Incorporated with code by Dean Edwards (http://dean.edwards.name/weblog/2006/06/again)
 * Takes a function as an argument which should be executed once the DOM has loaded
 * Parameters: function func
 * Example:
 *		addLoadEvent(myFunction);
 *		addLoadEvent(function() {
 *			alert ('a');
 *		});
 *******************************************************************************************/

	//--------------------------------------------------
	// Loading setup

		var addLoadEventStack = []; // Array
		var addLoadEventCount = 0;

		function addLoadEvent(func) {
			addLoadEventStack[addLoadEventCount++] = func;
		}

	//--------------------------------------------------
	// Execute the functions when the page has loaded

		var addLoadEventDone = false;
		var addLoadEventSafariTimer;

		function addLoadEventInit() {

			//--------------------------------------------------
			// Do not run the functions twice

				if (addLoadEventDone) {
					return;
				} else {
					addLoadEventDone = true;
				}

			//--------------------------------------------------
			// Remove the loading timer for Safari

				if (addLoadEventSafariTimer) {
					clearInterval(addLoadEventSafariTimer);
				}

			//--------------------------------------------------
			// Execute the functions - cannot use variable 'i'
			// as the executed functions scope could change it

				var addLoadEventProgress;
				for (addLoadEventProgress = 0; addLoadEventProgress < addLoadEventCount; addLoadEventProgress++) {
					addLoadEventStack[addLoadEventProgress]();
				}

		}

	//--------------------------------------------------
	// Triggers for the different browsers

		//--------------------------------------------------
		// For DOM compatible browsers - Firefox and Opera

		 	if (document.addEventListener) {
		 		document.addEventListener('DOMContentLoaded', addLoadEventInit, false);
		 	}

		//--------------------------------------------------
		// For Safari

			if (/WebKit/i.test(navigator.userAgent)) { // sniff
				addLoadEventSafariTimer = setInterval(function() {
					if (/loaded|complete/.test(document.readyState)) {
						addLoadEventInit(); // call the onload handler
					}
				}, 10);
			}

		//--------------------------------------------------
		// For Internet Explorer

			/*@cc_on @*/
			/*@if (@_win32)

				document.write('<script id="addLoadEventIeOnload" defer="defer" src=//:><\/script>');
				var script = document.getElementById("addLoadEventIeOnload");
				script.onreadystatechange = function() {
					if (this.readyState === "complete") {
						addLoadEventInit(); // call the onload handler
					}
				};

			@end @*/

		//--------------------------------------------------
		// Fall back for other browsers

			window.onload = addLoadEventInit;

