// This file demonstrates the different options of the pagination plugin
// It also demonstrates how to use a JavaScript data structure to 
// generate the paginated content and how to display more than one 
// item per page with items_per_page.
        
/**
 * Callback function that displays the content.
 *
 * Gets called every time the user clicks on a pagination link.
 *
 * @param {int}page_index New Page index
 * @param {jQuery} jq the container with the pagination links as a jQuery object
 */
function pageselectCallback(page_index, jq){
    // jq.id can be called to get the id of the 'Pagination'
    // from there, you can use that for Searchresult and items_per_page too

    // Get number of elements per pagionation page from form
    var items_per_page = $('#items_per_page').val();
    var max_elem = Math.min((page_index+1) * items_per_page, getElementCount());
    var newcontent = '';
    
    // Iterate through a selection of the content and build an HTML string
    for(var i=page_index*items_per_page;i<max_elem;i++)
    {
        newcontent += getElementHtmlAt(i);
    }
    
    // Replace old content with new content
    $('#Searchresult').html(newcontent);
    
    // Prevent click eventpropagation
    return false;
}

function getElementHtmlAt(i) {
    responseObj = document.getElementById("paginationItem_"+(i+1));
    if (responseObj!=null) {
        return('<div class="paginationDisplayItem">'+responseObj.innerHTML+"</div>");
    } else {
        return("");
    }
}

function getElementCount() {
    searchResultObj = document.getElementById("paginationDataPool");
    if (searchResultObj == null) {
        return(0);
    }
    return(searchResultObj.getElementsByTagName('div').length);
}
// The form contains fields for many pagiantion optiosn so you can 
// quickly see the resuluts of the different options.
// This function creates an option object for the pagination function.
// This will be be unnecessary in your application where you just set
// the options once.
function getOptionsFromForm(){
    var opt = {callback: pageselectCallback};
    // Collect options from the text fields - the fields are named like their option counterparts
    opt["items_per_page"] = parseInt(document.getElementById("items_per_page").value);
    opt["num_display_entries"] = parseInt(document.getElementById("num_display_entries").value);
    opt["num_edge_entries"] = parseInt(document.getElementById("num_edge_entries").value);
    opt["prev_text"] = document.getElementById("prev_text").value;
    opt["next_text"] = document.getElementById("next_text").value;
    return opt;
}

