$(document).ready(function() {

    //colorbox pour les plaquettes
    $("a.jBplaquette").colorbox({width:"350px", height:"150px", iframe:true});
    $("a.jBnewletter").colorbox({width:"350px", height:"150px", iframe:true});

    var delai = 6000;

    //slider jqEncart
    var timer ;
    $(".BarreNavigation ul li").click(function(){
        clearInterval(timer);
        var n = $(this).index()+1;
        showjsEncart(n);
        return false;
    });

    function showjsEncart(n){
        var idparent = $(".BarreNavigation").parent('div').attr('id');
        var reg=new RegExp("([a-zA-Z]+)([0-9]+)","gi");
        idparent = idparent.replace(reg, "$1");
        $(".jqEncart").hide();
        $("#"+idparent+n).show();
    }

    if($(".jqEncart").length){
        var n;
        timer = setInterval(function(){
            var total = $(".jqEncart:visible .BarreNavigation ul li").length;
            
            if(n>=total){
                n = 1;
            }else{
                n = $(".jqEncart:visible .BarreNavigation ul li.Active").next(".BarreNavigation ul li").index()+1;
            }
            showjsEncart(n);
        },delai);
    };

    //sondage coloration
    $(".PropoSondage input:radio").click(function(){
        //$(".PropoSondage").parent('form.sondage:first').children(".Active").removeClass(".Active");
        $(this).parents('form.sondage').children(".Active").removeClass("Active");
        $(this).parent('.PropoSondage').addClass("Active");
    });

    $(".PropoSondage label").click(function(){
        $("~ input:radio",this).click();
    });

    //slider avec detail jContentPetiteHist
    $(".jOpenBoxInfo").click(function(){

        //window.clearTimeout(timer2);

        var id = $(this).parent("div.bloc").attr("id");
        var oldid = $(".BoutonHistActif").parent("div.bloc").attr("id");

        if(typeof(oldid)!="undefined"){
            if(id!=oldid){
                $(".PetiteHistNiveau2."+oldid).slideUp('slow',function(){
                    $(".PetiteHistNiveau2."+id).slideDown('slow');
                });
                $(".BoutonHistActif").removeClass("BoutonHistActif").addClass("BoutonHist");
            }else{
                $(".PetiteHistNiveau2."+oldid).slideUp('slow');
            }
        }else{
                $(".PetiteHistNiveau2."+id).slideDown('slow');
        }

        if($(this).hasClass("BoutonHistActif")){
                $(this).removeClass("BoutonHistActif");
                $(this).addClass("BoutonHist");
        }else{
            $(this).removeClass("BoutonHist");
            $(this).addClass("BoutonHistActif");
        }

        return false;
    });

    var pos = 1;
    var timer2;

    $(".jContentPetiteHist .jNext").click(function(){
       // window.clearTimeout(timer2);
        $(".PetiteHistNiveau2").css("display", "none");
        $(".jOpenBoxInfo").removeClass("BoutonHistActif").addClass("BoutonHist");
        var total = $(".jContentPetiteHist .bloc").length-2;

  
        if(total == pos) {
//        timer2 = setTimeout(function(){$(".jContentPetiteHist .jPrev").click();},delai);
            return;
        }
        
        if(pos<=total){
            $("#j"+pos).hide();
            $("#j"+pos).next(".separateurVertival:first").hide();
            $("#j"+(pos+3)).show();
            $("#j"+(pos+2)).next(".separateurVertival:first").show();
            
            if(pos<total)pos++;
        }

       // alert(pos+"/"+total);
        if(pos>=total)$("#barreDroite img").attr("src", pathBase+"images/barreGriseD.jpg"); else $("#barreDroite img").attr("src", pathBase+"images/barreGriseD2.jpg");
        if(pos>=1)$("#barreGauche img").attr("src", pathBase+"images/barreGriseG2.jpg"); else $("#barreGauche img").attr("src", pathBase+"images/barreGriseG.jpg");

//        if(pos>total-1){
//            timer2 = setTimeout(function(){$(".jContentPetiteHist .jPrev").click();},delai);
//        }else{
//            timer2 = setTimeout(function(){$(".jContentPetiteHist .jNext").click();},delai);
//        }
    });

    $(".jContentPetiteHist .jPrev").click(function(){
        //window.clearTimeout(timer2);
        $(".PetiteHistNiveau2").css("display", "none");
        $(".jOpenBoxInfo").removeClass("BoutonHistActif").addClass("BoutonHist");

        var total = $(".jContentPetiteHist .bloc").length-3;
        pos--
        if(pos>=1){
            
            $("#j"+pos).show();
            $("#j"+pos).next(".separateurVertival:first").show();
            $("#j"+(pos+3)).hide();
            $("#j"+(pos+2)).next(".separateurVertival:first").hide();
            if(pos<1)pos=1;
        }

        if(pos>total)$("#barreDroite img").attr("src", pathBase+"images/barreGriseD.jpg"); else $("#barreDroite img").attr("src", pathBase+"images/barreGriseD2.jpg");
        if(pos>1)$("#barreGauche img").attr("src", pathBase+"images/barreGriseG2.jpg"); else $("#barreGauche img").attr("src", pathBase+"images/barreGriseG.jpg");

//        if(pos<=1){
//            timer2 = setTimeout(function(){$(".jContentPetiteHist .jNext").click();},delai);
//        }else{
//            timer2 = setTimeout(function(){$(".jContentPetiteHist .jPrev").click();},delai);
//        }
    });

//    if($(".jContentPetiteHist").length){
//        timer2 = setTimeout(function(){$(".jContentPetiteHist .jNext").click();},delai);
//    }

    //slider avec detail jNiveau2Int
    var pos2 = 1;
    var timer3;

    function showjNiveau2Int(sens){
        var total = $(".jNiveau2Int .bloc2").length;
        var total_visible = $(".jNiveau2Int .bloc2:visible").length;
        var total_deplac = (total-total_visible)+1;

        if(sens=="next"){
            if(total_deplac == pos2) {return true;}
            $("#j"+pos2).hide();
            $("#j"+pos2).next(".separateurVertival:first").hide();
            $("#j"+(pos2+total_visible)).show();
            $("#j"+(pos2+(total_visible-1))).next(".separateurVertival:first").show();
            if(pos2<(total))pos2++;
        }

        if(sens=="prev"){
            pos2--
            if(pos2<1){pos2=1;return true;}
            $("#j"+pos2).show();
            $("#j"+pos2).next(".separateurVertival:first").show();
            $("#j"+(pos2+total_visible)).hide();
            $("#j"+(pos2+total_deplac-1)).next(".separateurVertival:first").hide();
        }
        if(pos2>=total_deplac)$("#barreDroite img").attr("src", pathBase+"images/barreGriseD.jpg"); else $("#barreDroite img").attr("src", pathBase+"images/barreGriseD2.jpg");
        if(pos2>1)$("#barreGauche img").attr("src", pathBase+"images/barreGriseG2.jpg"); else $("#barreGauche img").attr("src", pathBase+"images/barreGriseG.jpg");
        return false;
    }

    $(".jNiveau2Int .jNext").click(function(){
       // window.clearInterval(timer3);
        showjNiveau2Int("next");
        return false;
    });
    $(".jNiveau2Int .jPrev").click(function(){
        //window.clearInterval(timer3);
        showjNiveau2Int("prev");
        return false;
    });

    if($(".jNiveau2Int").length){
        var n2 = "next";
        /*
         *timer3 = setInterval(function(){
            if(n2=="next")
                if(showjNiveau2Int("next"))n2 = "prev";
            if(n2=="prev")
                if(showjNiveau2Int("prev")) n2 = "next";
        },delai);
         */
    };

});
