var cBasePreloader = {
	autoStart: true,
	images: null,
    imgLoaded: 0,
    imgToLoad: 0,
	callbackImgLoaded: null,
	callbackAllLoaded: null
}

var cPreloader = function() {
	// closure vars
	var selfRef = this;
	var args = null;
	
	this.init = function() {
		this.imgToLoad = this.images.length;
		if (this.autoStart
			&& this.images.length > 0) 
		{
			this.preloadImg(0);
		}
	} 
	
	this.start = function() {
		if (!this.autoStart
			&& this.images.length > 0)	
		{			
			this.preloadImg(0);
		}		
	}
	
	this.preloadImg = function(index) {
		if (index >= 0 
			&& index < this.images.length) 
		{		    
			if (this.images[index].hasOwnProperty('path') 
				&& this.images[index]['path'] != null) 
			{
				this.images[index]['img'] = new Image();
			
				$(this.images[index]['img'])
					.load(function() {
						$(this).unbind('load');
						selfRef.imgLoaded++;
						// call callback					
						if (typeof selfRef.callbackImgLoaded == 'function' 
							&& selfRef.callbackImgLoaded.length == 2) 
						{
							selfRef.callbackImgLoaded(index, selfRef.images[index]['img']);
						}
						selfRef.preloadImg(++index);
					})
					.attr('src', this.images[index]['path']);
				return;	
			}
			// in case there is no path property or it is null go to next index
			selfRef.preloadImg(++index);
			return;
		}
		// after preloading
		if (typeof this.callbackAllLoaded == 'function' 
			&& this.callbackAllLoaded.length == 0) 
		{
			this.callbackAllLoaded();
		}		
	}
	
	// add args
	this.args = arguments[0];
	
	// bind arguments
    for (var n in arguments[0]) {
        this[n] = arguments[0][n];
    };
	
	// init preloader
	this.init();
}
cPreloader.prototype = cBasePreloader;

