
    /**
     * AkenaTechnologies
     * Lazy Load
     *
     * Usage:
     *
     *      $( <DOMelement> ).lazy_load( <user_function> );
     *
     */



    // Tableau de chargement des objets "lazy"
    // Chaque ligne contient 3 colonnes :
    //      - objet DOM
    //      - chargement effectue
    var _lazy_load = [];


    (function( $ ){

        "use strict";

        // Register Lazy events
        $.fn.lazy_load = function(func){
            _lazy_load[_lazy_load.length] = { referer: $(this), loaded: false, callback: func, mode: 'bottom' };
            return true;
        };

        // Register Lazy events
        $.fn.lazy_load_reverse = function(func){
            _lazy_load[_lazy_load.length] = { referer: $(this), loaded: false, callback: func, mode: 'reverse' };
            return true;
        };
        
        // Catch events
        function start_lazy_load(e){

            // ATTENTION : window.scrollY fait tout plante dans IE,
            // pageYOffset resoud le probleme
            var pp = parseInt(window.pageYOffset, 10) + parseInt(window.innerHeight, 10);
            var pt = parseInt(window.pageYOffset, 10);
            for (var i=0; i<_lazy_load.length; i++) {
                var item = _lazy_load[i];
                var pi = parseInt(item.referer.offset().top, 10);
                if (item.mode=='reverse') pi+= parseInt(item.referer.height(), 10);
                if (item.mode=='bottom' && pp>=pi) {
                    if (item.loaded) { continue; }
                    (item.callback)();
                    item.loaded = true;
                }
                else if (item.mode=='reverse' && pt>=pi) {
                    (item.callback)('show');
                    item.loaded = true;
                }
                else if (item.mode=='reverse' && pt<pi) {
                    (item.callback)('hide');
                    item.loaded = true;
                }
            }

        };

        // Trigger event on scroll
        $(window).on('scroll', start_lazy_load);
        $(window).on('ready', start_lazy_load);

    }( jQuery ));


