$(function () {
    "use strict";

    /*---------------------------
     Commons Variables
     ------------------------------ */
    var $window = $(window),
            $body = $("body");

    /*---------------------------
     Menu Fixed On Scroll Active
     ------------------------------ */

//    $(window).scroll(function () {
//        var screen = $(window).width();
//        var window_top = $(window).scrollTop() + 1;
//        console.log(window_top);
//        if (screen > 1200) {
//            if (window_top > 30) {
//                $(".col-header").addClass("menu_fixed ");
//                $(".header-wrapper").addClass('margin_bottom_450');
//
//            } else {
//                $(".col-header").removeClass("menu_fixed ");
//                $(".header-wrapper").removeClass('margin_bottom_450');
//            }
//        }
//
//        if (screen <= 1200) {
//            if (window_top > 30) {
//                $(".mobile-header").addClass("menu_fixed");
//            } else {
//                $(".mobile-header").removeClass("menu_fixed ");
//                ;
//            }
//        }
//
//
//    });



    var lastScrollTop = 0, delta = 5;
    $(window).scroll(function () {
        var nowScrollTop = $(this).scrollTop();
        var screen = $(window).width();
        var window_top = $(window).scrollTop() + 1;
        if (Math.abs(lastScrollTop - nowScrollTop) >= delta) {
            if (nowScrollTop > lastScrollTop) {
                // ACTION ON
                // SCROLLING DOWN 

                $('.header-menu').removeClass('fixo');
                if (screen > 1200) {
                    if (window_top > 30) {
                        $(".col-header").addClass("menu_fixed ");
                        $(".header-wrapper").addClass('margin_bottom_450');

                    } else {
                        $(".col-header").removeClass("menu_fixed ");
                        $(".header-wrapper").removeClass('margin_bottom_450');
                    }
                }

                if (screen <= 1200) {
//                    console.log("baixando", window_top);
                    if (window_top > 30) {
//                        console.log('add');
                        $(".mobile-header").addClass("menu_fixed");
                    } else {
                        $(".mobile-header").removeClass("menu_fixed ");
                    }
                }
            } else {
                // ACTION ON
                // SCROLLING UP 
//                console.log('up') 

//console.log(window_top);
                if (screen > 1200) {
                    if (window_top > 30) {

                        $('.header-menu').addClass('fixo');
                        $(".header-wrapper").addClass('margin_bottom_450');

                    } else {
                        $(".col-header").removeClass("menu_fixed ");
                        $('.header-menu').removeClass('fixo');
                        $(".header-wrapper").removeClass('margin_bottom_450');
                    }
                }

                if (screen <= 1200) {
//                    console.log("subindo", window_top);

                    if (window_top > 30) {

                        $('.header-menu').addClass("fixo");
                    } else {
                        $(".mobile-header").removeClass("menu_fixed");
                        $('.header-menu').removeClass("fixo ");
                        ;
                    }
                }

            }
            lastScrollTop = nowScrollTop;
        }
    });



//    $(window).resize(function () {
//        var screen = $(window).width();
//        var window_top = $(window).scrollTop() + 1;
//        var maquina = "desktop";
//        if (screen <= 768) {
//            maquina = "mobile";
//        } else if (screen < 994) {
//            maquina = "tablet";
//        } else {
//            maquina = "desktop";
//        }
//        console.log(screen, window_top)
//        if (maquina != "desktop") {
//            if (window_top > 150) {
//                $(".mobile-header").addClass("menu_fixed  animated fadeInDown");
//            } else {
//                $(".mobile-header").removeClass("menu_fixed animated fadeInDown");
//            }
//        }
//    });
//    $('window').on('load',function(){
    $('.list-product').removeClass('produto-load');
//    })
    /*---------------------------
     Menu menu-content
     ------------------------------ */

//    $(".header-menu-vertical .menu-title").on("click", function (event) {
//        $(".header-menu-vertical .menu-content").fa(500);
//        console.log('click')
//    });


    $(".menu-content").each(function () {
        var $ul = $(this),
                $lis = $ul.find(".menu-item:gt(7)"),
                isExpanded = $ul.hasClass("expanded");
        $lis[isExpanded ? "show" : "hide"]();

        if ($lis.length > 0) {
            $ul.append(
                    $('<li class="expand">' + (isExpanded ? '<a href="javascript:;"><span><i class="ion-android-remove"></i>Fechar Departamentos</span></a>' : '<a href="javascript:;"><span><i class="ion-android-add"></i>Mais Departamentos</span></a>') +
                            "</li>").click(function (event) {
                var isExpanded = $ul.hasClass("expanded");
                event.preventDefault();
                $(this).html(isExpanded ? '<a href="javascript:;"><span><i class="ion-android-add"></i>Fechar Departamentos</span></a>' : '<a href="javascript:;"><span><i class="ion-android-remove"></i>Fechar Departamentos</span></a>');
                $ul.toggleClass("expanded");
                $lis.toggle(300);
            })
                    );
        }
    });

    /*---------------------------------
     Off Canvas Function
     -----------------------------------*/
    (function () {
        var $offCanvasToggle = $(".offcanvas-toggle"),
                $offCanvas = $(".offcanvas"),
                $offCanvasOverlay = $(".offcanvas-overlay"),
                $mobileMenuToggle = $(".mobile-menu-toggle");
        $offCanvasToggle.on("click", function (e) {
            e.preventDefault();
            var $this = $(this),
                    $target = $this.attr("href");
            $body.addClass("offcanvas-open");
            $($target).addClass("offcanvas-open");
            $offCanvasOverlay.fadeIn();
            if ($this.parent().hasClass("mobile-menu-toggle")) {
                $this.addClass("close");
            }
        });
        $(".offcanvas-close, .offcanvas-overlay").on("click", function (e) {
            e.preventDefault();
            $body.removeClass("offcanvas-open");
            $offCanvas.removeClass("offcanvas-open");
            $offCanvasOverlay.fadeOut();
            $mobileMenuToggle.find("a").removeClass("close");
        });
    })();

    /*----------------------------------
     Off Canvas Menu
     -----------------------------------*/
    function mobileOffCanvasMenu() {
        var $offCanvasNav = $(".offcanvas-menu, .overlay-menu"),
                $offCanvasNavSubMenu = $offCanvasNav.find(".sub-menu");

        /*Add Toggle Button With Off Canvas Sub Menu*/
        $offCanvasNavSubMenu.parent().prepend('<span class="menu-expand"></span>');

        /*Category Sub Menu Toggle*/
        $offCanvasNav.on("click", "li a, .menu-expand", function (e) {
            var $this = $(this);
            if ($this.attr("href") === "#" || $this.hasClass("menu-expand")) {
                e.preventDefault();
                if ($this.siblings("ul:visible").length) {
                    $this.parent("li").removeClass("active");
                    $this.siblings("ul").slideUp();
                    $this.parent("li").find("li").removeClass("active");
                    $this.parent("li").find("ul:visible").slideUp();
                } else {
                    $this.parent("li").addClass("active");
                    $this.closest("li").siblings("li").removeClass("active").find("li").removeClass("active");
                    $this.closest("li").siblings("li").find("ul:visible").slideUp();
                    $this.siblings("ul").slideDown();
                }
            }
        });
    }
    mobileOffCanvasMenu();

    /*------------------------------
     Hero Slider
     -----------------------------------*/

    var swiper = new Swiper(".hero-slider", {
        spaceBetween: 30,
        speed: 750,
        effect: "fade",
        loop: true,
        autoplay: {
            delay: 7000,
            disableOnInteraction: false,
        },
        pagination: {
            el: ".swiper-pagination",
            clickable: true,
        },
    });

    /*------------------------------
     Category Slider
     -----------------------------------*/
    var swiper = new Swiper(".category-slider", {
        slidesPerView: 5,
        spaceBetween: 30,
        speed: 750,
        navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
        },
        breakpoints: {
            0: {
                slidesPerView: 1,
            },
            478: {
                slidesPerView: 2,
            },
            576: {
                slidesPerView: 2,
            },
            768: {
                slidesPerView: 3,
            },
            1024: {
                slidesPerView: 4,
            },
            1200: {
                slidesPerView: 5,
            },
        },
    });

    /*------------------------------
     Feature Slider
     -----------------------------------*/

    var swiper = new Swiper(".feature-slider", {
        slidesPerView: 5,
        spaceBetween: 14,
        navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
        },
        breakpoints: {
            0: {
                slidesPerView: 1,
            },
            478: {
                slidesPerView: 2,
            },
            576: {
                slidesPerView: 2,
            },
            768: {
                slidesPerView: 3,
            },
            1024: {
                slidesPerView: 4,
            },
            1200: {
                slidesPerView: 5,
            },
        },
    });

    /*------------------------------
     Feature Slider Two
     -----------------------------------*/

    var swiper = new Swiper(".feature-slider-two", {
        slidesPerView: 5,
        spaceBetween: 30,
        navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
        },
        breakpoints: {
            0: {
                slidesPerView: 2,
            },
            478: {
                slidesPerView: 2,
            },
            576: {
                slidesPerView: 2,
            },
            768: {
                slidesPerView: 3,
            },
            1024: {
                slidesPerView: 4,
            },
            1200: {
                slidesPerView: 5,
            },
            1300: {
                slidesPerView: 5,
            },
        },
    });

    /*------------------------------
     Hot Deal Slider
     -----------------------------------*/
    var swiper = new Swiper(".deal-slider", {
        slidesPerView: 2,
        spaceBetween: 30,
        speed: 750,
        navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
        },
        breakpoints: {
            0: {
                slidesPerView: 1,
            },
            1024: {
                slidesPerView: 1,
            },
            1200: {
                slidesPerView: 2,
            },
        },
    });

    /*------------------------------
     Feature-2 Slider
     -----------------------------------*/

    var swiper = new Swiper(".feature-slider-2", {
        slidesPerView: 4,
        spaceBetween: 30,
        navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
        },
        breakpoints: {
            0: {
                slidesPerView: 1,
            },
            478: {
                slidesPerView: 2,
            },
            576: {
                slidesPerView: 2,
            },
            768: {
                slidesPerView: 2,
            },
            1024: {
                slidesPerView: 2,
            },
            1200: {
                slidesPerView: 4,
            },
        },
    });

    /*------------------------------
     Brand Slider
     -----------------------------------*/

    var swiper = new Swiper(".brand-sliders", {
        slidesPerView: 6,
        spaceBetween: 30,
        speed: 750,
        navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
        },
        breakpoints: {
            0: {
                slidesPerView: 2,
            },
            478: {
                slidesPerView: 2,
            },
            576: {
                slidesPerView: 3,
            },
            768: {
                slidesPerView: 4,
            },
            1024: {
                slidesPerView: 5,
            },
            1200: {
                slidesPerView: 6,
            },
        },
    });
    /*------------------------------
     Departamento Slider
     -----------------------------------*/

    var swiper = new Swiper(".departamento-slider", {
//        slidesPerView: 5,
        spaceBetween: 30,
         centeredSlides: true,
         loop: true,
        speed: 750,
        navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
        },
        breakpoints: {
            0: {
                slidesPerView: 3,
            },
            478: {
                slidesPerView: 3,
            },
            576: {
                slidesPerView: 4,
            },
            768: {
                slidesPerView: 5,
            },
            1024: {
                slidesPerView: 6,
            },
            1200: {
                slidesPerView: 7,
            },
        },
    });

    /*------------------------------
     Recent Slider
     -----------------------------------*/

    var swiper = new Swiper(".recent-slider", {
        slidesPerView: 4,
        spaceBetween: 30,
        speed: 750,
        navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
        },
        breakpoints: {
            0: {
                slidesPerView: 1,
            },
            478: {
                slidesPerView: 1,
            },
            576: {
                slidesPerView: 1,
            },
            768: {
                slidesPerView: 2,
            },
            1024: {
                slidesPerView: 3,
            },
            1200: {
                slidesPerView: 4,
            },
        },
    });

    /*------------------------------
     Recent Slider Two
     -----------------------------------*/
    var swiper = new Swiper(".recent-slider-two", {
        slidesPerView: 3,
        spaceBetween: 0,
        navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
        },
        breakpoints: {
            0: {
                slidesPerView: 1,
            },
            478: {
                slidesPerView: 1,
            },
            576: {
                slidesPerView: 1,
            },
            768: {
                slidesPerView: 1,
            },
            992: {
                slidesPerView: 2,
            },
            1024: {
                slidesPerView: 2,
            },
            1200: {
                slidesPerView: 3,
            },
        },
    });

    /*------------------------------
     Single Product Slider
     -----------------------------------*/
    var swiper = new Swiper(".single-product-slider", {
        slidesPerView: 4,
        spaceBetween: 20,
        navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
        },
        breakpoints: {
            0: {
                slidesPerView: 1,
            },
            478: {
                slidesPerView: 1,
            },
            576: {
                slidesPerView: 2,
            },
            768: {
                slidesPerView: 3,
            },
            992: {
                slidesPerView: 3,
            },
            1024: {
                slidesPerView: 4,
            },
            1200: {
                slidesPerView: 4,
            },
        },
    });

    /*----------------------------  
     All Category toggle  
     ------------------------------*/
    $(".filter-close").click(function () {
        $(".shop-sidebar-wrap").removeClass("fixed");
    });
    $(".header-open").click(function () {
        $(".shop-sidebar-wrap").addClass("fixed");
    });

    $(".category-toggle").click(function () {
        $(".category-menu").slideToggle("show");
    });
    $(".category-menu li.menu-item-has-children").each(function () {
        var id = $(this).data("id");
        $(".menu-item-has-children-" + id).click(function () {
            $(".category-mega-menu-" + id).slideToggle("slow");
        });

    });

    /*-----------------------------  
     Category more toggle  
     -------------------------------*/

    $(".category-menu li.hidden").hide();
    $("#more-btn").on("click", function (e) {
        e.preventDefault();
        $(".category-menu li.hidden").toggle(500);
        var htmlAfter = '<i class="ion-ios-minus-empty" aria-hidden="true"></i> Less Categories';
        var htmlBefore = '<i class="ion-ios-plus-empty" aria-hidden="true"></i> More Categories';

        if ($(this).html() == htmlBefore) {
            $(this).html(htmlAfter);
        } else {
            $(this).html(htmlBefore);
        }
    });

    /*---------------------
     Countdown
     --------------------- */
    $("[data-countdown]").each(function () {
        var $this = $(this);
        var finalDate = $(this).data("countdown");
        $this.countdown(finalDate, function (event) {
            $this.html(event.strftime('<span class="cdown day">%-D <p>Dias</p></span> <span class="cdown hour">%-H <p>Horas</p></span> <span class="cdown minutes">%M <p>Min</p></span> <span class="cdown second">%S <p>Seg</p></span>'));
        });
    });

    /*---------------------
     Scroll Up
     --------------------- */
//    $.scrollUp({
//        scrollText: '<i class="ion-android-arrow-up"></i>',
//        easingType: "linear",
//        scrollSpeed: 900,
//        animation: "fade",
//    });

    /*----------------------------
     Cart Plus Minus Button
     ------------------------------ */
    var CartPlusMinus = $(".cart-plus-minus");
    CartPlusMinus.prepend('<div class="dec qtybutton">-</div>');
    CartPlusMinus.append('<div class="inc qtybutton">+</div>');
    var boxmessage = $('.message-quant');
    $(".qtybutton").on("click", function () {
        boxmessage.html('');
        var $button = $(this);
        var parente = $button.parent().find("input");
        var oldValue = parente.val();
        var maximo = parente.data('max');
        var minimo = parente.data('min');
        console.log(maximo, minimo);
        if ($button.text() === "+") {
            var newVal = parseFloat(oldValue) + 1;
        } else {
            // Don't allow decrementing below zero
            if (oldValue > 1) {
                var newVal = parseFloat(oldValue) - 1;
            } else {
                newVal = 1;
            }
        }
        if (maximo < newVal) {
            boxmessage.html('<span class="text-primary">Quandidade máxima de compra é ' + maximo + '</span>');
            newVal = maximo;
        }
        if (minimo > newVal) {
            boxmessage.html('<span class="text-primary">Quandidade mínima de compra é ' + minimo + '</span>');
            newVal = minimo;
        }

        $button.parent().find("input").val(newVal);
    });



    /*--------------------------
     Product Zoom
     ---------------------------- */

    

//$(".zoompro").elevateZoom()
//    $('.zoompro')
//    .wrap('<span style="display:inline-block"></span>')
//    .css('display', 'block')
//    .parent()
//    .zoom();

//    $(".zoompro").elevateZoom({
//        gallery: "gallery",
//        galleryActiveClass: "active",
//        zoomWindowWidth: 300,
//        zoomWindowHeight: 100,
//        scrollZoom: false,
//        zoomType: "inner",
//        cursor: "crosshair",
//        easing: true,
//        responsive: true,
//    });



//    $(".galeria-produto").on('click', 'a', function () {
//        var image = $(this).data('image');
//        var image_zoom = $(this).data('zoom-image');
//        $('.zoompro').attr('src', image_zoom);
//        $('.zoompro').attr('data-zoom-image', image);
//        $('.zoompro-2').trigger('zoom.destroy');
//        $('.zoompro-2').zoom();
//
//    })


    /*------------------------------
     Single Product Slider
     -----------------------------------*/
    var swiper = new Swiper(".product-dec-slider-2", {
        slidesPerView: 4,
        spaceBetween: 10,
        breakpoints: {
            0: {
                slidesPerView: 4,
            },
            478: {
                slidesPerView: 4,
            },
            576: {
                slidesPerView: 4,
            },
            768: {
                slidesPerView: 4,
            },
            992: {
                slidesPerView: 4,
            },
            1024: {
                slidesPerView: 4,
            },
            1200: {
                slidesPerView: 4,
            },
        },
    });

    /*------------------------------
     Single Product Slider
     -----------------------------------*/
    var swiper = new Swiper(".product-dec-slider-3", {
        slidesPerView: 4,
        spaceBetween: 10,
        direction: 'vertical',
        breakpoints: {
            0: {
                slidesPerView: 4,
            },
            478: {
                slidesPerView: 4,
            },
            576: {
                slidesPerView: 4,
            },
            768: {
                slidesPerView: 4,
            },
            992: {
                slidesPerView: 4,
            },
            1024: {
                slidesPerView: 4,
            },
            1200: {
                slidesPerView: 4,
            },
        },
    });



    /*-----------------------------
     Blog Gallery Slider 
     -------------------------------- */
    var swiper = new Swiper(".blog-gallery", {
        slidesPerView: 1,
        spaceBetween: 0,
        navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
        },
    });

    /*-------------------------------
     Create an account toggle
     ---------------------------------*/
    $(".checkout-toggle2").on("click", function () {
        $(".open-toggle2").slideToggle(1000);
    });

    $(".checkout-toggle").on("click", function () {
        $(".open-toggle").slideToggle(1000);
    });

    /*---------------------------
     Quick view Slider 
     ------------------------------ */
    var galleryThumbs = new Swiper('.gallery-thumbs', {
        spaceBetween: 10,
        slidesPerView: 4,
        freeMode: true,
        watchSlidesVisibility: true,
        watchSlidesProgress: true,
    });
    var galleryTop = new Swiper('.gallery-top', {
        spaceBetween: 0,
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
        thumbs: {
            swiper: galleryThumbs
        }
    });




//    window.onload(function () {
//        var dados_config = $('#camera_wrap_3').data('config');
//        
//        $('#camera_wrap_3').camera(dados_config); //the basic method
//    });

});
