/* Header Search, Header Dashboard scripts */
/* -------------------------------------------------------------------- */

$(".mk-header-search, .mk-side-dashboard, .dashboard-trigger, .search-ajax-input, .mk-quick-contact-inset").click(function(event) {
    if (event.stopPropagation) {
        event.stopPropagation();
    } else if (window.event) {
        window.event.cancelBubble = true;
    }
});


$("html").click(function() {
    $('.header-search-icon').removeClass('search-clicked');
    $('form.header-searchform-input').fadeOut(250);
    $('#mk-header').removeClass('header-search-triggered');
    $('.mk-secondary-header').removeClass('header-search-triggered');
    $('.dashboard-trigger').removeClass('dashboard-active');
    $('.theme-main-wrapper, .mk-side-dashboard').removeClass('dashboard-opened');

});


$('.header-search-icon').on('click', function(e) {
    var $this = $(this);
    //console.log(mk_boxed_header); 
    $this.parent().parent().parent().next().fadeIn(250);
    $this.parent().parent().parent().next().find('input[type=text]').focus(); 
    if(mk_boxed_header == '0'){
        $this.parent().parent().parent().parent().addClass('header-search-triggered');
    }else{
        $this.parent().parent().parent().parent().parent().addClass('header-search-triggered');
    }
    e.preventDefault(); 
});

$('.header-search-close').on('click', function(e) {
    var $this = $(this);
    $this.parent().fadeOut(250);
    $this.parent().parent().parent().removeClass('header-search-triggered');
    e.preventDefault();
});



$('.dashboard-trigger').on('click', function(e) {

    var $this = $(this);

    if (!$this.hasClass('dashboard-active')) {

        $this.addClass('dashboard-active');
        $('.theme-main-wrapper, .mk-side-dashboard').addClass('dashboard-opened');

    } else {

        $this.removeClass('dashboard-active');
        $('.theme-main-wrapper, .mk-side-dashboard').removeClass('dashboard-opened');
    }
    e.preventDefault();
});


$('.responsive-nav-link').on('click', function(e) {
    var $this = $(this),
        $header = $('#mk-header'),
        id = $this.closest('ul').attr('id'),
        res_nav = $('.responsive-nav-container');

    if (!$this.hasClass('active-burger')) {
        $('.'+id+'-mk-responsive-nav').show();
        if($header.hasClass('theme-main-header')) {
            $this.addClass('active-burger');
            res_nav.slideDown();
            $('body').removeClass('mk-closed-nav').addClass('mk-opened-nav').trigger('mk-opened-nav');
        } else {
            var offset = $this.offset().top - global_admin_bar_height;
            abb.smoothScrollTo(offset, 500);
            $this.addClass('active-burger');
            setTimeout(function() {
                res_nav.slideDown();
                $('#mk-responsive-wrap').css({
                    'position': 'absolute',
                    'top': 'auto',
                    'max-height': ($(document).height() + 'px'),
                    'width': ($(window).width()) + 'px'
                });
            }, 300);
        }
    } 
    else {
        setTimeout(function() {
            $('.mk-responsive-nav').hide();
        },300);
        if($header.hasClass('theme-main-header')) {
            $this.removeClass('active-burger');
            res_nav.slideUp();
            $('body').removeClass('mk-opened-nav').addClass('mk-closed-nav').trigger('mk-closed-nav');
        } else {
            $this.removeClass('active-burger');
            res_nav.slideUp();
        }
    }
    e.preventDefault();
});
