$(document).ready(function() {

  $('.survey_item').not('.nohelp').children('.questionSubtext').help();

  var error_dialog = $('.survey_error');
  if (error_dialog.length > 0) {
      error_dialog.addClass('modal_error');
      error_dialog.dialog({
          modal: true,
          position: 'center',
          title: "Please correct the following errors",
          width: 500,
          resizable: false
      });
  };

  $('.jpnd-date').datepicker({dateFormat: 'dd-mm-yy'});
  $('.jpnd-number').numeric();
  if ($("#basic-search").length > 0) {
    if ($("input.advanced").filter(function()
                                   { return $.trim($(this).val()) != "" }
                                  ).length == 0)
    {
      $("#advanced-search .hidden").hide();
    }
    $("#advanced-search").append(function() {
      var link = $("<a href=\"#\">Advanced options...</a>");
      link.click(function(e) {
        $("#advanced-search .hidden").slideToggle();
        e.preventDefault();
      });
      return link;
    });
  }

  $(".vrecord_preview .vrecord_info .investigators").hide();
  $(".vrecord_preview .vrecord_info .investigators").after(function() {
    var link = $("<p class=\"investigator_control\"><a href=\"#\">Principal Investigators...</a></p>");
    link.click(function(e) {
      $(this).prev(".investigators").slideToggle();
      e.preventDefault();
    });
    return link;
  });
});

(function ($) {

$.fn.help = function () {

  return this.each(function () {
    // get jQuery version of 'this'
    var $input = $(this);
    var $label = $input.parent().children(':first');

    $input.hide();
    $label.append('<span class="questionSubtext_control"> [help]</span>');
    $label.children('span.questionSubtext_control').click( function(event) {
      // alert("hello world");
      $(this).parent().siblings('.questionSubtext').slideToggle();
      // event.preventDefault();
    });



  });
};

})(jQuery);

