var ImgLoader = Class.create();
ImgLoader.prototype = {
	initialize: function() {
		this.queue = {};
		this.interval = null;
	},
	
	add: function(opt) {
		if (opt.wrapper == null || $(opt.wrapper) == null) return;
		
		var width = opt.width != null ? opt.width : $(opt.wrapper).getWidth();
		var height = opt.height != null ? opt.height : width;
		var evalCode = opt.evalCode != null ? opt.evalCode : null;
		
		$(opt.wrapper).update('<div style="width: ' + width + 'px; height: ' + height + 'px; background: url(/images/ajax_indicator.gif) center no-repeat">&nbsp;</div>');
		
		var image = new Image();
		image.src = opt.src;
		this.queue[opt.wrapper] = { img: image, 'wrapper': opt.wrapper, 'evalCode': evalCode, 'width': width };
		
		if (this.interval == null)
			this.interval = setInterval(this.update.bindAsEventListener(this), 100);
	},
	
	update: function() {
		var count = 0;
		for (var wrapper in this.queue) {
			count++;
			if (this.queue[wrapper].img.complete) {
				$(wrapper).update('<img src="' + this.queue[wrapper].img.src + '" width="' + this.queue[wrapper].width + '">');
				if (this.queue[wrapper].evalCode != null)
					this.queue[wrapper].evalCode();
				delete this.queue[wrapper];
			}
		}
		
		if (!count) {
			clearInterval(this.interval);
			this.interval = null;
		}
	}
}
