function ImageLoader()
{
	this.resources = [];
	this.totalResourcesCount = 0;
	this.completedResourcesCount = 0;
	this.onAllLoadedCallback = null;
	this.onLoadProgressCallback = null;
	
	this.concurrentLoads = 5;
	
	var self = this;
	
	this.add = function(url, title, type)
	{
		this.totalResourcesCount++;
		this.resources.push( new ImageResource( url, title, type ) ); 
	}
	
	this.load = function( onAllLoadedCallback , onLoadProgressCallback )
	{
		this.onAllLoadedCallback = onAllLoadedCallback; 
		this.onLoadProgressCallback = onLoadProgressCallback;
		
		var c = 0;
		for( i in this.resources  )
		{
			c++;
			this.resources[i].load( this.onItemLoaded, this.onItemError );
			if( c >  this.concurrentLoads )
			{
				break;
			}
		}
	}
	
	this.checkCompleted = function()
	{
		if( this.completedResourcesCount == this.resources.length )
		{
			if( this.onAllLoadedCallback )
			{
				this.onAllLoadedCallback();
			}
		}
	}
	
	this.getProgress = function ()
	{
		return ( self.totalResourcesCount > 0 ) ? ( self.completedResourcesCount / self.totalResourcesCount ) : 1;
	}
	
	this.onItemLoaded = function(image)
	{
		if( self.onLoadProgressCallback )
		{
			self.onLoadProgressCallback( self.getProgress() );
		}
		
		self.startNextItem();
		
		self.completedResourcesCount++;
		self.checkCompleted();
	}
	
	this.startNextItem = function ()
	{
		if( self.getCurrentConcurrentLoads() < self.concurrentLoads )
		{
			for( i in self.resources )
			{
				if( !self.resources[i].loadStarted)
				{
					self.resources[i].load( self.onItemLoaded, self.onItemError );
					break;
				}
			}
		}		
	}
	
	this.getCurrentConcurrentLoads = function()
	{
		var c = 0;
		for( i in this.resources )
		{
			if( this.resources[i].loadStarted && !this.resources[i].loadCompleted )
			{
				c++;
			}
		}
		return c;
	}
	
	this.onItemError = function(image)
	{
		self.completedResourcesCount++;
		
		self.startNextItem();
		
		self.checkCompleted();
	}
	

	
	this.stop = function()
	{
		var c = 0; 
		for( i in this.resources )
		{
			this.resources[i].stop();
		}
	}
	
}

function ImageResource(url, title, type)
{
	
	var self = this;
	
	this.url = url;
	
	this.loadCompleted = false;
	this.loadError = false;
	
	this.loadStarted = false;
	
	this.onLoadCallback = null;
	this.onErrorCallback = null;
	this.image = null;
	this.type = type;
	
	
	this.onImageLoad = function()
	{
		self.loadError = false;
		self.loadCompleted = true;
		
		if( self.onLoadCallback )
		{
			self.onLoadCallback(this);
		}
	}
	
	this.onImageError = function()
	{
		self.loadError = true;
		self.loadCompleted = true;
		if( self.onErrorCallback )
		{
			self.onErrorCallback(this);
		}		
	}
	
	this.load = function(onLoad, onError)
	{
		
		this.onLoadCallback = onLoad;
		this.onErrorCallback = onError;
		
		this.image = new Image();
		this.image.onload = this.onImageLoad;
		this.image.onerror = this.onImageError;
		
		this.loadStarted = true;
		this.image.src = this.url;
		
	}
	
	this.stop = function()
	{
		if( !this.loadCompleted )
		{
			if( this.image )
			{
				this.image.src = "";
			}
			
			delete this.image;
			
		}
		
	}

	
	
}
