var JQ = jQuery.noConflict();
//alert('ok');

JQ( document ).ready(function( ) {


    var search = JQ("#ramSearch");
    var connexion = JQ("#ramConnexion");
    var searchBtn = JQ("#ramSearchBtn");
    var connectBtn = JQ("#ramConnectBtn");
    
    
 
   ///// fermeture de et ouverture  
    
    connectBtn.click(function(){
      
        if ( connexion.is( ":hidden" ) ) { 
            if( search.is( ":visible" ) ){
                search.slideUp( "fast" ); 
                searchBtn.removeClass('active');
            } 
            connexion.slideDown( "fast" );
            JQ(this).addClass('active');
        }/*else{   
            connexion.slideUp("slow" );   
        }*/
      
    });


     searchBtn.click(function(){
      
        if ( search.is( ":hidden" ) ) {
            
            if( connexion.is( ":visible" ) ){ 
                connexion.slideUp( "fast" ); 
                connectBtn.removeClass('active');
             
            }
            JQ(this).addClass('active');
            search.slideDown( "fast" );
   
        }
      
    });

    ////// fermeture les volets par 
    
    JQ('.close').click(function(){
        connectBtn.removeClass('active');
        searchBtn.removeClass('active');
        connexion.hide(0);
        search.hide(0);

    });
    
    /******* Menu********/
    JQ('.closeMenu').click(function(){
        JQ("#uMenu").hide(0);
        JQ("#fog").hide(0);
    });
    
    JQ('#burger').click(function(){
        
        JQ("#fog").fadeIn(200);
        JQ("#uMenu").fadeIn(200);
    });
    
    /// accordion
    
    JQ('#accordion > div').click(function(){
        
        if( JQ(this).next("ul").eq(0).is( ":visible" ) ){ 
 
            JQ(this).next("ul").eq(0).slideUp(100);
            JQ(this).css("background-position", "245px -50px");
        }else{
            JQ(this).next("ul").eq(0).slideDown(100); 
            JQ(this).css("background-position", "245px 13px");
        
        }

        //JQ(this).addClass('active');

    });
    
    /**** panel Drop ***/
 
    
   
    
    
    JQ(".panelDrop > span").click(function(){
            
         if ( JQ(".panelDrop > div").height() === 80 ) { 
           
            JQ(".panelDrop > div").animate({ 
                height:  350
            },500,function(){JQ(".panelDrop > span").css("background-position","0 0");});

        }else{
            
            JQ(".panelDrop > div").animate({ 
                height: 80
            },500,function(){JQ(".panelDrop > span").css("background-position","0 -96px");});

            
            
        }
      
    });
    
    // panel-faq
    JQ(".other > a").click(function(){           
         if ( JQ(".pannel-faq .row").height() === 125 ) { 
           
            JQ(".pannel-faq .row").animate({ //hauteur total de chauqe ligne
                height: ( (JQ(".pannel-faq .row > div").length)  )*150 
            },500,function(){

                JQ(".other > a > span ").removeClass("glyphicon-menu-down");
                JQ(".other > a > span ").addClass("glyphicon-menu-up");
        
            });

        }else{
            
            JQ(".pannel-faq .row").animate({ 
                height: 125
            },500,function(){
                JQ(".other > a > span ").removeClass("glyphicon-menu-up");
                JQ(".other > a > span ").addClass("glyphicon-menu-down");
            });     
        }
    });
    
    console.log(JQ(".pannel-faq > div ").length+"  "+(JQ(".pannel-faq .row > div ").length)*150 );
     
     
    // panel-search
    
    JQ(".selectProfile > li").click(function(){
        
        JQ(this).index();
        
       JQ("field-profile").text(JQ(this).index().text());
       
       
       
        //JQ("#selectProfile > option").attr
        
        
        
        //console.log( );
        
    });
     
     
    
    /*** Carousel ***/ // BOOTSTRAP //
    
    //JQ('#myCarousel').carousel({interval: 4000});
            
    JQ("#myModal").modal('hide');

    JQ("#vid").click(function(){

        JQ("#myModal").modal("show");
        JQ("#myModal .modal-body").html(
           '<video fullScreen controls autoplay poster="http://sandbox.thewikies.com/vfe-generator/images/big-buck-bunny_poster.jpg" width="100%" height="100%"><source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" type="video/mp4"></video>'    
        );
    });

    JQ("#myModal .close").click(function(){
        JQ("#myModal .modal-body").html('');           
    });
    
    
    /******* ACTU *******/
 if(JQ("#actus").size()){ 
     
  
    var navCursor   =   JQ("navCursor"), 
        globalSize  =   JQ(".globalSlide").width(), //taille total du slide    
        mountActu   =   JQ(".globalSlide > ul"),
        dayActu     =   JQ(".globalSlide > ul > li:eq(0)").width()+1,
        prev        =   JQ(".prevM"),
        next        =   JQ(".nextM"),
        
    
        test        = 0,
        slidePos    = 0;
     
     
     
   /* if( JQ("body").width() < 1000){   
        JQ(".globalSlide > ul> li ").css( "width", JQ("#mois").width());
        
    } //TEST 1000*/
     
     
    //console.log(dayActu);
    //console.log( JQ(".globalSlide > ul").eq(1).html());
    
      



    // JQ(".currentM").next().css("background", "red");
    //JQ(".currentM").css("background", "red");
    //JQ(".currentM").next("ul").width();
    
    var posLeft     =   JQ(".currentM").position().left;
    var sizeWidth   =   JQ(".currentM").width();
  
    //console.log( posLeft+'     '+sizeWidth);

    
        
    
        
    ////// Navigation dans la time line  

    var spaceBlist = [];        
    var widthUl = [];
    var heightUl = [];
    
    //var i = 0;
    var addSize_w = 0; 
    var addSize_h = 0;
   
   
   /*** initialisation du curseur et du mois dans la time-line ***/
    var spaceB = 0;
    var cursorIndex = 0;


    function spaceBefore(){  // init
        
        
         mountActu.each(function(index){ // initcursor & init size
        
            if( JQ(this).html() === "" ){
               // console.log("OK");
                JQ("#navCursor > li").eq(index).addClass("void");
                JQ(this).css("width","0");

            }
            addSize_w += JQ( this ).width();
            spaceBlist.push(addSize_w);
            widthUl.push( JQ( this ).width());
            addSize_h += JQ( this ).height();
            /*JQ(this).find("li").each(function(index){ // pour connetre la hauteur de la cellule 
                addSize_h += JQ( this ).height(); 
                console.log(index);
            });*/


            addSize_h += JQ( this ).height();
            heightUl.push( addSize_h );
            addSize_h = 0;//re init
            //console.log(addSize[index]);

        });
    
        
        
        mountActu.each(function(){ // position  initiel du carousel
            //
           //console.log(JQ(this).attr('class'));
            if(JQ(this).attr('class') !==  "currentM"){
                /*if( JQ("body").width() > 1000){ 
                    spaceB +=  JQ(this).width(); 
                    cursorIndex++;
                
                }else{ }*/
                    spaceB +=  JQ(this).width(); 
                    cursorIndex++;
                    
                    
               
               // console.log("spaceB => " +spaceB +' '+(cursorIndex) ); 
            }else{ 
                JQ("#navCursor > li").eq(cursorIndex).addClass("cursorCurren");
                
                return false; // i breack 
                console.log( 'CURSOR = '+(cursorIndex));
            } 
       });
       
    }spaceBefore(); 
    
    

   
    //console.log("taille total => "+ globalSize);

    // console.log("spaceB:  "+ spaceB +"  "+posLeft );//console.log( 
    JQ(".globalSlide").css("left", (slidePos = -spaceB)); // position initial
   
   
   
   
   
    for(i=0; i< spaceBlist.length; i++ ){  //test
       console.log(i+"   w:"+widthUl[i]+"   h:"+heightUl[i]+ "   "+spaceBlist[i]);
    }

   
    
    next.click(function(){

        test = slidePos + globalSize - dayActu;
        //console.log( test ); 
        if( JQ("body").width() > 1000){ 
            if( test !== 0){

                slidePos -= dayActu;


                JQ(".globalSlide").css("left",(slidePos));
                //console.log( posLeft+'     '+sizeWidth);            
                console.log( JQ(".globalSlide").position().left );        

                JQ("#navCursor > li").removeClass("cursorCurren");


                for(i=0; i< spaceBlist.length; i++ ){ // cursor

                   if((-spaceBlist[i]+ dayActu) < JQ(".globalSlide").position().left){
                        //console.log( spaceBlist[i]+"  "+(test)+"  "+(i+1)  );
                        JQ("#navCursor > li").eq(i).addClass("cursorCurren"); ////! a optimisé/
                        //mountActu.eq(i).css("height", heightUl);
                       
                        break;                
                    }

                }

            }else{console.log("NO");}      
  
        }else{  ///////////////////////////////////////////////////////////////////////////////
             
            test = slidePos + globalSize - dayActu;     
            // console.log(dayActu+"  "+_w+"  "+slidePos+"     "+test+"     "+JQ(".globalSlide").position().left  );
            ///console.log( JQ(".globalSlide").position().left );
            
            if( test > (-addSize_w+dayActu*2 ) ){ /** x2 ?*/

                slidePos -= dayActu;
                
                console.log( JQ(".globalSlide").position().left+"  slidePos  "+slidePos  );
                JQ(".globalSlide").css("left",(slidePos));         
                //console.log( JQ(".globalSlide").position().left );        
                JQ("#navCursor > li").removeClass("cursorCurren");

                for(i=0; i< spaceBlist.length; i++ ){  // cursor

                   if((-spaceBlist[i]+ dayActu) < JQ(".globalSlide").position().left){
                       // console.log( spaceBlist[i]+"  "+(test)+"  "+(i+1)  );
                        JQ("#navCursor > li").eq(i).addClass("cursorCurren"); ////! a optimisé/                    
                        JQ("#mois").css("height", heightUl[i]/2);
                        //console.log("mois = "+(i+1)) ;
                        break;                
                    }

                }
                //console.log(test+" "+widthUl[cursorIndex]+" "+(test-widthUl[cursorIndex]));
                //console.log( JQ(".globalSlide").position().left );
                //console.log( posLeft.left+'     '+sizeWidth);
            }else{console.log("NO");}
            
            
        }    

    });

    console.log(JQ("body").width());

    prev.click(function(){
        if( JQ("body").width() > 1000){
            if(slidePos < 0){
                slidePos += dayActu;
                JQ(".globalSlide").css("left", (slidePos));  
                //console.log( JQ(".globalSlide").position().left );
                JQ("#navCursor > li").removeClass("cursorCurren");

                for(i=0; i< spaceBlist.length; i++ ){  //cursor

                   if((-spaceBlist[i]+ dayActu) <= JQ(".globalSlide").position().left){

                        //console.log( spaceBlist[i]+"  "+(test)+"  "+(i+1)  );
                        JQ("#navCursor > li").eq(i).addClass("cursorCurren"); ///! a optimisé/                  
                        break;                 
                    }
                }

            }else{console.log("NO");}
            
        }else{   ///////////////////////////////////////////////////////////////////////////////
            
            if(slidePos < 0){
                slidePos += dayActu;
                JQ(".globalSlide").css("left", (slidePos));  
                console.log( JQ(".globalSlide").position().left+"  slidePos  "+slidePos  );
                JQ("#navCursor > li").removeClass("cursorCurren");

                for(i=0; i< spaceBlist.length; i++ ){ 

                   if((-spaceBlist[i]+ dayActu) <= JQ(".globalSlide").position().left){

                        //console.log( spaceBlist[i]+"  "+(test)+"  "+(i+1)  );
                        JQ("#navCursor > li").eq(i).addClass("cursorCurren"); ///! a optimisé/                  
                        JQ("#mois").css("height", heightUl[i]/2);
                        break;                 
                    }
                }
            }
            
            
        }
       // console.log("next");
       // console.log( JQ(".globalSlide").css("left"));
    });

    var JQwindow             =   JQ(window),
        positionMinNav      =   JQ("#actus").position().top + JQ("#bodyNav").position().top/*+JQ("#newsLine > .navSlideFloat").position().top() */ ,
        positionMaxNav    =     JQ("#footer").position().top ,
        navSlideFloat       =   JQ("#navSlideFloat");
        // navSlideFloat.css("border","1px solid red");

        // navSlideFloat.addClass(".navCFloat");

    JQwindow.scroll(function () {

        if (positionMinNav < JQwindow.scrollTop()){
            //navSlideFloat.css("border","1px solid red");

           // console.log(navSlideFloat.size()+ "  POsition-top  "+positionMinNav );
            JQ("#navFloat  .navSlideFloat").show(0);

            //navSlideFloat.addClass("navCFloat");

        }else{
            JQ("#navFloat  .navSlideFloat").hide(0);
        }

        if (JQwindow.scrollTop() > positionMaxNav + 60) {
            JQ("#navFloat  .navSlideFloat").hide(0);

            //console.log(positionMaxNav);

         }/*else{
            JQ("#navFloat  .navSlideFloat").hide(0);
        }/*else if (JQwindow.scrollTop() > positionMaxNav) {
             navSlideFloat.removeClass("navCFloat");
             console.log("scroll");
         }*/

    });

       
 } //end if(JQ("#actus").size())        
         
         
         
});