(function($) {
	$.fn.slider = function(settings) {
		settings = jQuery.extend({
					animationSpeed: 'fast', /* fast/slow/normal */
					itemsPerPage: 3,
					paging: true,
					orientation: 'horizontal' /* vertical/horizontal */
				}, settings);

		// Calculate the max height.
		maxHeight = 0;				

		$(this).each(function(){
			maxHeight = 0;		
			
			$(this).find('li').each(function(){ maxHeight = ($(this).height() > maxHeight) ? $(this).height() : maxHeight; });
		
			var $listToSlide = $(this);
			var currentPage = 1;
		
			// Fix the height
			$listToSlide.find('li').css({'overflow':'hidden','height':maxHeight});
	
			$listToSlide.find('li').click(function(){
				location.href = $(this).find('a:first').attr('href');
			});
		
			if (settings.paging) {
				
				var pageCount = Math.ceil($listToSlide.find('>li').size() / settings.itemsPerPage);
				
				if(settings.orientation == 'horizontal'){
					var itemDimension = $listToSlide.find('li:first').width() + parseFloat($listToSlide.find('li:first').css('marginRight'));				
					// Fix the lists height
					$listToSlide.css({'height':$listToSlide.find('li:first').height(),'width':$listToSlide.width(),'overflow':'hidden','position':'relative'});
			
					// Fix the list
					$listToSlide.find('>li').each(function(i){
						$(this).css({
							'position':'absolute',
							'left': (i*itemDimension)+1,
							'top':0
						});
					});
				}else{
					var itemDimension = $listToSlide.find('li:first').height() + parseFloat($listToSlide.find('li:first').css('marginBottom'));
					
					// Fix the list
					$listToSlide.find('>li').each(function(i){
						$(this).css({
							'position':'absolute',
							'left': 0,
							'top':i*itemDimension
						});
					});
				}
				
				var pageDimension = itemDimension * settings.itemsPerPage;
				
				// If no nav needed, remove it
				if(pageCount <= 1){ $listToSlide.prev().find('ul:first').remove(); return; }
			
				// Find the paging nav
				$paging = $listToSlide.prev().find('ul ul:first');
			
				if(pageCount > 1) {
					// Put the links in
					toInject = "";
					for(var i=1, pages = pageCount+1; i < pages; i++){
						toInject +=  "<li><a href='#'>"+i+"</a></li>";
					}
					$paging.html(toInject).find('li:first').addClass('selected');
					
					pageLinkWidth = $paging.find('li:first a:first').width() + parseFloat($paging.find('li:first a:first').css('marginRight'));
			
					// Fix the width for IE6
					$paging.parent().width((pageCount * pageLinkWidth) + parseFloat($paging.css('paddingLeft')) + parseFloat($paging.css('marginRight')));
			
					// Bind the functions
					$paging.find('a').each(function(i){
						$(this).click(function(){
							_goToPage(this,i + 1);
							return false;
						});
					});
			
					$paging.parents('ul:first').find('a.b_next').click(function(){
						_goToPage(this,'up');
						return false;
					});
			
					$paging.parents('ul:first').find('a.b_prev').click(function(){
						_goToPage(this,'down');
						return false;
					});
			
					_selectItem(null,currentPage);
				}
			}else{
				$listToSlide.prev().find('ul:first').remove();
			};
		
			function _selectItem(caller,which) {
				if($(caller).hasClass('disabled')) return;

				// Find the paging nav
				$paging = $listToSlide.prev().find('ul ul:first');

				// Un-select the current item
				$paging.find('li.selected').removeClass('selected');

				// Select the proper item
				$paging.find('li:eq('+(which-1)+')').addClass('selected');

				// Disable the next/previous if needed.
				$paging.parents('ul:first').find('.disabled').removeClass('disabled');
				if(currentPage == 1){
					$paging.parents('ul:first').find('a.b_prev').addClass('disabled');
				}else if(currentPage == pageCount){
					$paging.parents('ul:first').find('a.b_next').addClass('disabled');
				};
			};
		
			function _goToPage(caller,which) {
				if($(caller).hasClass('disabled')) return;

				// Define where and how to move.
				if(which == 'up'){
					currentPage++;
				}else if(which == 'down'){
					currentPage--;
				}else{
					currentPage=which;
				}

				// Slide the page
				$listToSlide.find('>li').each(function(i){
					if(settings.orientation == 'horizontal'){
						$(this).stop().animate({'left': ((i * itemDimension)+1) + (pageDimension * -(currentPage-1))});
					}else{
						$(this).stop().animate({'top': (i * itemDimension) + (pageDimension * -(currentPage-1))});
					}
				});

				// Select the item
				_selectItem(caller,currentPage);
			};
		});
	}
})(jQuery);
