TO_LOAD_INDEX++;
TO_LOAD[TO_LOAD_INDEX]='load_home_evian();';

function load_home_evian(){
	SLIDESHOW_TIMOUT=0;
	ANIMATING=true;
	OVER_SLIDESHOW=false;
	SLIDES_COUNT=$(".gallery .slide").length;



	

	$(window).resize(resizeCampagne);


	$(".gallery .prev").click(previousSlide);
	$(".gallery .next").click(nextSlide);

	$(".gallery .prev,.gallery .next").hide();

	$(".gallery").hover(displaySlideshowNav,hideSlideshowNav);

	$(".final").mouseenter(showSlideshow);


	centerSlides(".gallery .slide");
	resizeCampagne();

	if($(".gallery").hasClass('open')){
		$(".final").hide();
		initSlideshow();
	}else{
		$(".gallery").hide();
		$(".final").show();
	}
	
	$("#top").css("visibility",'visible');
}

function displaySlideshowNav(){
	showHideArrows()
	$(".gallery .controls").fadeIn(300);
	OVER_SLIDESHOW=true;
}

function hideSlideshowNav(){
	showHideArrows();
	$(".gallery .controls").fadeOut(300);
	OVER_SLIDESHOW=false;
}

function resizeCampagne(){
	centerSlides(".gallery .slide");
	var o=($(window).width()-960)/2;
	$(".gallery .prev").css('left',o);
	$(".gallery .next").css('left',960+o-55);
}

function initSlideshow(){
	$(".gallery .slide").hide();
	$(".gallery .slide:first").show();

	centerSlides(".gallery .slide:first");
	resizeCampagne();


	CURRENT_INDEX=0;

	if($(".gallery .slide").length>=1){
		startNewCycle(true);
	}
}

/**
 *
 * @access public
 * @return void
 **/
function showHideArrows(){
	if(SLIDES_COUNT==1){
		$(".gallery .prev,.gallery .next").fadeOut(300);
	}
	if(CURRENT_INDEX==0){
		$(".gallery .prev").fadeOut(300);
	}else{
		$(".gallery .prev").fadeIn(300);
	}

	if(CURRENT_INDEX>=SLIDES_COUNT-1){
		$(".gallery .next").fadeOut(300);
	}else{
		$(".gallery .next").fadeIn(300);
	}
}

function startNewCycle(first){
	ANIMATING=false;
	CURRENT_SLIDE=getSlideByIndex(CURRENT_INDEX);
	var r=$(CURRENT_SLIDE).attr('rel').split('$');
	var duration=parseInt(r[0]);
	var height=parseInt(r[1]);

	SLIDESHOW_TIMEOUT=setTimeout('autoNextSlide();',duration*1000);
	if(first){
		$(".gallery").css({
			height:height+'px'
		});
		$(".gallery .next,.gallery .prev").css('top',(height-55)/2);
	}else{
		$(".gallery").animate({
			height:height+'px'
		},250);
		$(".gallery .next,.gallery .prev").animate({
			top:(height-55)/2
		});
	}
}

function gotoSlide(index){
	if(ANIMATING==true){
		return;
	}

	$(".gallery .slide").stop();
	ANIMATING=true;
	clearTimeout(SLIDESHOW_TIMEOUT);
	NEXT_SLIDE=getSlideByIndex(index);

	var initalPos=0;
	var endPos=0;
	NEXT_INDEX=normalizeIndex(index)
	$(NEXT_SLIDE).show();
	$(CURRENT_SLIDE).show();
	if(NEXT_INDEX>CURRENT_INDEX){
		initialPos=$(window).width();
		endPos=$(CURRENT_SLIDE).width()*-1;
	}else{
		initialPos=$(NEXT_SLIDE).width()*-1;
		endPos=$(window).width();
	}

	$(NEXT_SLIDE).css('left',initialPos+'px');
	$(NEXT_SLIDE).animate({
		left:getFinalPosition(NEXT_SLIDE)+'px'
	},{
		duration:1000,
		complete:afterAnimation
	});
	$(CURRENT_SLIDE).animate({
		left:endPos+'px'
	},{
		duration:1000
	});
}

function afterAnimation(){
	$(CURRENT_SLIDE).stop().fadeOut(250,function(){
		$(CURRENT_SLIDE).css('left','0px');
		startNewCycle(false);
	});
	CURRENT_INDEX=NEXT_INDEX;

	showHideArrows();
}

function autoNextSlide(){
	if(normalizeIndex(CURRENT_INDEX+1)==0 && !OVER_SLIDESHOW){
		hideSlideshow();
		return;
	}
	nextSlide();
}

function nextSlide(){
	if(ANIMATING){
		return;
	}
	NEXT_INDEX=CURRENT_INDEX+1;
	gotoSlide(NEXT_INDEX);
}

function previousSlide(){
	if(ANIMATING){
		return;
	}
	NEXT_INDEX=CURRENT_INDEX-1;
	gotoSlide(NEXT_INDEX);
}

function normalizeIndex(index){
	var slides=$(".gallery .slide");
	index=(index+slides.length)%slides.length;
	return index;
}

function getSlideByIndex(index){
	var slides=$(".gallery .slide");
	return slides[normalizeIndex(index)];
}

function centerSlides(slides){
	$(slides).each(function(){
		$(this).css('left',getFinalPosition(this)+'px');
	});
}

function getFinalPosition(slide){
	return ($(window).width()-$(slide).width())/2;
}

function hideSlideshow(){
	$(".gallery").slideUp(250);
	$(".gallery .slide").fadeOut(250,function(){
		$(".final").slideDown(250,function(){
			$("#top").trigger('resize');
		});
	});
}
function showSlideshow(){

	$(".gallery .slide:first").fadeOut(0);
	$(".final").slideUp(250,function(){
		$(".gallery .slide:first").fadeIn(250,function(){
			$("#top").trigger('resize');
		});
		initSlideshow();
	});
	$(".gallery").slideDown(250);
}
