window.preloader = new Preloader();



function Preloader() {
	this.images = new Array();
	this.loadCount = 0;
	this.loaded = false;
	this.percent = 0;

	if (window.preloader != null) {
		alert('You cannot use multiple loaders');
		return;
	}

	window.preloader = this;

	//set to something else to trigger this function when images are loaded
	this.onload = null;
	this.onchange = null;
}

Preloader.prototype.load = function (path, lst) {
	loaded = false;
	var i = this.images.length;
	var und;
	if (path==und || path==null) path = '';

	//an array to add
	if (typeof lst == "object" && lst != null) {
		if ( (lst.constructor + '').indexOf("Array") > 0 ) {

			//put the src separate so that it wouldn't trigger the loaded flag too often
			for (var j=0; j<lst.length; j++) {
				this.images[i+j] = new Image();
				this.images[i+j].onload = this.imageLoaded;
			}

			for (var j=0; j<lst.length; j++) {
				this.loaded = false;
				this.images[i+j].src = path + lst[j];
			}

		}
	} else
	//single item to add
	{

		this.loaded = false;
		this.images[i] = new Image();
		this.images[i].onload = this.imageLoaded;
		this.images[i].src = path + lst;
	}

	return this.images[this.images.length-1];
}

Preloader.prototype.getImage = function(strName) {
	for (var i=0; i<this.images.length; i++) {
		if (this.images[i].src.indexOf(strName) > 0)
			return this.images[i];
	}


	return this.load('', strName);
}

Preloader.prototype.imageLoaded = function () {

	//another image has been loaded, keep track here
	var ptr = window.preloader;
	ptr.loadCount++;



	ptr.percent = Math.round(
		(ptr.loadCount / ptr.images.length) * 100);


	//set all image's onload events to null if it has been loaded
	for (var z=0; z<ptr.images.length; z++) {
		if (ptr.images[z] != null)
			if (ptr.images[z].complete)
				ptr.images[z].onload = null;
	}

	if (ptr.onchange != null) ptr.onchange();

	if (ptr.loadCount == ptr.images.length) {
		ptr.loaded = true;
		if (ptr.onload != null) {
			ptr.onload();
		}
	}

}
function loaded() {
	//window.status = 'ok';
}

function changed() {
	//window.status = window.preloader.percent;
}
