/*
 *  ImagePreloader
 *  A class to preload all images in a html page. it triggers an action
 *	after all images have been loaded correctly
 *
 *  @version 0.1
 *  @license GPL
 *
*/

var ImagePreloader = Class.create();
ImagePreloader.prototype=
{
	initialize: function(options)
	{
		this.counter = 0;
		this.allImages = new Array();
		if (options && options.onAfterLoad)
			this.onAfterLoad = options.onAfterLoad;
	},
		
	addImagesToPreload: function()
	{
		for(var i=0; i<arguments.length; i++)
		{
			var imageObject = new Image();
			imageObject.parent = this;
			imageObject.chanceToLoad = 2;
			imageObject.onload = function() {
				try{
					this.parent.counter++;
					if( this.parent.counter == this.parent.allImages.length ) {
						if (this.parent.onAfterLoad)
							this.parent.onAfterLoad();
						this.parent.allImages = null;
					}
				}catch(e){
				}finally{
					return true;
				}
			};
			imageObject.onerror = function() {
				try{
					this.chanceToLoad--;
					if(this.chanceToLoad > 0)
						this.src = this.src;
				}catch(e){
				}finally{
					return true;
				}
			};
			imageObject.src = arguments[i];

			try{
				this.allImages.push(imageObject);
			}catch(e){
			}
		}
	}
};
