$(document).ready(function() {
    if (!$.exists('.mk-edge-one-pager') && typeof webkit_smoothscroll == 'function') {
        webkit_smoothscroll();
    }
    mk_go_to_top();
    mk_instagram();
    sticky_header();
    transparent_header_sticky();
    loop_audio_init();
    mk_header_margin_style();
    mk_social_share();
    mk_google_maps();
    mk_event_countdown();
    mk_text_typer();
    mk_flexslider_init();
    mk_one_page_scroller();
    mk_one_pager_resposnive();
    mk_child_ul_toggle_event();
    mk_responsive_header_toolbar();
    mk_header_checkout();
    

    $(window).load(function() {
        mk_update_globals();
        mk_animated_columns();
        mk_fade_onload();
        mk_lightbox_init();
        mk_hideHeaderInMobile();
        mk_portfolio_image();
        mk_gallery_image();
        mk_window_scroller();
        mk_expandable_page_section();
        mk_accordion();
        mk_employees();
        mk_process_steps();
        mk_gallery_thumbs_width();
        mk_edge_slider_init();
        mk_edge_slider_resposnive();
        mk_tab_slider();
        mk_swipe_slider();
        mk_hash_scroll();
        mk_center_caption();
        loops_iosotop_init();
        isotop_load_fix();
        mk_page_title_intro();
        mk_section_intro_effects();
        mk_secondary_header_res();
        mk_tabs();
        secondary_header();
        mk_portfolio_hovers();
        mk_theatre_responsive_calculator();
        mk_mobile_tablet_responsive_calculator();
        mk_button_animation();
        mk_tabs_responsive();
        mk_header_wpml();
        mk_vertical_menu_submenu();
        mk_theatre_autoplay_freeze();
        mk_imagebox_autoplay_freeze();
        shop_isotop_init();
        shop_categories_isotop_init();
        mk_one_pager_resposnive();
        mk_main_nav_scroll();
        section_to_full_height();
        mk_section_parallax();
        mk_callout_action();
        mk_page_section_social_video_bg();
        fullscreen_menu_logo();
    });



    $(window).on("debouncedresize", function() {
        mk_hideHeaderInMobile();
        mk_update_globals();
        mk_portfolio_image();
        section_to_full_height();
        mk_employees();
        mk_window_scroller();
        mk_section_parallax();
        mk_section_intro_effects();
        mk_secondary_header_res();
        secondary_header();
        mk_center_caption();
        mk_one_pager_resposnive();
        mk_theatre_responsive_calculator();
        mk_mobile_tablet_responsive_calculator();
        transparent_header_sticky();
        sticky_header();
        mk_tabs_responsive();
        mk_responsive_header_toolbar();
        mk_main_nav_scroll();
        mk_callout_action();
        fullscreen_menu_logo();
    });

    /* Hide Header in Mobile Devices */
    /* -------------------------------------------------------------------- */
    function mk_hideHeaderInMobile(){
        if ($.exists('#mk-header.hide-on-mobile') && window.matchMedia('(max-width: 767px)').matches) {
            var mobileHeader = $('#mk-header.hide-on-mobile');
            mobileHeader.attr("data-height", 0);
            mobileHeader.attr('data-sticky-height',0);
            mobileHeader.attr('data-responsive-height',0); 
        }
    }


    /* Floating Go to top Link */
    /* -------------------------------------------------------------------- */
    $('.mk-go-top, .mk-back-top-link').on('click', function(e) {
        e.preventDefault();
        $("html, body").animate({
          scrollTop: 0
          }, 800);
    });

    function mk_go_to_top() {
        var mk_go_top = $('.mk-go-top');
        var mk_quick_mail = $('.quick-button-container');
        var animationSet = function() {
            if (scrollY > 700) {
                mk_go_top.removeClass('off').addClass('on');
                mk_quick_mail.removeClass('go-right').addClass('go-left');
            } else {
                mk_go_top.removeClass('on').addClass('off');
                mk_quick_mail.removeClass('go-left').addClass('go-right');
            }
        }
        debouncedScrollAnimations.add(animationSet);
    }



    /* Love This */
    /* -------------------------------------------------------------------- */

    function mk_love_post() {

        $('body').on('click', '.mk-love-this', function() {
            var $this = $(this),
                $id = $this.attr('id');

            if ($this.hasClass('item-loved')) {
                return false;
            }

            if ($this.hasClass('item-inactive')) {
                return false;
            }

            var $sentdata = {
                action: 'mk_love_post',
                post_id: $id
            };

            $.post(ajaxurl, $sentdata, function(data) {
                $this.find('span').html(data);
                $this.addClass('item-loved');
            });

            $this.addClass('item-inactive');
            return false;
        });


    }

    mk_love_post();



    /* Element Click Events */
    /* -------------------------------------------------------------------- */
    function mobilecheck() {
        var check = false;
        (function(a) {
            if (/(android|ipad|playbook|silk|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0, 4))) check = true
        })(navigator.userAgent || navigator.vendor || window.opera);
        return check;
    }


    /* Margin Header Style */
    /* -------------------------------------------------------------------- */


    function mk_header_margin_style() {

        var eventtype = mobilecheck() ? 'touchstart' : 'click';


        $('.mk-margin-header-burger').on(eventtype, function() {
            var $this = $(this),
                $mainNav = $('#mk-main-navigation');
            if (!$this.hasClass('active-burger')) {
                $this.addClass('active-burger');
                $mainNav.addClass('show-menu');
            } else {
                $this.removeClass('active-burger');
                $mainNav.removeClass('show-menu');
            }

        });

        //TODO add it here to find best place
        $('.mk-fullscreen-header-burger').on(eventtype, function() {
            var $body = $('body');
            if (!$body.hasClass('fullscreen-menu-opened')) {
                $body.addClass('fullscreen-menu-opened');
            } else {
                $body.removeClass('fullscreen-menu-opened');
            }

        });




    }


    function fullscreen_menu_logo(){

        if($.exists('.mk-fullscreen-menu') || $.exists('.mk-fullscreen-menu')){
            
            var windowHeight = $(window).height();
            var fullmenuHeight = $('.mk-fullscreen-menu').height();
            var headerPadding = mk_header_padding;

            var maxHeight = (windowHeight - (fullmenuHeight + headerPadding + global_admin_bar_height + 20 ))/2;
            $('.fullscreen-header-holder .mk-header-logo img').css('max-height', maxHeight);
            $('.fullscreen-header-holder .mk-header-logo, .fullscreen-header-holder .mk-header-logo a').css('margin-bottom', "0px");

        }
    }

    /* Product Category Accordion */
    /* -------------------------------------------------------------------- */
    function mk_child_ul_toggle_event() {
        $('.widget_product_categories ul > .cat-item').each(function() {

            var $this = $(this),
                $subLevel = $this.find('ul');

            if ($this.hasClass('cat-parent')) {
                $this.hoverIntent({
                    over: function() {
                        $subLevel.slideDown(500);
                    },
                    out: function() {
                        $subLevel.slideUp(500);
                    },
                    timeout: 1000
                });
            }

        });
    }

    /* Fancy Link Button Hover Animation */
    /* -------------------------------------------------------------------- */
    function mk_button_animation() {
        $('.fancy_link-button').each(function() {

            var $this = $(this),
                $line = $this.find('.line');

            $this.hoverIntent({
                over: function() {
                    $line.css({
                        'width': '100%',
                        'right': 'auto',
                        'left': '0',
                    });
                },
                out: function() {
                    $line.css({
                        'width': '0',
                        'right': '0',
                        'left': 'auto',
                    });
                },
                timeout: 100
            });

        });
    }



    /* Theatre Slider Autoplay freeze */
    /* -------------------------------------------------------------------- */

    function mk_theatre_autoplay_freeze() {
        $(".theatre-slider-container.autoplay-true").each(function() {
            var $container = $(this),
                $source = $container.data('source'),
                player;


            if ($source == 'self_hosted') {
                player = $container.find('video')[0];
            }
            if ($source == 'youtube') {
                var youtube = $container.find('iframe')[0];
                player = new YT.Player(youtube);
            }
            if ($source == 'vimeo') {
                var vimeo = $container.find('iframe')[0];
                player = $f(vimeo);
            }


            $container.on('inview', function(event, visible) {
                if (visible == true) {
                    if ($source == 'self_hosted') {
                        player.play();
                    }
                    if ($source == 'youtube') {
                        player.playVideo();
                    }
                    if ($source == 'vimeo') {
                        player.api('play');
                    }
                } else {
                    if ($source == 'self_hosted') {
                        player.pause();
                    }
                    if ($source == 'youtube') {
                        player.pauseVideo();
                    }
                    if ($source == 'vimeo') {
                        player.api('pause');
                    }
                }
            });
        });
    }

    /* Theatre Slider Responsive Calculator */
    /* -------------------------------------------------------------------- */

    function mk_theatre_responsive_calculator() {
        var $laptopContainer = $(".laptop-theatre-slider");
        var $computerContainer = $(".computer-theatre-slider");

        if ($.exists('.laptop-theatre-slider')) {
            $laptopContainer.each(function() {
                var $this = $(this),
                    $window = $(window),
                    $windowWidth = $window.outerWidth(),
                    $windowHeight = $window.outerHeight(),
                    $width = $this.outerWidth(),
                    $height = $this.outerHeight(),
                    $paddingTop = 32,
                    $paddingRight = 110,
                    $paddingBottom = 47,
                    $paddingLeft = 110;

                var $player = $this.find('.player-container');

                if ($windowWidth > $width) {
                    $player.css({
                        'border-left-width': ($width * $paddingLeft) / 920,
                        'border-right-width': ($width * $paddingRight) / 920,
                        'border-top-width': ($height * $paddingTop) / 536,
                        'border-bottom-width': ($height * $paddingBottom) / 536
                    });
                }

            });
        }

        if ($.exists('.computer-theatre-slider')) {
            $computerContainer.each(function() {
                var $this = $(this),
                    $window = $(window),
                    $windowWidth = $window.outerWidth(),
                    $windowHeight = $window.outerHeight(),
                    $width = $this.outerWidth(),
                    $height = $this.outerHeight(),
                    $paddingTop = 37,
                    $paddingRight = 35,
                    $paddingBottom = 190,
                    $paddingLeft = 38;

                var $player = $this.find('.player-container');

                if ($windowWidth > $width) {
                    $player.css({
                        'border-left-width': ($width * $paddingLeft) / 920,
                        'border-right-width': ($width * $paddingRight) / 920,
                        'border-top-width': ($height * $paddingTop) / 705,
                        'border-bottom-width': ($height * $paddingBottom) / 705,
                    });
                }

            });
        }

    }

    /* Mobile and Tablet Slideshow Responsive Calculator */
    /* -------------------------------------------------------------------- */
    function mk_mobile_tablet_responsive_calculator() {
        var $mobilePortrait = $(".mk-mobile-slideshow.portrait-style");
        var $mobileLandscape = $(".mk-mobile-slideshow.landscape-style");
        var $tabletPortrait = $(".mk-tablet-slideshow");

        if ($.exists(".mk-mobile-slideshow.portrait-style")) {
            $mobilePortrait.each(function() {
                var $this = $(this),
                    $window = $(window),
                    $windowWidth = $window.outerWidth(),
                    $windowHeight = $window.outerHeight(),
                    $width = $this.outerWidth(),
                    $height = $this.outerHeight(),
                    $paddingTop = 106,
                    $paddingRight = 25,
                    $paddingBottom = 100,
                    $paddingLeft = 30;

                var $player = $this.find(".slideshow-container");

                $player.css({
                    "padding-left": ($width * $paddingLeft) / 357,
                    "padding-right": ($width * $paddingRight) / 357,
                    "padding-top": ($height * $paddingTop) / 741,
                    "padding-bottom": ($height * $paddingBottom) / 735,
                });

            });
        }

        if ($.exists(".mk-mobile-slideshow.landscape-style")) {
            $mobileLandscape.each(function() {
                var $this = $(this),
                    $window = $(window),
                    $windowWidth = $window.outerWidth(),
                    $windowHeight = $window.outerHeight(),
                    $width = $this.outerWidth(),
                    $height = $this.outerHeight(),
                    $paddingTop = 40,
                    $paddingRight = 125,
                    $paddingBottom = 40,
                    $paddingLeft = 135;

                var $player = $this.find(".slideshow-container");
                $player.css({
                    "padding-left": ($width * $paddingLeft) / 902,
                    "padding-right": ($width * $paddingRight) / 902,
                    "padding-top": ($height * $paddingTop) / 436,
                    "padding-bottom": ($height * $paddingBottom) / 436,
                });
            });
        }

        if ($.exists(".mk-tablet-slideshow")) {
            $tabletPortrait.each(function() {
                var $this = $(this),
                    $window = $(window),
                    $windowWidth = $window.outerWidth(),
                    $windowHeight = $window.outerHeight(),
                    $width = $this.outerWidth(),
                    $height = $this.outerHeight(),
                    $paddingTop = 78,
                    $paddingRight = 36,
                    $paddingBottom = 83,
                    $paddingLeft = 30;

                var $player = $this.find(".slideshow-container");
                $player.css({
                    "padding-left": ($width * $paddingLeft) / 501,
                    "padding-right": ($width * $paddingRight) / 501,
                    "padding-top": ($height * $paddingTop) / 739,
                    "padding-bottom": ($height * $paddingBottom) / 739,
                });
            });
        }
    }

    /* Imagebox Video Player Autoplay Freeze
    /* -------------------------------------------------------------------- */

    function mk_imagebox_autoplay_freeze() {
        $(".mk-image-box.autoplay-true").each(function() {
            var $container = $(this),
                $source = $container.data('source'),
                player;


            if ($source == 'self_hosted') {
                player = $container.find('video')[0];
            }
            if ($source == 'youtube') {
                var youtube = $container.find('iframe')[0];
                player = new YT.Player(youtube);
            }
            if ($source == 'vimeo') {
                var vimeo = $container.find('iframe')[0];
                player = $f(vimeo);
            }


            $container.on('inview', function(event, visible) {
                if (visible == true) {
                    if ($source == 'self_hosted') {
                        player.play();
                    }
                    if ($source == 'youtube') {
                        player.playVideo();
                        player.mute();
                    }
                    if ($source == 'vimeo') {
                        player.api('play');
                        player.api('setVolume', 0);
                    }
                } else {
                    if ($source == 'self_hosted') {
                        player.pause();
                    }
                    if ($source == 'youtube') {
                        player.pauseVideo();
                    }
                    if ($source == 'vimeo') {
                        player.api('pause');
                    }
                }
            });
        });
    }

    function shop_isotop_init() {
        if ($.exists('.products') && !$('.products').hasClass('related')) {
            $('.products.isotope-enabled').each(function() {
                //console.log('shop_isotop_init_inside');
                if (!$(this).parents('.mk-woocommerce-carousel').length) {
                    var $woo_container = $(this),
                        $container_item = '.products .product';

                    $woo_container.isotope({
                        itemSelector: $container_item,
                        masonry: {
                            columnWidth: 1
                        }
                    });
                }
            });
        }
    }

    function shop_categories_isotop_init() {
        if ($.exists('.mk-product-categories')) {
            $('.mk-product-categories-list').each(function() {
                //console.log('shop_isotop_init_inside');
                var $woo_container = $(this),
                    $container_item = '.mk-product-categories-list .product-item';

                $woo_container.isotope({
                    itemSelector: $container_item,
                    masonry: {
                        columnWidth: 1
                    }
                });
            });
        }
    }



    /* Vertical Menu Accordion */
    /* -------------------------------------------------------------------- */
    function mk_vertical_menu_submenu() {
        if ($.exists(".vertical-header")) {
            $('.mk-vertical-menu .menu-item').hoverIntent({
                over: function() {
                    if ($(this).is('.menu-item-has-children')) {
                        $(this).find('> .sub-menu').slideToggle();
                    }
                },
                out: function() {
                    if ($(this).is('.menu-item-has-children')) {
                        $(this).find('> .sub-menu').slideToggle();
                    }
                },
                timeout: 300
            });
        }
    }


    /* WPML Language Selector */
    /* -------------------------------------------------------------------- */
    function mk_header_wpml() {
        $('.mk-header-wpml-ls').hoverIntent({
            over: function() {
                $('.language-selector-box').fadeIn(200);
            },
            out: function() {
                $('.language-selector-box').fadeOut(200);
            },
            timeout: 500
        });
    }




    /* Woocmmerce Header Checkout */
    /* -------------------------------------------------------------------- */

    function mk_header_checkout() {
        $('header').on('mouseenter', '.mk-shopping-cart', function() {
            $('.mk-shopping-box').stop(true, true).fadeIn(150).css({'z-index' : 100});
        });
        $('header').on('mouseleave', '.mk-shopping-cart', function() {
            $('.mk-shopping-box').stop(true, true).delay(1000).fadeOut(200);
        });

        // we need to delegate events to bind them 'live'. Element is processed off the page and reinserted
    }



    /* Woocommerce Scripts */
    /* -------------------------------------------------------------------- */

    function product_loop_add_cart() {
        var $body = $('body');

        $body.on('click', '.add_to_cart_button', function() {
            var product = $(this).parents('.product:eq(0)').addClass('adding-to-cart').removeClass('added-to-cart');
        });

        $body.bind('added_to_cart', function() {
            $('.adding-to-cart').removeClass('adding-to-cart').addClass('added-to-cart');
            mk_header_checkout();
        });
    }
    product_loop_add_cart();

    /* Ajax Search */
    /* -------------------------------------------------------------------- */

    function mk_ajax_search() {
        if ($.exists('.search-ajax-input')) {
            var security = $('#mk-ajax-search-input').siblings('input[name="security"]').val(),
            _wp_http_referer = $('#mk-ajax-search-input').siblings('input[name="_wp_http_referer"]').val();
            $(".search-ajax-input").autocomplete({
                delay: 50,
                minLength: 2,
                messages: {
                    noResults: '',
                    results: function() {}
                },
                appendTo: $(".header-searchform-input"),
                source: function(req, response) {
                    $.getJSON(ajaxurl + '?callback=?&action=mk_ajax_search&security='+security+'&_wp_http_referer='+_wp_http_referer, req, response);
                },
                select: function(event, ui) {
                    window.location.href = ui.item.link;
                }

            }).data("ui-autocomplete")._renderItem = function(ul, item) {


                return $("<li>").append("<a>" + item.image + "<span class='search-title'>" + item.label + "</span><span class='search-date'>" + item.date + "</span></a>").appendTo(ul);

            };
        }
    }

    mk_ajax_search();



    /* Contact Form */
    /* -------------------------------------------------------------------- */

    function mk_contact_form() {

        if ($.tools.validator != undefined) {
            $.tools.validator.addEffect("contact_form", function(errors) {
                $.each(errors, function(index, error) {
                    var input = error.input;

                    input.addClass('mk-invalid');
                });
            }, function(inputs) {
                inputs.removeClass('mk-invalid');
            });


            $(".captcha-change-image").on("click", function(e) {
                e.preventDefault();
                changeCaptcha();
            });

            $(".captcha-form").each(function() {
              $(this).on("focus", function() {
                $(this).attr("placeholder", mk_captcha_placeholder).removeClass('contact-captcha-invalid contact-captcha-valid');
              });
            });

            var changeCaptcha = function() {
                $(".captcha-image").attr("src", mk_captcha_url + "?" + Math.random());
            }

            var sendForm;
            var checkCaptcha = function(form, enteredCaptcha) {
                $.get(mk_captcha_check_url, {
                    captcha: enteredCaptcha
                }).done(function(data) {
                    if (data != "ok") {
                        changeCaptcha();
                        form.find(".captcha-form").val("").addClass('contact-captcha-invalid').attr("placeholder",mk_captcha_invalid_txt);
                    } else {
                        sendForm();
                        changeCaptcha();
                        form.find(".captcha-form").val("").addClass('contact-captcha-valid').attr("placeholder", mk_captcha_correct_txt);
                    }
                });
            }

            $('.mk-contact-form').validator({
                effect: 'contact_form'
            }).submit(function(e) {
                var form = $(this);
                if (!e.isDefaultPrevented()) {
                    // progressButton.loader(form);
                    // $(this).find('.mk-contact-loading').fadeIn('slow');

                    var data = {
                        action: 'mk_contact_form',
                        security : form.find('input[name="security"]').val(),
                        _wp_http_referer : form.find('input[name="_wp_http_referer"]').val(),
                        p_id : form.find('input[name="p_id"]').val(),
                        sh_id: form.find('input[name="sh_id"]').val(),
                        name: form.find('input[name="contact_name"]').val(),
                        phone: form.find('input[name="contact_phone"]').val(),
                        email: form.find('input[name="contact_email"]').val(),
                        content: form.find('textarea[name="contact_content"]').val()
                    };


                    sendForm = function() {
                        progressButton.loader(form);
                        $.post(ajaxurl, data, function(response) {
                            form.find('.mk-contact-loading').fadeOut('slow');
                            form.find('.text-input').val('');
                            form.find('textarea').val('');
                            progressButton.success(form);
                        });
                    };

                    var enteredCaptcha = form.find('input[name="captcha"]').val();
                    if (form.find('.captcha-form').length) {
                        checkCaptcha(form, enteredCaptcha);
                    } else {
                        sendForm();
                    }

                    e.preventDefault();
                }
            });

        }
    }

    mk_contact_form();


    $(this).find('.mk-form-row input, .comment-form-row input, .mk-login-form input').each(function() {

        $(this).focusin(function() {
            $(this).siblings('i').addClass('input-focused');
        });
        $(this).focusout(function() {
            $(this).siblings('i').removeClass('input-focused');
        });

    });



    /* Ajax Login Form */
    /* -------------------------------------------------------------------- */

    function mk_login_form() {

        $('form.mk-login-form').each(function() {
            var $this = $(this);
            $this.on('submit', function(e) {
                $('p.mk-login-status', $this).show().text(ajax_login_object.loadingmessage);
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: ajax_login_object.ajaxurl,
                    data: {
                        'action': 'ajaxlogin',
                        //calls wp_ajax_nopriv_ajaxlogin
                        'username': $('#username', $this).val(),
                        'password': $('#password', $this).val(),
                        'security': $('#security', $this).val()
                    },
                    success: function(data) {
                        $('p.mk-login-status', $this).text(data.message);
                        if (data.loggedin === true) {
                            document.location.href = ajax_login_object.redirecturl;
                        }
                    }
                });
                e.preventDefault();
            });
        });
    }

    mk_login_form();

    /* quick contact form function */
    /* -------------------------------------------------------------------- */
    var eventtype = mobilecheck() ? 'touchstart' : 'click';
    jQuery('.mk-quick-contact-link').on(eventtype, function() {
        var $this = jQuery(this),
            $quickContact = jQuery('.mk-quick-contact-overlay'),
            $quickContactInner = $quickContact.find('.mk-quick-contact-inset');

        $quickContact.addClass('mk-quick-contact-visible');

        disableScroll();

        return false;
    });
    jQuery( document ).on(eventtype, function() {
        $('.mk-quick-contact-overlay').removeClass('mk-quick-contact-visible');
        enableScroll();
    });

    var $contactForm = $( '.mk-contact-form' );

    $contactForm.find('a').on( eventtype, stopPropagation );
    $contactForm.find('input').on( eventtype, stopPropagation );
    $contactForm.find('textarea').on( eventtype, stopPropagation );
    $contactForm.find('button').on( eventtype, stopPropagation );

    function stopPropagation(ev) {
        ev.stopPropagation();
    };

    /* message box close function */
    /* -------------------------------------------------------------------- */

    $('.box-close-btn').on(eventtype, function() {
        $(this).parent().fadeOut(300);
        return false;
    });


    /* Smooth scroll using hash */
    /* -------------------------------------------------------------------- */
    function mk_hash_scroll() {
        var headerHeight = ken.modules.header.calcHeight();
        $(".mk-smooth, .blog-comments").on('click', function(e) {
            TweenLite.to(window, 1.2, {
                scrollTo: {
                    y: $($(this).attr("href")).offset().top - (headerHeight + 2)
                },
                ease: Expo.easeInOut
            });

            e.preventDefault();
        });
    }

    /* Responsive Header Toolbar */
    /* -------------------------------------------------------------------- */
    function mk_responsive_header_toolbar() {

        $('.mk-toolbar-responsive-icon').on('click', function(e) {
            var $this = $(this),
                res_header_toolbar = $this.parents('.mk-responsive-header-toolbar').prev('.mk-header-toolbar');

            if (!$this.hasClass('active-header-toolbar')) {
                $this.addClass('active-header-toolbar');
                res_header_toolbar.show();
            } else {
                $this.removeClass('active-header-toolbar');
                res_header_toolbar.hide();
            }
            e.preventDefault();
        });
    }

    /* Responsive Toolbar */
    /* -------------------------------------------------------------------- */
    

    /* Scroll function for main navigation on one page concept */
    /* -------------------------------------------------------------------- */



    function mk_main_nav_scroll() {

        var lastId, topMenu = $(".main-navigation-ul, .mk-responsive-nav"),
            menuItems = topMenu.find(".menu-item a"),
            headerHeight = ken.modules.header.calcHeight(),
            href;

        menuItems.each(function() {
            var href_attr = $(this).attr('href');
            if (typeof href_attr !== 'undefined' && href_attr !== false) {
                href = href_attr.split('#')[0];
                if(typeof href_attr.split('#')[1] !== 'undefined' && href_attr.split('#')[1].length) {
                    $(this).addClass("one-page-nav-item");
                }
            } else {
                href = "";
            }

            if (href === window.location.href.split('#')[0] && (typeof $(this).attr("href").split('#')[1] !== 'undefined') && href !== "") {

                $(this).attr("href", "#" + $(this).attr("href").split('#')[1]);
                $(this).parent().removeClass("current-menu-item");
            }
        });

        var onePageMenuItems = $('.one-page-nav-item');

        var scrollItems = onePageMenuItems.map(function() {
            var item = $(this).attr("href");

            if (/^#\w/.test(item) && $(item).length) {
                return $(item);
            }
        });


        topMenu.on('click', '.one-page-nav-item', function(e) {
            var href = $(this).attr("href");
            if (typeof $(href).offset() !== 'undefined') {
                var href_top = $(href).offset().top;
            } else {
                var href_top = 0;
            }

            if($(window).width() < mk_nav_res_width) {
                headerHeight = 0;
            }

            var offsetTop = href === "#" ? 0 : href_top - (headerHeight + 2);

            /*
             * We need to trigger click as it will close menu and pass another event 'mk-closed-nav' which will unlock scrolling
             * blocked by edge one pager
             */
            if($.exists('.responsive-nav-link.active-burger')) {
              $('.responsive-nav-link.active-burger').trigger('click');
            }

            console.log(offsetTop)

            $('html, body').stop().animate({
              scrollTop: offsetTop
            }, {
              duration: 1200,
              easing: "easeInOutExpo"
            });

            e.preventDefault();
        });


        var fromTop;
        var animationSet = function() {

            if (!scrollItems.length) {
                return false;
            }

            fromTop = scrollY + (headerHeight + 10);

            var cur = scrollItems.map(function() {
                if ($(this).offset().top < fromTop) { 
                    return this;
                }
            });
            //console.log(cur);
            cur = cur[cur.length - 1];
            var id = cur && cur.length ? cur[0].id : "";

            if (lastId !== id) {
                lastId = id;

                console.log(fromTop);

                onePageMenuItems.parent().removeClass("current-menu-item");
                //console.log(id);
                if (id.length) {
                    onePageMenuItems.filter("[href=#" + id + "]").parent().addClass("current-menu-item");
                    //console.log(id);
                }
            }
        };
        $(window).scroll(animationSet)
        // debouncedScrollAnimations.add(animationSet);
    }

});