﻿ImagesPreloader = function (imgArray, ctPanID, onCompleteHandler, onInitHandler) {
    this.imurls = imgArray;

    this.imgs = new Array();

    this.ctp = null;
    if (ctPanID != "") {
        this.ctp = $("#" + ctPanID);
        if (this.ctp[0] != null) {
            this.ctp[0].style.visibility = 'hidden';
        }
    }

    this.nil = 0;
    this.icn = this.imurls.length;

    this.oncomplete = onCompleteHandler;
    this.oninit = onInitHandler;

    this._raiseOnComplete = function () {
        this.LW('Complete');
        if (this.oncomplete != null) {
            this.oncomplete(this);
        }
    }

    this._raiseOnInit = function () {
        this.LW('Init');
        if (this.oninit != null) {
            this.oninit(this);
        }
    }

    this.FindDocumentImage = function (iurl) {
        for (var i = 0; i < document.images.length; i++) {
            if (document.images[i].src.endsWith(iurl)) {
                return document.images[i];
            }
        }
        return null;
    }

    this.LW = function (msg) {
        /*
        if (typeof (LogWrite) == "function") {
        LogWrite(msg);
        }
        */
    }

    this._onImgLoaded = function (ev) {
        var ipl = ev.data.ipl;
        var ci = ev.data.im;

        //document.getElementById("infoText").value = document.getElementById("infoText").value + ci.toString() + "\n";
        ipl.LW('Image Loaded: ' + ci.src);

        ipl.nil++;
        if (ipl.nil == ipl.icn) {
            if (ipl.ctp != null) {
                if (ipl.ctp[0] != null) {
                    ipl.ctp[0].style.visibility = 'visible';
                }
            }

            ipl._raiseOnComplete();
        }
    }

    this._onImgLoadError = function (ev) {
        var ipl = ev.data.ipl;
        var ci = ev.data.im;

        ipl.nil++;
        if (ipl.nil == ipl.icn) {
            if (ipl.ctp != null) {
                if (ipl.ctp[0] != null) {
                    ipl.ctp[0].style.visibility = 'visible';
                }
            }

            ipl._raiseOnComplete();
        }
    }

    this.LW('Preloading Images: ' + this.icn.toString());

    if (document.images) {
        for (var i = 0; i < this.imurls.length; i++) {
            var ciurl = this.imurls[i];

            var di = this.FindDocumentImage(ciurl);

            if (di == null) {
                this.LW('Loading Image: ' + ciurl);

                var ci = new Image();
                ci.ipl = this;
                $(ci).bind("load", { ipl: this, im: ci }, this._onImgLoaded);
                $(ci).bind("error", { ipl: this, im: ci }, this._onImgLoadError);
                ci.src = ciurl;

            } else {
                this.LW('Image Found: ' + ciurl);
                this.icn--;
            }
        }

        this.LW('ICN: ' + this.icn.toString());

        this._raiseOnInit();

        if (this.icn == 0) {
            this._raiseOnComplete();
        }
    }
}
