
var data = false;
var template = $("#template").html();

function show() {
  if (!data) {

    $("#list").html(UI_loading);

    $.getJSON("../../api/institutions.php",
     function (response) {
      data = response;
      $("#list").html(Mustache.render(template, data));
    });
  } else {
    $("#list").html(Mustache.render(template, data));
  }

}

$("#search").on("input", function () {

  var search = new RegExp($("#search").val(), "i");
  var search_key = $("#search_select").find(":selected").data("key");

  $.each(data["data"], function (key, value) {
    if (value[search_key].search(search) != -1) {
      $("#" + value.id).slideDown(200);
    } else {
      $("#" + value.id).slideUp(200);
    }
  });

});

function reset_search() {
  $("#search").val("").trigger("input");
}

$("#search_select").on("change", reset_search);

