$(function () {

    ///Show submenu
    $(".showSubMenu").click(function () {
        $(this).nextAll("ul").toggleClass("show");
        $(this).toggleClass('open');
    })

    $(".main-menu-head").hover(function () {
        $(this).children().find(".main-menu-sub").first().addClass('main-menu-sub-active');
        $(this).children().addClass('active');
    })
    $(".main-menu-head").mouseleave(function () {
        $(this).children().find(".main-menu-sub").first().removeClass('main-menu-sub-active');
        $(this).children().removeClass('active');
    })
    $(".main-menu li").mouseover(function () {

        $(".main-menu li").removeClass("main-menu-sub-active-li");
        $(this).addClass("main-menu-sub-active-li");
        $(".main-menu-sub").removeClass('main-menu-sub-active');
        $(this).children('ul').removeClass('main-menu-sub-active');
        $(this).children('ul').addClass('main-menu-sub-active');
    });
    $(".main-menu-sub-active").mouseleave(function () {
        $(".main-menu-sub-active").removeClass("main-menu-sub-active");
    })

    ///Product counter to add to cart
    $("input.counter").TouchSpin({
        min: 1,
        max: '1000000000000000',
        buttondown_class: "btn-counter waves-effect waves-light",
        buttonup_class: "btn-counter waves-effect waves-light"
    });

    ///Color picker
    $(".category-sort .form-checks .form-check").click(function () {
        $(".category-sort .form-checks .form-check").removeClass("active");
        $(this).addClass('active');
        $(".category-sort .form-checks .form-check").children("input[type=radio]").attr('checked', false);
        $(this).children("input[type=radio]").attr('checked', true);
    })


    ///Choose a delivery time
    $(".send-item").click(function () {
        $(".send-item").removeClass("active");
        $(this).addClass('active');
    })

    ///Choose a shipping method
    $(".shipping-item").click(function () {
        $(".shipping-item").removeClass("active");
        $(this).addClass('active');
    })

    ///Choose a payment method
    $(".bank-item").click(function () {
        $(".bank-item").removeClass("active");
        $(this).addClass('active');
    })


    jQuery('[data-bs-toggle="tooltip"]').tooltip();
    jQuery('[data-bs-toggle="modal"][title]').tooltip();

});



/* ----------------------------------------
   New Mega Menu – Vanilla JavaScript (with hover close)
----------------------------------------- */
document.addEventListener('DOMContentLoaded', function () {
    const menuItems = document.querySelectorAll('.custom-mega-category');
    const panels = document.querySelectorAll('.custom-mega-panel');
    const megaMenu = document.querySelector('.custom-mega-menu');
    const megaContent = document.querySelector('.custom-mega-content');
    const megaTrigger = document.querySelector('.custom-mega-trigger');

    // Set default active panel
    const defaultPanel = document.querySelector('[data-mega-target="4"]');
    if (defaultPanel) {
        defaultPanel.classList.add('active');
    }

    // Switch panel on hover
    menuItems.forEach(item => {
        item.addEventListener('mouseenter', function () {
            const targetId = this.getAttribute('data-mega-id');

            // Deactivate all items
            menuItems.forEach(i => i.classList.remove('active'));

            // Activate current item
            this.classList.add('active');

            // Hide all panels
            panels.forEach(panel => panel.classList.remove('active'));

            // Show related panel
            const targetPanel = document.querySelector(`[data-mega-target="${targetId}"]`);
            if (targetPanel) {
                targetPanel.classList.add('active');
            }
        });
    });

    // Close mega menu when mouse leaves the menu area
    if (megaMenu && megaContent) {
        megaMenu.addEventListener('mouseleave', function () {
            megaContent.style.display = 'none';
        });

        // Optional: re-open when hovered again
        megaMenu.addEventListener('mouseenter', function () {
            megaContent.style.display = 'block';
        });
    }

    // Show mega menu when hovering over the trigger
    if (megaTrigger && megaContent) {
        megaTrigger.addEventListener('mouseenter', function () {
            megaContent.style.display = 'block';
        });
    }
});

/**
 * open search form flaot
 */

$(document).ready(function () {

    /**
     * open
     */
    $(".header-search").click(function () {
        $(".search-float").toggleClass("open");
    });

    /**
     * close
     */

    $(".search-float-close").click(function () {
        $(".search-float").toggleClass("open");
    });


});

/**
 * open basket float with click
 */

$(document).ready(function () {

    /**
     * open
     */
    $(".header-cart-icon-toggle").click(function () {
        $(".min-cart").toggleClass("open");
    });

});


/**
 * config floating contact
 */
$('#btncollapzion').Collapzion({
    _child_attribute: [{
        'label': 'پشتیبانی تلفنی',
        'url': 'tel:0930555555555',
        'icon': 'bi bi-telephone'
    },
        {
            'label': 'پشتیبانی تلگرام',
            'url': 'https://tlgrm.me',
            'icon': 'bi bi-telegram'
        },
        {
            'label': 'پشتیبانی واتس آپ',
            'url': 'https://wa.me/444444444',
            'icon': 'bi-whatsapp'
        },

    ],
});


// When the user clicks on the button, scroll to the top of the document
function topFunction() {
    document.body.scrollTop = 0; // For Safari
    document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}

/*
*
* mega menu floating
*
* */
$(function () {
    var lastScrollTop = 0, delta = 50;
    $(window).scroll(function () {
        var nowScrollTop = $(this).scrollTop();
        if (Math.abs(lastScrollTop - nowScrollTop) >= delta) {
            if (nowScrollTop > lastScrollTop) {
                // ACTION ON
                // SCROLLING DOWN
                $(".mega-menu").removeClass("mega-menu-top");
                //$(".header").addClass("shadow-md pb-3");
            } else {
                // ACTION ON
                // SCROLLING UP
                $(".mega-menu").addClass("mega-menu-top");
                //$(".header").removeClass("shadow-md pb-3");
            }
            lastScrollTop = nowScrollTop;
        }
    });
});


/**
 * delivery item form check
 */

$(document).ready(function () {

    $(".delivery-item").click(function () {
        $(this).find('input').prop("checked", true);
    });

    $(".delivary-payment-bank-item").click(function (){
        $(".delivary-payment-bank-item").removeClass("active");
        $(this).addClass("active");
    });
});


/** Multi-step login/registration form*/

$(document).ready(function (){
    ///disable fild password in load form
    $(".step-passwd").hide();
    ///disable button submit in step one
    $(".step-two").hide();

    ///show filed password in step two form
    $(".step-one").click(function (){

        ///check empty fild username
        if($(".step-username #username").val() != ""){
            ///hide username filed
            $(".step-username").hide();
            ///show password filed
            $(".step-passwd").show();
            ///hide button step one
            $(this).hide();
            ///show button submit
            $(".step-two").show();
        }else{
            $(".step-username #username").addClass("border-danger border-2");
        }


        ///check empty fild password

        $(".btnForm").click(function(){
            if($(".step-passwd #passwd").val() !=""){
                $("#form-auth").submit();
            }else{
                $(".step-passwd #passwd").addClass("border-danger border-2");
            }
        })


    })

})
