﻿/*
* THIS FILE WAS MODIFIED HEAVILY FROM THE ORIGINAL
*
* jQuery pager plugin
* Version 1.0 (12/22/2008)
* @requires jQuery v1.2.6 or later
*
* Example at: http://jonpauldavies.github.com/JQuery/Pager/PagerDemo.html
*
* Copyright (c) 2008-2009 Jon Paul Davies
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
* 
* Read the related blog post and contact the author at http://www.j-dee.com/2008/12/22/jquery-pager-plugin/
*
* This version is far from perfect and doesn't manage it's own state, therefore contributions are more than welcome!
*
* Usage: .pager({ currentPageNumber: 1, totalPageCount: 15, buttonClickCallback: PagerClickTest });
*
* Where currentPageNumber is the visible page number
*       totalPageCount is the total number of pages to display
*       buttonClickCallback is the method to fire when a pager button is clicked.
*
* buttonClickCallback signiture is PagerClickTest = function(pageclickednumber) 
* Where pageclickednumber is the number of the page clicked in the control.
*
* The included Pager.CSS file is a dependancy but can obviously tweaked to your wishes
* Tested in IE6 IE7 Firefox & Safari. Any browser strangeness, please report.
*/
(function($) {

  $.fn.pager = function(options) {

    var opts = $.extend({}, $.fn.pager.defaults, options);

    return this.each(function() {

      // empty out the destination element and then render out the pager with the supplied options
      $(this).empty().append(renderpager(parseInt(opts.currentPageNumber), parseInt(opts.totalPageCount),
        opts.numberPagesShown, opts.buttonClickCallback,
        opts.firstLabel, opts.previousLabel, opts.nextLabel, opts.lastLabel,
        opts.ulClass, opts.liClass, opts.currentClass));
    });
  };

  // render and return the pager with the supplied options
  function renderpager(currentPageNumber, totalPageCount, numberPagesShown, buttonClickCallback,
    firstLabel, previousLabel, nextLabel, lastLabel, ulClass, liClass, currentClass) {

    // setup $pager to hold render
    var $pager = $('<ul class="' + ulClass + '"></ul>');

    // add in the previous and next buttons
    if (currentPageNumber > 1) {
      $pager
        .append(renderButton(firstLabel, 1, totalPageCount, buttonClickCallback, liClass))
        .append(renderButton(previousLabel, currentPageNumber - 1, totalPageCount, buttonClickCallback, liClass));
    }
    // pager currently only handles 10 viewable pages ( could be easily parameterized, maybe in next version ) so handle edge cases
    var startPoint = 1;
    var endPoint = numberPagesShown;

    if (currentPageNumber > Math.ceil(numberPagesShown / 2)) {
      startPoint = currentPageNumber - Math.floor(numberPagesShown / 2);
      endPoint = currentPageNumber + Math.ceil(numberPagesShown / 2) - 1;
    }

    if (endPoint > totalPageCount) {
      startPoint = totalPageCount - (numberPagesShown - 1);
      endPoint = totalPageCount;
    }

    if (startPoint < 1) {
      startPoint = 1;
    }

    // loop thru visible pages and render buttons
    for (var page = startPoint; page <= endPoint; page++) {
      var destination = (page == currentPageNumber) ? null : page;
      var currentButton = renderButton(page, destination, totalPageCount, buttonClickCallback, liClass);

      if (page == currentPageNumber) {
        currentButton.addClass(currentClass);
      }

      currentButton.appendTo($pager);
    }

    // render in the next and last buttons before returning the whole rendered control back.
    if (currentPageNumber < totalPageCount) {
      $pager
        .append(renderButton(nextLabel, currentPageNumber + 1, totalPageCount, buttonClickCallback, liClass))
        .append(renderButton(lastLabel, totalPageCount, totalPageCount, buttonClickCallback, liClass));
    }

    return $pager;
  }

  // renders and returns a 'specialized' button, ie 'next', 'previous' etc. rather than a page number button
  function renderButton(buttonLabel, destinationPage, totalPageCount, buttonClickCallback, liClass) {

    var $Button = $('<li class="' + liClass + '">' + buttonLabel + '</li>');
    if (destinationPage != null) {
      $Button.click(function() { buttonClickCallback(destinationPage); });
    }
    return $Button;
  }

  // pager defaults. hardly worth bothering with in this case but used as placeholder for expansion in the next version
  $.fn.pager.defaults = {
    currentPageNumber: 1,
    totalPageCount: 1,
    numberPagesShown: 5,
    buttonClickCallback: null,
    firstLabel: 'first',
    previousLabel: 'previous',
    nextLabel: 'next',
    lastLabel: 'last',
    ulClass: 'Pages',
    liClass: 'PagingElement',
    currentClass: 'CurrentPage'
  };

})(jQuery);






