/* Main Navigation Init */
/* -------------------------------------------------------------------- */


function mk_main_navigation_init() {

    // $(".main-navigation-ul").dcMegaMenu({
    //     rowItems: '6',
    //     speed: 200,
    //     effect: 'fade',
    //     fullWidth: true
    // });

  "use strict";

  var $body = $('body');

  if (!$body.hasClass('navigation-initialised')) {

    $(".main-navigation-ul").MegaMenu({
      type: "vertical",
      delay: 200
    });

    $('#mk-vm-menu').dlmenu();

    $body.addClass('navigation-initialised');

  }

  

}



(function ($) {
  'use strict';

  /* Main Navigation mobile mode */
  /* -------------------------------------------------------------------- */

  var is_res_nav_appended = false;

  function mk_main_navigation_functions() {
      var isDesktop = global_window_width > mk_nav_res_width;

      if( !$.exists('.mk-responsive-nav') ) {

        var $resNav = $('.responsive-nav-container');
        $('.main-navigation-ul, .mk-vertical-menu').each(function() {
            var $this = $(this),
                id = $this.attr('id');

            $this.clone().attr({
                "class": "mk-responsive-nav "+id+"-mk-responsive-nav"
            }).appendTo($resNav);
            $resNav.addClass(id+'-res-nav-appended');
        });

        $('.mk-responsive-nav > li').stop(true).on('click', function(e) { 
            var $this = $(this),
                $arrow = $this.find('.mk-nav-arrow');

            if($(e.target).hasClass('mk-nav-arrow') || $(e.target).hasClass('mk-nav-open')) {
              if ($arrow.hasClass('mk-nav-sub-closed')) {
                $arrow.parent().siblings('ul').slideDown(450).end().end().removeClass('mk-nav-sub-closed').addClass('mk-nav-sub-opened');
              } else {
                $arrow.parent().siblings('ul').slideUp(450).end().end().removeClass('mk-nav-sub-opened').addClass('mk-nav-sub-closed');
              }
              e.preventDefault();
          }
        });

        if(!$.exists('#mk-responsive-wrap')) {
          var $header_height = 0;
          var $window_height = $(window).outerHeight();

          if ($.exists('#wpadminbar')) {
            $header_height += $('#wpadminbar').outerHeight();
          }

          if($.exists('.responsive-nav-container')) {
            $header_height += $('#mk-header').height();
            var nav_height = $window_height - $header_height;

            $('.responsive-nav-container').wrap('<div id="mk-responsive-wrap" style="max-height:'+nav_height+'px; top: '+$header_height+'px;"></div>');
          }

          $(window).on('resize', function() {
            $('#mk-responsive-wrap').css({
              'max-height' : ($(window).height() - $header_height) + 'px'
            });
          });

        }

        $('.mk-responsive-nav > li > ul').each(function() {
            if(!$(this).siblings('a').find('span').length) {
                $(this).siblings('a').append('<span class="mk-theme-icon-bottom-big mk-nav-arrow mk-nav-sub-closed"></span>');
            }
        });

      }

      if (isDesktop) {

          $('.mk-responsive-nav').hide();
          setTimeout( mk_main_navigation_init, 200);

          if ($('#mk-header').attr('data-header-style') == 'transparent') {
              $('#mk-header').addClass('transparent-header ' + $('#mk-header').attr('data-transparent-skin') + '-header-skin');
          }

      } else {

          if ($('#mk-header').attr('data-header-style') == 'transparent') {
              $('#mk-header').removeClass('transparent-header ' + $('#mk-header').attr('data-transparent-skin') + '-header-skin');
          }

      }

      if (global_window_width < mk_grid_width) {
          $('.main-navigation-ul .sub-container.mega, .main-navigation-ul .sub-container.mega .row').each(function() {
              $(this).css('width', global_window_width - 40);
          });
      }

  }

  $(document).ready(mk_main_navigation_functions);
  $(window).on("debouncedresize", mk_main_navigation_functions);

}(jQuery));