pcwa = { core: {} };

pcwa.core.scope = function scope(sc, par){
	if ( typeof sc == 'string' && sc.length > 0 ){
		var obj = null;
		var id = sc.split('.', 1)[0];

		par = par == null ? '' : par + '.';
		try{
			eval('obj=' + par + id );
			if ( typeof obj == 'undefined' ){
				eval(par + id + '={}');
			}
		}catch(e){
			eval(par + id + '={}');
		}
		scope(sc.slice(id.length + 1), par + id);
	}
};


pcwa.core.ScriptLoader = function ScriptLoader(config)
{
	this.fileList = config.fileList || [];
	this.currLoad = 0;
	this.config = config;	
	
	if ( !config ){ config = {};}
	if ( !config.onLoadComplete ){config.onLoadComplete = null;}
	if ( !config.scope ){config.scope = window;}
};
pcwa.core.ScriptLoader.prototype = {
	processLoadResponse: function processLoadResponse(){
		if ( --this.currLoad == 0 ){
			if ( this.config.onLoadComplete && typeof this.config.onLoadComplete == 'function'){
				this.config.onLoadComplete.call(this.config.scope);
			}
		}
	},
	processIELoadResponse: function processIELoadResponse(){
		if ( window.event.srcElement.readyState == 'loaded' ){
			this.processLoadResponse();
		}
	},
	setFile: function setFile(file){
		this.fileList[this.fileList.length] = file;
	},
	load_js: function load_js(){
		var script;
		for ( i = 0; i < this.fileList.length; i++ ){
			this.currLoad++;
			script = document.createElement('script');
			script.type = 'text/javascript';
			script.src = this.fileList[i];
			// TODO: This is a hack that creates an unwanted dependency.
			// Needs to be changed to cross browser check.
			if ( Ext.isIE ){
				script.onreadystatechange = this.processIELoadResponse.createDelegate(this);
			}else{
				script.onload = script.onerror = this.processLoadResponse.createDelegate(this);
			}
			document.getElementsByTagName('head').item(0).appendChild(script);
		}
	}
};

