/**
 * Callback function for the pagination links.
 * It uses an Ajax call to replace the details DIV with
 * the requested content from the server.
 * It uses the following global variables - items_per_page, contents_length, get_list_url
 * @param {page_index} index number of the clicked link (indexes start at 0 for link 1)
 */
function handle_pagination(page_index) {
    var max_elem = Math.min((page_index + 1) * window.items_per_page, window.contents_length);
    var pageNumberValue = page_index + 1;
    
    $('#in').empty();
    $('#loading').show();
    
    $.get(
        window.get_list_url, 
        {pageNumber:pageNumberValue, maxItemsPerPage:window.items_per_page}, 
        function(data) {
            $('#in').html(data);
            $('#loading').hide();
        }
    );
    
    var new_pg_total = 'Displaying <span class="b">' + ((page_index*window.items_per_page)+1) + '-' + max_elem + '</span> of <span class="b">' + window.contents_length + '</span>';
    $('.pgtotal').html(new_pg_total);
    return false;
}

// assign pagination callback and options to DIV elements
$(document).ready(function() {
    $('#loading').show();
    $.pagination(
        window.contents_length,
        {
            callback: handle_pagination,
            display_entries: 8,
            num_edge_entries: 1,
            items_per_page: window.items_per_page
        }
    );
});