<%#= encoding: utf-8 %>
$(document).ready(function(){

  $(document).on('click', '.survey_question_item a.remove_existing_record_question', function(){
    if(confirm("<%= I18n.t(:sure?)%>")){
      $(this).next('.should_destroy').val(1);
      $(".survey_question_item#" + $(this).prev().attr('value')).hide();
    }
  });

  $(document).on('click', '.survey_question_option_item a.remove_existing_record_option', function(){
    if(confirm("<%= I18n.t(:sure?)%>")){
      $(this).next('.should_destroy').val(1);
      $(".survey_question_option_item#" + $(this).prev().attr('value')).hide();
    }
  });

  $(document).on('click', '.survey_question_radiogroup_item a.remove_existing_record_radiogroup', function(){
    if(confirm("<%= I18n.t(:sure?)%>")){
      $(this).next('.should_destroy').val(1);
      $(".survey_question_radiogroup_item#" + $(this).prev().attr('value')).hide();
    }
  });

  $('.survey_question_item .type-selector').live('change', function(){
    $item = $(this).parents('.survey_question_item');
    $item.find('.type-specify').hide();
    $item.find('.type-specify.type-' + $(this).val()).show();
    $item.find('.type-specify.hide-type-' + $(this).val()).hide();
  });

  $('.survey_question_item .type-selector').trigger('change');

  $('.survey_question_item .add_survey_question_option a.add').live('click', function(){
    var new_id = $(this).prev().attr('value');
    var old_id = new RegExp("new_survey_question_options", "g");
    $(this).prev().attr('value', parseInt(new_id) + 1);
    $_new = $($(this).next().html().replace(old_id, new_id));
    $(this).parents('.survey_question_options_wrapper').find('tbody').append($_new);
    $_new.find('a.delete_option').on('click', function(){
      $(this).parents('.survey_question_option_item').remove();
    });
  });

  $('.survey_question_item .add_survey_question_radiogroup a.add').live('click', function(){
    var new_id = $(this).prev().attr('value');
    var old_id = new RegExp("new_survey_question_radiogroups", "g");
    $(this).prev().attr('value', parseInt(new_id) + 1);
    $_new = $($(this).next().html().replace(old_id, new_id));
    $(this).parents('.survey_question_radiogroups_wrapper').find('tbody').append($_new);
    $_new.find('a.delete_radiogroup').on('click', function(){
      $(this).parents('.survey_question_radiogroup_item').remove();
    });
  });

});