//-------------------------------------------------
//		Quick Pager jquery plugin
//		Created by dan and emanuel @geckonm.com
//		www.geckonewmedia.com
// 
//
//		18/09/09 * bug fix by John V - http://blog.geekyjohn.com/
//		1.2 - allows reloading of pager with new items
//-------------------------------------------------

(function ($) {

    $.fn.quickPager = function (options) {

        var defaults = {
            pageSize: 5,
            currentPage: 1,
            holder: null,
            pagerLocation: "after"
        };

        var options = $.extend(defaults, options);
        return this.each(function () {
            var selector = $(this);
            var pageCounter = 1;
            selector.wrap("<div class='simplePagerContainer'></div>");
            selector.parents(".simplePagerContainer").find("ul.simplePagerNav").remove();
            selector.children().each(function (i) {
                if (i < pageCounter * options.pageSize && i >= (pageCounter - 1) * options.pageSize) {
                    $(this).addClass("simplePagerPage" + pageCounter);
                }
                else {
                    $(this).addClass("simplePagerPage" + (pageCounter + 1));
                    pageCounter++;
                }
            });

            // show/hide the appropriate regions 
            selector.children().hide();
            selector.children(".simplePagerPage" + options.currentPage).show();
            if (pageCounter <= 1) {
                return;
            }

            //Build pager navigation
            var PrevPage = options.currentPage - 1;
            var NextPage = options.currentPage + 1;

            var pageNav = "<div class='pagination-bottom'><ul class='simplePagerNav group'>";
            pageNav += '<li class="prev"><a rel="" style="display:none" href="">Prev</a></li>';
            for (i = 1; i <= pageCounter; i++) {
                if (i == options.currentPage) {
                    pageNav += "<li class='currentPage minwidth simplePageNav" + i + "'><a rel='" + i + "' href='#'>" + i + "</a></li>";
                }
                else {
                    pageNav += "<li class='minwidth simplePageNav" + i + "'><a rel='" + i + "' href='#'>" + i + "</a></li>";
                }
            }
            pageNav += '<li class="next"><a rel="' + NextPage + '"href="">Next</a></li></ul></div>';

            if (!options.holder) {
                switch (options.pagerLocation) {
                    case "before":
                        selector.before(pageNav);
                        break;
                    case "both":
                        selector.before(pageNav);
                        selector.after(pageNav);
                        break;
                    default:
                        selector.after(pageNav);
                }
            }
            else {
                $(options.holder).append(pageNav);
            }

            //pager navigation behaviour
            selector.parent().find(".simplePagerNav a").click(function () {
                //grab the REL attribute 
                var clickedLink = $(this).attr("rel");
                options.currentPage = clickedLink;

                $("ul.simplePagerNav li.currentPage").removeClass("currentPage");
                $("ul.simplePagerNav li.simplePageNav" + options.currentPage).addClass("currentPage");

                //hide and show relevant links
                selector.children().hide();
                selector.find(".simplePagerPage" + clickedLink).show();

                var PrevPage = parseInt(options.currentPage) - 1;
                var NextPage = parseInt(options.currentPage) + 1;

                $("li.prev a").css("display", "inline");
                $("li.next a").css("display", "inline");

                if (PrevPage == 0) {
                    $("li.prev a").css("display", "none");
                }
                else {
                    $("li.prev a").attr("rel", PrevPage);
                }

                if (NextPage == pageCounter + 1) {
                    $("li.next a").css("display", "none");
                }
                else {
                    $("li.next a").attr("rel", NextPage);
                }
                return false;
            });
        });
    }
})(jQuery);
