$.fn.pager = function(clas, options) {

	var settings = {		
		navId: 'nav',
		navClass: 'nav',
		navAttach: 'append',
		highlightClass: 'highlight',
		prevText: '&laquo;',
		nextText: '&raquo;',
		linkText: null,
		linkWrap: null,
		height: null,
		showAll: false,
		showAllDivider: '|',
		showAllText: 'Show all items',
		showPage: false,
		showPageDynamic: true,
		showPageText: 'View %p pages',
		useFade: false
	}
	
	if(options) $.extend(settings, options);
		
	return this.each( function () {
		var me = $(this);
		var size;
		var i = 0;		
		var navid = '#'+settings.navId;
		
		function init () {
			size = $(clas, me).not(navid).size();
			if(settings.height == null) {			
				settings.height = getHighest();
			}
			if(size > 1) {
				makeNav();
				if(settings.showAll) {
					$('#'+settings.navId+'ShowPage').hide();
				}
//				showPage();
				$(clas+':visible').hide();
				$(clas+':nth('+i+')').show();

				highlight();
			}			
			sizePanel();
			if(settings.linkWrap != null) {
				linkWrap();
			}
		}

		function makeNav () {		
			var str = '';
			
			str += '<div id="'+settings.navId+'" class="'+settings.navClass+'">';
			
			if(settings.showAll) {
				str += '<span id="'+settings.navId+'ShowAll">';
				str += '<a href="#" rel="showAll" class="'+settings.navId+'ShowAllText">'+settings.showAllText+'</a>';
				str += '<span id="'+settings.navId+'ShowAllDivider">'+settings.showAllDivider+'</span>';
			}
			
			str += '<a href="#" rel="prev" class="'+settings.navId+'PrevText">'+settings.prevText+'</a>';
			for(var i = 0; i < size; i++) {
				var j = i+1;
				str += '<a href="#" rel="'+j+'" class="'+settings.navId+'LinkText">';
				str += (settings.linkText == null) ? j : settings.linkText[j-1];				
				str += '</a>';
			}
			str += '<a href="#" rel="next" class="'+settings.navId+'NextText">'+settings.nextText+'</a>';

			if(settings.showAll) {
				str += '</span>';
				if(settings.showPage) {
					str += '<span id="'+settings.navId+'ShowPage">';
					str += '<a href="#" rel="showPage" class="'+settings.navId+'ShowPageText">'+((settings.showPageDynamic) ? settings.showPageText.replace(/%p/, size) : settings.showPageText.replace(/%p /, ''))+"</a>" ;
					str += '</span>';
				}
			}
			
			str += '</div>';

			switch (settings.navAttach) {		
				case 'before':
					$(me).before(str);
					break;
				case 'after':		
					$(me).after(str);
					break;
				case 'prepend':
					$(me).prepend(str);
					break;
				default:
					$(me).append(str);
					break;
			}
		}

		function showPage () {
			if(settings.useFade) {
				$(clas+':visible').fadeOut('normal', function(){$(clas+':nth('+i+')').fadeIn('normal');});
			}
			else {
				$(clas+':visible').hide();
				$(clas+':nth('+i+')').show();
			}
		}		
		
		function showAll () {
		}

		function highlight () {
			$('a.'+settings.highlightClass).removeClass(settings.highlightClass);
			$('a.'+settings.navId+'LinkText[@rel='+(i+1)+']').addClass(settings.highlightClass);
		}

		function sizePanel () {
			if($.browser.msie) {
				$(me).find(clas).not(navid).css( {
					height: settings.height
				});	
			} else {
				$(me).find(clas).not(navid).css( {
					minHeight: settings.height
				});
			}
		}
		function getHighest () {
			var highest = 0;
			$(me).find(clas).not(navid).each(function () {
				
				if(this.offsetHeight > highest) {
					highest = this.offsetHeight;
				}
			});
			highest = highest + "px";
			return highest;
		}
		function getNavHeight () {
			var nav = $(navid).get(0);
			return nav.offsetHeight;
		}
		function linkWrap () {
			$(me).find(navid).find("a").wrap(settings.linkWrap);
		}

		init();

		$(navid+' a').click(function () {
			var linkRel = $(this).attr('rel');
			
			switch (linkRel) {
				case 'prev':
					if(i > 0) {	
						i = i-1;
					}
					showPage();
					highlight();
					break;
				case 'next':
					if(i + 1 < size) {
						i = i+1;
					}
					showPage();
					highlight();
					break;
				case 'showAll':
					$('#'+settings.navId+'ShowAll').hide();
					$('#'+settings.navId+'ShowPage').show();
					if(settings.useFade) {
						$(clas+':visible').fadeOut('normal', function () {$(clas).fadeIn('normal');});
					}
					else {
						$(clas).show();
					}
	//				showAll();
					break;
				case 'showPage':
					$('#'+settings.navId+'ShowPage').hide();
					$('#'+settings.navId+'ShowAll').show();
					if(settings.useFade) {
//						$(clas).fadeOut('normal', function () {$(clas+':nth('+i+'):hidden').fadeIn('normal');});
						$(clas).fadeOut('normal', function () {$(clas+':nth('+i+')').filter(':hidden').fadeIn('normal');});
					}
					else {
						$(clas+':visible').hide();
						$(clas+':nth('+i+')').show();
					}

					//showPage();
					highlight();
					break;
				default:
					var j = linkRel;	
					i = j-1;		
					showPage();
					highlight();
					break;
			}

		return false;
		
		});
	});	
}