/* called from build.js to load all js files */

var fileNumber = 0;
var loaderPercent = 17;
$(document).ready(function() {
	//$(".loader_percent").html(loaderPercent + "% document ready");
	setEngineGlobalMSIE();
	writeLoaderInfo("%"+loaderPercent)
	if(in_preview_mode == false){
	initFileArray = new Array(
		['js/config.js','true']
	)
	}else{
	initFileArray = new Array(
		['assets/js/preview_config.js','true']	
		)	
	}
	preLoadFile(initFileArray,fileNumber,'init');	
});

function setEngineGlobalMSIE(){
	engine = null;
	if (window.navigator.appName == "Microsoft Internet Explorer")
	{
	   // This is an IE browser. What mode is the engine in?
	   if (document.documentMode) // IE8 or later
		  engine = document.documentMode;
	   else // IE 5-7
	   {
		  engine = 5; // Assume quirks mode unless proven otherwise
		  if (document.compatMode)
		  {
			 if (document.compatMode == "CSS1Compat")
				engine = 7; // standards mode
		  }
		  // There is no test for IE6 standards mode because that mode  
		  // was replaced by IE7 standards mode; there is no emulation.
	   }
	   // the engine variable now contains the document compatibility mode.
	}	
}

function getEngineGlobalMSIE(){
	engine = null;
	if (window.navigator.appName == "Microsoft Internet Explorer")
	{
	   // This is an IE browser. What mode is the engine in?
	   if (document.documentMode) // IE8 or later
		  engine = document.documentMode;
	   else // IE 5-7
	   {
		  engine = 5; // Assume quirks mode unless proven otherwise
		  if (document.compatMode)
		  {
			 if (document.compatMode == "CSS1Compat")
				engine = 7; // standards mode
		  }
		  // There is no test for IE6 standards mode because that mode  
		  // was replaced by IE7 standards mode; there is no emulation.
	   }
	   // the engine variable now contains the document compatibility mode.
	}
	return engine;	
}

function writeLoaderInfo(string){
	$(".loader_percent").html(string);
}

//writeViewPortMeta()

/* 
   Added condition to preLoadFile function to check when the file has loaded as 
   IE 8 uses onreadystatechange and IE 9 uses onreadystatechange and onload
   Also IE use readyState == loaded as well as == complete
   so we check for either state
*/

function preLoadFile(fileArray,fileNumber,type)
{ 
	//$(".loader_percent").html(loaderPercent + "% " + fileArray[fileNumber][0]);
	writeLoaderInfo("%"+loaderPercent)
	if(eval(fileArray[fileNumber][1] +'== true'))
	{
		var script = document.createElement('script');  
		script.setAttribute("id","dynamicScript");
		script.setAttribute("type","text/javascript");
		script.setAttribute("src",fileArray[fileNumber][0]);
		// .onload is not firing in IE. Use .onreadystatechange instead.
		//alert("navigator.appName = " + navigator.appName);
		if(navigator.appName == 'Microsoft Internet Explorer')
		{
		    //alert("ie");
		    if(navigator.appVersion.indexOf("MSIE 9")!=-1 && (engine == 9))
		    {
		        //alert("IE 9");
		        script.onload = function() 
		        { 		       
		            //alert("script.readyState = " + script.readyState);
	                if(script.readyState == 'complete' || script.readyState == 'loaded') 
			        {
				        fileLoaded(fileArray,fileNumber,type); 
			        } 
		        }
		    }
		    else
		    {
		        //alert("not IE 9");
		        script.onreadystatechange = function () 
		        {
		            //alert("script.readyState = " + script.readyState);
			        if(script.readyState == 'complete' || script.readyState == 'loaded') 
			        {
				        fileLoaded(fileArray,fileNumber,type); 
			        }
		        }
		    }
		}
		else
		{
		    script.onload = function() 
		    { 		       
	            //alert("fileArray = " + fileArray);
		        fileLoaded(fileArray,fileNumber,type); 
		    }
		}
		//alert("script = " + fileArray[fileNumber][0]);
		document.getElementsByTagName("head")[0].appendChild(script)
	} 
	else 
	{
		fileLoaded(fileArray,fileNumber,type); 
	}
}

function fileLoaded(fileArray,fileNumber,type)
{
	fileNumber++;
	loaderPercent++;
	//alert("fileNumber = " + fileNumber);
	if(fileNumber<fileArray.length)
	{
		preLoadFile(fileArray,fileNumber,type);
	} 
	else 
	{
		fileNumber = 0;
		if(type == 'default')
		{
			//alert('All Default files Loaded');
			if(requiredFileArray.length > 0)
			{
				preLoadFile(requiredFileArray,fileNumber,'required');
			}
		} 
		else if(type == 'required')
		{
			//alert('All Required files Loaded');
			loadedInit(); /* changed name to stop conflict with tracking data */
		} 
		else if(type == 'init')
		{
			//alert('config file loaded');
			preLoadFile(defaultFileArray,fileNumber,'default');
		}
	}
}

var css;
function include_css(css_file) {
    var html_doc = document.getElementsByTagName('head')[0];
    css = document.createElement('link');
    css.setAttribute('rel', 'stylesheet');
    css.setAttribute('type', 'text/css');
    css.setAttribute('href', css_file);
    html_doc.appendChild(css);
    // alert state change
    css.onreadystatechange = function () {
        if (css.readyState == 'complete') {
            //alert('CSS onreadystatechange fired');
        }
    }
    css.onload = function () {
        //alert('CSS onload fired');
    }
    return false;
}

/* changed name to stop conflict with tracking data */
function loadedInit(){
	loaderPercent = loaderPercent + 5;
	//$(".loader_percent").html(loaderPercent + "% init");
	writeLoaderInfo(loaderPercent+"%")
	setUpScreens();
}

function writeViewPortMeta(){
	wi = screen.width;
	if (wi>320){
		document.write('<meta name="viewport" content="initial-scale = 1.0,  minimum-scale=1.0,  maximum-scale=1.0" >')
	}else{
		document.write('<meta name="viewport" content="width=320" >')
	}
}