(function ($, Drupal, once) {
  'use strict';

  Drupal.behaviors.logo = {
    attach: function (context, settings) {
      $(once('block-logo', '.block-logo')).each(function () {
        var $logo = $(this).find('img');
        //the following structure deals with errors caused by image caching.
        var tmpImg = new Image();
        tmpImg.onload = function () {
          var $logoHeight = $logo.height();
          $logo.attr('style', 'top: ' + Math.abs(($logoHeight / 2) - 60) + 'px');
        };
        tmpImg.src = $logo.attr('src');
      });
    }
  };

  Drupal.behaviors.disableParentClick = {
    attach: function (context, settings) {
      $('.block.block--system-menu-block .large-horizontal .link--container > a:first-of-type').on('click', function(e) {
        e.preventDefault();
      });
    }
  };

  Drupal.behaviors.overflowMenu = {
    attach: function (context, settings) {
      $(once('top-bar', '.top-bar')).each(function () {
        var $topBar = $(this);
        var $navBar = $topBar.find('.top-bar-section > .nav-bar');
        var $navBarItems = $navBar.children('li').not('.overflow-menu');
        var $socialMediaMenu = $('.social-media-menu');
        var $dropdown = $dropdown || $('<li class="has-dropdown not-click overflow-menu">' +
                    '<a class="menu-icon"><span></span></a>' +
                    '<ul class="dropdown" id="overflow-list">' +
                    '<li class="parent-link hide-for-medium-up"><a class="parent-link js-generated" href="#"></a></li>' +
                    '</ul></li>');
        $socialMediaMenu.length ? $dropdown.insertBefore($socialMediaMenu) : $dropdown.appendTo($navBar);

        var $overflowMenu = $('.overflow-menu');
        var $overflowList = $('#overflow-list');
        var $overflowListItems = $overflowList.children('li:not(.title.back.js-generated, .parent-link.hide-for-medium-up)');

        function updateDropdown() {
          // add dropdown items back to .nav-bar
          if ($overflowListItems.length) {
            $overflowListItems.each(function () {
              var $this = $(this);
              var $currentNavBarItems = $navBar.children('li:not([class])');
              $this.insertAfter($currentNavBarItems[$currentNavBarItems.length - 1]);
            });
          }
          var totalWidth = $overflowMenu.outerWidth() + $socialMediaMenu.outerWidth();
          var widthAdjusted = false;

          // for each menu item, keep it or add to dropdown
          $navBarItems.each(function () {
            var $this = $(this);
            var width = $this.outerWidth();
            totalWidth += width;

            // if one thing has been added to the dropdown, add the rest
            if (widthAdjusted) {
              $this.appendTo($overflowList);
              totalWidth -= width;
            }
            // if doesn't fit in width, add to dropdown
            var topBarWidth = $topBar.outerWidth();
            if (totalWidth > topBarWidth) {
              if (widthAdjusted === false) {
                $this.appendTo($overflowList);
                totalWidth -= width;
                widthAdjusted = true;
              }
            }
          });
          // if there are items in the dropdown, show it, otherwise hide it
          $overflowListItems = $overflowList.children('li:not(.title.back.js-generated, .parent-link.hide-for-medium-up)');
          $overflowListItems.length ? $dropdown.show() : $dropdown.hide();
        }
        updateDropdown();
        $(window).on('resize', Foundation.utils.throttle(function (e) {
          if (Foundation.utils.is_medium_up()) {
            updateDropdown();
          }
        }, 300));
      });
    }
  };
})(jQuery, Drupal, once);
