var ImageLoader = Class.create({
	images: [],
	paused: true,
	initialize: function(options) {
		options = options || {};
		this.onLoad = options.onLoad || false;
		this.beforeStart = options.beforeStart || false;
	},
	add: function(image, options, nocheck) {
		options = options || {};
		if (image instanceof Array) {
			for (i = 0; i < image.length; i++) {
				this.add(image[i], options, true);
			}
			this.check();
			return;
		}
		var image = {
			img: image,
			beforeStart: (options.beforeStart) ? options.beforeStart : this.beforeStart,
			onLoad: (options.onLoad) ? options.onLoad : this.onLoad,
		};
		if (options.priority == 'now') {
			this.load(image);
			this.pause();
			return;
		} else if (options.priority == 'high') this.images.unshift(image);
		else this.images.push(image);
		
		if (!nocheck) this.check();
	},
	load: function(image) {
		if (image.beforeStart) image.beforeStart(image.img);
		var preload = new Image();
		preload.onload = (function() {
			image.img.src = image.img.readAttribute('source');
			if (this.images.indexOf(image) != -1) {
				this.images.shift();
				this.check();
			} else this.resume();
			if (image.onLoad) image.onLoad(image.img);
		}).bind(this, image);
		preload.src = image.img.readAttribute('source');
	},
	check: function() {
		if (this.images.length && !this.paused) this.load(this.images[0]);
	},
	pause: function() {
		this.paused = true;
	},
	resume: function() {
		this.paused = false;
		this.check();
	},
	toggle: function() {
		if (this.paused) this.resume();
		else this.pause();
	},
	status: function() { return (this.paused) ? 'paused' : 'active'; }
});
