$(document).ready(function(){
    
    var etatMenu = 1;
    var lectureAuto = true;
    var tempsAttente = 2500;
    var tempsBase = 8000;
    var hauteurDepart = 35;
    var posAnim = 1;
    if(lectureAuto){
        //startAnim2();
        $("#ssmenu_1").stop(true, true).animate({ marginTop:250 }, 1000, function(){
            if(posAnim == 1 && etatMenu == 1){
                $("#ssmenu_1").find(".slideshow_fleche").animate({ top: hauteurDepart }, 500, function(){
                    $("#ssmenu_1").find("li").first().css("color", "white");
                    $(".slideshow_zone_gauche").find("#visuel_1-1").fadeTo(500,1);
                });
            }
        });
    }
    function startAnim2() { timerAnim1 = setInterval(lancementAnim, tempsBase); }
    
    function lancementAnim() {
	if(etatMenu == 0){
            etatMenu = 1;
            clearInterval(timerAnim1);
        }
    }
    
/*    function lectureAuto_1() {
        switch (etatMenu) {
            // -- CAS 1 ----------------------------------------------------- //
            case 1:
                var nbMachines = $("#ssmenu_1 li").length;
                
                $("#lien_melange").addClass("selected");
                $("#lien_paillage").removeClass("selected");
                $("#lien_desilage").removeClass("selected");
                $("#ssmenu_2").stop(true, true).animate({ height:235 }, 500);
                $("#ssmenu_2").stop(true, true).animate({ marginTop:0 }, 500);
                $("#ssmenu_3").stop(true, true).animate({ height:195 }, 500);
                $("#ssmenu_3").stop(true, true).animate({ marginTop:0 }, 500);
                $("#ssmenu_1").stop(true, true).animate({ height:195 }, 500);
                $("#ssmenu_1").stop(true, true).animate({ marginTop:250 }, 1000, function(){
                    if(posAnim == 1 && etatMenu == 1){
                        $("#ssmenu_1").find(".slideshow_fleche").animate({ top: hauteurDepart }, 500, function(){
                            $("#ssmenu_1").find("li").first().css("color", "white");
                        });
                    }
                });
                
                if(posAnim <= nbMachines) {
                    var nbCarac = ($("#ssmenu_1").find(".slideshow_fleche").css("top")).indexOf('x');
                    var decalageHaut = ($("#ssmenu_1").find(".slideshow_fleche").css("top")).substring(0,nbCarac-1);

                    $("#ssmenu_1").find(".slideshow_fleche").animate({ top: 35 + (posAnim-1)*40 }, 500, function(){
                        $("#ssmenu_1").find("#machine_" + etatMenu + "-" + posAnim).siblings().css("color", "#6c6c6c");
                        $("#ssmenu_1").find("#machine_" + etatMenu + "-" + posAnim).css("color", "white");
                        $(".slideshow_zone_gauche").find("#visuel_" + etatMenu + "-" + posAnim).siblings().fadeOut();
                        $(".slideshow_zone_gauche").find("#visuel_" + etatMenu + "-" + posAnim).fadeIn();
                        posAnim++;
                    });
                } else {
                    etatMenu = 2; posAnim = 1;
                    $("#ssmenu_1").find(".slideshow_fleche").animate({ top: -30 }, 500, function(){
                        $("#ssmenu_1").find("li").last().css("color", "#6c6c6c");
                    });
                }
            break;
            // -- CAS 2 ----------------------------------------------------- //
            case 2:
                var nbMachines = $("#ssmenu_2 li").length;
                
                $("#lien_melange").removeClass("selected");
                $("#lien_paillage").addClass("selected");
                $("#lien_desilage").removeClass("selected");                
                $("#ssmenu_1").stop(true, true).animate({ height:235 }, 500);
                $("#ssmenu_1").stop(true, true).animate({ marginTop:0 }, 500);
                $("#ssmenu_3").stop(true, true).animate({ height:195 }, 500);
                $("#ssmenu_3").stop(true, true).animate({ marginTop:0 }, 500);
                $("#ssmenu_2").stop(true, true).animate({ height:235 }, 500);
                $("#ssmenu_2").stop(true, true).animate({ marginTop:250 }, 1000, function(){
                    if(posAnim == 1 && etatMenu == 2){
                        $("#ssmenu_2").find(".slideshow_fleche").animate({ top: hauteurDepart }, 500, function(){
                            $("#ssmenu_2").find("li").first().css("color", "white");
                        });
                    }
                });
                
                if(posAnim <= nbMachines) {
                    var nbCarac = ($("#ssmenu_2").find(".slideshow_fleche").css("top")).indexOf('x');
                    var decalageHaut = ($("#ssmenu_2").find(".slideshow_fleche").css("top")).substring(0,nbCarac-1);

                    $("#ssmenu_2").find(".slideshow_fleche").animate({ top: 35 + (posAnim-1)*40 }, 500, function(){
                        $("#ssmenu_2").find("#machine_" + etatMenu + "-" + posAnim).siblings().css("color", "#6c6c6c");
                        $("#ssmenu_2").find("#machine_" + etatMenu + "-" + posAnim).css("color", "white");
                        $(".slideshow_zone_gauche").find("#visuel_" + etatMenu + "-" + posAnim).siblings().fadeOut();
                        $(".slideshow_zone_gauche").find("#visuel_" + etatMenu + "-" + posAnim).fadeIn();
                        posAnim++;
                    });
                } else {
                    etatMenu = 3; posAnim = 1;
                    $("#ssmenu_2").find(".slideshow_fleche").animate({ top: -30 }, 500, function(){
                        $("#ssmenu_2").find("li").last().css("color", "#6c6c6c");
                    });
                }
            break;
            // -- CAS 3 ----------------------------------------------------- //
            case 3:
                var nbMachines = $("#ssmenu_3 li").length;
                
                $("#lien_melange").removeClass("selected");
                $("#lien_paillage").removeClass("selected");
                $("#lien_desilage").addClass("selected");
                $("#ssmenu_2").stop(true, true).animate({ height:235 }, 500);
                $("#ssmenu_2").stop(true, true).animate({ marginTop:0 }, 500);
                $("#ssmenu_1").stop(true, true).animate({ height:195 }, 500);
                $("#ssmenu_1").stop(true, true).animate({ marginTop:0 }, 500);
                $("#ssmenu_3").stop(true, true).animate({ height:195 }, 500);
                $("#ssmenu_3").stop(true, true).animate({ marginTop:250 }, 1000, function(){
                    if(posAnim == 1 && etatMenu == 3){
                        $("#ssmenu_3").find(".slideshow_fleche").animate({ top: hauteurDepart }, 500, function(){
                            $("#ssmenu_3").find("li").first().css("color", "white");
                        });
                    }
                });
                
                if(posAnim <= nbMachines) {
                    var nbCarac = ($("#ssmenu_3").find(".slideshow_fleche").css("top")).indexOf('x');
                    var decalageHaut = ($("#ssmenu_3").find(".slideshow_fleche").css("top")).substring(0,nbCarac-1);

                    $("#ssmenu_3").find(".slideshow_fleche").animate({ top: 35 + (posAnim-1)*40 }, 500, function(){
                        $("#ssmenu_3").find("#machine_" + etatMenu + "-" + posAnim).siblings().css("color", "#6c6c6c");
                        $("#ssmenu_3").find("#machine_" + etatMenu + "-" + posAnim).css("color", "white");
                        $(".slideshow_zone_gauche").find("#visuel_" + etatMenu + "-" + posAnim).siblings().fadeOut();
                        $(".slideshow_zone_gauche").find("#visuel_" + etatMenu + "-" + posAnim).fadeIn();
                        posAnim++;
                    });
                } else {
                    etatMenu = 1; posAnim = 1;
                    $("#ssmenu_3").find(".slideshow_fleche").animate({ top: -30 }, 500, function(){
                        $("#ssmenu_3").find("li").last().css("color", "#6c6c6c");
                    });
                }
            break;
            default:
            break;
        }
    }
*/  
    $("#lien_melange").bind("click",function() {
        if(etatMenu != 1){
            $("#lien_melange").addClass("selected");
            $("#lien_paillage").removeClass("selected");
            $("#lien_desilage").removeClass("selected");
            $("#ssmenu_2").stop(true, true).animate({ height:235 }, 500);
            $("#ssmenu_2").stop(true, true).animate({ marginTop:0 }, 500);
            $("#ssmenu_3").stop(true, true).animate({ height:195 }, 500);
            $("#ssmenu_3").stop(true, true).animate({ marginTop:0 }, 500);
            $("#ssmenu_1").stop(true, true).animate({ height:195 }, 500);
            $("#ssmenu_1").stop(true, true).animate({ marginTop:250 }, 500);
            etatMenu = 1;
            $("#ssmenu_1").find(".slideshow_fleche").animate({ top: hauteurDepart }, 500, function(){ $("#ssmenu_1").find("li").first().css("color", "white"); });
            $(".slideshow_zone_gauche").find("div").each(function(){ $(this).fadeOut(500); });
            $(".slideshow_zone_gauche").find("#visuel_" + etatMenu + "-1").fadeTo(500,1);
        }
    });
    $("#lien_paillage").bind("click",function() {
        if(etatMenu != 2){
            $("#lien_melange").removeClass("selected");
            $("#lien_paillage").addClass("selected");
            $("#lien_desilage").removeClass("selected");
            $("#ssmenu_3").stop(true, true).animate({ height:195 }, 500);
            $("#ssmenu_3").stop(true, true).animate({ marginTop:0 }, 500);
            $("#ssmenu_1").stop(true, true).animate({ height:195 }, 500);
            $("#ssmenu_1").stop(true, true).animate({ marginTop:0 }, 500);
            $("#ssmenu_2").stop(true, true).animate({ height:235 }, 500);
            $("#ssmenu_2").stop(true, true).animate({ marginTop:250 }, 500);
            etatMenu = 2;
            $("#ssmenu_2").find(".slideshow_fleche").animate({ top: hauteurDepart }, 500, function(){ $("#ssmenu_2").find("li").first().css("color", "white"); });
            $(".slideshow_zone_gauche").find("div").each(function(){ $(this).fadeOut(500); });
            $(".slideshow_zone_gauche").find("#visuel_" + etatMenu + "-1").fadeTo(500,1);
        }
    });
    $("#lien_desilage").bind("click",function() {
        if(etatMenu != 3){
            $("#lien_melange").removeClass("selected");
            $("#lien_paillage").removeClass("selected");
            $("#lien_desilage").addClass("selected");
            $("#ssmenu_2").stop(true, true).animate({ height:235 }, 500);
            $("#ssmenu_2").stop(true, true).animate({ marginTop:0 }, 500);
            $("#ssmenu_1").stop(true, true).animate({ height:195 }, 500);
            $("#ssmenu_1").stop(true, true).animate({ marginTop:0 }, 500);
            $("#ssmenu_3").stop(true, true).animate({ height:195 }, 500);
            $("#ssmenu_3").stop(true, true).animate({ marginTop:250 }, 500);
            etatMenu = 3;
            $("#ssmenu_3").find(".slideshow_fleche").animate({ top: hauteurDepart }, 500, function(){ $("#ssmenu_3").find("li").first().css("color", "white"); }); 
            $(".slideshow_zone_gauche").find("div").each(function(){ $(this).fadeOut(500); });
            $(".slideshow_zone_gauche").find("#visuel_" + etatMenu + "-1").fadeTo(500,1);
        }
    });
    
    $(".slideshow_ssmenu li").bind("click", function(){
        var posMenu = ($(this).attr("id")).substring(8,9);
        var posFleche = ($(this).attr("id")).substring(10,11);
        $("#ssmenu_" + posMenu).find(".slideshow_fleche").animate({ top: hauteurDepart + (posFleche-1)*40 }, 500, function(){
            $("#ssmenu_" + posMenu).find("#machine_"+ posMenu + "-" + posFleche).siblings().css("color", "#6c6c6c");
            $("#ssmenu_" + posMenu).find("#machine_"+ posMenu + "-" + posFleche).css("color", "white");
            $(".slideshow_zone_gauche").find("div").each(function(){ $(this).fadeOut(500); });
            $(".slideshow_zone_gauche").find("#visuel_" + posMenu + "-" + posFleche).fadeTo(500,1);
            $("#ssmenu_" + posMenu).find(".slideshow_fleche a").attr("href",$("#ssmenu_" + posMenu).find("#machine_"+ posMenu + "-" + posFleche).attr("rel"));
        }); 
    });
});
