var Wick = Wick || {};
Wick.Autocomplete = (function(){
  var $search = $('.autocomplete input');

  var init = function () {
    var substringMatcher = function(strs, by) {
      return function findMatches(q, cb) {
        var matches, substringRegex;
        matches = [];
        substrRegex = new RegExp(q, 'i');
        $.each(strs, function(i, str) {
          if (substrRegex.test(str[by])) {
            matches.push(str);
          }
        });
        cb(matches);
      };
    };

    var suggestions = [
      {'term':'cooker hoods with extractor'},
      {'term':'cookers'},
      {'term':'cooker hoods 600mm'},
      {'term':'cooker switch'},
      {'term':'cooker cable'},
      {'term':'cooker hood filter'},
      {'term':'cooker socket'},
      {'term':'cooling fans'},
      {'term':'cooker hose'},
      {'term':'cooker hood venting kit'},
      {'term':'makita 18v'},
      {'term':'makita drill'},
      {'term':'makita combi drill'},
      {'term':'makita radio'},
      {'term':'makita battery'},
      {'term':'makita impact driver'},
      {'term':'makita'},
      {'term':'makita circular saw'},
      {'term':'makita cordless drills'}
    ];

    var products = [
      {'name': 'AEG Surround Cook Double Tower Stainless Steel Electric Oven DCK731110M'},
      {'name': 'AEG Single Multifunction SenseCook Pyrolytic Stainless Steel Electric Oven BPK842720M'},
      {'name': 'AEG Surround Cook Double Multifunction Stainless Steel Electric Oven DUK731110M'},
      {'name': 'AEG Built-In Compact Sense Cook Multifunction Stainless Steel Electric Oven KPK742220M'}
    ]

    $search.typeahead({
      hint: false,
      highlight: false,
      minLength: 2,
      classNames: {
        menu: 'autocomplete__menu',
        dataset: 'autocomplete__dataset',
        suggestion: 'autocomplete__suggestion',
        cursor: 'autocomplete__cursor'
      }
    },
    {
      name: 'suggestions',
      display: 'term',
      source: substringMatcher(suggestions, 'term'),
      templates: {
        suggestion: function(obj){
          return [
            '<a class="_autocomplete__suggestion-link" href="/page_search-results.html">',
              obj.term,
            '</a>',
          ].join('\n')
        }
      }
    },
    {
      name: 'products',
      display: 'name',
      source: substringMatcher(products, 'name'),
      templates: {
        header: [
          '<div class="autocomplete__dataset-header">',
            'Products',
          '</div>'
        ].join('\n'),
        suggestion: function(obj){
          return [
            '<div>',
              '<a class="autocomplete__suggestion-link" href="/page_product-details.html">',
                '<img class="autocomplete__suggestion-img" src="/img/placeholders/[e-img-4to3].jpg">',
                '<div class="autocomplete__suggestion-info">',
                  '<span class="autocomplete__suggestion-title">',
                    obj.name,
                  '</span>',
                  '<div class="autocomplete__suggestion-price">£2,099.00</div>',
                '</div>',
              '</a>',
            '</div>'
          ].join('\n')
        }
      }
    })
    .bind('typeahead:select', function(e, suggestion) {
      if (suggestion.term) {
        window.location.href = './page_search-results.html';
      } else if (suggestion.name){
        window.location.href = './page_product-details.html';
      }
    })
    .bind('typeahead:render', function() {
      $('.autocomplete__suggestion-title').each(function (index, value) {
        $clamp(value, {clamp: 2});
      });
    });
  }

  if (!$search.length) {
    return;
  }

  init();
})();
