var intellichatPingLoaded = false;
var intelliPingTimeout = false;
var intellichatPing = false;

function intellichatPingReturn(time)
{
	if(!intellichatPingLoaded)
	{
		intellichatPingLoaded = true;
		clearTimeout(intelliPingTimeout);
		intellichatPing.src = 'about:blank';
		if(time == 1)
		{
			var e = document.createElement("script");
			e.src = intellichatLaunchUrl;
			e.type = "text/javascript";
			document.getElementsByTagName("body")[0].appendChild(e);
		}
		else
		{
			//frames['intellichatping'].location.href = 'about:blank';
			function DisableIntelliChat(){};
			function DisableSASChat(){};
			function ForceDisableSASChat(){};
			function ForceDisableIntelliChat(){};
			function EnableIntelliChat(){};
			function EnableSASChat(){};
			var intellichat = {Version: '3.0.0',Debug: 'Failed to Connect to Intellichat'};
		}
	}
}

var intellipreload = {
	Browser: {
		IE:     !!(window.attachEvent && !window.opera),
		Opera:  !!window.opera,
		Firefox:  navigator.userAgent.indexOf('Firefox') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
		Safari:	navigator.userAgent.indexOf('Apple') > -1
	}
};

intellipreload.Ready = function(){

	if(!intellichatPing)
	{
		intellichatPing = document.createElement('div');
		intellichatPing.innerHTML = '<iframe id="intellichatping" src="https://www.intellichat.com/launch/checkload.php" width="0" height="0" style="display:none;" onload="intellichatPingReturn(1);"></iframe>';
		document.body.appendChild(intellichatPing);
		
		intelliPingTimeout = setTimeout("intellichatPingReturn(2);",intellichatTimeout);
	}
	
};

intellipreload.DomEnable = function()
{
	if(intellipreload.ReadyBind) return;
	intellipreload.ReadyBind = true;

	// Mozilla, Opera (see further below for it) and webkit nightlies currently support this event
	if ( document.addEventListener && !intellipreload.Browser.Opera)
	document.addEventListener( "DOMContentLoaded", intellipreload.Ready, false );

	// If IE is used and is not in a frame
	// Continually check to see if the document is ready
	if ( intellipreload.Browser.IE && window == top ) (function(){
		if (intellipreload.DomLoaded) return;
		try {
			// If IE is used, use the trick by Diego Perini
			// http://javascript.nwbox.com/IEContentLoaded/
			document.documentElement.doScroll("left");
		} catch( error ) {
			setTimeout(arguments.callee, 50);
			return;
		}
		// and execute any waiting functions
		intellipreload.Ready();
	})();


	if ( intellipreload.Browser.opera )
	document.addEventListener( "DOMContentLoaded", function () {
		if (intellipreload.DomLoaded) return;
		for (var i = 0; i < document.styleSheets.length; i++)
		if (document.styleSheets[i].disabled) {
			setTimeout( arguments.callee, 0 );
			return;
		}
		// and execute any waiting functions
		intellipreload.Ready();
	}, false);


	if ( intellipreload.Browser.safari ) {
		var numStyles;
		(function(){
			if (intellipreload.DomLoaded) return;
			if ( document.readyState != "loaded" && document.readyState != "complete" ) {
				setTimeout( arguments.callee, 0 );
				return;
			}
			if ( numStyles === undefined )
			numStyles = intellichat("style, link[rel=stylesheet]").length;
			if ( document.styleSheets.length != numStyles ) {
				setTimeout( arguments.callee, 0 );
				return;
			}
			// and execute any waiting functions
			intellipreload.Ready();
		})();
	}

	// A fallback to window.onload, that will always work
	window.onload = function(){intellipreload.Ready();};

};

intellipreload.DomEnable();
