/***********************************************************************
 * Copyright © 2007 Mads Brunn <mab@acto.dk>
 * This script adds pagination to an un-/ordered list
 *
 * Requirements: jQuery javascript library (> version 1.2)
 * 
 * Usage:   Simply add 'class="PaginatedList"' to any UL-element that
 *          should be paginated.
 *
 * If you would like to change the HTML for the navigation, check
 * the function 'renderPaging' below
 ***********************************************************************/


//number of elements per page
var listPerPage = 5;


//array of jQuery objects (internal - don't mess with it)
var PaginationLists = [];

//document.onload ...
$(function(){
    $(".PaginatedList").each(function(i){
        //register list
        PaginationLists[i] = this;

        //number of elements in list
        hitsTotal = $('li',this).length;
        
        //number of pages
        pageTotal = Math.ceil(hitsTotal / listPerPage);

        //index
        pageIndex = 1;

        //only do this stuff if there are more than one page        
        if(pageTotal > 1){
            $('li',this).each(function(e){
                if(e > listPerPage-1){
                    $(this).css('display', 'none');
                }            
            });
            $(this).after('<div id="Paging' + i + '" class="Paging">' + renderPaging(i, pageIndex, pageTotal) + '</div>');
        }
    });
});


/**
 * Renders pagination in list
 * @param   integer     index of the list (internal)
 * @param   integer     the page index
 * @param   integer     total number of pages in list
 */
function renderPaging(i,pageIndex,pageTotal){
    var out = '';
    out += ' <div class="PagePrevious">';
    if(pageIndex > 1){
        out += '<a href="#" title="Tilbage" onclick="return prevPage('+i+',' + ( pageIndex-1 ) + ')">&lt;&lt;&lt;</a>';
    }
    out += ' </div>';
    out += ' <div class="PageNext">';
    if(pageTotal > pageIndex){ 
        out+= '<a href="#" title="" onclick="return nextPage('+i+',' + ( pageIndex + 1 ) + ')">&gt;&gt;&gt;</a>';
    }
    out += ' </div>';
    out += ' <div class="PageInfo" style="color:#000;">'+ pageIndex + '/' + pageTotal + '</div>';
    return out;
}


/**
 * Turns the list one page forward
 * @param   integer     the index of the list (internal)
 * @param   integer     the page index
 */
function nextPage(i,pageIndex){
    jObj = PaginationLists[i];

    hitsTotal = $('li',jObj).length;                //total number of elements in list
    pageTotal = Math.ceil(hitsTotal / listPerPage); //total number of pages
    Start = (pageIndex-1) * listPerPage;            //the first element to be displayed
    End = Start + listPerPage;                      //the last element to be displayed
    
    if(pageIndex <= pageTotal){
        $('li',jObj).each(function(e){
            if(e >= Start &&  e < End){
                $(this).css('display','block');
            } else {
                $(this).css('display','none');
            }            
        });
		$('#Paging'+i).html(renderPaging(i,pageIndex,pageTotal));
    }
    return false;
}


/**
 * Turns the list one page backward
 * @param   integer     the index of the list (internal)
 * @param   integer     the page index
 */
function prevPage(i,pageIndex){

    jObj = PaginationLists[i];

    hitsTotal = $('li',jObj).length;                //total number of elements in list
    pageTotal = Math.ceil(hitsTotal / listPerPage); //total number of pages
    Start = (pageIndex-1) * listPerPage;            //the first element to be displayed
    End = Start + listPerPage;                      //the last element to be displayed
    
    if(pageIndex > 0){
        $('li',jObj).each(function(e){
            if(e >= Start &&  e < End){
                $(this).css('display','block');
            } else {
                $(this).css('display','none');
            }            
        });
        $('#Paging'+i).html(renderPaging(i,pageIndex,pageTotal));
    }
    return false;
}