(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')) {
						$(this).append('<div class="controls"></div>');
					}
					
					$('.controls', this).prepend('<a class="prev">Previous</a><div class="navi"></div><a class="next">Next</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>' + (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);









/*(function($){
	$.fn.slideshow = function(options){
		var settings = {
			interval: 3500,
			speed: 1000
		};
		
		if (options) $.extend(settings, options);
	
		return this.each(function(){
			var $this = $(this);
			if ($this.children().length > 1) {
				$this.children('img:gt(0)').hide();
				$this.append('<div class="controls"><a class="prev"><div class="navi"></div><a class="next"></a></div>')
					.children('img').each(function() { $this.find('.navi').append('<a></a>'); });
				
				$('.controls a', $this).click(function() {
					clearInterval(fadeTid);
					
					if ($(this).hasClass('prev')) {
						
					} else if ($(this).hasClass('next')) {
						
					} else {
						console.log('goto i');
					}
					
					return false;
				});
				
				fadeTid = setInterval(function(){
					$this.children('img').eq(0).fadeOut(settings.speed)
						.next().fadeIn(settings.speed)
						.end().appendTo($this);
				}, settings.interval);
			}
		});
	};
})(jQuery);*/
