jQuery(document).ready(function($) {
	$(document).on('click', '.product-view-type-item', function(event) {
        var viewtype = $(this).data('item');
        $('.product-view-type-item').removeClass('active');
        $(this).addClass('active');
        $('ul.products').removeClass(function(index, className) {
            return (className.match(/(^|\s)product-view-\S+/g) || []).join(' ');
        });
        $('ul.products').addClass('product-view-'.concat(viewtype));
    });

    /* ============== Quantity buttons ============== */

    // Target quantity inputs on product pages
    $('input.qty:not(.product-quantity input.qty)').each(function() {
        var min = parseFloat($(this).attr('min'));

        if (min && min > 0 && parseFloat($(this).val()) < min) {
            $(this).val(min);
        }
    });

    $(document).on('click', '.plus, .minus', function() {

        // Get values
        var $qty = $(this).closest('.quantity').find('.qty'),
            currentVal = parseFloat($qty.val()),
            max = parseFloat($qty.attr('max')),
            min = parseFloat($qty.attr('min')),
            step = $qty.attr('step');

        // Format values
        if (!currentVal || currentVal === '' || currentVal === 'NaN') currentVal = 0;
        if (max === '' || max === 'NaN') max = '';
        if (min === '' || min === 'NaN') min = 0;
        if (step === 'any' || step === '' || step === undefined || parseFloat(step) === 'NaN') step = 1;

        // Change the value
        if ($(this).is('.plus')) {

            if (max && (max == currentVal || currentVal > max)) {
                $qty.val(max);
            } else {
                $qty.val(currentVal + parseFloat(step));
            }

        } else {

            if (min && (min == currentVal || currentVal < min)) {
                $qty.val(min);
            } else if (currentVal > 0) {
                $qty.val(currentVal - parseFloat(step));
            }

        }

        // Trigger change event
        $qty.trigger('change');
    });

    $(document).on('click', '.woocommerce-review-link', function(event) {
        var $sect_id = $(this).attr('href');
        jQuery('html, body').animate({
            scrollTop: $($sect_id).offset().top - 50
        }, 500);
    });

    $(function() {
        $('[data-toggle="tooltip"]').tooltip({
            html: true,
        });
    });


    var is_rtl = storez_script_obj.rtl;
    is_rtl = (is_rtl) ? true : false;
    window.home_carousel = $('.main-slider-carousel').owlCarousel({
        rtl: is_rtl,
        nav: true,
        loop: true,
        margin: 1,
        responsiveClass: true,
        smartSpeed: 1000,
        items: 1,
        navText: ['<i class="fa fa-angle-left"></i>', '<i class="fa fa-angle-right"></i>'],
        dots: true,
        autoplay: false,
        autoplaySpeed: 2000,
    });

    var product_carasol = $('.home-products-recents').owlCarousel({
        nav: true,
        loop: false,
        margin: 10,
        responsiveClass: true,
        items: 5,
        navText: ['<i class="fa fa-angle-left"></i>', '<i class="fa fa-angle-right"></i>'],
        dots: false,
        responsiveClass:true,
        responsive:{
            0:{
                items:1,
                nav:false,
                margin:0
            },
            500:{
                items:2,
                nav:false
            },
            768:{
                items:3,
            },
            992:{
                items:5,
            },
        },
        autoplay: true,
        autoplaySpeed: 2000
    });

    window.brand_carousel = $('.brand-carousel').owlCarousel({
        rtl: is_rtl,
        nav: true,
        loop: true,
        margin: 15,
        responsiveClass: true,
        items: 5,
        navText: ['<i class="fa fa-angle-left"></i>', '<i class="fa fa-angle-right"></i>'],
        dots: true,
        responsiveClass: true,
        responsive: {
            0: {
                items: 1,
                nav: false
            },
            500: {
                items: 2,
                nav: false
            },
            768: {
                items: 3,
            },
            992: {
                items: 5,
            },
        }
    });

    $(document).on('click', '.btn-filter', function(e) {
        var cat = $(this).data('filter');
        
        /* return if current */
        if ($(this).hasClass('btn-active')) return;

        /* active current */
        $(this).addClass('btn-active').siblings().removeClass('btn-active');

        $elemCopy = $('<div>').css('display', 'none');
        if (typeof($(product_carasol).data('owl-clone')) == 'undefined') {
            //console.log($(owl).data('owl-clone'));
            $(product_carasol).find('.owl-item:not(.cloned)').clone().appendTo($elemCopy);
            $(product_carasol).data('owl-clone', $elemCopy);
        } else {
            $elemCopy = $(product_carasol).data('owl-clone');
        }
        var elemLength = $elemCopy.children().length;
        for (var i = 0; i < elemLength; i++) {
            product_carasol.trigger('remove.owl.carousel', i);
        }
        /* Filter */
        switch (cat) {
            case '*':
                $elemCopy.find('.item').each(function() {
                    product_carasol.trigger('add.owl.carousel', [$(this).clone()]);

                });
                break;
            default:
                $elemCopy.find('.' + cat).each(function() {
                    product_carasol.trigger('add.owl.carousel', [$(this).clone()]);
                });
                break;
        }
        product_carasol.trigger('refresh.owl.carousel').trigger('to.owl.carousel', [0]);
    });

      $('.widget-product-carousel').each(function(index, el) {
        var items = $(this).data('items');
        
        var show_nav = false;
        if($(this).hasClass('show-carousel-nav')){
            var show_nav = true;
        }

        $(el).owlCarousel({
            rtl: is_rtl,
            nav: show_nav,
            loop: true,
            margin: 15,
            responsiveClass: true,
            items: items,
            navText: ['<i class="fa fa-angle-left"></i>', '<i class="fa fa-angle-right"></i>'],
            dots: false,
            responsiveClass: true,
            responsive: {
                0: {
                    items: 1,
                    nav: show_nav
                },
                500: {
                    items: 2,
                    nav: show_nav
                },
                768: {
                    items: 3,
                },
                992: {
                    items: items,
                },
            },
            autoplay: true,
            autoplaySpeed: 1500,
            smartSpeed:1500,
            autoplayHoverPause:true,
        });
    });

    if (jQuery(window).width() > 767) {
        $('.mobile-nav').prop('disabled', true);
    }else{
        $('.mobile-nav').prop('disabled', false);
    }

    jQuery('li.dropdown').find('.mobile-nav').each(function() {
        jQuery(this).on('click', function(event) {
            event.preventDefault();
            if (jQuery(window).width() < 768) {
                var nav = $(this).parent().parent();
                var dropdown = $(this).parent().siblings('.dropdown-menu');
                if (nav.hasClass('show')) {
                    nav.removeClass('show');
                    dropdown.removeClass('show');
                } else {
                    nav.addClass('show');
                    dropdown.addClass('show');
                }
            }
            return false;
        });
    });

    $(document).on('focusin', 'li.menu-item a', function(event) {
        $(this).siblings('ul').addClass('nav-link-focus');
        $(this).parent().siblings('li.menu-item').children('ul').removeClass('nav-link-focus');
    });

    $(document).on('focusin', '.product-catalogue-heading', function(event) {
        $(this).siblings('.product-catalogue-container').addClass('show');
    });

    $(document).on('focusin', 'a, button', function(event) {
        if(!$(this).is('ul li a')){
            $('li').children('ul').removeClass('nav-link-focus');
        }
        if(!$(this).parents('.catalogue-container').length){
            $('.product-catalogue-container').removeClass('show');
        }
    });

    $(document).on('focusout', '.product-catalogue-container', function(event) {
        
        // $(this).removeClass('show');
    });

    $('.the-post-img').simpleLightbox();
});