/*
 * jQuery Pagination Plugin 1.0
 *
 * Copyright (c) 2007 Jesse Skinner (thefutureoftheweb.com)
 *
 */
(function($){
	$.fn.gt = function(num) {
		return this.filter(':gt(' + parseInt(num) + ')');
	};
	$.fn.lt = function(num) {
		return this.filter(':lt(' + parseInt(num) + ')');
	};

	$.fn.paginate = function(options) {
		options = $.extend({
			per_page: 10,
			get_page_number: function(num) {
				return num
			},
			selected_link_class: 'selected',
			page_link_class: 'page-link',
			control_class: 'pagination',
			num_of_total_class: 'num-of-total',
			previous_class: 'previous',
			next_class: 'next',			
			onafter: function(){},
			get_link: function(hash){
				return $('<a href="#' + hash + '"/>');
			},
			get_num_of_total: function(from, to, total){
				return ''+from+" to "+to+" of "+total+' Results';
			},
			add_controls_before: true,
			add_controls_after: true,
			next_label: 'Next',
			previous_label: 'Previous'
		}, options);

		this.each(function(){
			var list = $(this),
				items = $('>', list),
				num_pages = Math.ceil(items.length / options.per_page),
				current_page = 1;

			if (num_pages <= 1) return;
			
			items.gt(options.per_page - 1).hide();

			function page (num) {
				current_page = num;
				
				var start = (num - 1) * options.per_page;
					
				var show_items = items.gt(start - 1).lt(options.per_page);
				
				show_items.show();
				items.not(show_items).hide();
				
				if (list.is('ol, ul, dd')) {
					list.attr('start', start + 1);
				}

				var controls = $('div.' + options.control_class, list.parent());

				// change the numth link classes
				var links = controls.children('.' + options.page_link_class + ':nth-child(' + (num + 2) + ')');
				
				links.show()
					 .addClass(options.selected_link_class)
					 .siblings().removeClass(options.selected_link_class);

				// only show 10 links at a time
				var show_to = Math.min(num + 4, num_pages),
					show_from = Math.max(show_to - 9, 1);
				if (show_from == 1) {
					show_to = Math.min(show_from + 9, num_pages)
				}
				links.siblings('.' + options.page_link_class).hide();
				for (var i=show_from;i <= show_to;i++) {
					links.siblings(':nth-child(' + (i + 2) + ')').show();
				}

				var previous = controls.children('.' + options.previous_class),
					next = controls.children('.' + options.next_class);

				previous.show();
				next.show();
				
				if (num == 1) {
					previous.hide();
				} else if (num == num_pages) {
					next.hide();
				}
				
				var num_of_total = $('span.' + options.num_of_total_class, controls);
				
				num_of_total.text(options.get_num_of_total(start + 1, Math.min(items.length, start + options.per_page), items.length));
			}

			function get_controls(){
				var controls = $('<div>').addClass(options.control_class)
					num_of_total = $('<span/>')
										.addClass(options.num_of_total_class)
										.text(options.get_num_of_total(1, Math.min(items.length, options.per_page), items.length))
										.appendTo(controls);

				// add 'Previous' link, but hidden at the start
				options.get_link('previous')
					.addClass(options.previous_class)
					.text(options.previous_label)
					.click(function() {
						page(current_page - 1);
						return false;
					})
					.hide()
					.appendTo(controls);

				for (var i=1;i <= num_pages;i++) {
					(function(num){
						var link = options.get_link('page-' + num);

						link.text('' + options.get_page_number(num))
							.addClass(options.page_link_class)
							.click(function(){
								page(num);
								return false;
							})
							.appendTo(controls);
						
						if (i == 1) link.addClass(options.selected_link_class);
						
						if (i > 10) link.hide();
					})(i);
				}

				// add 'Next' link, visible from the start
				var n = options.get_link('next')
					.addClass(options.next_class)
					.text(options.next_label)
					.click(function() {
						page(current_page + 1);
						return false;
					})
					.appendTo(controls);

												
		
				 
				 
				return controls;
			}
			
			var controls_before = get_controls(),
				controls_after = get_controls();

			if (options.add_controls_before) {
				controls_before.insertBefore(list);
			}
			
			if (options.add_controls_after)
				controls_after.insertAfter(list);
			
			options.onafter(list, controls_before, controls_after);
			

			
		});
	};
})(jQuery);
