if( typeof Array.prototype.shift==='undefined' ) 
{
/* extends array for browsers that don't define array.shift */
	Array.prototype.shift = function() 
	{
  		for( var i = 0, b = this[0], l = this.length-1; i<l; i++ ) 
  		{
   			this[i] = this[i+1];
  		}
  		this.length--;
		return b;
 	};
}

function imagePreloader ()
{
	var self = this;
	this.images = new Array();
	this._preloader = new Image()
	this._preloader.onload = function(){self._loaded()};
	this._preloader.onerror = function(){self._error()};
	this._preloader.onabort = function(){self._aborted()};
	this.finished = false;
	this.response = null;
	
	this.timer = null;
}

imagePreloader.prototype.add = function(src, callback)
{
	if (src != null && src != "")
	{
		var n = 0;
		while (n < this.images.length)
		{
			if(this.images[n] == null)
				continue;
			if(this.images[n].src == src)
			{
				if (callback != null)
					this.images[n].callbacks.push(callback)
				return;
			}
			n++;
		}
		
		var image = new Object();
		image.src = src;
		image.callbacks = new Array();
		if (callback != null)
			image.callbacks.push(callback)
		image.loading = false;
		this.images.push(image);
		
		this._preload();
	}
}
	
imagePreloader.prototype._preload = function()
{
	var self = this;
	if (this.images.length > 0 && !this.images[0].loading)
	{
		this.images[0].loading = true;
		this.finished = false;
		this._preloader.src = this.images[0].src;
		/* I beg You, Jesus Christ, Buddah and Spongebob
		   Please free me of Internet Explorer
		   Please
		   Pretty please
		   Pretty pretty please
		   Pretty pretty please with sugar, chocolate ice cream, honey and a bottle of champagne
		   Sob!
		   Internet Explorer greets me with a nice pop up saying that some stack has overflown at line 0
		   if I call this._preload() from  this._loaded(), this._error() or this._aborted().
		   Ok, setup a timer for checking when the image is loaded.
		   Powerful Thor, please, hit that sucking Internet Explorer with your hammer and free us of that plague!*/
		   
		if (isIE)
		{
			if (this.timer == null)
				this.timer = Timer.add(function(){return self._timer();}, 50);
		}
		else if (isKonq)
		{
		/* I knew that Spongebob wasn't a reliable divinity.... Lisa Simpson looks smart, but.....!
		   Konqueror 3.5.x doesn't fire the onload event when src is an animated gif... sob.
		   Fire up a timer to check the Image.complete property..... groan*/
			if (this.timer == null)
				this.timer = Timer.add(function(){return self._stoopidkonqueror();}, 50);}
		}
	
	else if (this.images.length == 0 && (isIE || isKonq))
	{
		Timer.remove(this.timer);
		this.timer = null;
	}
}

imagePreloader.prototype._loaded = function()
{
	this.finished = true;
	this.response = true;
	if (this.images.length > 0)
	{
		var cb = null;
		var n = 0;
		while ( (cb = this.images[0].callbacks[n]) != null )
		{
			cb(this.images[0].src, true, this._preloader);
			n++;
		}
		this.images.shift();
		if (!isIE)
			this._preload();
	}
}

imagePreloader.prototype._error = function()
{
	this.finished = true;
	this.response = false;
	if (this.images.length > 0)
	{
		var cb = null;
		var n = 0;
		while ( (cb = this.images[0].callbacks[n]) != null )
		{
			cb(this.images[0].src, false);
			n++;
		}
		this.images.shift();
		if (!isIE)
			this._preload();
	}
}

imagePreloader.prototype._aborted = function()
{
	this.finished = true;
	this.response = -1;
	if (this.images.length > 0)
	{
		var cb = null;
		var n = 0;
		while ( (cb = this.images[0].callbacks[n]) != null )
		{
			cb(this.images[0].src, -1);
			n++;
		}
		this.images.shift();
		if (!isIE)
			this._preload();
	}
}

imagePreloader.prototype._timer = function()
{
	if (this.finished)
		this._preload();
	return true;	
}

imagePreloader.prototype._stoopidkonqueror = function()
{
	if (this._preloader.complete && !this.finished)
	{
		this._loaded();
	}
	return true;	
}

var isIE = (navigator.appName.indexOf("Microsoft") !=-1);
var isKonq = (navigator.appName.indexOf("Konqueror") !=-1);

		
		
		
		
		
		
		
		
		
