// TODO cache using localStorage?
(function($, Trie, exports){
"use strict";


// configuration
var $elem = $(".q");
var $ctl = $('#view-map a.btn');


var autocomplete_trie = new Trie();
var _data = [[], [], []];
var activeIdx = 0;

// prebuild data source
var build_sources = function(data){
  var newdata = _data, inst;
  for (var i = 0; i < data.length; i++){
    inst = data[i];
    newdata[0].push(inst.name);
    newdata[1 + !inst.is_private].push(inst.name);
  }
  return newdata;
};

// jQuery UI autocomplete
var autocomplete_institutions = function(data) {
  _data = build_sources(data);

  // Map intitution names to URIs
  var urisByName = {};
  $.map(data, function(o) {
    urisByName[o.name] = o.uri;
  });

  // Insert institutions to trie
  $.each(data, function(i, o) {
    autocomplete_trie.insert(o.name.toLowerCase(), o);
  });

  // Initialize autocomplete
  $elem.autocomplete({
    source: _data[0],
    select: function(e, o) {
      var uri = urisByName[o.item.label];
      if (typeof(uri) !== "undefined") {
        location.href = uri;
      }
    }
  });

  $elem.on("keydown", function(e){
    if (e.which == 9) {  // TAB
      activeIdx = (activeIdx + 1) % 3;
      $elem.autocomplete('option', 'source', _data[activeIdx]);
      $elem.autocomplete('search', $elem.val());
      $ctl.eq(activeIdx).addClass('active').siblings('.active').removeClass('active');
      e.preventDefault();
    }

  });
};

// Patch jQuery autocomplete to filter using fuzzy matching
$.ui.autocomplete.filter = function(array, term) {
  var results = autocomplete_trie.search(term);
  return $.map(results, function(r) { return r.data.name; });
};


// TODO camelCase
exports.autocomplete_institutions = autocomplete_institutions;


})(jQuery, Trie, window);

@@ -170,21 +170,48 @@
 trie
+s
  = 
-new Trie()
+%5Bnew Trie(), new Trie(), new Trie()%5D
 ;%0Ava
@@ -341,16 +341,27 @@
 ta, inst
+, namelower
 ;%0A  for 
@@ -463,17 +463,16 @@
 ata%5B1 + 
-!
 inst.is_
@@ -497,16 +497,218 @@
 .name);%0A
+    // Insert institutions to trie%0A    namelower = inst.name.toLowerCase();%0A    autocomplete_tries%5B0%5D.insert(namelower, data%5Bi%5D);%0A    autocomplete_tries%5B1 + inst.is_private%5D.insert(namelower, data%5Bi%5D);%0A
   %7D%0A  re
@@ -967,201 +967,145 @@
 / In
-sert institutions to trie%0A  $.each(data, function(i, o) %7B%0A    autocomplete_trie.insert(o.name.toLowerCase(), o);%0A  %7D);%0A%0A  // Initialize autocomplete%0A  $elem.autocomplete(%7B%0A    source: _data%5B0%5D,
+itialize autocomplete%0A  $elem.autocomplete(%7B%0A    source: %5B%22foo%22, %22bar%22, %22baz%22%5D,  // source does nothing, but pass in an array to trick it
 %0A   
@@ -1330,16 +1330,42 @@
  // TAB%0A
+      e.preventDefault();%0A
       ac
@@ -1396,24 +1396,114 @@
 ) %25 3;%0A     
+ $ctl.eq(activeIdx).addClass('active').siblings('.active').removeClass('active');%0A      //
  $elem.autoc
@@ -1548,16 +1548,33 @@
 veIdx%5D);
+  // does nothing
 %0A      $
@@ -1619,121 +1619,8 @@
 ));%0A
-      $ctl.eq(activeIdx).addClass('active').siblings('.active').removeClass('active');%0A      e.preventDefault();%0A
     
@@ -1776,16 +1776,28 @@
 ete_trie
+s%5BactiveIdx%5D
 .search(

add ability to search based on is public/private