/**
*
*	Table Pager module by Marius ILIE	v1.0
*	http://dev.mariusilie.net/content/table-pager
*  plus extras AF
**/
(function($){ $.fn.addTablePager = function(options){
	return this.each(function() {
		var defaults = {
			id		 	: 'table-pager',
			results 	: 10,
			position	: 'top',
			firstBut	: '<img src="./images/recnav/start.gif" width="30" height="20" border="0" title="first">',
			prevBut	: '<img src="./images/recnav/back.gif" width="30" height="20" border="0" title="previous">',
			nextBut	: '<img src="./images/recnav/next.gif" width="30" height="20" border="0" title="next">',
			lastBut	: '<img src="./images/recnav/end.gif" width="30" height="20" border="0" title="last">',
			infos		: '#1 of #2'
		};
		var opts = $.extend(defaults, options);
		var table = this;
		$(table).wrap('<div></div>');
		if(opts.position == 'top')
			$(table).before('<div id="' + opts.id + '-links" class="ui-corner-all table-pager"></div>');
		else 
			$(table).after('<div id=' + opts.id + '-links" class="ui-corner-all table-pager"></div>');
		var container = $(table).parent();
		table.page = 0;
		var maxRows = $('tbody > tr', table).length;
		var totalPages = Math.ceil(maxRows / opts.results);
		pagerInfos = opts.infos.split('#1').join(table.page + 1).split('#2').join(totalPages);
		$('div#' + opts.id + '-links', container).html('<a href="#" class="' + opts.id + '-first-but">' + opts.firstBut + '</a> <a href="#" class="' + opts.id + '-prev-but">' + opts.prevBut + '</a> <span class="' + opts.id + '-infos"></span> <a href="#" class="' + opts.id + '-next-but">' + opts.nextBut + '</a> <a href="#" class="' + opts.id + '-last-but">'+opts.lastBut+'</a>');
		$('div#' + opts.id + '-links > span.' + opts.id + '-infos', container).html(pagerInfos);
		$('tbody > tr', table).hide();
		for(var i = table.page * opts.results + 1; i <= table.page * opts.results + opts.results; i++) {
			$('tr:nth-child(' + i + ')', table).show();
		}
		$('a.' + opts.id + '-last-but', container).click(function(){
			if(table.page < totalPages - 1) {
				table.page = totalPages - 1;
				movePage();
			}
			return false;
		})
		$('a.' + opts.id + '-next-but', container).click(function(){
			if(table.page < totalPages - 1) {
				table.page++;
				movePage();
			}
			return false;
		})
		$('a.' + opts.id + '-prev-but', container).click(function(){
			if(table.page > 0) {
				table.page--;
				movePage();
			}
			return false;
		})
		$('a.' + opts.id + '-first-but', container).click(function(){
			if(table.page > 0) {
				table.page = 0;
				movePage();
			}
			return false;
		})
		
		function movePage(){
			var pagerInfos = opts.infos.split('#1').join(table.page + 1).split('#2').join(totalPages);
			$('div#' + opts.id + '-links > span.' + opts.id + '-infos', container).html(pagerInfos);
			$('tbody > tr', table).hide();
			for(var i = table.page * opts.results + 1; i <= table.page * opts.results + opts.results; i++) {
				$('tbody > tr:nth-child(' + i + ')', table).show();
			}
		}
	});
}})(jQuery);
