var tableFileCSS = new Array();
var tableFileCSSId = new Array();
var tableFileJS = new Array();
var tableFileJSReady = new Array();

tableFileCSS[0] = cssRootpath+'jquery-ui-1.8.11.custom.css';
tableFileCSS[1] = cssRootpath+'validationEngine.jquery.css';
tableFileCSS[2] = cssRootpath+'header_v2.css';
tableFileCSS[3] = cssRootpath+'button_v2.css';
tableFileCSS[4] = cssRootpath+'home_2_v2.css';
tableFileCSS[5] = cssRootpath+'popUp_v2.css';
tableFileCSS[6] = cssRootpath+'home_1_v2.css';
tableFileCSS[7] = cssRootpath+'faq.css';
tableFileCSS[8] = cssRootpath+'cgu.css';
tableFileCSS[9] = cssRootpath+'generalClasses.css';	
tableFileCSS[10] = cssRootpath+'edit-user-profile.css';	
tableFileCSS[11] = cssRootpath+'edjingConnect.css';
tableFileCSS[12] = cssRootpath+'jquery.fileupload-ui.css';

tableFileCSSId[0] = 'css1';
tableFileCSSId[1] = 'css2';
tableFileCSSId[2] = 'css3';
tableFileCSSId[3] = 'css4';
tableFileCSSId[4] = 'css5';
tableFileCSSId[5] = 'css6';
tableFileCSSId[6] = 'css7';
tableFileCSSId[7] = 'css8';
tableFileCSSId[8] = 'css9';
tableFileCSSId[9] = 'css10';
tableFileCSSId[10] = 'css11';
tableFileCSSId[11] = 'css12';
tableFileCSSId[12] = 'css13';

tableFileJS[0] = jsRootpath+'header_v2.js';
/*tableFileJS[1] = jsRootpath+'js_v2.js';*/
tableFileJS[1] = jsRootpath+'popup.js';
tableFileJS[2] = jsRootpath+'formSubmit.js';
tableFileJS[3] = jsRootpath+'facebook.js';
tableFileJS[4] = jsRootpath+'fbconnect.js';
/*tableFileJS[5] = jsRootpath+'header.js';*/
/*tableFileJS[7] = jsRootpath+'jquery/jquery-1.5.1.min.js';
tableFileJS[8] = jsRootpath+'jquery/jquery-ui-1.8.11.custom.min.js';*/
tableFileJS[5] = jsRootpath+'SWFObject.js';	
/*tableFileJS[6] = jsRootpath+'menu-v2.js';*/	
/*tableFileJS[11] = jsRootpath+'jquery/jquery-1.5.1.js';	*/
tableFileJS[6] = jsRootpath+'jquery/jquery.ui.core.js';	
tableFileJS[7] = jsRootpath+'jquery/jquery.ui.widget.js';
tableFileJS[8] = jsRootpath+'jquery/jquery.ui.mouse.js';
tableFileJS[9] = jsRootpath+'jquery/jquery.ui.draggable.js';
tableFileJS[10] = jsRootpath+'jquery/jquery.ui.droppable.js';
tableFileJS[11] = jsRootpath+'jquery/jquery.ui.sortable.js';
tableFileJS[12] = jsRootpath+'jquery/jquery.ui.accordion.js';
tableFileJS[13] = jsRootpath+'jquery/jquery.ui.position.js';
tableFileJS[14] = jsRootpath+'jquery/jquery.ui.resizable.js';
tableFileJS[15] = jsRootpath+'jquery/jquery.ui.dialog.js';
tableFileJS[16] = jsRootpath+'jquery/jquery.tinyscrollbar.js';
tableFileJS[17] = jsRootpath+'objet3.js';
tableFileJS[18] = jsRootpath+'edit-user-profile.js';
tableFileJS[19] = jsRootpath+'jquery.ajax.upload.js';
tableFileJS[20] = jsRootpath+'stats.js';
tableFileJS[21] = jsRootpath+'edjingConnect.js';
tableFileJS[22] = jsRootpath+'jquery-import/importItunes.js';
tableFileJS[23] = jsRootpath+'jquery/jquery.tmpl.min.js';
tableFileJS[24] = jsRootpath+'json2.js';

var countTime = 0;
var countTime2 = 0;

for(var i=0; i<tableFileJS.length;i++){
	tableFileJSReady[i] = false;
}


$(document).ready(function(){
	/*initPopup(urlInscription);
	startIdArgument(playlistIdArgument,musicIdArgument);*/
	
	for(var i=0;i<tableFileCSS.length;i++){
		var text = '<div class="test'+tableFileCSSId[i]+'"></div>';
		$("#testChargementCSS").append(text);
		includeCSS(tableFileCSS[i],tableFileCSSId[i]);
	}
	
	startCheckCSSReadyInterval = setInterval(function(){
		if(checkCSSReady()){
			clearInterval(startCheckCSSReadyInterval);
			$("#testChargementCSS").remove();
			for(var i=0;i<tableFileJS.length;i++){
				includeJS(tableFileJS[i]);
			}
			countTime = 0;
			startCheckJavascriptReadyInterval = setInterval(function(){
				if(checkJavascriptReady()){
					clearInterval(startCheckJavascriptReadyInterval);
					/* start on ready function for each javascript file */
					readyHeader();
					readyObjet();
				}
				if(countTime>150){
					clearInterval(startCheckJavascriptReadyInterval);
					$("#popup-loading-error-central").fadeIn("slow")
					.click(function(event){
						window.location.reload();
					});
					countTime = 0;
					return false;
				}
				else{
					countTime++;
				}
			},250);
		}
		if(countTime>60){
			if(countTime2<0){
				reloadCSS();
				countTime = 0;
				countTime2++;
			}
			else{
				clearInterval(startCheckCSSReadyInterval);
				$("#popup-loading-error-central").fadeIn("slow")
				.click(function(event){
					window.location.reload();
				});
				countTime = 0;
				countTime2 = 0;
				return false;
			}
		}
		else{
			countTime++;
		}
	},250);
	
});

function javascriptFileReady(fileName){
	for(var i=0; i<tableFileJS.length;i++){
		if(tableFileJS[i] == fileName){
			tableFileJSReady[i] = true;
			return true;
		}
	}
	return false;
}

function checkJavascriptReady(){
	for(var i=0; i<tableFileJSReady.length;i++){
		if(tableFileJSReady[i] == false){
			return false;
		}
	}
	return true;
}

function checkCSSReady(){
	for(var i=0;i<tableFileCSS.length;i++){
		if($("#testChargementCSS .test"+tableFileCSSId[i]).width() < 95 || $("#testChargementCSS .test"+tableFileCSSId[i]).width() > 105){
			return false;
		}
	}
	return true;
}

function reloadCSS(){
	for(var i=0;i<tableFileCSS.length;i++){
		var text = '<div class="test'+tableFileCSSId[i]+'"></div>';
		if($("#testChargementCSS .test"+tableFileCSSId[i]).width()!=100){
			$("#cssStyleSheet"+tableFileCSSId[i]).remove();
			includeCSS(tableFileCSS[i],tableFileCSSId[i]);
		}
	}
}

function includeJS(file) 
{ 
	var script = document.createElement('script'); 
	script.setAttribute('type','text/javascript'); 
	script.setAttribute('src',file); 
	document.getElementsByTagName('head')[0].appendChild(script); 
}

function includeCSS(file,id) 
{ 
	var link = document.createElement('link'); 
	link.setAttribute('id','cssStyleSheet'+id); 
	link.setAttribute('rel','stylesheet'); 
	link.setAttribute('type','text/css'); 
	link.setAttribute('href',file); 
	document.getElementsByTagName('head')[0].appendChild(link); 
}
