var slideshowRunning = true;

$(document).ready(function()
{
	if ($('#Slideshow').length == 0) return;

	$('#Slideshow div.slide').css({opacity: 0.0});
	
	// Obtenir l'actif
	var $active = $('#Slideshow div.active');
	if ($active.length == 0) $active = $('#Slideshow div:first');
	$active.css({opacity: 1.0});
	
    setInterval( "slideSwitch()", 5000 );
});

function slideSwitch() 
{
	if (!slideshowRunning) return;
	
	nextSlide(false);
}

function toggleSlideshow()
{
	slideshowRunning = !slideshowRunning;
}

function nextSlide(_stopSlideshow)
{
	slideshowRunning = !_stopSlideshow;
	
	var $active = $('#Slideshow div.active');

	// Aucun actif, assigner le premier
	if ($active.length == 0) 
		$active = $('#Slideshow div:first');

	var $next;
	
	// Il y a un prochain
	if ($active.next().length && $active.next().hasClass('slide'))
		$next = $active.next();
		
	// Il n'y a pas de prochain
	else
	{
		var found = false;
		var tmp = $('#Slideshow div:first');
		while (!found)
		{
			found = tmp.hasClass('slide');
			
			if (!found)
				tmp = tmp.next();
		}
		
		$next = tmp;
	}

	// L'actif est le dernier actif
	$active.addClass('last-active');

	// Le prochain est invisible
	$next.css({opacity: 0.0});
	
	// Le prochain est actif
	$next.addClass('active');
	
	// Animer pour que le prochaine soit opaque (Par dessus l'actif)
	$next.animate({opacity: 1.0}, 400, function() { $active.removeClass('active last-active'); });
	$active.animate({opacity: 0.0}, 400, function() {  });
}

function previousSlide(_stopSlideshow)
{
	slideshowRunning = !_stopSlideshow;
	
	var $active = $('#Slideshow div.active');

	// Aucun actif, assigner le premier
	if ($active.length == 0) 
		$active = $('#Slideshow div:first');

	var $previous;
	
	// Il y a un précédent
	if ($active.prev().length && $active.prev().hasClass('slide'))
		$previous = $active.prev();
		
	// Il n'y a pas de précédent
	else
	{
		var found = false;
		var tmp = $('#Slideshow div:last');
		while (!found)
		{
			found = tmp.hasClass('slide');
			
			if (!found)
				tmp = tmp.prev();
		}
		
		$previous = tmp;
	}
	
	// L'actif est le dernier actif
	$active.addClass('last-active');

	// Le prochain est invisible
	$previous.css({opacity: 0.0});
	
	// Le prochain est actif
	$previous.addClass('active');
	
	// Animer pour que le prochaine soit opaque (Par dessus l'actif)
	$previous.animate({opacity: 1.0}, 400, function() { $active.removeClass('active last-active'); });
	$active.animate({opacity: 0.0}, 400, function() {  });
}
