(function($) {
	
	$.fn.quickPager = function(options) {
	
		var page_hash = document.location.hash.split('-');
		var page = page_hash[1] ? page_hash[1] : 1;
				
		var defaults = {
			pageSize: 10,
			currentPage: page,
			holder: null,
			pagerLocation: "after"
		};
		
		var options = $.extend(defaults, options);
		

		
		setInterval( function () {
			var newHash = document.location.hash.split('-');
			if (page_hash[1] != newHash[1]) {
				page_hash[1] = newHash[1];
				
				if (document.location.hash != '') {
					var obj = $('.simplePagerContainer').eq(0).find('a').filter("[href$=" + document.location.hash + "]");					
					obj.click();
				} else {
					var selector2 = $('.simplePagerContainer > .categoryView');
					selector2.children().hide();			
					selector2.find(".simplePagerPage1").show();
					
					var selector3 = $('.simplePagerContainer .pagination');
					selector3.find("li").removeClass("selected");
					selector3.find('.simplePageNav1').addClass('selected');
					selector3.find("li.first").fadeTo("slow", 0.2);
					selector3.find("li.prev").fadeTo("slow", 0.2);
					selector3.find("li.prev a").attr('rel',  1);
					selector3.find("li.next").fadeTo("slow", 1);
					selector3.find("li.last").fadeTo("slow", 1);
					selector3.find("li.next a").attr('rel', 2);
					/*
					selector.children().hide();			
					selector.find(".simplePagerPage"+clickedLink).show();
					*/
				}
			} 
		} , 100);
		
		
		
		return this.each(function() {
	
						
			var selector = $(this);	
			var pageCounter = 1;
			
			selector.wrap("<div class='simplePagerContainer'></div>");
			
			
			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 ++;
				}	
				
			});
			options.pageCounter = pageCounter;
			// show/hide the appropriate regions 
			selector.children().hide();
			selector.children(".simplePagerPage"+options.currentPage).show();
			
			if(pageCounter <= 1) {
				return;
			}
			
			//Build pager navigation
			var pageNav = "<ul class='pagination'>";
			pageNav += "<li class='simplePageNav first'><a rel='1' href='#page-1'>&laquo;</a></li>";
			pageNav += "<li class='simplePageNav prev'><a href='#'>&lt;</a></li>";
			for (i=1;i<=pageCounter;i++){
				if (i==options.currentPage) {
					pageNav += "<li class='selected simplePageNav"+i+"'><a rel='"+i+"' href='#page-"+i+"'>"+i+"</a></li>";	
				}
				else {
					pageNav += "<li class='simplePageNav"+i+"'><a rel='"+i+"' href='#page-"+i+"'>"+i+"</a></li>";
				}
			}
			pageNav += "<li class='simplePageNav next'><a href='#'>&gt;</a></li>";
			pageNav += "<li class='simplePageNav"+pageCounter+" last'><a rel='"+pageCounter+"' href='#page-" + pageCounter + "'>&raquo;</a></li>";
			pageNav += "</ul><div class='clear'></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);
			}
			
			var node = $(".simplePagerContainer .pagination");
			
			node.find("li.first").fadeTo("slow", 0.2);
			node.find("li.prev").fadeTo("slow", 0.2);
			
			if (options.currentPage>1) {
				node.find("li.prev a").attr('rel',  parseInt(options.currentPage)-1);
			} else {
				node.find("li.prev a").attr('rel',  1);
			}
			if (options.currentPage <= options.pageCounter) {
				node.find("li.next a").attr('rel', parseInt(options.currentPage)+1);
			}
			if (options.currentPage <= options.pageCounter) {
				node.find("li.next a").attr('rel', options.pageCounter);
			}
			
			$('.pagination').css('width', (options.pageCounter+4)*21);
			//$('.pagination').css('margin', '0 auto');
			
			
			//pager navigation behaviour
			selector.parent().find(".pagination a").click(function() {
					
				//grab the REL attribute 
				var clickedLink = $(this).attr("rel");
				options.currentPage = clickedLink;
				//alert(options.pageCounter);
				if(options.holder) {
					$(this).parent("li").parent("ul").parent(options.holder).find("li.selected").removeClass("selected");
					$(this).parent("li").parent("ul").parent(options.holder).find("a[rel='"+clickedLink+"']").parent("li").addClass("selected");
				}
				else {
					//remove current current (!) page
					$(this).parent("li").parent("ul").parent(".simplePagerContainer").find("li.selected").removeClass("selected");
					//Add current page highlighting
					$(this).parent("li").parent("ul").parent(".simplePagerContainer").find("a[rel='"+clickedLink+"']").parent("li").addClass("selected");
				}
				var node = $(this).parent("li").parent("ul").parent(".simplePagerContainer").find('.pagination');
				if (options.currentPage>1) {
					node.find("li.first").fadeTo("slow", 1);
					node.find("li.prev").fadeTo("slow", 1);
					node.find("li.prev a").attr('rel',  parseInt(options.currentPage)-1);
				} else {
					node.find("li.first").fadeTo("slow", 0.2);
					node.find("li.prev").fadeTo("slow", 0.2);
					node.find("li.prev a").attr('rel',  1);
				}
				if (options.currentPage < options.pageCounter) {
					node.find("li.next").fadeTo("slow", 1);
					node.find("li.last").fadeTo("slow", 1);
					node.find("li.next a").attr('rel', parseInt(options.currentPage)+1);
				} else {
					node.find("li.next").fadeTo("slow", 0.2);
					node.find("li.last").fadeTo("slow", 0.2);
					node.find("li.next a").attr('rel', options.pageCounter);
				}
								
				//hide and show relevant links
				selector.children().hide();			
				selector.find(".simplePagerPage"+clickedLink).show();
				
				
				
				
				document.location.hash= $(this).attr('href');				
				return false;
			});
		});
	}
	

})(jQuery);


