var myDefinedLibraries = [
	{name:'EXTJSBASE', url:'/imageserver/RemoteGadgets/PublisherEditor/js/extjs3/adapter/ext/ext-base.js'},
	{name:'EXTJSALL', url: '/imageserver/RemoteGadgets/PublisherEditor/js/extjs3/ext-all.js'},
	{name:'FCKEDITOR', url: '/imageserver/RemoteGadgets/PublisherEditor/js/ckeditor/ckeditor.js'},
	{name:'PTUTILFIX', url: '/imageserver/RemoteGadgets/PublisherEditor/js/PTUtil-fix.js'},
	{name:'EXTFILEUPLOAD', url: '/imageserver/RemoteGadgets/PublisherEditor/js/FileUploadField.js'}
];

var myDefinedStyles = [
	{name:'EXTCSS', url:'/imageserver/RemoteGadgets/PublisherEditor/js/extjs3/resources/css/ext-all.css'},
	{name:'PUB_EDITOR', url:'/imageserver/RemoteGadgets/PublisherEditor/css/styles.css'},
];

// dyamically loads the JS library into the DOM
dynJSLoad = function(js_key, js_file) {
	if (document.getElementById && !document.getElementById(js_key)) {
		var fileref=document.createElement('script');
		fileref.setAttribute("type","text/javascript");
		fileref.setAttribute("src", js_file);
		fileref.setAttribute("id", js_key);
		document.getElementsByTagName("head")[0].appendChild(fileref);
	}
}

// dyamically loads the CSS into the DOM
dynCSSLoad = function(css_key, css_file) {
	if (document.getElementById && !document.getElementById(css_key)) {
		var css = document.createElement('link');
		css.setAttribute('rel', 'stylesheet');
		css.setAttribute('type', 'text/css');
		css.setAttribute('href', css_file);
		css.setAttribute("id", css_key);
		document.getElementsByTagName('head')[0].appendChild(css);
	}
}

// uses a name key to load the requested JS once and only once
loadJSLibraryByKey = function(key, defer) {
	for (var i=0; i<myDefinedLibraries.length; i++) {
		if (myDefinedLibraries[i].name == key) {
			if (defer) {
				document.PCC.RegisterForWindowEvent('onload', "new Function(window.setTimeout(\'dynJSLoad(\""+key+"\",\""+myDefinedLibraries[i].url+"\")\',5))");
			}
			else {
				dynJSLoad(key, myDefinedLibraries[i].url);
			}
			return;
		}
	}
}

// uses a name key to load the requested CSS once and only once
loadCSSByKey = function(key) {
	for (var i=0; i<myDefinedStyles.length; i++) {
		if (myDefinedStyles[i].name == key) {
			dynCSSLoad(key, myDefinedStyles[i].url);
			return;
		}
	}
}

// uses a name key to load the requested CSS once and only once
unloadCSSByKey = function(key) {
	if (document.getElementById && document.getElementById(key)) {
		try {
			document.getElementById(key).parent.removeChild(document.getElementById(key));	
		}
		catch (err) {
			try {
				document.getElementById(key).parentElement.removeChild(document.getElementById(key));	
			}
			catch (err) {}
		}
	}
	return;
}




iScriptLoader = (function()
{
	var pendingLoad = [];
	var notLoaded = [];
	var callback = [];

	return {
		/// The list of loaded scripts in their loading order.
		loadedScripts : [],

		loadPending : function()
		{
			var scriptName = pendingLoad.shift();

			if ( !scriptName )
				return;

			var scriptSrc="";
			for (var i=0; i<myDefinedLibraries.length; i++) {
				if (myDefinedLibraries[i].name == scriptName)
					scriptSrc = myDefinedLibraries[i].url;
			}

			// if no mapping was found, assume the name passed in was the actual
			// src url
			if (scriptSrc == "")
				scriptSrc=scriptName;


			var script = document.createElement( 'script' );
			script.type = 'text/javascript';
			script.src = scriptSrc;

			function onScriptLoaded(scriptName)
			{
				window.status="Loaded Library [" + scriptName+ "]";
				notLoaded.splice(notLoaded.indexOf(scriptName), 1);
				if (notLoaded.length==0) {
					window.status="Libraries Loaded";
					iScriptLoader.runCallbacks();
				}

				// Append this script to the list of loaded scripts.
				iScriptLoader.loadedScripts.push( scriptName );

				// Load the next.
				iScriptLoader.loadPending();
			}

			// We must guarantee the execution order of the scripts, so we
			// need to load them one by one.
			if (navigator.appName == "Microsoft Internet Explorer")
			{
				script.onreadystatechange = function()
				{
					if ( script.readyState == 'loaded' || script.readyState == 'complete' )
					{
						script.onreadystatechange = null;
						onScriptLoaded( scriptName );
					}
				};
			}
			else
			{
				script.onload = function()
				{
					// Some browsers, such as Safari, may call the onLoad function
					// immediately. Which will break the loading sequence. (#3661)
					setTimeout( function() { onScriptLoaded( scriptName ); }, 0 );
				};
			}

			document.body.appendChild( script );
		},

		/**
		 * Loads a specific script, including its dependencies. This is not a
		 * synchronous loading, which means that the code the be loaded will
		 * not necessarily be available after this call.
		 */
		load : function( scriptName, defer )
		{
			// Check if the script has already been loaded.
			if ( scriptName in this.loadedScripts ) {
				// if we're not deferring, see if all scripts are loaded
				if (!defer) {
					if (notLoaded.length==0) { // run callbacks
						window.status="Libraries Loaded";
						iScriptLoader.runCallbacks();
					}
					else { // load pending scripts
						window.status="Loading Libraries";
						this.loadPending();
					}
				}

				return;
			}

			// Mark the script as loaded, even before really loading it, to
			// avoid cross references recursion.
			this.loadedScripts[ scriptName ] = true;

			var scriptSrc="";
			for (var i=0; i<myDefinedLibraries.length; i++) {
				if (myDefinedLibraries[i].name == scriptName)
					scriptSrc = myDefinedLibraries[i].url;
			}

			// if no mapping was found, assume the name passed in was the actual
			// src url
			if (scriptSrc == "")
				scriptSrc=scriptName;

			// Append the <script> element to the DOM.
			window.status="Loading Libraries [" + scriptSrc + "]";
			if ( document.body )
			{
				pendingLoad.push( scriptName );
				notLoaded.push( scriptName );

				if ( !defer )
					this.loadPending();
			}
			else
			{
				// Append this script to the list of loaded scripts.
				this.loadedScripts.push( scriptName );

				document.write( '<script src="' + scriptSrc + '" type="text/javascript"><\/script>' );
			}
		},

		setCallBack: function(callbackName) {
			callback.push(callbackName);
		},
		
		runCallbacks: function() {
			var fn = callback.shift();
			while (fn) {
				fn();
				fn = callback.shift();
			}
		}
	};
})();
