﻿
ScriptLoader = function() {
	this.timeout = 30;
	this.scripts = [];
	this.disableCaching = false;
};

ScriptLoader.prototype = {

	load: function(url) {
		$.ajax({
			type: "GET",
			url: url,
			dataType: "text",
			async: false,
			success:function(){
				var head = document.getElementsByTagName('HEAD').item(0);
				var script = document.createElement('script');
					script.src = url;
					script.type = 'text/javascript';
					head.appendChild(script);			
			},
			failure: function(){},
			timeout:(this.timeout * 1000),
			cache: this.disableCaching
		});
	},

	loadCss :function(url){

		$.ajax({
			type: "GET",
			url: url,
			dataType: "text",
			async: false,
			success:function(response){

				var head = document.getElementsByTagName('HEAD').item(0);  
				var style = document.createElement('link');  
					style.href = url;  
					style.rel = 'stylesheet' 
					style.type = 'text/css';  
					head.appendChild(style); 
/*
				debugger;
				var style = document.createElement('STYLE');     
					style.setAttribute("type", "text/css");     
					if (style.styleSheet){         
						style.styleSheet.cssText=response;      
					}else {          
						style.appendChild(document.createTextNode(response));      
					}
				document.getElementsByTagName("head")[0].appendChild(style); */
			},
			failure: function(){},
			timeout:(this.timeout * 1000),
			cache: this.disableCaching
		});		
	}

};

ScriptLoaderMgr = function() {
	this.loader = new ScriptLoader();
	this.load = function(o) {
		if (!$.isArray(o.scripts)) {
			o.scripts = [o.scripts];
		}

		if (!$.isArray(o.csses)) {
			o.csses = [o.csses];
		}
		
		while(o.scripts.length>0){						
			this.loader.load(o.scripts.shift());
		}                

		while(o.csses.length>0){						
			this.loader.loadCss(o.csses.shift());
		}                

	};
};
