/*
 * * * * * * * * * * * * * * * * * * * * * *
 *	  s l a s h j q u e r  y . c  o m
 * tablePager v 1.1 - jQuery table widget
 * Copyright (c) 2008 Rasmus Styrk
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * * * * * * * * * * * * * * * * * * * * * *
 * Modified 9/23/2008 :- Made the pager and display code slightly more flexible and efficient
*/
 
(function($)
{
	$.fn.tablePager = function(options)
	{
		/*	Default values
		*/
		var defaults = {
			offset: 0, //where to start
			limit: 2, // how many rows each page
			pagerClass: '',
			pageActiveClass: 'active',
			prevText: '< Previous Page',
			nextText: 'Next Page >',
			showSurround: 2
		};

		var options = $.extend(defaults, options);

		/*	Plugin variables
		*/
		var parent = this;
		var rows = $("tbody > tr", parent);
		var totalItems = rows.length;
		var totalPages = Math.ceil((totalItems / options.limit));
		var currentPage = 1;

		/*	If the total amount of rows is bigger than the defined limit for each page, we do anything;
		*/
		if (totalItems > options.limit)
		{
			/*	Did we have more than 1 page?, if yes, create some page links ;-)
			*/
			var pages = "";
			if (totalPages > 1)
			{
				for (var i = 1; i <= totalPages; i++)
				{
					pages += "<a href='javascript://void(0);' class='page' pnum='" + i + "'>" + i + "</a> ";
				}
			}

			/*	HTML Layout for the links
			*/
			var tb = $("<div><a href='javascript://void(0);' class='previousPage'>" + options.prevText + "</a> " + pages + "<a href='javascript://void(0);' class='nextPage'>" + options.nextText + "</a></div>");
			tb.addClass("tbpPager"); // to tag the pager
			if (options.pagerClass != '')
				tb.addClass(options.pagerClass);			

			parent.before(tb.clone(true));
			parent.after(tb.clone(true));

			/*	And then we create functions for the links
			*/
			$(".previousPage").click(
				function()
				{
					options.offset = (options.offset == 0) ? (totalItems - options.limit) : (options.offset - options.limit);
					currentPage = (currentPage == 1) ? totalPages : currentPage - 1;
					parent.renderTable();
				}
			);

			$(".nextPage").click(
				function()
				{
					options.offset = ((options.offset + options.limit) == totalItems) ? 0 : (options.offset + options.limit);
					currentPage = (currentPage == totalPages) ? 1 : currentPage + 1;
					parent.renderTable();
				}
			);

			$(".page").click
			(
				function()
				{
					var pnum = $(this).attr("pnum");
					options.offset = (pnum - 1) * options.limit;
					currentPage = currentPage - (currentPage - pnum);
					parent.renderTable();
				}
			);
		}

		/*	And finally we try to sort out witch rows should be visible ;-)
		*/
		$.fn.renderTable = function()
		{
			var currentItem = 0;

			var pager = $(".tbpPager");
			$("*", pager).removeClass(options.pageActiveClass);
			$("a[pnum=" + currentPage + "]", pager).addClass(options.pageActiveClass);

			var start = Math.min(Math.max(currentPage - options.showSurround, 1), totalPages - 1 - (options.showSurround * 2));
			var end = Math.min(Math.max(1 + (options.showSurround * 2), currentPage + options.showSurround), totalPages);
			$("a[pnum]", pager).hide().each(function()
			{
				var pnum = $(this).attr("pnum");
				if (pnum >= start && pnum <= end)
					$(this).show();
			});

			/*	So, if we are at last page we hide the next link ;-)
			*/
			if (currentPage == totalPages)
			{
				$(".nextPage").hide();
			}
			else
			{
				$(".nextPage").show();
			}

			/*	If we are at first page we hide the prev link
			*/
			if (currentPage == 1)
			{
				$(".previousPage").hide();
			}
			else
			{
				$(".previousPage").show();
			}

			/*	Update: use selectors to determine which rows to show
			*/
			$("tbody > tr", this).show();
			$("tbody > tr:lt(" + options.offset + ")", this).hide();
			$("tbody > tr:gt(" + (options.offset + options.limit) + ")", this).hide();
		}

		/*	Initialize on page load
		*/
		this.renderTable();
	};
})(jQuery);  