var preloader = new Preloader;

function Preloader()
{	
	this.preloader;
	this.pe = null;
	this.preImage;
	this.target;
	this.cachedImages = new Array();
	this.checkInterval = 0.2 //sec
	this.minLoadingTime = 0.6;
	this.totalLoadingTime = 0;
	
	//nacitanie preloadovacieho obrazku aby bol pripraveny
	var preImage = new Image;
} 

Preloader.prototype.loadImage = function(src, target)
{
	this.stop();
	
	this.target = target;
	
	this.preImage = null;
	this.preImage = new Image();
	this.preImage.src = src;
	
	this.pe = new PeriodicalExecuter(this.checkLoadingProcess.bind(this), this.checkInterval);
}

Preloader.prototype.stop = function()
{
	if(this.pe)
	{
		this.pe.stop();
		this.pe = null;
	}
}

Preloader.prototype.checkLoadingProcess = function()
{
	if(!this.target)
	{
		this.stop();
		return;
	}
	
	if(this.preImage.complete)
	{
		this.target.src = this.preImage.src;
		if(this.preloader)
		{
			this.preloader.hide();
		}
		this.stop();
		return;
	}
}


Preloader.prototype.cacheImages = function(images)
{
	var preImages = new Array();
	for(var i = 0; i < images.length; i++)
	{
		if(this.cachedImages.indexOf(images[i]) == -1)
		{
			preImages[i] = new Image;
			preImages[i].src = images[i];
			this.cachedImages[this.cachedImages.length] = images[i];
		}
	}
}




