//var myTimer;

$(document).ready(function(e){
   $('p.root a').click(function(e){
        e.preventDefault();
        var id = $(this).closest('p').attr('id');
        var cpt_r = $(this).closest('div').find('p.child.id_' + id).length;
        var cpt_a = $(this).closest('p').nextUntil('p.root','p.annee').length;
        var myA = $(this);
        if(cpt_r > 0)
        {
               
               if($(this).is('.open'))
                {
                        //closeAll();
                        myPs = $('p.projets:visible');
                        myActus = $('div.txt.annee.actualite:visible');
                        myAnnee = $(this).closest('p').nextUntil('p.root','p.annee');
                        var myCallback = function(){
                                myA.removeClass('open');
                                myA.closest('p').nextUntil('p.root','p.v').show();
                                //$(this).closest('div').find('p.child.id_' + id).hide();
                                myA.closest('div').find('br').hide();
                                myA.closest('div').find('br.init').show();
                                $("p.projets").hide();
                                animateInlineElement(myA.closest('div').find('p.child.id_' + id),'hide',function(){
                                    myA.closest('p').find('span.h').hide(); 
                                });
                                getContentCategorie(0);
                                
                        };
                        
                        if(myPs.size() > 0)
                        {
                            
                            closeAllChild(function(){
                                myCallback();
                            },'projet');
                        }
                        else if(myActus.size() > 0)
                        {
                            
                            closeAllChild(function(){
                                myCallback();
                            },'txt');
                        }
                        else if(myAnnee.size() > 0)
                        {
                            
                            closeAllChild(function(){
                                myCallback();
                            },'projet');
                        }
                        else
                        {
                            myCallback();
                        }
                        
                        //closeAllChild(function(){},'txt');
                        
                        
                }
                else
                {
                   
                   closeAll(
                       function(){
                           myA.addClass('open');
                           myA.closest('p').find('span.h').show();
                           //on enleve la virgule
                           myA.closest('p').nextUntil('p.root','p.v').hide();
                           //on montre les enfants
                           myA.closest('div').find('p.child.id_' + id + " a").addClass('hover');
                           //$(this).closest('div').find('p.child.id_' + id).show();
                           animateInlineElement(myA.closest('div').find('p.child.id_' + id),'show',function(){
                           });
                           
                           myA.closest('div').find('br').hide();
                           getContentCategorie(id);
                            
                       },
                       e
                   );
                   
                }  
        }
        else if(cpt_a > 0)
        {
            myA.closest('p').nextUntil('p.root','p.annee').find('a').css('color','#000');
            myA.closest('p').nextUntil('p.root','p.annee').find('a').hover(
                function(){
                    $(this).attr('style','');
                },
                function(){
                    $(this).css('color','#000');
                }
            );
            if($(this).is('.open'))
            {
                var myCallback = function(){
                    myA.removeClass('open');
                   //myA.closest('p').nextUntil('p.root','p.annee').hide();
                   animateInlineElement(myA.closest('p').nextUntil('p.root','p.annee'),'hide',function(){myA.closest('p').find('span.h').hide();});
                   myA.closest('p').nextUntil('p.root','div.txt').hide();
                   getContentCategorie(0);
                   myA.closest('p').nextUntil('p.root','p.v').show(); 
                };
                //closeAllChild(function(){
                   
                //},'txt');
                closeAllTxtAnnee(function(){
                        myCallback();
                });
                   
            }
            else
            {
               
               closeAll(
                    function(){
                       getContentCategorie(id);
                       myA.addClass('open');
                       myA.closest('p').find('span.h').show();
                       animateInlineElement(myA.closest('p').nextUntil('p.root','p.annee'),'show',function(){});
                       //$(this).closest('p').nextUntil('p.root','p.annee').show();
                       myA.closest('p').nextUntil('p.root','p.v').hide();
                    },
                    e
                ); 
                
                   
                   
            } 
        }
        else
        {
           /* if($(this).is('.open'))
            {
                var myCallback = function(){
                    myA.removeClass('open');
                    $('div#content_categorie').html('');
                   
                   
                };
                //closeAllChild(function(){
                   
                //},'txt');
                closeAllChild(function(){
                        myCallback();
                },'projet');
                   
            }
            else
            {
               
               closeAll(
                    function(){
                       myA.addClass('open');
                       
                        $('div#content_categorie').load(
                            url_categorie + "/id/" + id
                        );
                    },
                    e
                ); 
                
                   
                   
            }*/ 
           
        }
        setColor($(this).closest('p'));
        
        
        
   });
   /*$('p.root a').hover(
        
        function(){
             myDefaultColor = getDefaultColorCss();
             setColor($(this).closest('p'),myDefaultColor);
             
        },
        function(){
            
           if($(this).is('.open'))
           {
               
               
           }
           else
           {
               setColorCss(myDefaultColor);
           }
           
        }
);*/
   $('p.child a').click(function(e){
        e.preventDefault();
        //clearTimeout(myTimer);
        var root_cat = $(this).closest('p').firstBefore('p.root');
        var cpt_p = $(this).closest('p').nextUntil('p.child','p.projets').length;
        var cpt_a = $(this).closest('p').nextUntil('p.child','p.annee').length;
        //var cpt_a = $(this).closest('p').nextUntil('p.child','div.txt.annee').length;
        var myA = $(this);
        var txt_p = myA.closest('p').find('span.description').text();
        /*if(cpt_a > 0)
        {
            //check root ?
            cpt_r = $(this).closest('p').nextUntil('p.root','div.txt.annee').length;
            if(cpt_r > 0)
            {
                
            }
            else
            {
                cpt_a = 0;
            }
        }*/
        //alert(cpt_a);
        //cpt_a = 0;
        if(cpt_p > 0 || txt_p != "")
        {
            
            if($(this).is('.open'))
            {
                   
                   closeAllVignette(function(){
                       
                       
                       //myA.closest('p').nextUntil('p.child','p.projets').hide();
                       animateInlineElement(myA.closest('p').nextUntil('p.child','p.projets'),'hide',function(){
                           root_cat.find('a').attr('style','');
                           myA.removeClass('open');
                           myA.closest('p').find('span.description').hide();
                           myA.closest('p').find('span.s').removeClass('open')
                           myA.closest('p').next('br.projets').hide();
                           $('p.child a').addClass('hover');
                           $('div#content_categorie').html('');
                           myId = root_cat.attr('id');
                           getContentCategorie(myId);
                       });
                       
                       
                   });
                   
                   
            }
            else
            {
                   var myId = $(this).closest('p.child').attr('id');
                   closeAllChild(function(){
                       root_cat.find('a').css('color','#000');
                       myA.addClass('open');
                       myA.closest('p').find('span.description').show();
                       myA.closest('p').find('span.s').addClass('open');
                       //myA.closest('p').nextUntil('p.child','p.projets').show();
                       animateInlineElement(myA.closest('p').nextUntil('p.child','p.projets'),'show',function(){});
                       $('p.child a').removeClass('hover');
                       myA.closest('p').next('br.projets').show();
                       
                       
                        myId = getIdFromString(myId,'child_');
                        getContentCategorie(myId);
            
                   },'projet');
                  

            }
        }
        if(cpt_a > 0)
        {
           
            if($(this).is('.open'))
            {
                    root_cat.find('a').attr('style','');
                    myA.removeClass('open');
                   //myA.closest('p').nextUntil('p.root','p.annee').hide();
                   animateInlineElement(myA.closest('p').nextUntil('p.root','p.annee'),'hide',function(){myA.closest('p').find('span.h').hide();});
                   myA.closest('p').nextUntil('p.root','div.txt').hide();
                   myId = root_cat.attr('id');
                   getContentCategorie(myId);
                   
                
                
                
                   
            }
            else
            {
                       var myId = $(this).closest('p.child').attr('id');
                       root_cat.find('a').css('color','#000');
                       myA.addClass('open');
                       myA.closest('p').find('span.h').show();
                       animateInlineElement(myA.closest('p').nextUntil('p.root','p.annee'),'show',function(){});
                       //$(this).closest('p').nextUntil('p.root','p.annee').show();
                       myA.closest('p').nextUntil('p.root','p.v').hide();
                       myId = getIdFromString(myId,'child_');
                       getContentCategorie(myId);
                
               
            } 
        }
        
        
        
        

   });
   $('p.projets a').click(function(e){
       /*e.preventDefault();
       var id = $(this).closest('p').attr('id');
       var cpt = $(this).closest('p').nextUntil('p.projets','div.vignette').length;
       var myA = $(this);
       if(cpt == 1)
       {
           if($(this).is('.open'))
            {
                   myA.removeClass('open');
                   mySlideUp(id,function(){});
            }
            else
            {
                
                   closeAllVignette(function(){
                   });
                   myA.addClass('open');
                   mySlideDown(id);
                   
                   
                   
                   
            }
       }*/
   });
   $('p.annee a').click(function(e){
       e.preventDefault();
       var annee_c = $(this).closest('p').attr('class');
       annee_c = getIdFromString(annee_c, 'annee ');
       var cpt = $(this).closest('p').nextUntil('p.child','div.txt').length;
       if(cpt > 0)
       {
           if($(this).is('.open'))
            {
                   $(this).removeClass('open');
                   mySlideUpTxtAnnee($(this), annee_c,function(){})
                   
            }
            else
            {
                
                    var myA = $(this);
                    closeAllTxtAnnee(function(){
                        $('p.annee a').removeClass('open');
                        myA.addClass('open');
                    });
                    
                    mySlideDownAnnee($(this) , annee_c)
                   
            }
       }
   });
   
   $('div.txt.annee dl dt a').click(function(e){
       e.preventDefault();
       myDd = $(this).closest('dl').find('dd');
       if(myDd.is('.publication'))
       {
           $(this).addClass('hover');
           
           if(myDd.is('.open'))
           {
               myDd.hide();
               myDd.removeClass('open');
           }
           else
           {
               $('div.txt.annee dl dd.publication').hide();
               myDd.show();
               myDd.addClass('open');
           }
       }
       
   });
   $('div.txt.annee dl dd.publication').closest('div.col').css('padding-bottom','20px');
   $('div.txt.annee').find('br').addClass('annee');
    $("body").mousemove(function(event) {
        disableKeyNav();
    });
    
   $('h1').click(function(e){
       e.preventDefault();
       closeAll(function(){
           $('div#accueil').show();
       },e);
       
   });
   
   $('div.txt.annee dl dd img').load(function(e){
      $(this).closest('div.col').css('width','auto');
      $(this).closest('dl').css('width','auto');
   });
   
});


