// ===========================================
// >>>> NOTE, this file requires jquery
// ===========================================

// ===========================================
// ========    IMAGELOADER OBJECT    =========
// ===========================================

function ImageLoader(lstPathImg_Arr) {

    // Fields           ======================================================
    var stopLoading = false;

    // Attributes       ======================================================
    var listPathImage_Arr = new Array();
    this.getListpathImage_Arr = function () {
        return listPathImage;
    }

    var imageLoad_Fun = function (img) { };
    this.setImageLoad_Fun = function (imgLoad_Fun) {
        if ($.isFunction(imgLoad_Fun)) {
            imageLoad_Fun = imgLoad_Fun;
        } else {
            imageLoad_Fun = function (img) { };
        }
    }


    // Public methods   ======================================================
    var startLoad = this.startLoad = function () {
        stopLoading = false;
        recursiveLoadImg(0);
    }

    var stopLoading = this.stopLoading = function () {
        stopLoading = true;
    }

    // Private methods  ======================================================
    var recursiveLoadImg = function (idxImageToLoad) {

        if (idxImageToLoad < 0 || idxImageToLoad >= listPathImage_Arr.length || stopLoading) {
            return;
        }

        var img = $("<img />").attr('src', listPathImage_Arr[idxImageToLoad] + "?" + new Date().getTime()).load(
		    function () {

			    if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
				    imageLoad_Fun(img);
				    recursiveLoadImg(idxImageToLoad + 1);
			    } else {
				    imageLoad_Fun(img);
				    recursiveLoadImg(idxImageToLoad + 1);
			    }
		    }
	    );
    }


    // Constructor      ======================================================
    var constructor = function (lstPathImg_Arr) { //Constructor definition
        if ($.isArray(lstPathImg_Arr)) {
            listPathImage_Arr = lstPathImg_Arr;
        } else {
            listPathImage_Arr = new Array();
        }
    }
    constructor(lstPathImg_Arr); //Constructor call

}
