(function($) {

	$.fn.slideshow = function(options) {

		var defaults = {
			'controls': '.controls li',
			'duration': 500,
			'easing': 'easeInOutQuart',
			'autoplay': true
		};

		options = $.extend(true, defaults, options);

		return this.each(function() {

			var $content = $(this);
			var $controls = $(options.controls);
			var autoplayTimer; 
			var height = 0;

			$content.children().each(function() {
				height += $(this).height();
			});

			$content.height(height);
			
			$controls.mousedown(function(){
				clearInterval(autoplayTimer);
			});

			$controls.click(function(e) {
				$controls.removeClass('active');
				$(this).addClass('active');
				var index = Math.min($(this).index(), $content.children().length - 1);
				var css = {
					top: -$content.children().eq(index).position().top + 'px'
				};
				$content.stop().animate(css, options.duration, options.easing);
				e.preventDefault();
			}).first().click();
			
			if (options.autoplay) {
			  autoplayTimer = setInterval(function(){
					var $active = $controls.filter('.active');
					var $next = $active.length ? $active.next() : $controls.eq(1);
					if (!$next.length) $next = $controls.first();
					$next.click();
				}, 10000);
			}
		});
	};

})(jQuery);

