/*
 * 
 * selectcat.js
 * 
 * 
 * 
 * 
 */



// Si le dom est chargé
$(document).ready(function() {
  

  
  // Si on change le select des cats principales
  $('#mainCats li a').click( function(){
    
    selectMainCat($(this));
    
    // On affiche les sous cats correpondantes
    afficheSubCat($(this));
    
  });
  
  // Si on click sur une checkbox des sous-cats
  $('#subCats input').click( function(){
    // Mise à jour du résumé
    return updateResume();
    
  });
  
  // Initialisation du résumé et de l'affichage des subcats,
  // si selections par défaut

  updateResume();
  //initAfficheSubCat();

});


function selectMainCat(elmt){
  $('#mainCats li').attr('class','');
  $(elmt).parent().attr('class','on');
}

// Savoir si il y a de l'experience par defaut
var init=true;

function initAfficheSubCat(){
  $('#subCats ul:first-child').slideDown(300);
}

function countSelectedSubCats(){
  var cnt=0;
   $('#subCats ul li label input').each(function(){
        
        if($(this).attr('checked')==true){
          cnt+=1;
        }

    });
  return cnt;
}

function afficheSubCat(elmt){  
  $('#selectfirst').hide();
  id=$(elmt).attr('id')
  id=id.split('_');
  $('#subCats ul').hide();
  $('#cat_'+id[1]).slideDown(300);
}

function highlight(idEtape){

}

function updateResume(){

  // Calcul du nombre de cats selectionnées
  var cnt=countSelectedSubCats();
  

  
  if(cnt > nbMaxSubCats){
    // affichage de l'erreur
    
    // Si déjà affichée => blink
    if($('#errorCats').css('display')=='block'){
      $("#errorCats").fadeOut(200).fadeIn(200);
    }
    // Sinon affichage
    else {
      $('#errorCats').slideDown();
    }
    // On garde le nb de cats select à l'identique
    $('#nbSelectedCats').html(nbMaxSubCats);
    
    // permet de ne pas selectionner la cat
    return false;
  }
  // Si on est tjrs ok
  else {
    

    
    $('ul#resume').html('');
    $('#errorCats').slideUp(200);
    
    var tmp=':';
    
    var oldCats=$('#selectedSubCats').val();
    oldCats=oldCats.split(':');
    
    
    flag=false;
    lastSubCat=false;
    $('#subCats').each(function(){
      $('#subCats ul li label input').each(function(){
        
        if($(this).attr('checked')==true){
          $('ul#resume').append('<li id="subcat'+$(this).val()+'"><label class="checkbox"><input type="checkbox" checked="checked" onclick="supprSubCat(\''+$(this).val()+'\');" title="'+supprLibelle+'" />&nbsp;'+subCatLibelle[$(this).val()].title+'</label></li>');
          tmp+=subCatLibelle[$(this).val()].id+':';
          flag=true
        }
      });
      

      
    });
    if(flag==true){
      $('#subcat-for-alert').hide();
    }
    else {
      $('#subcat-for-alert').show();
    }
    
    
    init=false;
    
    if(flag!=false){
      $('#mainCat_'+flag).attr('selected','selected');
      $('#etape3').show();
      $('#etape3 resume').show();
      cnt=countSelectedSubCats();
    }
    

    
    $('#selectedSubCats').val(tmp);
    $('#nbSelectedCats').html(cnt);
    if(cnt>0){
      $('#etape3').css('visibility','visible');
      $('#etape3').fadeTo(300,1);
      highlight('');
    }
  }
  
}


function supprSubCat(id){
  $('#'+id).attr('checked','');
  $('#subcat'+id).fadeOut(200);
  //$('#nbSelectedCats').html(countSelectedSubCats())
  updateResume();
  $('#errorCats').slideUp(200);
  
  var tmp=':';
    
  $('.subCatCheckBox').each(function(){
    
    if($(this).attr('checked')==true){
      tmp+=subCatLibelle[$(this).val()].id+':';
      
    }
    
  });
  $('#selectedSubCats').val(tmp);
}
