var MassLoader  = function(param_callback){
	
	this.callback = param_callback;
	this.imageExtensions = ['jpg' , 'jpeg' , 'bmp' , 'gif' , 'png'];
	this.scriptExtensions = ['js'];
	this.textExtension = ['txt' , 'htm' , 'html' , 'xml'];
	this.styleExtension = ['css'];
	
	this.loaders = [];
	this.completedItems = 0;
};



MassLoader.prototype = {	
				
		TYPE_IMAGE:'type_image',
		TYPE_SCRIPT:'type_script',
		TYPE_TEXT:'type_text',
		TYPE_LINK:'type_link',
		
		
		reset:function(){
			this.loaders = [];
			this.completedItems = 0;
		},
		
		
		launch:function(){
			
			if(this.loaders.length == 0)
				this.callback.call();
				return false;
			
			
			this.completedItems = 0;
			for (var i in this.loaders){
				this.loaders[i].loader.load();
			}
		},
		
		
		addFile:function(param_path , param_id , param_group , param_options ,  param_force_type){
			if(this.getItemByID(param_id) != null){
				console.error("Massloader : denied overwrite attempt on id " + param_id)
				return false;
			}
			
			var newItem = {};
			newItem.id = param_id;
			newItem.options = param_options;
			newItem.group = param_group;
			var extension = param_path.split('.');
			extension = String(extension[(extension.length - 1)]).toLowerCase();
			
			var self  = this;
			var callbackFunc = function(url){
				self.fileComplete();
			}
			
			if(param_force_type != undefined){
				switch(param_force_type){
					case MassLoader.prototype.TYPE_IMAGE:
						newItem.loader = new ImageLoader(param_path , callbackFunc);
					break;
					case MassLoader.prototype.TYPE_SCRIPT:
						newItem.loader = new ScriptLoader(param_path , callbackFunc);
					break;
						newItem.loader = new TextLoader(param_path , callbackFunc);
					break;
					case MassLoader.prototype.TYPE_LINK:
						newItem.loader = new CSSLoader(param_path , callbackFunc);
					break;
				}
			}
			else if(this.imageExtensions.indexOf(extension)> -1)
				newItem.loader = new ImageLoader(param_path , callbackFunc);
			else if(this.scriptExtensions.indexOf(extension)> -1)
				newItem.loader = new ScriptLoader(param_path , callbackFunc);
			else if(this.textExtension.indexOf(extension)> -1)
				newItem.loader = new TextLoader(param_path , callbackFunc);
			else if(this.styleExtension.indexOf(extension)> -1)
				newItem.loader = new CSSLoader(param_path , callbackFunc);
			else{
				console.error(extension + ' file extension is not loadable' );
				return false;
			}

			this.loaders.push(newItem);
		},
		

		
		getLoadedData:function(param_id){
			var data = new Object();
			if(param_id != null)
				return this.getItemByID(param_id).loader.getLoadedData();
		},
		
		
		getItemByID:function(param_id){
			for(var i  = 0; i < this.loaders.length ; ++i){
				if(this.loaders[i].id == param_id)
					return this.loaders[i];
			}
			return null;
		},
		
		
		fileComplete:function(){
			 this.completedItems++;
			 if(this.completedItems >=  this.loaders.length){
				 this.callback();
			 }
		},
		
		
};
