// utilities-imagepreloader.js
	
// initialize the callback object to false
var callbackObj = false;

function ImagePreloader (aCallbackObj) {
		
	// set the callback property
	this.callbackObj = aCallbackObj;
	
	// initialize the images array
	this.images = new Array();
	this.imageIndex = -1;
	
	// initialize the is-active flag
	this.active = false;
	
}

ImagePreloader.prototype.PreloadImages = function (aImagesArray) {
	
	// walk each image in the provided array
	for (var i = 0; i < aImagesArray.length; i++) {
		
		// build the current image's array
		var tCurrentImage = new Array();
		tCurrentImage["id"] = aImagesArray[i]["id"];
		tCurrentImage["type"] = aImagesArray[i]["type"];
		tCurrentImage["date"] = aImagesArray[i]["date"];
		tCurrentImage["copyright"] = aImagesArray[i]["copyright"];
		tCurrentImage["description"] = aImagesArray[i]["description"];
		tCurrentImage["file"] = aImagesArray[i]["file"];
		tCurrentImage["gallery"] = aImagesArray[i]["gallery"];
		tCurrentImage["url"] = aImagesArray[i]["url"];
		tCurrentImage["image"] = null;
		
		// add the current image to the images array
		this.images.push(tCurrentImage);
		
	}
	
	// activate the processing if necessary
	this.ProcessNextImage();
	
}

ImagePreloader.prototype.ProcessNextImage = function () {
	
	// increment the image index
	this.imageIndex++;
	
	// check the index against the images array length
	if (this.imageIndex < this.images.length) {
		
		// set the active property
		this.active = true;
		
		// preload the next image
		this.images[this.imageIndex]["image"] = new Image;
		this.images[this.imageIndex]["image"].onLoad = this.ImageOnLoad();
		this.images[this.imageIndex]["image"].onError = this.ImageOnError();
		this.images[this.imageIndex]["image"].onAbort = this.ImageOnAbort();
		this.images[this.imageIndex]["image"].src = this.images[this.imageIndex]["url"];
		
	} else {
		
		// deactivate the processor
		this.active = false
		
	}
	
}

ImagePreloader.prototype.ImageOnLoad = function () {
	
	// pass the current image to the callback function
	this.callbackObj.PreloadCallback(this.images[this.imageIndex]);
	
	// process the next image
	this.ProcessNextImage();
	
}

ImagePreloader.prototype.ImageOnError = function () {
	
	// process the next image
	this.ProcessNextImage();
	
}

ImagePreloader.prototype.ImageOnAbort = function () {}