$(document).ready(function(){
    //ajouter une fleche sous le menu actif
    $('#menu .current').ready(function(){
        $('#menu .current').append('<div id="arrow_current"></div>');
    });
    
    /**Animation people**/
    $(".bloc_animation_people #float_animation").ready(function(){
        var duration = 5000;
        var nb_bl_pl = 0;
        var it=0;
        var id_current=1;
        var new_current=0;

        //lancer l'initialisation
        init_animation();

        //lancer l'nanimation
        if(nb_bl_pl>1){
            
            setInterval(function(){

                animation_bl_people_animation();
              },duration);
        }

        /**fonctions liées à l'animation**/
        function init_animation(){
            //compter tous les bl_pl
            $('.bloc_animation_people #float_animation').find('.content_bl_pl_anim').each(function(){
               nb_bl_pl++;
            });
            for(var i=1;i<(nb_bl_pl+1);i++){
                if(i==1){
                    $('.bloc_animation_people #float_animation').find('#bl_pl_'+i).addClass('current_bl_pl');
                }
                $('.bloc_animation_people #float_animation').find('#bl_pl_'+i).css('z-index', ((nb_bl_pl+1)-i));
            }
        }

        function animation_bl_people_animation(){
           id_current=$('.bloc_animation_people #float_animation').find('.current_bl_pl').attr('id').split('_');

           //extraction du numéro
           id_current=parseInt(id_current[2]);

           //initialisation de l'iteration
           it=nb_bl_pl;
           
           //animation fadeout
           $('.bloc_animation_people #float_animation').find("#bl_pl_"+id_current).fadeOut("slow",function(){

               //suprimmer tout les style des content_bl_pl_anim
               $('.bloc_animation_people #float_animation').find('.content_bl_pl_anim').removeAttr('style');

               //on supprime la class current_bl_pl
               $('.bloc_animation_people #float_animation').find("#bl_pl_"+id_current).removeClass('current_bl_pl');

               //on assigne au [((id_current+1)%nb_bl_pl)+1] la classe current_bl_pl
               new_current=parseInt([((id_current+1)%nb_bl_pl)]);
                if(new_current==0)
                       new_current=nb_bl_pl;
               $('.bloc_animation_people #float_animation').find('#bl_pl_'+new_current).addClass('current_bl_pl');
               //pour chaque bl_pl réorganiser les zindex
               while(it>0){
                  // alert(new_current);
                   //asigne le z-index
                    $('.bloc_animation_people #float_animation').find('#bl_pl_'+new_current).css('z-index', it);

                   //changer de bl_pl
                   it--;

                   //calculer le suivant
                   new_current++;
                   new_current=(new_current%nb_bl_pl);
                   if(new_current==0)
                       new_current=nb_bl_pl;

               }
           });
        }
        /**fint fonctions liée à l'animation**/
    });
    /**fin animation people**/
    /**Animation photo casino**/
    $("#photo_presentation #float_animation").ready(function(){
        var duration = 5000;
        var nb_bl_pl = 0;
        var it=0;
        var id_current=1;
        var new_current=0;

        //lancer l'initialisation
        init();

        //lancer l'nanimation
        setInterval(function(){
            animation_bl_people();
          },duration);

        /**fonctions liées à l'animation**/
        function init(){
            //compter tous les bl_pl
            $('#photo_presentation #float_animation').find('.content_bl_pl_anim').each(function(){
               nb_bl_pl++;
            });
            for(var i=1;i<(nb_bl_pl+1);i++){
                if(i==1){
                    $('#photo_presentation #float_animation').find('#bl_pl_'+i).addClass('current_bl_pl');
                }
                $('#photo_presentation #float_animation').find('#bl_pl_'+i).css('z-index', ((nb_bl_pl+1)-i));
            }
        }

        function animation_bl_people(){
           id_current=$('.current_bl_pl').attr('id').split('_');

           //extraction du numéro
           id_current=parseInt(id_current[2]);

           //initialisation de l'iteration
           it=nb_bl_pl;
           
           //animation fadeout
           $('#photo_presentation #float_animation').find("#bl_pl_"+id_current).fadeOut("slow",function(){

               //suprimmer tout les style des content_bl_pl_anim
               $('#photo_presentation #float_animation').find('.content_bl_pl_anim').removeAttr('style');

               //on supprime la class current_bl_pl
               $('#photo_presentation #float_animation').find("#bl_pl_"+id_current).removeClass('current_bl_pl');

               //on assigne au [((id_current+1)%nb_bl_pl)+1] la classe current_bl_pl
               new_current=parseInt([((id_current+1)%nb_bl_pl)]);
                if(new_current==0)
                       new_current=nb_bl_pl;
               $('#photo_presentation #float_animation').find('#bl_pl_'+new_current).addClass('current_bl_pl');
               //pour chaque bl_pl réorganiser les zindex
               while(it>0){
                  // alert(new_current);
                   //asigne le z-index
                    $('#photo_presentation #float_animation').find('#bl_pl_'+new_current).css('z-index', it);

                   //changer de bl_pl
                   it--;

                   //calculer le suivant
                   new_current++;
                   new_current=(new_current%nb_bl_pl);
                   if(new_current==0)
                       new_current=nb_bl_pl;

               }
           });
        }
        /**fint fonctions liée à l'animation**/
    });
    /**fin animation photo casino**/

    /**animation visuel actuellement**/
    $("#bloc_animation_actuelement #float_animation").ready(function(){
       var nb_visuel=0;
       var id_visuel;
       var it=0;
       var new_visuel=0;
       var duration=3000;
       var stop_animation=0;
       var fl_tb=["121px","189px","257px","325px","392px"];
       //initialisation
       init();

       //quand on clique sur un menu
       $('.min_visuel').click(function(){
           //on recupere l'id
           id_visuel=$(this).attr('id').split('_');
           id_visuel=parseInt(id_visuel[1]);

           
           $('#fleche_animation').fadeOut('fast', function(){
                $(this).css('top',fl_tb[(id_visuel-1)]);
                $(this).css('opacity','1');
                $(this).fadeIn('fast');
           });
           //suprimmer la classe current_visuel des touts les menu
           $('.min_visuel').removeClass('current_visuel');

           
           //suprimer tout les styles
           $('#bloc_animation_actuelement #blc_big_visuel img').removeAttr('style');

           //ajouter la classe à l'objet selectionné'
           $(this).addClass('current_visuel');
           
           $('#description_visuel').html($('#descript_'+id_visuel).val());
           //mettre l'elemnt selectionné en avant
           it=0;
           while(it<nb_visuel){

                $('.visuel_'+id_visuel).css('z-index',((nb_visuel-it)*2));
                $('.visuel_'+id_visuel).parent('a').css('z-index',((nb_visuel-it)*2));
                id_visuel++;
               id_visuel=(id_visuel%nb_visuel);
               if(id_visuel==0)
                   id_visuel=nb_visuel;
                it++;
           }
           
       });
       //quand le curseur est au dessus de l'animation otu s'arrete
       $('#blc_big_visuel img').hover(function(){
           stop_animation=1;
       },function(){
           stop_animation=0;
       });
       
       $('#blc_min_visuel').hover(function(){
           stop_animation=1;
       },function(){
           stop_animation=0;
       });

       //animation
        //lancer l'nanimation
        setInterval(function(){
            if(stop_animation==0)
                animation_visuel();
          },duration);

       /**fonction lié à l'animation**/
       function init(){
           //compter le nombre de menu visuel
           $('.min_visuel').each(function(){
               nb_visuel++;
           });
           $('#visuel_1').addClass('current_visuel');
           $('#description_visuel').html($('#descript_1').val());
           //pour chaque visuel les mettre dans le bon ordre
           for(var i=1;i<(nb_visuel+1);i++){

               $('.visuel_'+i).css('z-index',((nb_visuel+1)-i));
           }
       }


       function animation_visuel(){
           
           //recherche le current
           id_visuel=$('#blc_min_visuel .current_visuel').attr('id').split('_');
           
           //recuperer son id
           id_visuel=parseInt(id_visuel[1]);

           //rechercher le prochain id
           new_visuel=parseInt([((id_visuel+1)%nb_visuel)]);
           if(new_visuel==0)
               new_visuel=nb_visuel;
           
           $('#description_visuel').html($('#descript_'+new_visuel).val());
           $('#fleche_animation').fadeOut('fast', function(){
                $(this).css('top',fl_tb[(new_visuel-1)]);
                $(this).css('opacity','1');
                $(this).fadeIn('fast');
           });
           
           //fade de l'image
           $('.visuel_'+id_visuel).fadeOut('slow',function(){

               $(this).removeAttr('style');
               //suprimer le current visuel
               $('#blc_min_visuel .min_visuel').removeClass('current_visuel');

               //rechercher le prochain id
               new_visuel=parseInt([((id_visuel+1)%nb_visuel)]);
               if(new_visuel==0)
                   new_visuel=nb_visuel;
               
               //ajouter le visuel current au prochain
               $('#visuel_'+new_visuel).addClass('current_visuel');
               
               it=0;
               while(it<nb_visuel){
                   $('.visuel_'+new_visuel).css('z-index',(((nb_visuel+1)-it)*2));

                   new_visuel++;
                   new_visuel=(new_visuel%nb_visuel);
                   if(new_visuel==0)
                       new_visuel=nb_visuel;
                   it++;
               }
               
           });
       }

       /**fin de la fonction lié à l'animation**/
       
       
       
       
    });
    /**fin animation visuel**/
    
    //animation people slider
    function mycarousel_initCallback(carousel){
        // Disable autoscrolling if the user clicks the prev or next button.
        carousel.buttonNext.bind('click', function() {
            carousel.startAuto(0);
        });

        carousel.buttonPrev.bind('click', function() {
            carousel.startAuto(0);
        });

        // Pause autoscrolling if the user moves with the cursor over the clip.
        carousel.clip.hover(function() {
            carousel.stopAuto();
        }, function() {
            carousel.startAuto();
        });
    }

    jQuery('#diapo_').jcarousel({
        auto: 5,
        scroll:1,
        initCallback: mycarousel_initCallback
    });
    
    //animation de l'entet
    $('#visuel_now').hylades_transition();
    
    $("#fomulaire_offre").submit(function(){
           
           var code=$('#gchart_cg').val();
           var ig=$('#where').val();
           $.get('../includes/ajax.php',{type_ajax:'emploi_offre',id:ig,detail:code},function (data){
              if(data==0){
                alert('Les caractères ne sont pas correct');
                $('#where').val('');
                return false;
                
              }
              else
                  return true;
            });
        
    });

});
