var page = 2;
var paginator = "?PAGEN_1=";

(function() {
    var special = jQuery.event.special, uid1 = 'D' + (+new Date()), uid2 = 'D'
    + (+new Date() + 1);

    special.scrollstart = {
        setup : function() {

            var timer, handler = function(evt) {

                var _self = this, _args = arguments;

                if (timer) {
                    clearTimeout(timer);
                } else {
                    evt.type = 'scrollstart';
                    jQuery.event.handle.apply(_self, _args);
                }

                timer = setTimeout(function() {
                    timer = null;
                }, special.scrollstop.latency);

            };

            jQuery(this).bind('scroll', handler).data(uid1, handler);

        },
        teardown : function() {
            jQuery(this).unbind('scroll', jQuery(this).data(uid1));
        }
    };

    special.scrollstop = {
        latency : 300,
        setup : function() {

            var timer, handler = function(evt) {

                var _self = this, _args = arguments;

                if (timer) {
                    clearTimeout(timer);
                }

                timer = setTimeout(function() {

                    timer = null;
                    evt.type = 'scrollstop';
                    jQuery.event.handle.apply(_self, _args);

                }, special.scrollstop.latency);

            };

            jQuery(this).bind('scroll', handler).data(uid2, handler);

        },
        teardown : function() {
            jQuery(this).unbind('scroll', jQuery(this).data(uid2));
        }
    };

})();

jQuery(window).bind('scrollstop', function() {
    var scroll_top = $(document).scrollTop();// высота прокрученной области
    var page_height = $(document).height();// высота всей страницы
    var wind_height = $(window).height();// высота окна браузера
    // если непрокрученной области больше чем высота окна обраузера, то
    // подгружаем след. объекты
    

    if ((page_height - scroll_top) < wind_height * 3) {
        
        if (page != "end" && $('.number_str')[0]) {
            if (document.location.search) {
                paginator = document.location.search + '&PAGEN_1=';
            }
            
            jQuery.ajax({
                url:paginator+page,
                async:false,
                cache:true,
                success: function(data){
                    content = $(data).find('.tovar_block');
                    nexter = $(data).find("[src='/bitrix/templates/21shop/images/right_str.gif']");
                    console.info(nexter);
                    for (i=0; i < content.length + 1; i++) {
                        $('.tovar_block:last').after(content[i]);
                    }
                    if (nexter.length > 0){
                        page++;
                    } else {
                        page = "end";
                    }
                }
            });
        } else {
            $('#ajaxloading').hide();
            $('.footer').show();
        }
    }
});

$().ready(function(){
    if ($('.number_str')[0]) {
        $('.footer').hide();
        $('.tovar_block:last').after('<div id="ajaxloading"><img src="/bitrix/templates/21shop/images/loading.gif" /></div>');
    }
    $('.number_str').hide();
});
