let offset = 0;
const amount = 10;
let firstRun = true;
let itemsLeft = true;
let running = false;
let loadingUrl;

if(window.location.pathname.indexOf('popular') !== -1) {
    loadingUrl = '/posts/getPopularPosts';
} else {
    loadingUrl = '/posts/getFeedPosts';
}

function loadEntries() {
    if(!running && itemsLeft) {
        $.ajax({
            url: loadingUrl,
            data: {
                amount: amount,
                offset: offset
            },
            beforeSend: function () {
                $('.loading-spinner').show();
                running = true;
            },
            success: function (data) {
                $('.loading-spinner').hide();
                $('.post-container .comment-list').append(data);
                registerPostEvents();
                offset++;
                if (data === "") {
                    $('.post-container').append("<p>Es konnten keine weiteren Posts gefunden werden. Bitte schaue später nochmal vorbei!</p>");
                    itemsLeft = false;
                }
                firstRun = false;
                running = false;
            },
        });
    }
}

$(window).scroll(function() {
    if($(document).scrollTop() + 360 + $(window).height() >= $('.comment-list').position().top + $('.comment-list').outerHeight()) {
        loadEntries();
    }
});

loadEntries();

$('.search-input').focus(function(){
    $(this).parent().addClass('focus');
}).blur(function(){
    $(this).parent().removeClass('focus');
});