$(document).ready(function(){
    var showAjaxMessage = function() {
        $('div.ajaxMessage').remove();
        $('#publicationCommentList').empty();
        $('<div class="ajaxMessage">Retrieving data. Please wait...</div>').insertAfter('#publicationCommentList');
    }

    var hideAjaxMessage = function() {
        $('.ajaxMessage').remove();
    }

    var addPaginationEvent = function() {
        $('.pagination a').each(
            function() {
                if(!this.hasEventHandler) {
                    $(this).click(function(event) {
                        showAjaxMessage();
                        $.get($(this).attr('href'), {type: "json"}, function(data) {
                            $('#publicationComments').html(data.comments_html);
                            addPaginationEvent();
                            hideAjaxMessage();
                        }, "json");
                        $.scrollTo('#publicationComments', 500)
                        event.preventDefault()
                    });
                    this.hasEventHandler = true;
                };
            }
        )
    };
    addPaginationEvent()
});