$(document).ready(function(){
  $('.submit-link').livequery('click', function() {
    var form = $(this).parents('form');
    if ($(this).attr('href') != '#') {
      form.attr('action', $(this).attr('href'));
    }
    form.submit();
    return false;
  });

  $("#question_groups").sortable({
    handle: 'td.col-icon img',
    axis:'y',
    cursor: 'move',
    helper: function(e, ui) { return $("<table></table>").addClass
      ("helper").append(ui.contents().clone()); },
    start: function(e, ui) { $(ui.placeholder).append(ui.item.clone().find
      ("tr:first").each(function() { $(this).find("td").each(function() { $
      (this).empty()}) })); 
    },
    update: function(e, ui) {
      var position = $('tbody').index(ui.item) + 1;
      var id_regex = /question_group_(.+)/.exec(ui.item.attr('id'));

      if (id_regex == null)
        ui.item.find('input.position').val(position);
      else
        $.post(ui.item.find('a.question_group_position').attr('href'), {_method: 'put', question_group_id: id_regex[1], position: position}, null, 'json');
    }
  });

  $('.results').click(function() {
    var email = $("#results_email_input");

    if($(this).attr('checked'))
      email.show();
    else {
      email.hide();
      $('input#questionnaire_collection_email').val('');
    }
  });

  if(!$('.results').attr('checked'))
    $("#results_email_input").hide();

  $('.email-confirmation').click(function() {
    if(!$(this).attr('checked'))
      return confirm("Are you sure? This will PERMANENTLY remove the email template for this form.");
  });

  $('.email-confirmation').change(function() {
    var link = $(this).parent().find('a.edit-email-template');

    if(!$(this).attr('checked'))
      link.hide();
    else
      link.show();
  }).change();

  $("#filter_by").livequery('change', function() {
    var val = $(this).val();

    if (val == 'date range') {
      $('.search-bar #start_date').show();
      $('.search-bar #end_date').show();
    } else {
      $('.search-bar #start_date').hide();
      $('.search-bar #end_date').hide();
    }
  });

  $(".question_type_select").livequery('change', function() {
    var val = $(this).val();
    var body_name = $(this).attr('name').replace("question_type","body");
    var exp_text = $("textarea[name='" + body_name + "']").parents("tr");
    var choice = exp_text.next();
    var required_name = $(this).attr('name').replace("question_type","required");
    var required = $("input[name='" + required_name + "']")

    if (val == "explanatory_text")
      exp_text.show();
    else
      exp_text.hide();

    if (/^(heading|explanatory_text)$/.test(val)) {
      required.attr('checked', false);
      required.hide();
    } else
      required.show();

    if (/^(check_box|single_drop_down|radio)$/.test(val))
      choice.show();
    else
      choice.hide();

  });

  $(".new-question-group-destroy").livequery('click', function() {
    $(this).parents('.question_group').fadeAndRemove();
    return false;
  });

  $('.add-choice-button').livequery('click', function(event) {
    if (typeof attribute_fu_choice_count == 'undefined') 
      attribute_fu_choice_count = 0;

    var id_regex = /attributes\](.+)\[body/.exec($(this).parents('tr').prev().find('textarea').attr('name'));
    var question_group_id = id_regex[1];

    $(this).parents('tr').find('.choices').append($.template('<div class="choice">  <input id="questionnaire_question_group_attributes_new___number__choice_attributes_new___number__choice_label" name="questionnaire[question_group_attributes]${question_group_number}[choice_attributes][new][${number}][choice_label]" size="30" type="text" />  <a href="#" class="check-box-destroy"><img alt="Icon-x-delete" src="/images/icon-x-delete.png" /></a></div>'), { number: --attribute_fu_choice_count, question_group_number: question_group_id});
    $('#questionnaire_question_group_attributes_new___number__choice_attributes_new___number__choice_label').focus();
    return false;
  });

  $('.check-box-destroy').livequery('click', function() {
    var main_ul = $(this).parents('ul');

    $(this).parents('.choice').fadeAndRemove();
    if(main_ul.children().length <= 1) main_ul.hide(); 
    return false;
  });

  $('.datepicker').datepicker();
  $('.datepicker').attr("autocomplete","off");

});
