(function($){
	$.fn.slideshow = function( options ) {
		var settings = {
			    auto: true,
			    interval: 6000,
			    speed: 1000,
			    animation: 'fader', /* fader, vert-slider, horiz-slider */
			    slideSelector: '.slide',
			    slideshow: this,
			    startSelector: '> :first-child' /* default to first slide */
		    };
		
		if ( options ) $.extend( settings, options );
		
		var ss = {
				stopped: false,
				intID: null,
				animate: function( slideid ){
					
					if ( this.stopped ) return;
					
					var slideid = slideid || settings.startSelector,
				        nxt;
					
					this.current = this.find( slideid );
					
					switch ( settings.animation ) {
						case 'fader' :
							this.current.fadeOut( settings.speed, function(){
								nxt.fadeIn( settings.speed );
							});
							this.current = nxt;
							break;
						case 'vert-slider' :
							var p = this.slides.parent();
							var y = this.current.position().top;
							
							this.slides.animate({
								top: '-=' + y
							}, settings.speed );
							this.current = nxt;
							break;
						case 'horiz-slider' :
							var p = this.slides.parent();
							var x = this.current.position().left;
							
							this.slides.animate({
								left: '-=' + x
							}, settings.speed );
							
							break;
					}
					
					//this.current = nxt;
					
					
					if ( settings.auto && !this.stopped ) {
						this.slides.mouseenter( this.stop );
						this.slides.mouseleave( this.resume );
						
						if ( !this.intID ) {
							var th = this;
							this.intID = setInterval( function(){th.animate()}, settings.interval );
						}
					}
				},
				current: null,
				stop: function(){ 
					this.stopped = true;
					if ( this.intID ) {
						clearInterval( this.intID );
						this.intID = null;
					}
				},
				resume: function(){ 
					this.stopped = false;
					this.intID = setInterval( this.animate, settings.interval );
				}
			};
		
		return this.each( function(){			
			var $this = $.extend( $(this), ss );
			
			if ( !$this.slides ) {
				$this.slides = $( settings.slideSelector, $this );
				if( 'fader' == settings.animation ) $this.slides.hide();
			}
			
			var nth = 0;
			if ( 'horiz-slider' == settings.animation ) {
				$this.slides.each(function(){
					$(this).css(
						{
							position: 'absolute',
							left: ($(this).width() * nth++) + 'px',
							top: '0px'
						}	
					);
				});
			} else if ( 'vert-slider' == settings.animation ) {
				$this.slides.each(function(){
					$(this).css(
						{
							position: 'absolute',
							top: ($(this).height() * nth++) + 'px',
							left: '0px'
						}	
					);
				});
			} else if ( 'fader' == settings.animation ) {
				$this.slides.each(function(){
					$(this).css(
						{
							position: 'absolute',
							top: '0px',
							left: '0px'
						}	
					);
					$(this).hide();
				});				
			}
			
			( settings.init || function(){} )( $this ) && $this.animate( settings.startSelector );
		});
	}
})(jQuery);
