// Define Namespace
if (! window.util) {
    util = {};
}

// LazyImagesLoad
util.LazyImagesLoad = function (){
    var imagesList = [];

    var onImgLoad = function () {
        var ample = this.width;
        var alt = this.height;
        var w = this.maxWidth;
        var h = this.maxHeight;

        if (ample > w && alt <= h){
            alt = alt /
                (ample / w);
            ample = w;
        }
        if (alt > h && ample <= w){
            ample = ample /
                (alt / h);
            alt = h;
        }
        if (alt > h && ample > w){
            if(ample > alt) {
                alt = alt /
                    (ample / w);
                ample = w;
            }
            else {
                ample = ample /
                    (alt / h);
                alt = h;
            }
        }

        this.obj.src = this.src;
        this.obj.width = ample;
        this.obj.height = alt;
    }

    return {
        addImage : function (url, w, h, id) {
            imagesList.push([url, w, h, id]);
        },

        loadImages : function (e) {
            for(var i = 0; i < imagesList.length; i++) {
                var image = new Image();
                image.onload = onImgLoad;
                image.obj = document.getElementById(imagesList[i][3]);
                image.maxWidth = imagesList[i][1];
                image.maxHeight = imagesList[i][2];
                image.src = imagesList[i][0];
            }
        }
    }
}();

// Register onload function
if (window.addEventListener){
    window.addEventListener('load', util.LazyImagesLoad.loadImages, false); 
} else if (window.attachEvent){
    window.attachEvent('onload', util.LazyImagesLoad.loadImages);
} else {
    window.onload = util.LazyImagesLoad.loadImages;
}