var slideshow_timeout = '';

$(document).ready(function(){
	$('#slideshow img').hide();
	$('#slideshow img:first').fadeIn('slow').addClass('showing');
	$('#controls #buttons #dots a:first').addClass('current');
    slideshow_timeout = setTimeout('next_image()', 5000);
    
    $('#controls #buttons #dots a').click(function() {
    	clearTimeout(slideshow_timeout);
    	$('#slideshow img.showing').fadeOut('slow').removeClass('showing');
    	$('#slideshow img').eq($(this).index()).fadeIn('slow').addClass('showing');
    	$('#controls #buttons #dots a.current').removeClass('current');
    	$(this).addClass('current');
    	slideshow_timeout = setTimeout('next_image()', 5000);
    	return false;
    });
    
    $('#controls a#previous').click(function() {
    	clearTimeout(slideshow_timeout);
    	previous_image();
    	return false;
    });
    
    $('#controls a#next').click(function() {
    	clearTimeout(slideshow_timeout);
    	next_image();
    	return false;
    });
});

function next_image() {
	var current = $('#slideshow img.showing');
	var next = $(current).next('img');
	var current_dot = $('#controls #buttons #dots a.current');
	var next_dot = $(current_dot).next('a');
	
	if (next.length) {
		$(current).fadeOut('slow').removeClass('showing');
		$(next).fadeIn('slow').addClass('showing');
	}
	else {
		$(current).fadeOut('slow').removeClass('showing');
		$('#slideshow img:eq(0)').fadeIn('slow').addClass('showing');
	}
	
	if (next_dot.length) {
		$(current_dot).removeClass('current');
		$(next_dot).addClass('current');
	}
	else {
		$(current_dot).removeClass('current');
		$('#controls #buttons #dots a:eq(0)').addClass('current');
	}
	
	slideshow_timeout = setTimeout('next_image()', 5000);
}

function previous_image() {
	var current = $('#slideshow img.showing');
	var previous = $(current).prev('img');
	var current_dot = $('#controls #buttons #dots a.current');
	var previous_dot = $(current_dot).prev('a');
	
	if (previous.length) {
		$(current).fadeOut('slow').removeClass('showing');
		$(previous).fadeIn('slow').addClass('showing');
	}
	else {
		$(current).fadeOut('slow').removeClass('showing');
		$('#slideshow img:last').fadeIn('slow').addClass('showing');
	}
	
	if (previous_dot.length) {
		$(current_dot).removeClass('current');
		$(previous_dot).addClass('current');
	}
	else {
		$(current_dot).removeClass('current');
		$('#controls #buttons #dots a:last').addClass('current');
	}
	
	slideshow_timeout = setTimeout('next_image()', 5000);
}
