var Wick = Wick || {};
Wick.Accordion = (function(){
  var $accordion = $('.accordion'),
    accordionToggler = '.accordion__header',
    accordionIcon = '.accordion__action',
    accordionContent = '.accordion__content',
    collapsed = 'accordion_collapsed',
    $asideAccordion = $('.aside-accordion .js-toggle-aside-accordion');

  var bindEvents = function (index, element) {
    var $acc = $(element);

    var accordionTrigger = $acc.attr('data-action-element') === 'icon' ? accordionIcon : accordionToggler
    $acc.find(accordionTrigger).on('click', function(){
      $acc.toggleClass(collapsed);
      $acc.children(accordionContent).slideToggle(200);
    });
  };

  var init = function () {
    $accordion.each(bindEvents);

    $asideAccordion.click(function() {
      if (window.matchMedia("(min-width: 992px)").matches) {
          if ($(this).text().toLowerCase() != 'useful links') {
              $(this).closest('.aside-nav2__holder')
                     .toggleClass('is-active')
                     .find('.aside-nav2__list')
                     .slideToggle(250);
          }
      }
    });
  }

  init();
})();
