$(document).ready(function(){

  if( $('#candidate_profile_advance_notice_1') ) {
      if( $('#candidate_profile_advance_notice_1').attr('checked') == 1 ){
        switchToAdvanceNotice();   
      } else {
        switchToUnemployed()
      }
      
      $('#candidate_profile_advance_notice_1').click( function() {
        switchToAdvanceNotice();
      });
      
        $('#candidate_profile_advance_notice_0').click( function() {
        switchToUnemployed();
      });
  }
  
  if( $('#candidate_is_diploma_validated_1') ) {  
    if( $('#candidate_is_diploma_validated_1').attr('checked') == 1 ){
      switchDiplomaDate(true);
    } else {
      switchDiplomaDate(false);
    }
    
    $('#candidate_is_diploma_validated_1').click( function() {
      switchDiplomaDate(true);
    });
    
      $('#candidate_is_diploma_validated_0').click( function() {
      switchDiplomaDate(false);
    });
    
  }
  
  // Switching for candidacy status change
  if( $('#job_candidate_status_id') ) {
    $('#job_candidate_status_id').change(function() {
      newStatus = $(this).val();
      switch(newStatus) {
        case '1':
        case '2':
        case '6':
          $('#candidacy_reject_li').hide();
          $('#candidacy_ok_li').hide();
          $('#candidacy_wait_li').hide();
        break;
        
        case '3':
          $('#candidacy_reject_li').show();
          $('#candidacy_ok_li').hide();
          $('#candidacy_wait_li').hide();
        break;
        
        case '4':
          $('#candidacy_reject_li').hide();
          $('#candidacy_ok_li').show();
          $('#candidacy_wait_li').hide();
        break;
        
        case '5':
          $('#candidacy_reject_li').hide();
          $('#candidacy_ok_li').hide();
          $('#candidacy_wait_li').show();
        break;
      }
    });
  }                                       
  if($("#search_candidate")) {
    $("#search_candidate").slideUp();
  }
  if($("#search_candidate_btn")) {
      $("#search_candidate_btn").toggle(function(){
        $("#search_candidate").slideDown(); 
        $("#search_candidate_btn").addClass("search_candidate_btn_deploy"); 
        $("#search_candidate_btn").removeClass("search_candidate_btn");
      }, function(){ 
        $("#search_candidate").slideUp(); 
        $("#search_candidate_btn").addClass("search_candidate_btn"); 
        $("#search_candidate_btn").removeClass("search_candidate_btn_deploy"); 
      });
  }
  
  // add a reset button after file uploads fields
  var file_input_index = 0;
  $('input[type=file]').each(function() {
    file_input_index++;
    $(this).wrap('<div id="file_input_container_'+file_input_index+'"></div>');
    $(this).after('<input type="button" value="Vider" class="clean" onclick="reset_html(\'file_input_container_'+file_input_index+'\')" />');
  });
});

// reset an input file field
function reset_html(id) {
    $('#'+id).html($('#'+id).html());
}

function switchDiplomaDate( on ){
  if( on ){
    $('#diploma-date').show();
  } else {
    $('#diploma-date').hide();
  }
  
}

function switchToAdvanceNotice(){
   $('.advance-notice').each(function() {
    $(this).show();
  });
  
  $('.unemployed').each(function() {
    $(this).hide();
  }); 
}

function switchToUnemployed(){
  $('.advance-notice').each(function() {
    $(this).hide();
  });
  
  $('.unemployed').each(function() {
    $(this).show();
  });  
}


function _initExperience(max_experience){
   //experiences - remove add button if max
  if( max_experience >= 0 ){
    count_exp = $('.experience').size();  
    if (count_exp == max_experience){
      $('#experiences-add').hide();  
    }
  }
}

/**
 * Ajax call to add a new job experience
 */
function addExperience(max_count){
  var count = $('.experience').size();
  $.ajax({
    type:     "POST",
    url :     "/register/candidateExperienceForm", 
    data:     {count: count},
    dataType: "html",
    success : function(data) {
      $('#experiences').append(data);  
      if( max_count >= 0  ){
        if (count + 1 == max_count){
          $('#experiences-add').hide();  
        }
      }
    }
  });
  return false;  
}

/**
 * Ajax call to add a new language
 */
function addLanguage(max_count){
  var count = $('.language').size();
  $.ajax({
    type:     "POST",
    url :     "/register/candidateLanguageForm", 
    data:     {count: count},
    dataType: "html",
    success : function(data) {
      $('#languages').append(data);  
    }
  });
  return false;
}
