var slideshow_interval = '';

$(document).ready(function(){
	$('.slideshow a.slide img').hide();
	setTimeout(function() { $('.slideshow a.slide img:first').fadeIn('slow').addClass('showing'); }, 1000);
    slideshow_interval = setInterval('next_image()', 6000);
    
    $('.slideshow a#previous').click(function(e) {
    	e.preventDefault();
    	clearInterval(slideshow_interval);
    	previous_image();
    });
    
    $('.slideshow a#next').click(function(e) {
    	e.preventDefault();
    	clearInterval(slideshow_interval);
    	next_image();
    });
    
    $('#controls a:first').addClass('current');
});

function next_image() {
	var current = $('.slideshow a.slide img.showing');
	var next_index = $(current).index('.slideshow a.slide img') + 1;
	var next = $('.slideshow a.slide img:eq('+next_index+')');
	
	if (next.length) {
		$(current).stop(true, true).fadeOut('slow').removeClass('showing');
		$(next).stop(true, true).fadeIn('slow').addClass('showing');
	}
	else {
		$(current).stop(true, true).fadeOut('slow').removeClass('showing');
		$('.slideshow a.slide img:eq(0)').stop(true, true).fadeIn('slow').addClass('showing');
	}
}

function previous_image() {
	var current = $('.slideshow a.slide img.showing');
	var previous_index = $(current).index('.slideshow a.slide img') - 1;
	var previous = $('.slideshow a.slide img:eq('+previous_index+')');
	
	if (previous.length) {
		$(current).stop(true, true).fadeOut('slow').removeClass('showing');
		$(previous).stop(true, true).fadeIn('slow').addClass('showing');
	}
	else {
		$(current).stop(true, true).fadeOut('slow').removeClass('showing');
		$('.slideshow a.slide img:last').stop(true, true).fadeIn('slow').addClass('showing');
	}
}
