﻿/*
* 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({ pagenumber: 1, pagecount: 15, buttonClickCallback: PagerClickTest });
*
* Where pagenumber is the visible page number
*       pagecount 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(opts));
            
            // specify correct cursor activity
            $('.pages li').mouseover(function() { document.body.style.cursor = "pointer"; }).mouseout(function() { document.body.style.cursor = "auto"; });
        });
    };

    // render and return the pager with the supplied options
    function renderpager(options) {
        var pagenumber = parseInt(options.pagenumber);
        var pagecount = parseInt(options.pagecount);
        var numpages = parseInt(options.numpages);
        var buttonClickCallback = options.buttonClickCallback;

        // setup $pager to hold render
        var $pager = $('<ul class="pages"></ul>');

        // add in the previous and next buttons
        $pager.append(renderButton(options.firstText, options)).append(renderButton(options.prevText, options));

        // handle which page numbers show up (default to 1 - numpages)
        var startPoint = 1;
        var endPoint = numpages;

        // start scrolling page numbers over (so 1..2..3, 2..3..4, 3..4..5, etc)
        if (pagenumber > numpages - 1) {
            startPoint = pagenumber - 1;
            endPoint = pagenumber + numpages - 2;
        }
        // don't scroll pages past the last page
        if (endPoint > pagecount) {
            startPoint = pagecount - (numpages - 1);
            endPoint = pagecount;
        }

        // don't scroll pages past the first page
        if (startPoint < 1) {
            startPoint = 1;
        }

        // loop thru visible pages and render buttons
        for (var page = startPoint; page <= endPoint; page++) {

            var currentButton = $('<li class="page-number" page="' + page + '"><span class="left"><span class="right">' + (page) + '</span></span></li>');
            page == pagenumber ? currentButton.addClass('pgCurrent') : currentButton.click(function() { buttonClickCallback($(this).attr("page")); });
            currentButton.appendTo($pager);
        }

        // render in the next and last buttons before returning the whole rendered control back.
        $pager.append(renderButton(options.nextText, options)).append(renderButton(options.lastText, options));

        return $pager;
    }

    // renders and returns a 'specialized' button, ie 'next', 'previous' etc. rather than a page number button
    function renderButton(buttonLabel, options) {
        var pagenumber = parseInt(options.pagenumber);
        var pagecount = parseInt(options.pagecount);
        var buttonClickCallback = options.buttonClickCallback;

        var $Button = $('<li class="pgNext"><span class="left"><span class="right">' + buttonLabel + '</span></span></li>');

        var destPage = 1;

        // work out destination page for required button type
        switch (buttonLabel) {
            case options.firstText:
                destPage = 1;
                break;
            case options.prevText:
                destPage = pagenumber - 1;
                break;
            case options.nextText:
                destPage = pagenumber + 1;
                break;
            case options.lastText:
                destPage = pagecount;
                break;
        }

        // disable and 'grey' out buttons if not needed.
        if (buttonLabel == options.firstText || buttonLabel == options.prevText) {
            pagenumber <= 1 ? $Button.addClass('pgEmpty') : $Button.click(function() { buttonClickCallback(destPage); });
        }
        else {
            pagenumber >= pagecount ? $Button.addClass('pgEmpty') : $Button.click(function() { buttonClickCallback(destPage); });
        }

        return $Button;
    }

    // pager defaults. hardly worth bothering with in this case but used as placeholder for expansion in the next version
    $.fn.pager.defaults = {
        pagenumber: 1,
        pagecount: 1,
        firstText: '<<',
        prevText: '<',
        nextText: '>',
        lastText: '>>',
        numpages: 10
    };

})(jQuery);