(function($j) {

    $j.fn.quickPager = function(options) {

        var defaults = {
            pageSize: 10,
            currentPage: 1,
            holder: ""
        };
        var options = $j.extend(defaults, options);

        var selector = $j(this);
        var totalRecords = $j(this).children().length;
        var pageCounter = 1;
        var totalPages = (totalRecords-(totalRecords % options.pageSize))/options.pageSize;

        if(totalRecords % options.pageSize == 0) {
            lastPageEmpty = true
        } else {
            lastPageEmpty = false;
        }

        selector.children().each(function(i){
            if(i < pageCounter*options.pageSize && i >= (pageCounter-1)*options.pageSize) {
                $j(this).addClass("page"+pageCounter);
            }
            else {
                $j(this).addClass("page"+(pageCounter+1));
                pageCounter ++;
            }
        });

        // Show/hide the appropriate regions.
        selector.children().hide();
        $j(".page"+options.currentPage).show();

        // First check if there is more than one page. If so, build navigation.
        if(pageCounter > 1) {

            // Build pager navigation.
			var pageNav = "<div class='pageNav'>";
            pageNav += "<span id=\"previousgrey\" class=\"previous\">"+options.label_previous4+"</span>";
            pageNav += "<a id=\"previouslink\" class=\"previous\" rel='1' href=\"#\">"+options.label_previous4+"</a>";
            pageNav += "|<a id=\"nextlink\" class=\"next\" rel='1' href=\"#\">"+options.label_next4+"</a>";
            pageNav += "<span id=\"nextgrey\" class=\"next\">"+options.label_next4+"</span>";
			pageNav += "</div>";

            if(options.holder == "") {
                selector.after(pageNav);
            }
            else {
                $j(options.holder).append(pageNav);
            }

            // Pager navigation behaviour.
            $j(".pageNav a").live("click", function() {
                //grab the REL attribute
                var clickedLink = $j(this).attr("rel");
                options.currentPage = clickedLink;
                //remove current current (!) page
                $j("span.currentPage").removeClass("currentPage");

                //hide and show relevant links
                selector.children().hide();
                selector.find(".page"+clickedLink).show();
                return false;
            });

            // Update previous and next links when clicking previous.
            $j("#previouslink").click(function() {
                var linkPageNumber = parseInt($j(this).attr("rel"));
                if(linkPageNumber > 1) {
                    $j(this).attr("rel",linkPageNumber-1);
                    $j("#nextlink").attr("rel",parseInt($j("#nextlink").attr("rel"))-1);
                }
                if(linkPageNumber <= 2) {
                    $j(this).hide();
                    $j('#previousgrey').show();
                }

                $j('#nextlink').show();
                $j('#nextgrey').hide();
            });

            // Update previous and next links when clicking next.
            $j("#nextlink").click(function(){
                var linkPageNumber = parseInt($j(this).attr("rel"));
                if(linkPageNumber <= totalPages) {
                    $j(this).attr("rel",parseInt($j(this).attr("rel"))+1);
                    $j("#previouslink").attr("rel",parseInt($j("#previouslink").attr("rel"))+1);
                }
                if(lastPageEmpty) {
                    if(linkPageNumber >= totalPages - 1) {
                        $j(this).hide();
                        $j('#nextgrey').show();
                    }
                } else {
                    if(linkPageNumber >= totalPages) {
                        $j(this).hide();
                        $j('#nextgrey').show();
                    }
                }

                $j('#previouslink').show();
                $j('#previousgrey').hide();
            });

        }

    }


})(jQuery);
