(function ($, root, undefined) {

'use strict';

// Initalize lunr with the fields it will be searching on
window.idx = lunr(function() {
  this.ref('id');
  {% for field in site.search.fields %}
  this.field('{{ field.title }}'{% if field.boost %}, {boost: {{ field.boost }}}{% endif %});
  {% endfor %}
});

// Download the data from the JSON file we generated
window.data = $.getJSON('{{ site.search.data-url }}');

// Wait for the data to load and add it to lunr
window.data.then(function(loaded_data){
  $.each(loaded_data, function(index, value){
    window.idx.add(
      $.extend({ "id": index }, value)
    );
  });
});

})(jQuery, this);