function ImagePreloader(images, ids, maxWidth, maxHeight)
{
	this.maxW = maxWidth;
	this.maxH = maxHeight;
	this.ids = ids;
	
	// store the callback
	this.call_back_func = function()
	{
		var maxWidth = this.maxW;
		var maxHeight = this.maxH;
		
		var img = arguments[0][0];
		var width = img.width;
		var height = img.height;
		
		// Proportionally resize the image to the
		// max sizes specified above
		var scaledWidth = width;
		var scaledHeight = height;
		var xRatio = maxWidth / width;
		var yRatio = maxHeight / height;
		
		if ( (width <= maxWidth) && (height <= maxHeight) ) {
			scaledWidth = width;
			scaledHeight = height;
		}	else if ( xRatio < yRatio ) {
			scaledWidth = maxWidth;
			scaledHeight = Math.ceil(xRatio * height);
		}	else {
			scaledWidth = Math.ceil(yRatio * width);
			scaledHeight = maxHeight;
		}
		
		var objs = (this.ids + "").split(',');
		for ( i = 0; i < objs.length; i++ ) {
			var o = document.getElementById(objs[i]);
			if (o) {
				o.style.width = scaledWidth + 'px';
				o.style.height = scaledHeight + "px";
				o.src = img.src;
			}
		}
	}

	// initialize internal state.
	this.nLoaded = 0;
	this.nProcessed = 0;
	this.aImages = new Array();
	
	// record the number of images.
	this.nImages = images.length;
	
	// for each image, call preload()
	for ( i = 0; i < images.length; i++ ){
		this.preload(images[i]);		
	}
}

ImagePreloader.prototype.preload = function(image)
{
	// create new Image object and add to array
	var oImage = new Image();
	this.aImages.push(oImage);
	
	// set up event handlers for the Image object
	oImage.onload = ImagePreloader.prototype.onload;
	oImage.onerror = ImagePreloader.prototype.onerror;
	oImage.onabort = ImagePreloader.prototype.onabort;
	
	// assign pointer back to this.
	oImage.oImagePreloader = this;
	oImage.bLoaded = false;
	// assign the .src property of the Image object
	oImage.src = image;
}

ImagePreloader.prototype.onComplete = function()
{
	this.nProcessed++;
	if ( this.nProcessed == this.nImages )
	{
		this.call_back_func(this.aImages, this.nLoaded);
	}
}

ImagePreloader.prototype.onload = function()
{
	this.bLoaded = true;
	this.oImagePreloader.nLoaded++;
	this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onerror = function()
{
	this.bError = true;
	this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onabort = function()
{
	this.bAbort = true;
	this.oImagePreloader.onComplete();
}
