﻿/**
 *  (JavaScript Function)
 *
 *  OnDOMLoad (Community Version)
 *
 *  June 2007
 *
 *  A function which detects when all of 
 *  the HTML in a document has completed loading (before pictures), 
 *  and fires the user supplied "eventHandler" function.
 *
 *  Credit for all of the work should go to Dean Edwards
 *  <http://dean.edwards.name/weblog/2006/06/again/> and
 *  the credits he gives there.
 * 
 *  @param (Function) eventHandler
 *      A 'Function' reference to the function you want
 *      to be called when the DOM is fully loaded.
 *
 *  @param (Window) targetWindow (Optional)
 *      A DOM Window object reference, if you want to 
 *      target a window other than the current window
 *
 *  @return (void)
 *  	No return value.
 */

function OnDOMLoad (
	eventHandler /*: Function*/,
	targetWindow /*: Window*/
) /*: void*/
{
	var domLoaded /*: Boolean*/ = false;
	var isIE /*: Boolean*/ = false;
	var isMozillaOrOpera9 /*: Boolean*/ = false;
	var isKHTML /*: Boolean*/ = false;
	var isOther /*: Boolean*/ = false;
	var interval /*: HostObject*/ = null;
	var targetDocument /*: Document*/ = null;

	if (typeof eventHandler != "function")
	{
		return;
	}

	if (typeof targetWindow == "undefined")
	{
		targetWindow = window;
		targetDocument = window.document;
	}
	else
	{
		targetDocument = targetWindow.document;
	}

	OnDOMLoad_BrowserSniff();

	if (isMozillaOrOpera9) 
	{
		targetDocument.addEventListener("DOMContentLoaded", OnDOMLoad_Loaded, null);
	}
	else if (isIE)
	{
		var src /*: String*/ = "src='javascript:void(0)'";

		if (targetDocument.location.protocol == "https:") 
		{
			src = "src=//0";
		}

		targetDocument.write("<script id='DOMLoadScript' defer " + src + "><\/script>");

		var scriptElement /*: HTMLScriptElement*/ = targetDocument.getElementById("DOMLoadScript");

		scriptElement.onreadystatechange = function() 
		{
			if (scriptElement.readyState == "complete") 
			{
				/* Possible memory leakage?*/
				scriptElement.onreadystatechange = null;
				scriptElement.removeNode(true);

				OnDOMLoad_Loaded();
			}    
		};
	}
	else if (isKHTML) 
	{ 
		interval = targetWindow.setInterval(function() 
		{        
			if (/loaded|complete/.test(targetDocument.readyState)) 
			{ 
				OnDOMLoad_Loaded(); 
			}    
		}, 10);

		return;
	}
	/* for other browsers - no solution*/
	else
	{
		targetWindow.onload = OnDOMLoad_Loaded;	
	}

	function OnDOMLoad_BrowserSniff()
	{
		/*@cc_on @*/
		/*@if (@_jscript_version >= 0)
	
			isIE = true;
			return;
	
		@end @*/

		if (/KHTML|WebKit/i.test(navigator.userAgent))
		{
			isKHTML = true;
		}
		else if (targetDocument.addEventListener)
		{
			isMozillaOrOpera9 = true;
		}
		else
		{
			isOther = true;
		}
	}

	function OnDOMLoad_Loaded()
	{
		if (domLoaded)
		{
			return;
		}

		domLoaded = true;

		targetWindow.clearInterval(interval);
		interval = null;    

		/* Success!*/
		eventHandler();
	}
}
