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



// Si le dom est chargÃ©
$("post_projet_codeur").ready(function() {
  

  
  // Si on change le select des cats principales
  $('#mainCats').change( function(){
    
    // On affiche les sous cats correpondantes
    afficheSubCat($(this).attr('value'));
    
  });
  
  // Si on click sur une checkbox des sous-cats
  $('.subCat p label 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 initAfficheSubCat(){
  temp=0;
  $('#mainCats option').each(function(){
    
    if($(this).attr('selected')==true){      
      temp+=1; 
      afficheSubCat($(this).val());
      return false;
    }
    
  });
  if(temp==0) {
    $('#etape2').fadeTo(300,0);
    highlight('etape1');
  }
}

function countSelectedSubCats(){
  var cnt=0;
  
  $('.subCatCheckBox').each(function(){
    
    if($(this).attr('checked')==true){
      cnt+=1;
      
    }
    
  });
  
  return cnt;
}

function afficheSubCat(id){  
  $('.subCat').hide();
  $('#cat_'+id).show();
  
  $('#etape2').css('visibility','visible');
  $('#etape2').fadeTo(300,1);
  highlight('etape2');
}

function highlight(idEtape){
  $('#etape1').attr('class','no-highlight');
  $('#etape2').attr('class','no-highlight');
  $('#etape3').attr('class','no-highlight');
  
  if(idEtape!=''){
    $('#'+idEtape).attr('class','highlight');
  }
}
function initOpacity(){
  $('#etape2').fadeOut();
}
function updateResume(){

  // Calcul du nombre de cats selectionnÃ©es
  var cnt=countSelectedSubCats();
  if(cnt>0){
      $('#etape3').css('visibility','visible');
      $('#etape3').fadeTo(300,1);
      highlight('');
  }
  
  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 {
    $('#nbSelectedCats').html(cnt);
    
    if(cnt > 0){
      $('#coche-ok').css({'visibility' : 'visible'});
    }
    else {
      $('#coche-ok').css({'visibility' : 'hidden'});
    }
    
    $('ul#resume').html('');
    $('#errorCats').slideUp(200);
    
    var tmp=':';
    
    $('.subCatCheckBox').each(function(){
      if($(this).attr('checked')==true){
        $('ul#resume').append('<li id="subcat'+$(this).val()+'"><label class="label-subcats"><input type="checkbox" checked="checked" onclick="supprSubCat(\''+$(this).val()+'\');" title="'+supprLibelle+'" />&nbsp;'+subCatLibelle[$(this).val()].title+'</label></li>');
        tmp+=subCatLibelle[$(this).val()].id+':';
      }
      
    });
    
    $('#selectedSubCats').val(tmp);
    if(cnt==0){
      $('#etape3').fadeTo(300,0);
      highlight('etape2');
    }
  }
  
}


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);
}

