/* This is based on the ImagePreloader from http://www.webreference.com/programming/javascript/gr/column3. */

var ImagePreloader = Class.create({

	initialize: function(sources, caller) {
		this.caller = caller;
		this.images = new Array;
		this.imagesLoaded = 0;
		this.imagesPending = sources.length;
		this.imagesProcessed = 0;
		
		for (var i = 0; i < sources.length; i++) {
			this.preload(sources[i]);
		}
	},
	
	onAbort: function() {
	  this.bAbort = true;
	  this.imagePreloader.onLoadAll();
	},
	
	onError: function() {
	  this.bError = true;
	  this.imagePreloader.onLoadAll();
	},
	
	onLoad: function() {
	  this.bLoaded = true;
	  this.imagePreloader.imagesLoaded++;
	  this.imagePreloader.onLoadAll();
	},
	
	onLoadAll: function() {
	  this.imagesProcessed++;
	  if (this.imagesProcessed == this.imagesPending)
	    this.caller.onPreload(this.images);
	},
	
	preload: function(source) {
		var image = new Image;
		this.images.push(image);

		image.onabort = ImagePreloader.prototype.onAbort;
		image.onerror = ImagePreloader.prototype.onError;
		image.onload  = ImagePreloader.prototype.onLoad;

		// assign pointer back to this.
		image.imagePreloader = this;
		image.bLoaded = false; /**/

		// assign the .src property of the Image object
		image.src = source;
	}
		
});



