(function($){

	$.fn.slider = function(options) {
		var defaults = {
			element: ">li",
			wrapperClass: "SlideshowWrapper",
			nextClass: "Next",
			prevClass: "Prev",
			next: "next",
			prev: "prev",
			animationDuration: 200,
			showOnHover: true
		}
		
		var settings = $.extend({}, defaults, options);
		var $container = $(this);
		var $wrapper;
		var $items = $container.find(settings.element);
		var activeIndex = -1;
		var $next, $prev;
		
		$container.init = function() {
			$items.hide();
			
			$container.wrapAll('<div class="' + settings.wrapperClass + '"></div>');
			$wrapper = $container.parent();
			
			$prev = $('<a href="#" class="' + settings.prevClass + '">' + settings.prev + '</a>');
			$next = $('<a href="#" class="' + settings.nextClass + '">' + settings.next + '</a>');
			
			$next.insertAfter($container);
			$prev.insertAfter($container);
			
			$next.bind("click", function(e) {
				$container.next();
				e.preventDefault();
			});
			$prev.bind("click", function(e) {
				$container.prev();
				e.preventDefault();
			});
			
			$(window).load(function(){
				$container.openSlide(0);
			});
			
			if (settings.showOnHover) {
				$next.hide();
				$prev.hide();
				
				$wrapper.hover(
					function() {
						$next.stop(true, true).fadeIn(settings.animationDuration);
						$prev.stop(true, true).fadeIn(settings.animationDuration);
					}, 
					function() {
						$next.stop(true, true).fadeOut(settings.animationDuration);
						$prev.stop(true, true).fadeOut(settings.animationDuration);
					}
				);
			}
		}
		
		$container.prev = function() {
			$container.openSlide(activeIndex - 1);
		}
		
		$container.next = function() {
			$container.openSlide(activeIndex + 1);
		}

		$container.openSlide = function(newIndex) {
			if (activeIndex == newIndex) 
				return;

			var animationDuration = activeIndex < 0 ? 0 : settings.animationDuration;
			
			if (activeIndex >= 0 && activeIndex < $items.length) {
				$($items[activeIndex]).stop(true, true).fadeOut(animationDuration);
			}
			
			newIndex = newIndex % $items.length;
			if (newIndex < 0) 
				newIndex = $items.length + newIndex;
			
			$container.animate({
				height: $($items[newIndex]).height()
			}, animationDuration);
			
			$($items[newIndex]).stop(true, true).fadeIn(animationDuration);
			
			activeIndex = newIndex;
		}
		
		$container.init();
	}

})(jQuery);
