(function($) {
    $.fn.quickPager = function(options) {
        var defaults = {
            pageSize: 10,
            currentPage: 1
        };
        var options = $.extend(defaults, options);

        return this.each(function() {
            var selector = $(this);
            var pageCounter = 1;

            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;

            //Building pager html
            var pageNav = '<a href="#" rel="prev"><span>Forrige</span></a>';
            for (i = 1; i <= pageCounter; i++) {
                if (i === options.currentPage) { //first
                    pageNav += '<a class="active" href="#" rel="num' + i + '"><span>' + i + '</span></a>';
                    pageNav += '<a href="#" class="PagingSpacer" rel="firstdots"><span>...</span></a>';
                }
                else if (i === pageCounter) { //last
                    if (pageCounter > 4)
                        pageNav += '<a href="#" class="PagingSpacer" rel="lastdots"><span>...</span></a>';
                    pageNav += '<a href="#" rel="num' + i + '"><span>' + i + '</span></a>';
                }
                else
                    if (i < 4)
                    pageNav += '<a href="#" rel="num' + i + '"><span>' + i + '</span></a>';
                else
                    pageNav += '<a href="#" rel="num' + i + '"><span>' + i + '</span></a>';
            }
            pageNav += '<a href="#" rel="next"><span>N&aelig;ste</span></a>';
            
						$('div.Paging').each(function() { 
							$(this).html(pageNav)
						});					
            $('div.Paging a').click(function() { //Paging-links onclick event
                var rel = $(this).attr('rel');
                if (rel === options.currentPage) return;
                switch (rel) {
                    case 'firstdots':
                        //nothing
                        break;
                    case 'lastdots':
                        //nothing
                        break;
                    case 'prev':
                        if (options.currentPage > 1) {
                            selector.find(".simplePagerPage" + options.currentPage).hide();
                            options.currentPage--;
                            selector.find(".simplePagerPage" + options.currentPage).show();
                        }
                        break;
                    case 'next':
                        if (options.currentPage < pageCounter) {
                            selector.find(".simplePagerPage" + options.currentPage).hide();
                            options.currentPage++;
                            selector.find(".simplePagerPage" + options.currentPage).show();
                        }
                        break;
                    default:
                        selector.find(".simplePagerPage" + options.currentPage).hide();
                        options.currentPage = parseInt(rel.substr(3));
                        selector.find(".simplePagerPage" + options.currentPage).show();
                        break;
                }
								selector.find(".first").removeClass("first")
								selector.find(".simplePagerPage" + options.currentPage+":first").addClass("first")
								
                $('div.Paging a.active').removeClass("active");
                $('div.Paging a[rel="num' + options.currentPage + '"]').addClass("active");

                $('div.Paging a[rel^="num"][rel!="num1"][rel!="num' + pageCounter + '"]').each(function() { $(this).hide(); });
                if (options.currentPage > 3)
                    $('div.Paging a[rel="firstdots"]').show();
                else
                    $('div.Paging a[rel="firstdots"]').hide();
                if (pageCounter - options.currentPage > 2)
                    $('div.Paging a[rel="lastdots"]').show();
                else
                    $('div.Paging a[rel="lastdots"]').hide();

                if (options.currentPage === pageCounter) $('div.Paging a[rel="num' + (options.currentPage - 2) + '"]').show();
                $('div.Paging a[rel="num' + (options.currentPage - 1) + '"]').show();
                $('div.Paging a[rel="num' + (options.currentPage) + '"]').show();
                $('div.Paging a[rel="num' + (options.currentPage + 1) + '"]').show();
                if (options.currentPage === 1) $('div.Paging a[rel="num' + (options.currentPage + 2) + '"]').show();

                return false;
            });
						$('div.Paging a').eq(options.currentPage - 1).click();
        });
    }
})(jQuery);

