var loadImages;

$(document).ready(function(){
    $(".lazy-img img").on("load", function(){
        $(this).addClass("loaded");
    });
    
    /*$(".lazy-img-bg").on("load", function(){
        $(this).find(".bg-image").addClass("loaded");
    });*/
    
    $(document).on("scroll",function(){
        loadImages();
    });


    (loadImages = function loadImages(){
        /*$.each($(".lazy-img-bg"), function(){
            var block = $(this);
            var image = block.find(".bg-image").css('background-image');
            image = image.replace('url(','').replace(')','');
            
            if(isOnScreen(block)){
                var url = block.find(".bg-image").data("url");
                
                if(image != url){
                    block.find(".bg-image").css("background-image",'url(' + url + ')');
                };
            }
        });*/
        
        
        $.each($(".lazy-img"), function(){
            var block = $(this);
            var image = block.find("img");

            if(isOnScreen(block)){
                var url = image.data("url");

                if(image.attr("src") != url){
                    image.attr("src",url);
                };
            }
        });
    })();
    
    function isOnScreen(element){
        var win = $(window);
    
        var screenTop = win.scrollTop();
        var screenBottom = screenTop + win.height();
    
        var elementTop = element.offset().top;
        var elementBottom = elementTop + element.height();
    
        return elementBottom > screenTop && elementTop < screenBottom;
    }
});