//////////////////////////////////////////////////////
// bitLib addCode ////////////////////////////////////
//////////////////////////////////////////////////////

/*
 * Import scripts in html always in this way:
 * 
 *  <!-- Import all jsBitLib objects -->
 * 	<script type="text/javascript" src="../../shared/jsBitLib/import/addCode.js" id="jsBitLibAddCode"></script>
 *	<script type="text/javascript">
 *		// Set here the jsBitLib resources for import into the document
 *		importManager.importScripts("@RESOURCE@");
 *	</script>
 * 
 * Fill the matching import resource into @RESOURCE@
 */

addCode = {
	
	//////////////////////////////////////////////
	//////// CONFIGURATION DEFAULT VALUES ////////
	//////////////////////////////////////////////
	
	bitLibName : "jsBitLib"
	,addCodeScriptId : "jsBitLibAddCode"
	,bitLibPath : "../../shared/jsBitLib"
	,isBusy : false
	
	////////////////////////////////////////////////////////////////////
	////////////// Public methods //////////////////////////////////////
	////////////////////////////////////////////////////////////////////
	
	/*
	 * Two modes in function toHeader:
	 * 
	 * content = "path", src = "relative path in jsBitLib", type = "element type" 
	 * 
	 * content = "code", src = "the code itself", type = "element type"
	 */
	,toHeader : function(content, src, type, id, readyTask){
		//if (this.isBusy == false) {
			this.isBusy = true;
			this.getBitLibPath();
			var scriptElement = document.createElement('script');
			//scriptElement.setAttribute("type", type);
			//scriptElement.setAttribute("id", id);
			scriptElement.type = type;
			//scriptElement.id = id;
			if (content == "path") {
				//scriptElement.setAttribute("src", String(this.bitLibPath + "/" + src));
				scriptElement.src = String(this.bitLibPath + "/" + src);
			}
			else 
				if (content == "code") {
					scriptElement.text = src;
				}
				else {
					alert("Error on addCode.toHeader(), important objects are missing!");
					return false;
				}
			// loaded event Firefox
			scriptElement.onload = function(){
				//this.isBusy = false;
				readyTask();
			}
			// loaded event Internet Explorer
			scriptElement.onreadystatechange = function(){
				if (scriptElement.readyState == 'complete') {
					//this.isBusy = false;
					readyTask();
				}
			}
			document.getElementsByTagName('head')[0].appendChild(scriptElement);
		//}
	}
	
	,getBitLibPath : function(){
		//get the source path of the imported addCode object and generate the path to jsBitLib
		var importedElement = document.getElementById(this.addCodeScriptId);
		var bitLibPathA = importedElement.src.split("/");
		for (var j in bitLibPathA) {
			var tempString = bitLibPathA.pop();
			if (tempString == this.bitLibName) {
				bitLibPathA[bitLibPathA.length] = tempString; 
				this.bitLibPath = String(bitLibPathA.join("/"));
				break;
			}
		}
	}
	
	,addImportRecourcesToHeader : function(content, resource){
		if (typeof resource !== "undefined") {
			var counter = 0;
			var resourcesAmount = resource.lenght();
			
			this.toHeader("path", resource[counter].src, resource[counter].type, nextResource);
			
			function nextResource(){
				counter++;
				if (counter <= resourcesAmount) {
					addNextResource();
				}else{
					counter = 0;
				}
			}
			
			function addNextResource(){
				this.toHeader("path", resource[counter].src, resource[counter].type, nextResource());
			}
			
			/*
			for (var j=0; j<=resource.length; j++) {
				if(this.isBusy == true){
					j--;
					continue;
				}
				this.toHeader(content, resource[j].src, resource[j].type);
			}
			 
			for (var j in resource) {
				this.toHeader(content, resource[j].src, resource[j].type);
			}
		}else{
			alert("[addCode] ERROR: importRecources are worng");
		}
		*/
	}
	
	,baseResource : [
		{src: "config/configParams.js",			type: "text/javascript"}			
		,{src: "import/importResources.js",		type: "text/javascript"}
		,{src: "import/importManager.js", 		type: "text/javascript"}
	]
	
	,ifLoadedDo : function(element, toDo){
		var loadedElement = document.getElementById(element);
		loadedElement.onload(toDo);
	}
	
	,headerReady : function(){
		var documentHeader = getElementsByTagName('header');
		documentHeader.onload()
	}
}

//////////////////////////////////////////////
//////// prepare importManager ///////////////
//////////////////////////////////////////////

// Be sure object is present
addCode = window.addCode || {};

addCode.addImportRecourcesToHeader("path", addCode.baseResource);