var scriptsLoading = {};
var scriptsLoaded = {};
var scriptDependancies = [];
var scriptsLoadedInit = false;

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;
		}
	}

	return hasScript;
}

function StyleSheetAvailable( url )
{
	var hasScript = false;

	var scripts = document.getElementsByTagName('link');
	for( index = 0; index < scripts.length; index ++ )
	{
		if ( ( scripts[ index ].src == 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 )
{
	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 LoadScript( url )
{
	if ( IsScriptLoading( url ) )
	{
		return;
	}

	scriptsLoading[ url ] = true;

	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 RequireJavascriptLibrary( url )
{
	if ( !ScriptAvailable( url ) )
	{
		LoadScript( url );
	}
}

