$(document).ready(function() {
    $('#news_category_news_category_id').change(function(){
       update_results(this.value,1);
    })

    $('div.pagination a').live('click',function(){
        page = 1;
        current = new Number($('div.pagination span.current').html());

        if($(this).hasClass("prev_page")){
            page = current - 1;
        } else if($(this).hasClass("next_page")) {
            page = current + 1;
        }else{
            page = $(this).html();
        }
        update_results($("#news_category_news_category_id").val(),page);
        return false;
    });
});

function update_results(cat,page){
    $('#post_list').fadeOut("slow",function(){
        $('#post_list').animate({
            height: 200
        }, 500 );
        $('#post_list').html("<div id='filter_results_spinner'></div>");
        $('#post_list').fadeIn("slow",function(){
            $.ajax({
                type: "POST",
                url: "/news_filter",
                data: "cat="+ cat + "&pagenum=" + page + "&authenticity_token=" + AUTH_TOKEN,
                success: function (html) {
                    $('#post_list').fadeOut("slow", function(){
                        $('#post_list').html(html);

                        //code to get incoming height
                        $('#post_list').show();
                        var h = $('#post_list_wrapper').height();
                        $('#post_list').hide();

                        $('#post_list').animate({
                            height: h
                        }, 700 );
                        $('#post_list').fadeIn("slow");
                    });
                },
                error: function (html) {

                }
            });
        });
    });
}