(function(){
  var root;
  root = this;
  root.add_query_partial = function(addr) {
    var defaults;
    defaults = {
      duration: 1,
      changeMonth: true,
      changeYear: true,
      yearRange: '1900:2020',
      showAnim: 'fadeIn',
      constrainInput: true
    };
    $('#query_holder').append("<div class='query_partial'><img src='/images/ajax-loader.gif'>Loading...</div>");
    $('#query_holder').children().last().hide();
    $('#query_holder').children().last().fadeIn('fast');
    return $('#query_holder').children().last().load(("/patients/query_partial?i=" + ($('#query_holder').children().length) + "&partial=query_" + addr), null, function() {
      $('#query_holder .datepicker').datepicker(defaults);
      p360.simpleAutocomplete('#query_holder .simple-autocomplete');
      $(':radio').addClass("real_radio");
      return $(':checkbox').addClass("real_checkbox");
    });
  };
  root.reload_metric = function() {
    return $.getJSON('/check_metric_links', {
      "metric_year": $('#metric_metric_year').val(),
      "metric_type": $('#metric_metric_type').val(),
      "metric_group": $('#metric_metric_group').val(),
      "metric_number": $('#metric_metric_number').val(),
      "patient_id": $('#metric_patient_id').val()
    }, function(data) {
      var counter;
      counter = 0;
      jQuery.each(data, function(k, v) {
        var e, element;
        if ((typeof v !== "undefined" && v !== null)) {
          counter++;
          element = $(("[name='" + k + "']"));
          if (element.length > 1 && element.first().hasClass('real_radio')) {
            e = $('#' + formtastic_escape(k + "" + v));
            return e.attr("checked", "checked");
          } else if (element.hasClass('real_checkbox')) {
            if (v === true) {
              return element.attr("checked", "checked");
            } else {
              return element.attr("checked", "unchecked");
            }
          } else {
            return element.val(v);
          }
        }
      });
      if (counter > 0) {
        return $.gritter.add({
          title: "Notice",
          text: "Relevant medical values were found in the patient's medical record. They have been added to the metric for your convenience."
        });
      }
    });
  };
  root.remove_cached_alert = function(source, name, patient) {
    return $.getJSON('/remove_cached_alert', {
      "patient_id": patient,
      "name": name,
      "source": source
    }, function(message) {
      $(("#" + (patient))).parent().parent().remove();
      return $.gritter.add({
        title: "Notice",
        text: message['message']
      });
    });
  };
  root.formtastic_escape = function(str) {
    return str.replace(/'/g, "").replace(/\]\[|[^-a-zA-Z0-9:.]/g, '_').toLowerCase();
  };
  CFInstall.check({
    preventPrompt: true,
    onmissing: function() {
      return $("#chromeframe_notice").load("/chromeframe_notice");
    }
  });
})();
