/*
* 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($) {
	
	var opts = {};
	
    jQuery.fn.pager = function(options) {		

        opts = jQuery.extend({}, $.fn.pager.defaults, options);
		
        jQuery(this).each(function() {
            
			/**
			 * Empty out the destination element and then render out the pager
			 * Mod by tylerg@arc90.com
			 */
			$(this).empty().append(renderpager());

            // specify correct cursor activity
            $('.pages li').mouseover(function() { document.body.style.cursor = "pointer"; }).mouseout(function() { document.body.style.cursor = "auto"; });
        });
		
		return jQuery;
    };

    /**
	 * Render and return the pager with the supplied options
	 *
	 */
    function renderpager() {
		var pagenumber = parseInt(opts.pagenumber);
		var pagecount  = parseInt(opts.pagecount);
		var buttonClickCallback = opts.buttonClickCallback;
		
        // setup $pager to hold render
        var $pager = $('<ul class="pages"></ul>');
		
		
        /**
		 * Check to see if first and last are to display
		 * if so create the 'first' button
		 */
		if(opts.displayFirstLast)
		{
        	$pager.append(renderButton('first', pagenumber, pagecount, buttonClickCallback));
		}
		
		/**
		 * Create the previous button
		 */
		$pager.append(renderButton(opts.prevLabel, pagenumber, pagecount, buttonClickCallback));
		
		
        // pager currently only handles 10 viewable pages ( could be easily parameterized, maybe in next version ) so handle edge cases
        var startPoint = 1;
        var endPoint = 9;

        if (pagenumber > 4) {
            startPoint = pagenumber - 4;
            endPoint = pagenumber + 4;
        }

        if (endPoint > pagecount) {
            startPoint = pagecount - 8;
            endPoint = pagecount;
        }

        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"><a>' + (page) + '</a></li>');

            page == pagenumber ? currentButton.addClass(opts.currentClass) : currentButton.click(function() { buttonClickCallback($(this).children(1).text()); });
            currentButton.appendTo($pager);
        }
		
		/**
		 * Check to see if first and last are to display
		 * if so create the 'last' button
		 */
		if(opts.displayFirstLast)
		{
        	$pager.append(renderButton('last', pagenumber, pagecount, buttonClickCallback));
		}
        
		/**
		 * Create the next button
		 */
        $pager.append(renderButton(opts.nextLabel, pagenumber, pagecount, buttonClickCallback));

        return $pager;
    }
	
	
    /**
	 * renders and returns a 'specialized' button, ie 'next', 'previous' etc. rather than a page number button
	 *
	 */
    function renderButton(buttonLabel, pagenumber, pagecount, buttonClickCallback) {

        var $Button = $('<li class=' + opts.specialClass + '><a>' + buttonLabel + '</a></li>');

        var destPage = 1;

        // work out destination page for required button type
        switch (buttonLabel) {
            case "first":
                destPage = 1;
                break;
            case opts.prevLabel:
                destPage = pagenumber - 1;
                break;
            case opts.nextLabel:
                destPage = pagenumber + 1;
                break;
            case "last":
                destPage = pagecount;
                break;
        }

        /**
		 * disable and 'grey' out buttons if not needed.
		 */
        if (buttonLabel == "first" || buttonLabel == opts.prevLabel) {
            pagenumber <= 1 ? $Button.addClass(opts.emptyClass) : $Button.click(function() { buttonClickCallback(destPage); });
        }
        else {
            pagenumber >= pagecount ? $Button.addClass(opts.emptyClass) : $Button.click(function() { buttonClickCallback(destPage); });
        }

        return $Button;
    }

    /**
	 * Modifications by tylerg@arc90.com
	 *
	 */
    jQuery.fn.pager.defaults = {
        pagenumber: 1,
        pagecount: 1,
		emptyClass: 'pgEmpty',
		currentClass: 'pgCurrent',
		specialClass: 'pgNext',
		prevLabel: 'prev',
		nextLabel: 'next',
		displayFirstLast: true
    };

})(jQuery);