

function ImageLoader(imageUrls, callbackFunc) {

    this.callback = callbackFunc;

    this.numLoaded = 0;
    this.numProcessed = 0;

    this.images = new Array();
    this.numImages = imageUrls.length;

    for (var i = 0; i < imageUrls.length; i++) {
        this.preload(imageUrls[i]);
    }
}

ImageLoader.prototype.preload = function(imageUrl) {

    var img = new Image();
    this.images.push(img);

    img.onload = ImageLoader.prototype.onload;
    img.onerror = ImageLoader.prototype.onerror;
    img.onabort = ImageLoader.prototype.onabort;

    img.oImageLoader = this;
    img.bLoaded = false;

    img.src = imageUrl;
}

ImageLoader.prototype.onComplete = function() {
    this.numProcessed++;
    if (this.numProcessed == this.numImages) {
        this.callback(this.images, this.numLoaded);
    }
}

ImageLoader.prototype.onload = function() {
    this.bLoaded = true;
    this.oImageLoader.numLoaded++;
    this.oImageLoader.onComplete();
}

ImageLoader.prototype.onerror = function() {
    this.bError = true;
    this.oImageLoader.onComplete();
}

ImageLoader.prototype.onabort = function() {
    this.bAbort = true;
    this.oImageLoader.onComplete();
}
