var curPage = 1;
var listenToScroll = true;
$(document).ready(function() {
    $("#footerBar").hide();
    
    $(document).scroll(function(){
        if(listenToScroll == false)
            return;
        var scrollPos = $(document).scrollTop();
        
        if(scrollPos>0){
            $("#footerBar").fadeIn();
        }else{
            $("#footerBar").fadeOut();
        }
        var winHeight = $(window).height();
        if(scrollPos >= $(document).height()-winHeight){
            listenToScroll = false;
            $.get("/posts/page/"+curPage, function(data){
                /*if(data == ""){
                    listenToScroll = false;
                    return;
                }*/
                listenToScroll = true;
                $("#postList").append(data);
                curPage++;
            })
            
        }
    })
    
    $('#searchForm').submit(function() {
        window.location = "/search/"+$("#searchInput").val();
        return false;
    });
    
    media = '{{STATIC_URL}}'
    $('#commentForm').submit(function() {
        ajaxComment({'media': media});
        return false;
    });
});

//hide overlay
function hideOverlay(el){
    $(el).find(".overlay").stop().animate({
        opacity: 0
    }, 250);
}

//hide overlay
function showOverlay(el){
    $(el).find(".overlay").stop().animate({
        opacity: 1
    }, 250);    
}

//filter by categories
function showCategory(category, el){
    listenToScroll = true;
    url = ""
    if(el.hasClass('active')){
        el.removeClass('active');
        url += "/remove/"
    }else{
        el.addClass('active');
        url += "/add/"
    }
    var div = $("#postList");
    url = url+"category/"+category
    if(div.length>0){ //go ajax
        //div.html("loading");
        $.get(url, function(data){
            div.html(data);
        });
    }else{
        window.location.href = url;
    }
}
