var offsetWidth;
var doubleoffsetWidth;
var offsetHeight;
var recupnbrslide;
var scroller;
var dir = 1;
var loopnb = 0;
var currentpos = 1;
var delay = 10000;

/* DEBUT FONCTION POUR LE SLIDE AUTOMATIQUE */
function autoscroll(){
	if(dir>0){
		if(currentpos<recupnbrslide){
			slidenext();
		} else {	
			window.clearTimeout(scroller); 
			dir = -dir;
			currentpos = 0;
			$(".section").animate({"margin-left":-(currentpos*offsetWidth)+"px"}, 1500 );
			$("#next").show();
			$("#prev").hide();
		}
	}else{
		if(currentpos>1){
			slideprev();
		} else {
			dir = -dir;
			slidenext();
		}
	}
}
function delayedAction(){
	
		scroller = setTimeout("autoscroll()",delay);	

}
/* FIN FONCTION POUR LE SLIDE AUTOMATIQUE */

/* DEBUT FONCTION SUIVANT ET PRECEDENT POUR LE SLIDE */
function slidenext(){
	window.clearTimeout(scroller);
	$(".section").animate({"margin-left":-(currentpos*offsetWidth)+"px"}, 1500 );
	currentpos++;
	if(currentpos > 1){
		$("#prev").show();
		$("#next").show();
	}
	if(currentpos == recupnbrslide){
		$("#next").hide();
		$("#prev").show();
	}
	
	if (footerColors && footerColors[currentpos - 1]) {
	    changeFooterColor(footerColors[currentpos - 1]);
	}
	
	delayedAction();
};

function slideprev(){
	window.clearTimeout(scroller);
	var recupmarginleft = $(".section").css("margin-left");	
	var recupmarginleftint = parseInt(recupmarginleft.replace("px",""));
	var slideprev = recupmarginleftint+offsetWidth;
	$(".section").animate({"margin-left":+slideprev+"px"}, 1500 );
	currentpos--;	
	if(currentpos == 1){
		$("#prev").hide();
		$("#next").show();
	}
	if(currentpos > 1){
		$("#prev").show();
		$("#next").show();
	}
	
	if (footerColors && footerColors[currentpos - 1]) {
        changeFooterColor(footerColors[currentpos - 1]);
    }
	
	delayedAction();
};

function changeFooterColor(color) {
    document.getElementById('footer').style.color = "#" + color;
    document.getElementById('footerSubmitButtonLink').style.color = "#" + color;
    for (var i = 1; i < 6; i++) {
        document.getElementById('footerLinkNumber' + i).style.color = "#" + color;
    }
}
/* FIN FONCTION SUIVANT ET PRECEDENT POUR LE SLIDE */


jQuery(document).ready(function(){	

	recupnbrslide = $("#diaporama ul li").length;	
	if(recupnbrslide > 1){
		$("#next").show();
	}
	
	offsetWidth = $(window).width();
	doubleoffsetWidth = offsetWidth*recupnbrslide;
	offsetHeight = $(window).height();
	heightimgvisu = (offsetWidth*3)/4;	

	$("#diaporama, #diaporama ul li, #diaporama ul li img.imgpanel").width(offsetWidth);
	$("#diaporama .section, #diaporama .section ul").width(doubleoffsetWidth);
	$("#diaporama, #diaporama ul li").height(offsetHeight);
	$("#diaporama ul li img.imgpanel").height(heightimgvisu);
	
	if(offsetWidth < 1025){
		$(".homerecette .titre, .homerecette .soustitre, .homerecette .boutonform").css("margin-right","60px");
	}else{
		$(".homerecette .titre, .homerecette .soustitre, .homerecette .boutonform").css("margin-right","0");
	}
	
	$(".homerecette .bouton").click(function(){
		location.href="http://www.canderel.fr";
	});

	$("#next").click(function(){
		slidenext();
		return false;
	});
	$("#prev").click(function(){
		slideprev();
		return false;
	});
	
	if ($(window).height() < 770) {
		$('.hide_1024').hide();
		$('.bg_1024').css("min-width",1024);
	} else {
		$('.hide_1024').show();
		$('.bg_1024').css("min-width",1715);
	}
	$(window).resize(function(){							  
		window.clearTimeout(scroller);
		offsetWidth = $(window).width();
		doubleoffsetWidth = offsetWidth*recupnbrslide;
		offsetHeight = $(window).height();
		heightimgvisu = (offsetWidth*3)/4;
		widthtimgvisu = (offsetHeight*4)/3;
	
		$("#diaporama, #diaporama ul li").width(offsetWidth);	
		$("#diaporama .section, #diaporama .section ul").width(doubleoffsetWidth);
		$("#diaporama, #diaporama ul li").height(offsetHeight);
		
		if(offsetWidth > 1280){
			if(offsetHeight > offsetWidth){
				$("#diaporama ul li img.imgpanel").height(offsetHeight);
				$("#diaporama ul li img.imgpanel").width(widthtimgvisu);
			}else{
				$("#diaporama ul li img.imgpanel").height(heightimgvisu);
				$("#diaporama ul li img.imgpanel").width(offsetWidth);
			}
		}else{
			$("#diaporama ul li img.imgpanel").height(1024);
			$("#diaporama ul li img.imgpanel").width(1280);			
		}
		
		if(offsetWidth < 1025){
			$("#homerecette .titre, #homerecette .soustitre, #homerecette .bouton").css("margin-right","60px");
			$('.hide_1024').hide();
			$('.bg_1024').css("min-width",1024);
		}else{
			$("#homerecette .titre, #homerecette .soustitre, #homerecette .bouton").css("margin-right","0");
			$('.hide_1024').show();
			$('.bg_1024').css("min-width",1715);
		}
				
		$(".section").css({"margin-left":-((currentpos*offsetWidth)-offsetWidth)+"px"});
		delayedAction();
	});
	
});

/* INIT DU SLIDE AUTOMATIQUE */
delayedAction();
