var slideshow_interval = '';
$ = jQuery.noConflict();
$(document).ready(function(){
	$('.slideshow .slide').css('position', 'absolute');
	setTimeout(function() {
		$('.slideshow-wrapper .bg:first, .slideshow .slide:first').show().addClass('current');
		$('.slideshow .dot:first').fadeIn('slow').addClass('current');
	}, 600);
	if ($('.slideshow .slide').length > 1) {
		setTimeout(function() {
			$('.slideshow a.previous, .slideshow a.next').fadeIn('slow');
		}, 600);
	}
	
	slideshow_interval = setInterval('next_image()', 5000);

    $('.slideshow a.previous, a.quote-previous').click(function(e) {
    	e.preventDefault();
    	clearInterval(slideshow_interval);
    	previous_image();
    });
    
    $('.slideshow a.next, a.quote-next').click(function(e) {
    	e.preventDefault();
    	clearInterval(slideshow_interval);
    	next_image();
    });
    
    $('img').each(function () {
		if ($(this).attr('longdesc')) {
			$(this).attr('src', $(this).attr('longdesc'));
		}
	});
	
	$('.slideshow #controls .dot').click(function(e) {
		e.preventDefault();
    	clearInterval(slideshow_interval);
    	$('.slideshow .slide.current').fadeOut('slow').removeClass('current');
    	$('.slideshow .slide').eq($(this).index() - 1).fadeIn('slow').addClass('current');
    	$('.slideshow-wrapper .bg.current').fadeOut('slow').removeClass('current');
    	$('.slideshow-wrapper .bg').eq($(this).index() - 1).fadeIn('slow').addClass('current');
    	$('.slideshow #controls .dot.current').removeClass('current');
    	$(this).addClass('current');
    });
});

function next_image() {
	var current = $('.slideshow .slide.current');
	var next_index = $(current).index('.slideshow .slide') + 1;
	var next = $('.slideshow .slide:eq('+next_index+')');
	
	var current_dot = $('.slideshow #controls .dot.current');
	var next_dot = $(current_dot).next('.dot');
	
	var current_bg = $('.slideshow-wrapper .bg.current');
	var next_bg = $(current_bg).next('.bg');
	
	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 .slide:eq(0)').stop(true, true).fadeIn('slow').addClass('current');
	}
	
	if (next_dot.length) {
		$(current_dot).removeClass('current');
		$(next_dot).addClass('current');
	}
	else {
		$(current_dot).removeClass('current');
		$('.slideshow #controls .dot:eq(0)').addClass('current');
	}
	
	if (next_bg.length) {
		$(current_bg).stop(true, true).fadeOut('slow').removeClass('current');
		$(next_bg).stop(true, true).fadeIn('slow').addClass('current');
	}
	else {
		$(current_bg).stop(true, true).fadeOut('slow').removeClass('current');
		$('.slideshow-wrapper .bg:eq(0)').stop(true, true).fadeIn('slow').addClass('current');
	}
}

function previous_image() {
	var current = $('.slideshow .slide.current');
	var previous = $(current).prev('.slide');
	
	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 .slide:last').stop(true, true).fadeIn('slow').addClass('current');
	}
	
	var current_dot = $('.slideshow #controls .dot.current');
	var previous_dot = $(current_dot).prev('.dot');
	
	if (previous_dot.length) {
		$(current_dot).removeClass('current');
		$(previous_dot).addClass('current');
	}
	else {
		$(current_dot).removeClass('current');
		$('.slideshow #controls .dot:last').addClass('current');
	}
	
	var current_bg = $('.slideshow-wrapper .bg.current');
	var previous_bg = $(current_bg).prev('.bg');
	
	if (previous_bg.length) {
		$(current_bg).stop(true, true).fadeOut('slow').removeClass('current');
		$(previous_bg).stop(true, true).fadeIn('slow').addClass('current');
	}
	else {
		$(current_bg).stop(true, true).fadeOut('slow').removeClass('current');
		$('.slideshow-wrapper .bg:last').stop(true, true).fadeIn('slow').addClass('current');
	}
}
