(function($) {
	$.extend({
		tablesorterPager: new function() {
			
			function updatePageDisplay(c) {
			    var str = "";
			    for ( var i = 1; i <= c.totalPages; i++)
			    {
			        str = str + "<span class=\"pg\">" + i + "</span>";
			        if ( i != c.totalPages)
			        {
			        str = str + " ";
			        }
			    }
			    var s = $(c.cssPageDisplay,c.container).html(str);	
			}
			
			function setPageSize(table,size) {
				var c = table.config;
				c.size = size;
				c.totalPages = Math.ceil(c.totalRows / c.size);
				c.pagerPositionSet = false;
				moveToPage(table);
				fixPosition(table);
			}
			
			function fixPosition(table) {
				var c = table.config;
				if(!c.pagerPositionSet && c.positionFixed) {
					var c = table.config, o = $(table);
					if(o.offset) {
						c.container.css({
							top: o.offset().top + o.height() + 'px',
							position: 'absolute'
						});
					}
					c.pagerPositionSet = true;
				}
			}
			
			function moveToNextPage(table) {
				var c = table.config;
				c.page++;
				if(c.page >= (c.totalPages-1)) {
					c.page = (c.totalPages-1);
				}
				moveToPage(table);
			}
			
			function moveToPrevPage(table) {
				var c = table.config;
				c.page--;
				if(c.page <= 0) {
					c.page = 0;
				}
				moveToPage(table);
			}
			
			function moveDirectToPage(table,nbrPg) {
			    var c = table.config;
			    c.page = nbrPg;
			    c.page--;
			    moveToPage(table);
			}
			
			function moveToPage(table) {
				var c = table.config;
				if(c.page < 0 || c.page > (c.totalPages-1)) {
					c.page = 0;
				}
				$.jCache.sessionStorage.setItem('tablePageDisplay', c.page)
				$.jCache.sessionStorage.setItem('tableFromParam', window.location.search.split('&')[0]);
				renderTable(table,c.rowsCopy);
			}
			
			function renderTable(table,rows) {
				
				var c = table.config;
				var l = table.tBodies.length;
				var s = (c.page * c.size);
				var e = (s + c.size);
				if(e > rows.length ) {
					e = rows.length;
				}
				
				
				var tableBody = $(table);
				
				// clear the table body
				var head;
if ( table.config.header != null)
{
	head = table.config.header;
}
else
{
	if ($.browser.msie)
	{
		head = table.firstChild;
	}
	if( $.browser.opera ){
		head = table.tHead;
	}
	if ( $.browser.mozilla )
	{
		head = table.childNodes[1];
	}
	if ( head == null)
	{
		head = table.childNodes[1];
	}
	table.config.header = head;
}


				function empty() {
						while ( this.firstChild ) this.removeChild( this.firstChild );
					}
				empty.apply(table);
				//$.tablesorter.clearTableBody(table);
				table.appendChild(head);
				
				for(var i = s; i < e; i++) {
					
					
					var o = rows[i];
					var l = o.length;
					for(var j=0; j < l; j++) {
						
						table.appendChild(o[j]);
					}
					
				}
				
				fixPosition(table,tableBody);
				
				$(table).trigger("applyWidgets");
							
				if( c.page >= c.totalPages ) {
        			moveToLastPage(table);
				}
				if ($(c.cssPageDisplay,c.container).html() == "")
				{
				    updatePageDisplay(c);
				}
				
				for ( var z = 0; z < $(config.cssPageDisplayitem,config.cssPageDisplay).length ; z++)
				{
				    if ( $(config.cssPageDisplayitem,config.cssPageDisplay)[z].innerText == parseInt(c.page)+1 || $(config.cssPageDisplayitem,config.cssPageDisplay)[z].textContent == parseInt(c.page)+1 )
					{
					    $(config.cssPageDisplayitem,config.cssPageDisplay)[z].className = "pg red";
					}
					else
					{
					    $(config.cssPageDisplayitem,config.cssPageDisplay)[z].className = "pg";
				    }
			    }
			}
			
			this.appender = function(table,rows) {
				
				var c = table.config;
				
				c.rowsCopy = rows;
				c.totalRows = rows.length;
				c.totalPages = Math.ceil(c.totalRows / c.size);
				var oldPageInd = $.jCache.sessionStorage.getItem('tablePageDisplay');
				var oldPageParam = $.jCache.sessionStorage.getItem('tableFromParam');
				if (oldPageInd != null && oldPageInd <= c.totalPages && oldPageParam == window.location.search.split('&')[0])
				{
				    $.jCache.sessionStorage.removeItem('tablePageDisplay');
				    $.jCache.sessionStorage.removeItem('tableFromParam');
				    c.page = oldPageInd;
				    moveToPage(table);
				}
				else
				{
				    renderTable(table,rows);
				    }
			};
			
			this.defaults = {
				size: 20,
				offset: 0,
				page: 0,
				totalRows: 0,
				totalPages: 0,
				container: null,
				
				cssNext: '.next',
				
				cssPrev: '.prev',
				
				cssPageDisplay: '.pagedisplay',
				cssPageDisplayitem: '.pg',
				seperator: "/",
				positionFixed: true,
				appender: this.appender
			};
			
			this.construct = function(settings) {
				
				return this.each(function() {	
					
					config = $.extend(this.config, $.tablesorterPager.defaults, settings);
					
					var table = this, pager = config.container;
				
					$(this).trigger("appendCache");
					
					config.size = parseInt(20);
					
//					$(config.cssFirst,pager).click(function() {
//						moveToFirstPage(table);
//						return false;
//					});
					$(config.cssNext,pager).click(function() {
						moveToNextPage(table);
						return false;
					});
					$(config.cssPrev,pager).click(function() {
						moveToPrevPage(table);
						return false;
					});
					$(config.cssPageDisplayitem,config.cssPageDisplay).click(function() {
					    var nbrPg = $(this)[0].innerText;
					    if (nbrPg == null)
					    {
					        nbrPg = $(this)[0].textContent;
					    }
					    moveDirectToPage(table, nbrPg);
						return false;
					});
//					$(config.cssLast,pager).click(function() {
//						moveToLastPage(table);
//						return false;
//					});
//					$(config.cssPageSize,pager).change(function() {
//						setPageSize(table,parseInt($(this).val()));
//						return false;
//					});
				});
			};
			
		}
	});
	// extend plugin scope
	$.fn.extend({
        tablesorterPager: $.tablesorterPager.construct
	});
	
})(jQuery);				
