﻿var itemsByRank = ['51','52','35','50','53','28','47','54','55','43','41','45','44','40','37','49','48','38','57','59','17','61','33','6','198','201','195','2','196','105','4','62','60','65','58','63','23','21','22','19','20','24','27','26','18','56','185','194','152','25','153','151','145','147','150','148','34','143','36','192','39','154','29','156','155','68','157','85','32','42','46','31','30','67','101','91','103','95','93','99','97','111','110','113','112','106','0','109','108','89','87','121','122','159','124','123','120','116','115','117','119','118','167','165','169','171','172','114','161','163','193','187','189','188','176','177','178','175','173','191','174','179','184','190','186','183','180','181','182','203','204','197','199','200','202','205','11','10','12','15','14','13','5','3','84','7','9','8','77','78','75','76','82','83','81','79','80','16','69','133','135','134','73','74','72','70','71','88','90','1','86','126','127','125','104','107','131','132','130','128','129','98','96','94','92','102','100','142','144','146','136','138','137','139','141','140','66','160','158','149','64','170','162','168','166','164'];

function SortName()
{
    $.mlnSortMethod = "SortByName";
    Refresh();
}
            
function SortRank()
{
    $.mlnSortMethod = "SortByRank";
    Refresh();
}
function OnChangeNumItems()
{
    Refresh();
}

function Refresh()
{
    var opt = getOptionsFromForm();
    $("#Pagination").pagination(itemsByRank.length, opt);
}
    
function pageselectCallback(page_index, jq)
{
    // Get number of elements per pagionation page from form
    var items_per_page = $('#Select1').val();
    var max_elem = Math.min((page_index+1) * items_per_page, itemsByRank.length);
    var itemIdx;
    var divId;
    
    // hide all items
    for(var i=0; i < itemsByRank.length; i++)
    {
        divId = "ctl00_MainContent_divItem" + i;
        $("#" + divId).hide();
    }
    
    // Iterate through a selection of the content and show those items
    for(var i=page_index*items_per_page;i<max_elem;i++)
    {
/*    
        if($.mlnSortMethod == "SortByName")
            itemIdx = itemsByName[i];
        else
            itemIdx = itemsByRank[i];
*/
        itemIdx = i;
        divId = "ctl00_MainContent_divItem" + itemIdx;
        $("#" + divId).show();
    }
    
    // Prevent click eventpropagation
    return false;
}

// 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};
    var ipp = parseInt($('#Select1 :selected').val());
    opt["items_per_page"] = ipp;        // Number of items per page
    opt["num_display_entries"] = 10;    // Number of pagination links shown
    opt["num_edge_entries"] = 2;        // Number of start and end points
    opt["prev_text"] = "Prev";          // "Previous" label
    opt["next_text"] = "Next";          // "Next" label
    return opt;
}

// When document has loaded, initialize pagination and form 
$(document).ready(function(){
    $.mlnSortMethod = "SortByName";

	// Create pagination element with options from form
    var optInit = getOptionsFromForm();
    $("#Pagination").pagination(itemsByRank.length, optInit);
    
	// Event Handler for for button
	$("#setoptions").click(function(){
        var opt = getOptionsFromForm();
        // Re-create pagination content with new parameters
        $("#Pagination").pagination(itemsByRank.length, opt);
    }); 

});


