text: (function(root) {
  
    'use strict';
  
    root.app = root.app || {};
    root.app.View = root.app.View || {};
    root.app.Collection = root.app.Collection || {};
  
    // Model for getting the data
    root.app.Collection.SearchCollection = Backbone.Collection.extend({
      url: baseurl + '/json/search.json'
    });
  
    // View for display results
    root.app.View.SearchView = Backbone.View.extend({
  
      el: '#searchView',
  
      events: {
        'keyup #search-input' : 'search',
        'click #search-close' : 'removeResults'
      },
  
      resultsTemplate: HandlebarsTemplates['search'],
  
      initialize: function(settings) {
        var opts = settings && settings.options ? settings.options : {};
        this.options = _.extend({}, this.defaults, opts);
        this.collection = new root.app.Collection.SearchCollection();
  
        this.collection.fetch().done(function(){
          this.cache();
          this.initFuse();
        }.bind(this));
      },
  
      cache: function() {
        this.searchIndex = 0;
        this.$searchInput = this.$el.find('#search-input');
        this.$searchClose = this.$el.find('#search-close');
        this.$searchResults = this.$el.find('#search-results');
  
      },
  
      initFuse: function() {
        var json = this.collection.toJSON();
        this.fuse = new Fuse(json, {
          caseSensitive: false,
          includeScore: false,
          shouldSort: true,
          threshold: 0.6,
          location: 0,
          distance: 100,
          maxPatternLength: 32,
          keys: ['title','content','category','tags']
        });
      },
  
      search: function(e) {
        var val = $(e.currentTarget).val();
        switch(e.keyCode) {
          case 13:
            this.selectResult();
          break;
          case 27:
            this.removeResults();
          break;
          case 38:
            this.indexResults('up');
          break;
          case 40:
            this.indexResults('down');
          break;
          default:
            (!!val) ? this.setResults(val) : this.removeResults();
            this.highlightResults();
        }
      },
  
      indexResults: function(direction) {
        if (!!this.results.length) {
          switch(direction) {
            case 'up':
              (this.searchIndex != 0) ? this.searchIndex-- : this.searchIndex = 0;
            break;
            case 'down':
              (this.searchIndex < this.results.length - 1) ? this.searchIndex++ : this.searchIndex = this.results.length - 1;
            break;
          }
        }
        this.highlightResults();
      },
  
      highlightResults: function() {
        this.$searchResults.children('li').removeClass('-highlight');
        this.$searchResults.children('li').eq(this.searchIndex).addClass('-highlight');
      },
  
      selectResult: function() {
        var href = this.$searchResults.children('li').eq(this.searchIndex).children('a').attr('href');
        window.location = href;
      },
  
      setResults: function(val) {
        this.results = this.fuse.search(val).slice(0, 5);
        this.$searchResults.addClass('-active').html(this.resultsTemplate({ results: (!!this.results.length) ? this.results : null }));
        // svg addClass
        this.$searchClose.addClass('-active');
      },
  
      removeResults: function() {
        this.searchIndex = 0;
        this.results = this.fuse.search('');
        this.$searchInput.val('');
        this.$searchResults.removeClass('-active').html(this.resultsTemplate({ results: []}));
        // svg removeClass
        this.$searchClose.removeClass('-active');
      }
  
    });
  
  })(this);
  