(function($) {
	$.fn.slideshow = function(settings) {
		$this = this;
		timer = null;
		
		settings = $.extend({
			interval: 3500,
			items: 'img',
			showControls: false,
			speed: 1000
		}, settings);
		
		
		var startSlideshow = function(parent) {
			timer = setInterval(function() {
					if ( $('.items ' + settings.items, parent).index( $(settings.items + '.active')) == $('.items ' + settings.items, parent).length - 1 ) {
						fadeTo(parent, 0);
					} else {
						fadeTo(parent, $('.items ' + settings.items, parent).index( $(settings.items + '.active')) + 1);
					}
					
				}, settings.interval);
		};
		
		
		fadeTo = function(parent, index) {
			next = $('.items ' + settings.items, parent).eq(index);
			next.css({ zIndex:'1', display:'block' });
			$('.navi a', parent).removeClass('active').eq(index).addClass('active');
			$('.items ' + settings.items + '.active', parent).fadeOut(settings.speed, function() {
				$(this).removeClass('active');
				next.addClass('active').css('z-index','2');
			});
		};
	
	
		$(this).each(function() {
			if ($('.items ' + settings.items, this).length > 1) {
				
				if (settings.showControls) {
					if ($('.controls', this).is('div')) {
						$('.controls', this).prepend('<button class="prev">Previous</button><div class="navi"></div><button class="next">Next</button><a class="play">Play</a><a class="stop">Stop</a>');
					}
					
					$('.next', this).click(function() { $this.next(); return false; });
					$('.prev', this).click(function() { $this.previous(); return false; });
					$('.play', this).click(function() { $this.play(); return false; });
					$('.stop', this).click(function() { $this.stop(); return false; });
				}
				
				if ($('.navi', this).is('div')) {
					$('.items ' + settings.items, this).each(function(i) {
						$('.navi', $this).append('<a href="#">' + (i+1) + '</a>');
					});
					$('.navi a:first-child').addClass('active');
					
					$('.navi a', this).click(function() {
						$this.stop();
						fadeTo($this, $('.navi a').index(this));
						//startSlideshow();
						return false;
					});
				}
				
				startSlideshow(this);
			}
		});
		
		
		this.next = function() {
			this.stop();
			
			if ( $('.items ' + settings.items, this).index( $(settings.items + '.active')) == $('.items ' + settings.items, this).length - 1 ) {
				fadeTo(this, 0);
			} else {
				fadeTo(this, $('.items ' + settings.items, this).index( $(settings.items + '.active')) + 1);
			}
			
			//startSlideshow(this);
		};
		
		
		this.play = function() {
			if (timer)
				return;
			
			startSlideshow(this);
		};
		
		
		this.previous = function() {
			this.stop();
			
			if ( $('.items ' + settings.items, this).index( $(settings.items + '.active')) == 0 ) {
				fadeTo(this, $('.items ' + settings.items, this).length - 1);
			} else {
				fadeTo(this, $('.items ' + settings.items, this).index( $(settings.items + '.active')) - 1);
			}
			
			//startSlideshow(this);
		};
		
		
		this.stop = function() {
			timer = clearInterval(timer);
		};

		return this;
	}
})(jQuery);