(function($) {

	$.fn.slideshow = function(options) {
	
		var opts = $.extend({}, $.fn.slideshow.defaults, options);

		var parent = this;
		return ret = this.each(function() {
			$('.slide', this).slice(1).css({opacity: 0.0, display: 'block'});
		
//			var next = $('.slide:first', this);
//			setTimeout(function() {
//							$('.caption', next).animate({height: opts.captionHeight}, opts.captionDuration);
//				}, opts.captionDelay);

			setInterval(function() {
				var current = ($('.active', parent)?  $('.active', parent) : $('.slide:first', parent));

				//Get next image, if it reached the end of the slideshow, rotate it back to the first image
				var next = ((current.next().length) ? current.next() : $('.slide:first'));	

				var capheight = $('.caption', next).css('height');
				$('.caption', next).css({height: 0})

				//Set the fade in effect for the next image, show class has higher z-index
				next.css({opacity: 0.0})
				.addClass('active')
				.animate({opacity: 1.0}, opts.transitionDuration);

				//Hide the current image
				current.animate({opacity: 0.0}, opts.transitionDuration)
				.removeClass('active');

				setTimeout(function() {
					$('.caption', next).animate({height: opts.captionHeight}, opts.captionDuration);
				}, opts.captionDelay);

			}, opts.slideDuration);
		});
		
	};
	
	$.fn.slideshow.defaults = {
		slideDuration: 6000,
		transitionDuration: 1000,
		captionDelay: 2000,
		captionDuration: 500,
		captionHeight: 64
	}
})(jQuery);