$.fn.slideshow = function( params ) {
	var $this = this;
	var $slides = $this.find('.slides');
	var totalslides = $slides.children().length
	var pos = 0;
	var timer = null;

	function set_timer() {
		if ( params.autoSlideAfter ) {
			if ( timer ) { clearTimeout(timer); timer = null; }
			timer = setTimeout(function() { go(1); }, params.autoSlideAfter)
		}
	}

	function go (direction) {
		set_timer();

		pos = pos + direction

		// If we try to slide after the last one
		if ( pos >= totalslides ) {
			$slides.append( $slides.children().eq(0).clone() )
			var callback = function() {
				$slides.children().eq( $slides.children().length-1 ).remove()
				$slides.css('left', '0px')
				pos = 0;
			}
		}

		// If we try to slide before the first one
		if ( pos < 0 ) {
			var last = $slides.children().eq( $slides.children().length-1 );
			$slides.prepend(last.clone())
			$slides.css('left', (-1*params.slideWidth)+'px')
			pos = 0;
			var callback = function() {
				last.remove();
			}
		}

		var newleft = pos * params.slideWidth * -1
		$slides.animate({
			left : newleft+'px'
		},{
			duration: params.animationDuration,
			easing: params.animationEasing,
			complete: function() {
				if ( callback ) {
					callback()
				}
			}
		});
	}

	set_timer();

	$this.find('.arrow-left, .arrow-right').click(function() {
		if ( !$slides.is(':animated') ) {
			go( $(this).is('.arrow-right') ? 1 : -1 )
		}
	})

}
