Preloader = Class.create({
	images: [],
	loaded: [],
	loadedCount: 0,
	totalCount: 0,
	
	initialize: function(options) {
		this.options = {
			onImageLoad: Prototype.K,
			onComplete: Prototype.K
		};
		Object.extend(this.options,options || {});
	},
	
	add: function(images) {
		if(Object.isArray(images)) {
			this.images = this.images.concat(images);
		} else if (Object.isString(images)) {
			this.images.push(images);
		}
		return this;
	},
	
	load: function() {
		this.totalCount = this.images.length;
		this.images.each(function(imagePath){
			var img = new Image();
			img.onload = function(){this.imageCompleted(img)}.bind(this);
			img.src = imagePath;
		}.bind(this));
	},
	
	imageCompleted: function(img) {
		this.loaded.push(img);
		this.loadedCount++;
		this.options.onImageLoad(img,this);
		if(this.loadedCount == this.totalCount) {
			this.options.onComplete(this.loaded,this);
		}
	},
	
	getPercentageComplete: function() {
		return (this.totalCount > 0) ? (this.loadedCount / this.totalCount * 100).round() : 0;
	}
});
