/**
 * 滚动加载器
 * 当滚动到页面底部的时候, 触发一个回调
 * @param {function} callback
 * @param {boolean} init_trigger - 刚开始是否先触发一次回调
 */
function scroll_loader(callback, init_trigger) {
    var $window = $(window);
    var $document = $(document);
    var SCREEN_HEIGHT = document.documentElement.clientHeight;
    var DELTA_BUFFER_HEIGHT = 100;
    var previous_scroll_top = 0;

    var flag = true;

    // 先触发一次回调
    if (init_trigger) callback(function () {
        // Do Nothing
    });

    $window.on('scroll', function () {
        var document_height = $document.height();
        var CURRENT_SCROLL_TOP = $window.scrollTop();
        var IS_SCROLLING_UP = CURRENT_SCROLL_TOP > previous_scroll_top;
        var IS_REACH_BOTTOM = document_height - CURRENT_SCROLL_TOP < SCREEN_HEIGHT + DELTA_BUFFER_HEIGHT;

        previous_scroll_top = CURRENT_SCROLL_TOP;

        if (IS_SCROLLING_UP && IS_REACH_BOTTOM) {
            if (flag) {
                flag = false;
            } else {
                return true;
            }
            function complete_callback() {
                // 刷新页面的高度
                document_height = $(document).height();
                // 准备下一次触发回调
                flag = true;
            }

            callback(complete_callback);
        }
    });
};
