;(function($) {
	$.fn.pagination = function(options) {
		
	  var settings = $.extend(true, {}, $.fn.pagination.defaults, options);

		return this.each(function() {
			var $this = $(this);
			var $items = $(settings.listSelector + " li a[rel!='next'][rel!='prev']");
			var $active = $("a.inactive", $items.parent());
			var $prev = $(settings.listSelector + " li a[rel='prev']");
			var $next = $(settings.listSelector + " li a[rel='next']");
			
			$items.live("click", function(e) {				
				var $this = $(this);
				if (!$this.hasClass("inactive")) {
					if (settings.reset) $this.resetPagination($this.text());
					settings.onChange.call(this);
				}
				return false;
			});
			
			$prev.live("click", function() {
				var $this = $(this);
				if (!$this.hasClass("inactive")) settings.onChange.call(this);
				return false;
			});
			
			$next.live("click", function() {
				var $this = $(this);
				if (!$this.hasClass("inactive")) settings.onChange.call(this);
				return false;
			});
		});
	};
	
	$.fn.resetPagination = function(requested) {
		return this.each(function() {
			var $this = $(this);
			var $items = $("li a[rel!='next'][rel!='prev']", $this);
			var $prev = $("li a[rel='prev']", $this);
			var $next = $("li a[rel='next']", $this);
			
			var page = requested;
			var pages = $items.length;
			
			$items.each(function() {
				var $this = $(this);
				if (Number($this.text()) == page) $this.addClass("inactive");
				else $this.removeClass("inactive");
			});
		});
	};

	$.fn.pagination.defaults = {
		onChange:function() {},
		listSelector:"div.main ul.pagination",
		reset: false
	};

})(jQuery);