/**
 *  Toggle and slide out mobile menu
 */
var toggleMobileMenu = (function ($) {

  var pub        = {}; // public facing functions
  var active_nav = [];

  pub.init = function () {
    pub._menuToggleListener();
    pub._mobileSubMenuListener();
    pub._headerLeftSubMenuListener();
    pub._headerPhoneMenuListener();
  };

  pub._menuToggleListener = function () {
    var menuToggle = $('div[data-toggle="primary"]');
    var mobileMenu = $('.mobile__nav');
    var bodyElem = $('body');
    var uberMenuToggle = $('.ubermenu-responsive-toggle');

    if (menuToggle.length > 0) {
      menuToggle.on('click', function (e) {
        e.preventDefault();
        active_nav = mobileMenu;
        mobileMenu.toggleClass('mobile__nav--opened');
        bodyElem.toggleClass('overlay--active');

        if(uberMenuToggle.length > 0) {
          uberMenuToggle.click().hide();
        }

        /* close submenus when menu is closed */
        if( !$('.mobile__nav').hasClass('mobile__nav--opened') ){
          if($('.mobile__nav .sub-menu--toggle').hasClass('active')){
            $('.mobile__nav .sub-menu--toggle').removeClass('active');
            $('.mobile__nav .sub-menu--toggle').next('.sub-menu').removeAttr("style").hide();
          }
        }
        // fixes ubermenu mobile nav error where nav links disappeared on second load if clicked anywhere except arrow to close the mobile nav
        if (!$('#mobile-navigation').hasClass( 'mobile__nav--opened')) {
            $('.ubermenu-responsive-toggle-main').removeClass('ubermenu-responsive-toggle-open');
            $('.ubermenu-main').addClass('ubermenu-responsive-collapse');
        }

      });

      //close mobile navigation if user clicks off of the element
      $('.mobile__nav--overlay').on('click', function (e) {
        active_nav.removeClass('mobile__nav--opened active');
        bodyElem.toggleClass('overlay--active');
        /* close submenus when menu is closed */
        if($('.mobile__nav .sub-menu--toggle').hasClass('active')){
          $('.mobile__nav .sub-menu--toggle').removeClass('active');
          $('.mobile__nav .sub-menu--toggle').next('.sub-menu').removeAttr("style").hide();
        }
      });
    }
  };

  pub._headerLeftSubMenuListener = function () {
    var headerSubMenuToggle = $('.header__nav--left .sub-menu--toggle');
    headerSubMenuToggle.on('click', function (e) {
      e.preventDefault();

      $(this).toggleClass('active');
      $(this).next('.sub-menu').slideToggle();
    });
  };

  pub._mobileSubMenuListener = function () {
    var mobileSubMenuToggle = $('.mobile__nav .sub-menu--toggle');
    mobileSubMenuToggle.on('click', function (e) {
      e.preventDefault();

      var mobileSubMenu = $(this).next('.sub-menu');
      $(this).toggleClass('active');
      mobileSubMenu.slideToggle();
    });
  };

  pub._headerPhoneMenuListener = function () {
    var phoneToggle = $('div[data-toggle="phone"]'),
        phoneGroup  = $("#header__call-group"),
        bodyElem    = $('body');

    phoneToggle.on('click', function (e) {
      e.preventDefault();
      active_nav = phoneGroup;
      $(this).add(phoneGroup).toggleClass('active');
      bodyElem.toggleClass('overlay--active');
    });
  };

  return pub;

})(jQuery);
