﻿
$(document).ready(function () {

    // change dropdown hover to click on mobile
    var $this,
        table = false;

    if ($(window).outerWidth() <= 991) {
        table = true;
    }

    $('.dropdown').hover(
        function() {
            if (table) {
                return false;
            }
            $this = $(this);
            // $this.find('.dropdown-menu').stop(true, true).delay(200).fadeIn(500);
            $this.find('.dropdown-menu').stop(true, true).addClass("show");
        }, function() {
            if (table) {
                return false;
            }
            // $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeOut(500);
            $(this).find('.dropdown-menu').stop(true, true).removeClass("show");
        }
    );
    // END

    // header menu
    if (!table) {
        //console.log("not table");
        $("[data-header-submenu-dropdown]").eq(0).addClass("active");
        $("[data-header-menu-dropdown]").on("mouseover", function () {
            $("[data-header-menu-dropdown]").removeClass("active");
            $(this).addClass("active")
        });

        $("[data-header-menu-dropdown]").on("mouseleave", function () {
            $("[data-header-menu-dropdown]").removeClass("active")
        });
        $("[data-header-submenu-dropdown]").on("mouseover", function () {
            $("[data-header-submenu-dropdown]").removeClass("active");
            $(this).addClass("active")
        });


        $("[data-header-submenu-dropdown]").on("mouseleave", function () {
            $("[data-header-submenu-dropdown]").removeClass("active");
            $("[data-header-submenu-dropdown]").eq(0).addClass("active");
        });
    } else {
        //console.log("table");
        $("[data-dropdown-menu-arrow]").click(function () {
            if($(this).hasClass("active")){
                $(this).css("transform", "rotateX(" + 0 + "deg)");

                // $("[data-header-menu-dropdown]").removeClass("active");
                $(this).siblings("[data-dropdown-menu]").stop(true, true).slideUp();
                $(this).removeClass("active");
            } else {
                $(this).css("transform", "rotateX(" + 180 + "deg)");

                // $("[data-header-menu-dropdown]").addClass("active");
                $(this).siblings("[data-dropdown-menu]").stop(true, true).slideDown();
                $(this).addClass("active");
            }
            // $("[data-header-menu-dropdown]").toggleClass("active");
            // $(this).siblings(".main_menu-dropdown_container").stop(true, true).slideToggle();
        });
    }
    // header menu END

    $(".modal .tabs").click(function(el){
        clickOnModalTabs($(this))
    });


    // modal with tabs
    function clickOnModalTabs(el){
        var $el = $(el);
        if($el.hasClass("active")){
            return;
        }

        var curentTabId = $el.attr('id'),
            curentTab = "#" + curentTabId + "-1";

        $el.siblings().removeClass("active");

        $el.addClass("active").fadeIn(200);

        $(curentTab).siblings().removeClass("show").css("display", "none");
        $(curentTab).addClass("show").fadeIn(200);
    }
    // END


    // corect open auth modal tabs
    $("[data-modal-auth]").on("click", function(){
        var $this = $(this);
        var $el;

        $('#auth_modal').on('show.bs.modal', function () {
            if ($this.hasClass("header_auth-login")) {
                $el = $("#auth_modal-login_tab");
                // if(!$("#auth_modal-login_tab").hasClass("active")){
                    // $("#auth_modal-login_tab").trigger("click");
                clickOnModalTabs($el);
                // }
            } else if ($this.hasClass("header_auth-regist")) {
                $el = $("#auth_modal-regist_tab");
                // if(!$("#auth_modal-regist_tab").hasClass("active")){
                    // $("#auth_modal-regist_tab").trigger("click");
                    clickOnModalTabs($("#auth_modal-regist_tab"));
                // }
            }
            clickOnModalTabs($el);
        })
    });
    // END

    // remove min cart item
    // $("[data-min-cart-item-remove]").on("click", function(e){
    //     e.preventDefault();
    //     e.stopPropagation();
    //
    //     var $parent = $(this).closest("[data-min-cart-item]");
    //
    //     $parent.slideUp(400, function(){
    //         $(this).remove();
    //     });
    // });
    // END

    $("body").on("click", "[data-open-menu]", function () {
        openMobileMenu();
    })

    $("body").on("click", "[data-close-mobile-menu]", function () {
        closeMobileMenu();
    })
});


var headerFixed = false,
    header = $('header').outerHeight(),
    headerHeight = $('.main_header').outerHeight(),
    headerTop = $('.main_header').offset().top;

function fixedHeader(){
    if($(window).outerWidth() <= 991) {
        $("body").removeClass("page-scroll");
        $(".top_header").removeAttr("style");
        $(".main_header").removeAttr("style").removeClass("fixed");
        setTimeout(function () {
            $("[data-search-block]").removeAttr("style");
        }, 200);

        // headerFixed = false;
        return;
    }

    if ($(window).scrollTop() - 100 > header && !headerFixed) {
        // $(".main_header").css("top", -headerHeight);

        $("body").addClass("page-scroll");
        $(".main_header").addClass("fixed");
        $(".top_header").css("padding-bottom", headerHeight);
        $(".main_header").stop("true", "true").animate({
            top: "0",
            opacity: "1"
        }, 200);
        // remove animate on search container
        $("[data-search-block]").css("transition", "0s");
        headerFixed = true;
    } else if ($(window).scrollTop() - 100 < header && headerFixed) {
        $(".main_header").stop("true", "true").fadeOut(200, function () {
            $("body").removeClass("page-scroll");
            $(".top_header").removeAttr("style");
            $(".main_header").removeAttr("style").removeClass("fixed");
            setTimeout(function () {
                $("[data-search-block]").removeAttr("style");
            }, 200);
        });

        headerFixed = false;
    }
}

$(window).scroll(function(){
    // if($(window).width() > 992) {}
    fixedHeader();
})

$(window).on("load", function () {
    setTimeout(function(){
        corectSubmenuSize();
    }, 400);
});

$(window).resize(function () {
    fixedHeader();
    corectSubmenuSize();
});

// corect dropdown menu size
function corectSubmenuSize(){
    // corect submenu width
    var submenuWidth = $(".main_menu-dropdown_container").outerWidth(),
        menuListWidth = $("[data-header-submenu-dropdown]").outerWidth();

    if($(window).outerWidth() <= 992) {
        $(".main_menu-dropdown_submenu").removeAttr("style");
        $("[data-header-submenu-dropdown]").parent().removeAttr("style");
        return;
    }

    // corect menu height
    $(".main_menu-dropdown_submenu").css("width", submenuWidth - menuListWidth);

    var menuHeight = $("[data-header-submenu-dropdown]").outerHeight(),
        minMenuHeight = 0;

    $("[data-header-submenu-list]").each(function(i, el){
        var submenuListHeight = $(this).outerHeight();

        if(minMenuHeight < submenuListHeight){
            minMenuHeight = submenuListHeight;
        }
    });

    if(menuHeight < minMenuHeight){
        $("[data-header-submenu-dropdown]").parent().css("min-height", minMenuHeight);
    }
}
// END




// open mobile menu
function openMobileMenu() {
    $("[data-mobile-menu-bg]").fadeIn();
    $("[data-menu-container]").animate({
        left: '0'
    }, 200);
    $("body").addClass("modal-open");
    $("html").addClass("modal-open");
}
// END


// close mobile menu
function closeMobileMenu() {
    var mobileMenuWidth = $("[data-menu-container]").outerWidth();

    $("[data-menu-container]").animate({
        left: -mobileMenuWidth
    }, 200);
    $("[data-mobile-menu-bg]").fadeOut();
    $("body").removeClass("modal-open");
    $("html").removeClass("modal-open");
}
// END
