$(function() {
	$('.slideshow').each( function() {
		new Slideshow( this );
	});
});

function Slideshow( ul ) {
	this.currentIndex = 0;
	this.ul = ul;
	this.images = new Array();
	this.interval;
	
	this.initialize = function() {
		this.images = $('li', this.ul).get();
		
		if( this.images.length > 1 )
		{
			setTimeout( function(obj) { return function() { obj.start(); } }(this) , 500);
		}
	}
	
	this.start = function() {
		this.interval = setInterval( function(obj) { return function() { obj.next(true); } }(this), 5000);
	}
	
	this.next = function() {
		var prevIndex = this.currentIndex;
		this.currentIndex++;
		
		if( this.currentIndex == this.images.length )
		{
			this.currentIndex = 0;
		}
		
		$(this.images[prevIndex]).animate({left:'-700px'}, function() { $(this).hide(); });
		$(this.images[this.currentIndex]).show().css('left', '700px').animate({left:'0px'});
	}
	
	this.initialize();
}
