var slideshow_timeout = '';

$(document).ready(function(){
	$('.slideshow img').hide().css('position', 'absolute');
	setTimeout(function() { $('.slideshow img:first').fadeIn('slow').addClass('current'); }, 600);
	setTimeout(function() { $('.slideshow a.previous, .slideshow a.next').fadeIn('slow'); }, 600);
	
	if ($('body').hasClass('home')) {
	    slideshow_timeout = setTimeout('next_image()', 6000);
	}
    
    $('.slideshow a.previous').click(function(e) {
    	e.preventDefault();
    	clearTimeout(slideshow_timeout);
    	previous_image();
    });
    
    $('.slideshow a.next').click(function(e) {
    	e.preventDefault();
    	clearTimeout(slideshow_timeout);
    	next_image();
    });
    
    $('img').each(function () {
		if ($(this).attr('longdesc')) {
			$(this).attr('src', $(this).attr('longdesc'));
		}
	});
});

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

function previous_image() {
	var current = $('.slideshow img.current');
	var previous = $(current).prev('img');
	
	if (previous.length) {
		$(current).stop(true, true).fadeOut('slow').removeClass('current');
		$(previous).stop(true, true).fadeIn('slow').addClass('current');
	}
	else {
		$(current).stop(true, true).fadeOut('slow').removeClass('current');
		$('.slideshow img:last').stop(true, true).fadeIn('slow').addClass('current');
	}
	
	slideshow_timeout = setTimeout('next_image()', 5000);
}
