$(document).ready(function() {
	var delay	= 7000;
	var frame	= $("#slideshow .slides");
	var width	= frame.children(".slide").eq(0).width();
	var timeout;
	
	autoTransition();
	
	$("#slideshow .next").click(function(e) {
		e.preventDefault();
		
		clearTimeout(timeout);
		
		next();
	});
	
	$("#slideshow .prev").click(function(e) {
		e.preventDefault();
		
		clearTimeout(timeout);
		
		prev();
	});
	
	function autoTransition() {
		
		timeout = setTimeout(function() {
			next(autoTransition);
		}, delay);
	}
	
	function prev() {
		var ls = frame.children().last();
		
		ls.prependTo(frame); // Move the last slide to the front
		frame.css({left: -width}) // Move the slides 1 to the left
		
		frame.animate({left: 0}); // Move the new slide in
	}
	
	function next(callback) {
		var fs = frame.children().first();
		
		frame.animate({left: -width}, function() {
			fs.appendTo(frame); // Move the first slide to the end
			frame.css({left: 0}) // Set the frame back to 0
		});
		
		if(callback) {
			callback();
		}
	}
});
