$.fn.slider = function(settings) {
	settings = jQuery.extend({
				animationSpeed: 'fast', /* fast/slow/normal */
				itemsPerPage: 3,
				paging: false,
				hoverHeight: 100,
				outHeight: 152
			}, settings);

	// Calculate the max height.
	maxHeight = 0;
	$(this).each(function(){ $(this).find('li div.content').each(function(){ maxHeight = ($(this).height() > maxHeight) ? $(this).height() : maxHeight; }); });

	$(this).each(function(){
		
		var $listToSlide = $(this);
		var currentPage = 1;
		
		// Fix the height
		$listToSlide.find('li div.content').css({'overflow':'hidden','height':maxHeight});
	
		$listToSlide.find('li').hover(
			function(){
				$(this).find('.thumbnail').css('overflow','hidden').stop().fadeTo(125,outOpacity,function(){
					$(this).parents('li:first').addClass('selected');
					var h = (settings.hoverHeight - $(this).siblings('h5').height());
					
					if($('body').hasClass('light')){
						$(this).animate({height:h},settings.animationSpeed,function(){
							$(this).parent().find('.desc,ul.tools').css('opacity',0).show().fadeTo(settings.animationSpeed,hoverOpacity);
						});
					}else{
						$(this).animate({opacity:hoverOpacity,height:h},settings.animationSpeed,function(){
							$(this).parent().find('.desc,ul.tools').css('opacity',0).show().fadeTo(settings.animationSpeed,hoverOpacity);
						});
					}
					
				});
			},
			function(){
				$(this).removeClass('selected')
					.find('.desc,ul.tools')
					.fadeTo(settings.animationSpeed,0,function(){
						$(this).hide();
					});
				$(this).find('.thumbnail').stop().animate({opacity:outOpacity,height:settings.outHeight},settings.animationSpeed);
			}
		).click(function(){
			location.href = $(this).find('a:first').attr('href');
		});
		
		if (settings.paging) {
		  var firstItem = $listToSlide.find('li:first');
			var pageCount = $listToSlide.find('>li').size() / settings.itemsPerPage;
			var itemWidth = $listToSlide.find('li:first').width();
			var itemHeight = $listToSlide.find('li:first').height();
      var pageWidth = $listToSlide.width(); // itemWidth * settings.itemsPerPage;
      var pageHeight = Math.ceil((settings.itemsPerPage*itemWidth)/pageWidth)*itemHeight;
      
			
			// Fix the lists height
			$listToSlide.css({'height':pageHeight,'width':$listToSlide.width(),'overflow':'hidden','position':'relative'});
			
			// Fix the lis
			$listToSlide.find('>li').each(function(i){
				$(this).css({
					'position':'absolute',
					'left': (((i%settings.itemsPerPage)*itemWidth)%pageWidth + pageWidth*Math.floor(i/settings.itemsPerPage)),
					'top': ((Math.floor(((i%settings.itemsPerPage)*itemWidth)/pageWidth)*itemHeight)%pageHeight)
				});
			});
			
			// Find the paging nav
			$paging = $listToSlide.nextAll('.paging:first');
			
			if($listToSlide.find('>li').size() > 1) {
				// Put the links in
				toInject = "";
				for(i=1; i < pageCount + 1; i++){
					toInject +=  "<li><a href='#'>"+i+"</a></li>";
				}
				$paging.find('li:first').after(toInject);
				$paging.find('li:eq(1)').addClass('selected');
			
				// Bind the functions
				$paging.find('li:not(.prev):not(.next) a').each(function(i){
					$(this).click(function(){
						_goToPage(this,i + 1);
						return false;
					});
				});
			
				$paging.find('li.next a').click(function(){
					_goToPage(this,'up');
					return false;
				});
			
				$paging.find('li.prev a').click(function(){
					_goToPage(this,'down');
					return false;
				});
			
				_selectItem(null,currentPage);
			}else{
				$paging.hide();
			}
		};
		
		function _selectItem(caller,which) {
			if($(caller).parent().hasClass('disabled')) return;

			// Find the paging nav
			$paging = $listToSlide.nextAll('.paging:first');

			// Un-select the current item
			$paging.find('li.selected').removeClass('selected');

			// Select the proper item
			$paging.find('li:eq('+which+')').addClass('selected');

			// Disable the next/previous if needed.
			$paging.find('li.disabled').removeClass('disabled');
			if(currentPage == 1){
				$paging.find('li.prev').addClass('disabled');
			}else if(currentPage == pageCount){
				$paging.find('li.next').addClass('disabled');
			};
		};
		
		function _goToPage(caller,which) {
			if($(caller).parent().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){
			  // alert("i:"+i+"\n ((i*itemWidth)%pageWidth) = "+((i*itemWidth)%pageWidth)+"\n  (pageWidth * -(currentPage-1)) = "+(pageWidth * -(currentPage-1)) );
				$(this).stop().animate({'left': ( (((i%settings.itemsPerPage)*itemWidth)%pageWidth) + pageWidth*Math.floor(i/settings.itemsPerPage) + (pageWidth * -(currentPage-1)) ) }); // (i * itemWidth) + (pageWidth * -(currentPage-1))});
			});

			// Select the item
			_selectItem(caller,currentPage);
		};
	});
}
