var scriptsLoading = {};
var scriptsLoaded = {};
var scriptDependancies = [];
var scriptsLoadedInit = false;

function MakeSafeJquerySelector( idString )
{
	return idString.replace(/[#;&,\.\+\*~':"!\^\$\[\]\(\)=>|\/\\]/g, '\\$&');
}

function InitialiseInitialScripts()
{
	if ( scriptsLoadedInit )
	{
		return;
	}

	scriptsLoadedInit = true;

	var scripts = document.getElementsByTagName('script');

	for( index = 0; index < scripts.length; index ++ )
	{
		var url = scripts[ index ].src;

		if ( scripts[ index ].src.indexOf( "http" ) != -1 )
		{
			url = url.replace( window.location.protocol + '//' + window.location.host, "" );
		}

		scriptsLoaded[ url ] = true;
	}
}

function ScriptAvailable( url )
{
	var hasScript = false;

	var scripts = document.getElementsByTagName('script');
	for( index = 0; index < scripts.length; index ++ )
	{
		if ( ( scripts[ index ].src == window.location.protocol + '//' + window.location.host + url ) || ( scripts[ index ].src == url ) )
		{
			hasScript = true;
		}
	}

	if ( IsScriptLoaded( url ) )
	{
		return true;
	}

	return hasScript;
}

function StyleSheetAvailable( url )
{
	var hasScript = false;

	var scripts = document.getElementsByTagName('link');

	for( index = 0; index < scripts.length; index ++ )
	{
		if ( url.match( /jquery-ui\.css/ ) )
		{
			if ( scripts[ index ].href && scripts[ index ].href.match( /jquery-ui\.css/ ) )
			{
				hasScript = true;
			}
		}

		if ( ( scripts[ index ].href == window.location.protocol + '//' + window.location.host + url ) || ( scripts[ index ].src == url ) )
		{
			hasScript = true;
		}
	}

	return hasScript;
}

function IsScriptLoading( url )
{
	if ( typeof scriptsLoading[ url ] != "undefined" )
	{
		return true;
	}

	return false;
}

function IsScriptLoaded( url )
{
	if ( typeof scriptsLoaded[ url ] != "undefined" )
	{
		return true;
	}

	return false;
}

function GetScriptObject( url )
{
	var scripts = document.getElementsByTagName('script');

	for( index = 0; index < scripts.length; index ++ )
	{
		if ( ( scripts[ index ].src == window.location.protocol + '//' + window.location.host + url ) || ( scripts[ index ].src == url ) )
		{
			return scripts[ index ];
		}
	}
}

function RequireStyleSheet( url, media )
{
	if ( !StyleSheetAvailable( url ) )
	{
		var head = document.getElementsByTagName('head')[0];
	    var sheet = document.createElement('link');

	    sheet.type = 'text/css';
	    sheet.href = url;
	    sheet.rel = 'stylesheet';

	    head.appendChild(sheet);
	}
}

function CreateScriptDependance( urls, onLoadFunc )
{
	InitialiseInitialScripts();

	var dependancy = {
		monitor: urls,
		func: onLoadFunc,
		checked: false
	};

	scriptDependancies[ scriptDependancies.length ] = dependancy;

	MonitorDependancies();
}

var dependancyTimer;

function MonitorDependancies()
{
	clearTimeout( dependancyTimer );
	var allLoaded = true;

	for( var i = 0; i < scriptDependancies.length; i++ )
	{
		var dependancy = scriptDependancies[ i ];

		if ( dependancy.executed )
		{
			continue;
		}

		var loaded = true;

		for( var x = 0; x < dependancy.monitor.length; x++ )
		{
			var url = dependancy.monitor[ x ];

			if ( !ScriptAvailable( url ) )
			{
				LoadScript( url );
				loaded = false;
				break;
			}

			if ( !IsScriptLoaded( url ) )
			{
				loaded = false;
				break;
			}
		}

		if ( loaded )
		{
			dependancy.func();
			dependancy.executed = true;
		}
		else
		{
			allLoaded = false;
		}
	}

	if ( !allLoaded )
	{
		dependancyTimer = setTimeout( "MonitorDependancies()", 200 );
	}
}

function ScriptLoaded( url )
{
	scriptsLoaded[ url ] = true;
}

function JsManagerGetHttpRequest()
{
	if ( window.XMLHttpRequest ) // Gecko
	{
		return new XMLHttpRequest() ;
	}
	else if ( window.ActiveXObject ) // IE
	{
		return new ActiveXObject("MsXml2.XmlHttp") ;
	}
}


function LoadScript( url )
{
	if ( IsScriptLoading( url ) )
	{
		return;
	}

	scriptsLoading[ url ] = true;

	hostName = GetHostnameFromURL( url );

	if( hostName === false || hostName === document.location.host )
	{
		var xmlHttp = JsManagerGetHttpRequest() ;

		xmlHttp.onreadystatechange = function()
		{
			if ( xmlHttp.readyState == 4 )
			{
				if ( xmlHttp.status == 200 || xmlHttp.status == 304 )
				{
					JsManagerIncludeJS( url, xmlHttp.responseText );
				}
				else
				{
					alert( 'XML request error: ' + xmlHttp.statusText + ' (' + xmlHttp.status + ')' ) ;
				}
			}
		}

		xmlHttp.open( 'GET', url, true);
		xmlHttp.send(null);
	}
	else
	{
		var head = document.getElementsByTagName('head')[0];
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.src = url;

		var loadedEvent = function()
		{
		    scriptsLoaded[ url ] = true;
		    MonitorDependancies();
		}

		script.onload = loadedEvent;
		script.onreadystatechange = loadedEvent;
		head.appendChild( script );
	}
}

function JsManagerIncludeJS( url, source )
{
	if ( source != null )
	{
		var head = document.getElementsByTagName('HEAD').item(0);
		var script = document.createElement( "script" );
		script.language = "javascript";
		script.type = "text/javascript";
		script.defer = true;
		script.text = source;

		head.appendChild( script );

		scriptsLoaded[ url ] = true;
	    MonitorDependancies();
	}
}

function RequireJavascriptLibrary( url )
{
	if ( !ScriptAvailable( url ) )
	{
		LoadScript( url );
	}
}

function GetHostnameFromURL( url )
{
	var regex = new RegExp( '^[a-z]+\://([^/]+)', 'im' );
	if( regex.test( url ) )
	{
		return regex.exec( url )[1];
	}
	return false;
}
