// Variable declaration
var currentSlide = 1, oldSlide = 1; // Set the current slide and old slide to 1
var timeOut = 4000; // Set the timeout between slides to five seconds
var slideCount, slideWidth, slideID;
var moveDistance;

$(document).ready(function() {	
	slideCount = $(".slide").length;
	slideWidth = $(".slide").outerWidth();	
	
	currentSlide = currentSlide + 1;
	t = setTimeout("moveSlide(currentSlide)", timeOut);
	
	$("#gThumbs img").click(function(){
		clearTimeout(t);
		slideID = parseInt($(this).attr("id"));
		moveSlide(slideID);
		return false;
	});
	
	$("#slides").hover(function() {
		clearTimeout(t);
	}, function() {
		t = setTimeout("moveSlide(currentSlide)", timeOut - 2000);
	});
});

function moveSlide(slideTo) {
	var slideTime = 1000;
	// Re-set variable
	if (slideTo > 6) {
		slideTo = 1;
		currentSlide = 1;
		slideTime = 6000; // Slow down the transition when we go back
	}else{
		currentSlide = slideTo;
	}
	// Calculate the distance for the slide
	moveDistance = (slideTo * slideWidth) - slideWidth;
	moveDistance = moveDistance * -1;
	// Move slide
	$("#slides").animate({left : moveDistance + "px"}, slideTime, function() {
		// Update the currentSlide
		currentSlide++;
		// Re-set the timeout
		t = setTimeout("moveSlide(currentSlide)", timeOut);
	});
}