var Wick = Wick || {};

Wick.Search = (function() {
  var $searchBtn = $('#search-button'),
    $searchWrapper = $('#search-wrapper'),
    $searchInput = $('#search'),
    searchWrapperActive = 'search-wrapper--active',
    searchActive = 'search--active',
    navButton = $('.header-hamburger-menu'),
    openNavClass = 'header-hamburger-menu_close-visible';

    var closeNav = function () {
      if (navButton.hasClass(openNavClass)) {
        navButton
          .trigger('click')
          .trigger('blur')
      }
    };

    var showSearch = function () {
      $searchWrapper.addClass(searchWrapperActive);
      $searchInput.focus();
      $searchBtn.addClass(searchActive);
      closeNav();
    };

    var hideSearch = function () {
      $searchWrapper.removeClass(searchWrapperActive);
      $searchBtn.removeClass(searchActive);
    };

    $searchInput.focusout(function() {
      setTimeout(hideSearch,0);
    })

    $searchBtn.click(function () {
      if ($searchBtn.hasClass(searchActive)) {
        hideSearch();
      } else {
        showSearch();
      }
    })
})()
