﻿var maxHeight = function (elems) {
    return Math.max.apply(null, elems.map(function () {
        return $(this).height();
    }).get());
}

$(".menu-link").click(function () {
    openCloseMenu(false);
});


//close menu on click anywhere other than menu
$("#content").click(function () {
    openCloseMenu(true);
});


function openCloseMenu(closeOnly) {

    var width = $(window).width();
    var image = $("#menu-img");
    var imgPath = image.attr('src');

    if (closeOnly === true) {
        $("#mainmenu").hide();
        imgPath = "/Files/Templates/Designs/Witt/assets/images/menu-icon.svg";
    }
    else {
        $("#mainmenu").animate({
            height: 'toggle'
        }, 300, 'linear');

        if (imgPath === "/Files/Templates/Designs/Witt/assets/images/cross-icon.svg") {
            imgPath = "/Files/Templates/Designs/Witt/assets/images/menu-icon.svg";
            $("body").removeClass("removeScrollBar");
            // $("#mainmenu").removeClass("mobile-mega-menu");
        }
        else {
            imgPath = "/Files/Templates/Designs/Witt/assets/images/cross-icon.svg";

            if (width < 991) {
                $("body").addClass("removeScrollBar");
                $("#mainmenu").addClass("mobile-mega-menu");
                $("li.active").removeClass("active");
            }
            else {
                $("#mainmenu").removeClass("mobile-mega-menu");
            }
        }

        if (!$("#megamenu-content").is(":visible")) {
            $(".menu-col").addClass("bottom-border-dark");

            var firstItem = $("ul.leftnav li:first-child a");
            var id = firstItem.attr('id').trim();
            if (width > 991) {
                ToggleMenu(id);
            }
        }
    }
    image.attr('src', imgPath);
}

$(".menu-item").hover(function () {
    var fullid = this.id;
    var id = fullid.replace("megamenu-item-", "");
    $("div[id*='cta-']").hide();
    $("#cta-" + id).show();
});

$(".brand-item").hover(function () {
    var fullid = this.id;
    var id = fullid.replace("brand-item-", "");
    $("div[id*='cta-brand-']").hide();
    $("#cta-brand-" + id).show();
});



$(".sidemenu-btn").click(function () {
    var id = $(this).attr('id').trim();

    var width = $(window).width();

    if (width < 991) {
        $("#megamenu-side").hide();
    }
    else {
        $("#megamenu-side").show();
    }

    ToggleMenu(id);
});

$(".img-menu-heading").click(function () {
    var id = $(this).attr('id').trim();
    ToggleMenu(id);
    $("#megamenu-side").show();
    $("#megamenu-content").hide();
});


function ShowClass(myClass) {
    $("." + myClass).show();
}

function HideClass(myClass) {
    $("." + myClass).hide();
}

function ToggleMenu(activeId) {

    $("#megamenu-content").show();
    $(".menu-item").hide();

    $(".leftnav .active").removeClass("active");

    var parentid = activeId.replace("megamenu-item-", "");

    $("#cta-" + parentid).show();

    $(".menu-items-" + parentid).animate({
        width: 'toggle'
    }, 100, 'linear');

    $("#" + activeId).closest("li").addClass("active");

    $("div[id*='cta-']").hide();
    $("#cta-" + parentid).show();


    $(".img-menu-heading").hide();
    $("#heading-" + parentid).show();

    $("#mainmenu").addClass("bottom-border-dark");
    $(".menu-col").removeClass("bottom-border-dark");
}


function openCloseSubmenu(closeOnly) {
    var image = $("#submenu-btn-img");
    var imgPath = image.attr('src');


    if (closeOnly) {
        $("#mobile-sub-nav").hide();
    }
    else {
        $("#mobile-sub-nav").slideToggle();
    }

    //$("#stickymenu").addClass("sticky");
    //$("#mobile-sub-nav").addClass("fixed-mobile-menu");

    if (imgPath === "/Files/Templates/Designs/Witt/assets/images/cross-icon.svg") {
        imgPath = "/Files/Templates/Designs/Witt/assets/images/menu-icon.svg";
    }
    else {
        imgPath = "/Files/Templates/Designs/Witt/assets/images/cross-icon.svg";
    }

    image.attr('src', imgPath);
}

$(".submenu-btn").click(function () {
    openCloseSubmenu(false);
});

$("#page-content").click(function () {
    openCloseSubmenu(true);
});


//count-animation - numbers count up.
$(window).scroll(function () {

    //openCloseMenu(true);
    $('.counter-animation').each(function () {

        var oTop = $(this).offset().top - window.innerHeight;

        if ($(window).scrollTop() > oTop) {
            var $this = $(this),
                countTo = $this.attr('data-count');
            $({
                countNum: $this.text()
            }).animate({
                countNum: countTo
            },
                {
                    duration: 2500,
                    easing: 'swing',
                    step: function () {
                        $this.text(Math.floor(this.countNum));
                    },
                    complete: function () {
                        $this.text(this.countNum);
                    }
                });
        }

    });

    $(".btn-totop").each(function () {
        if ($('.teaserList').length) {
            var btnid = $(this).attr("id");
            var id = btnid.replace("btn-totop-", "");
            var paragraphid = "paragraph_" + id;

            var scrolltoParagraphId = $(this).attr("data-link");

            var scrollToParagraph = $(scrolltoParagraphId);

            if (scrollToParagraph !== null) {

                var paragraphPosition = $("#" + paragraphid).offset().top;

                if ($(window).scrollTop() > paragraphPosition) {
                    $("#" + btnid).show();

                }
                else {
                    $("#" + btnid).hide();
                }
            }
        }
    });

});

function goToByScroll(id) {
    // Scroll

    var stickyMenu = $("#stickymenu");
    var header = $("#header");

    var stickymenuHeight = 120;

    if (stickyMenu.length > 0) {
        stickymenuHeight = stickyMenu.height() + header.height();
    }
    else {
        stickymenuHeight = header.height();
    }

    $('html,body').animate({
        scrollTop: $(id).offset().top - stickymenuHeight
    }, 'slow');
}


// WittProductList 
$(".scrollTo-btn").click(function (e) {

    var link = $(this).attr("data-link");

    if (!link.includes("#")) {
        link = "#" + link;
    }

    if ($(link).length) {
        e.preventDefault();
        goToByScroll(link);
    }
});

$(".select-variants").on('change', function () {
    var id = $(this).attr('id');
    var productId = id.substring(id.lastIndexOf('-') + 1);

    var selectedValues;

    var slider = $("#productgallery-" + productId);
    var sliderNav = $("#productgallery-nav-" + productId);

    $('#variants-' + productId + ' select').each(function (index, value) {
        if (index === 0) {
            selectedValues = this.value;
        }
        else {
            selectedValues += "." + this.value;
        }

    });

    $("#productgallery-nav-" + productId + " .slick-col-container").each(function () {
        var variant = $(this).data("variant");
        if (variant !== 'all') {
            if (variant === selectedValues) {
                $(this).addClass("slide-visible");
                $(this).show();
            }
            else {
                $(this).removeClass("slide-visible");
                $(this).hide();
            }
        }
    });

    $("#productgallery-" + productId + " .slider-container").each(function () {
        var variant = $(this).data("variant");
        if (variant !== 'all') {
            if (variant === selectedValues) {
                $(this).addClass("slide-visible");
                $(this).show();
            }
            else {
                $(this).removeClass("slide-visible");
                $(this).hide();
            }
        }
    });

    slider.slick('unslick');
    sliderNav.slick('unslick');

    var sliderCounter = $('#productgallery-nav-' + productId + ' .slide-visible').length;

    slider.slick({
        slidesToShow: 1,
        slidesToScroll: 1,
        arrows: false,
        fade: true,
        slide: '.slide-visible'
    });

    sliderNav.slick({
        slidesToShow: sliderCounter,
        slidesToScroll: sliderCounter,
        dots: false,
        arrows: false,
        centerMode: false,
        focusOnSelect: true,
        slide: '.slide-visible',
        asNavFor: "#productgallery-" + productId
    });

    $("#productgallery-nav-" + productId + " .slick-col-container").each(function () {
        var variant = $(this).data("variant");
        if (variant !== 'all') {
            if (variant === selectedValues) {
                $(this).show();
            }
            else {
                $(this).hide();
            }
        }
    });

    $("#productgallery-" + productId + " .slider-container").each(function () {
        var variant = $(this).data("variant");
        if (variant !== 'all') {
            if (variant === selectedValues) {
                $(this).show();
            }
            else {
                $(this).hide();
            }
        }
    });
});

$(".modal-image").on('click', function () {

    var img = $(this).attr('data-img');

    if (typeof img === 'undefined' || img === null) {
        img = $(this).attr('src');
    }

    $("#show-img").attr('src', img);
    $("#imgmodal").modal('show');
});


$(".play-button").click(function (e) {

    var jVideoD = $(this).parent(".flex-container").find(".videoWrapper").find(".video-desktop");
    var jVideoM = $(this).parent(".flex-container").find(".videoWrapper").find(".video-mobile");

    var video;

    if (jVideoD.is(":visible")) {
        video = document.getElementById(jVideoD.attr("id"));
    }
    else {
        video = document.getElementById(jVideoM.attr("id"));
    }

    if (video.paused) {
        video.play();
        video.controls = true;
        $(this).hide();
    }
    else {
        video.pause();
    }
});

$("video").click(function () {
    if (this.paused) {
        this.play();
        this.controls = true;

        var playButton = $(this).closest(".videoWrapper").prev(".play-button-desktop");
        playButton.hide();
    }
    else {
        this.pause();
    }
});



//$('.collapse-btn').on('click', function () {
//    var $this = $(this);

//    if ($this.next().hasClass('current')) {
//        $this.next().removeClass('current');
//        $this.next().slideUp();
//    } else {
//        $this.parent().parent().find('li .inner').removeClass('current');
//        $this.parent().parent().find('li .inner').hide();
//        $this.next().toggleClass('current');
//        $this.next().slideToggle();
//    }
//});


function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

$(".category-menu-button").on('click', function () {
    var accordion = $(this).next(".category-menu-accordion");

    $(".category-menu-accordion").slideUp();
    if (!accordion.is(":visible")) {
        accordion.slideDown();
    }

    var hiddenMenuHeight = $("#hiddenMenuHeight").val();

    if (hiddenMenuHeight < 550) {
        hiddenMenuHeight = 550;
    }

    var minHeight = hiddenMenuHeight;
    var height = accordion.children().length * 65;

    if (height > minHeight) {
        var sideMenu = $("#side-menu-ecom");

        $("#hiddenMenuHeight").val(height);
        sideMenu.css("max-height", height);

        if (!sideMenu.hasClass("fixed")) {
            $('#topbanner').css("height", height);
        }
    }
});



//Accordion Hide/Show icon change
$('.collapse').on('hide.bs.collapse', function () {
    var iconContainer = $(this).prev(".accordion-link").find(".accordion-header-icon");

    if (iconContainer !== null) {
        iconContainer.html("<i class='fas fa-plus'></i>");
    }
});

$('.collapse').on('show.bs.collapse', function () {
    var iconContainer = $(this).prev(".accordion-link").find(".accordion-header-icon");

    if (iconContainer !== null) {
        iconContainer.html("<i class='fas fa-minus'></i>");
    }
});


$('.hastooltip').tooltip({
    placement: 'bottom'
});


$(".modalBtn").on("click", function () {
    var imgSrc = $(this).data("img");
    var size = $(this).data("class");

    $('#modalImgDialog').addClass(size);
    $('#modalImage').attr('src', imgSrc); // here asign the image to the modal when the user click the enlarge link
    $('#myModalImg').modal('show'); // imagemodal is the id attribute assigned to the bootstrap modal, then i use the show function
});




//Filter divs
$(function () {
    $(".filter-button").click(function () {

        var btns = $(this).parent().children('.filter-button');
        btns.removeClass("active");
        $(this).addClass("active");

        var value = $(this).attr('data-filter');
        if (value == "all") {
            $('.filter').show('1000');
        }
        else {
            $(".filter").not('.' + value).hide('2000');
            $('.filter').filter('.' + value).show('2000');

        }
    });
});



//LoginBox
$(document).on("click", function () {
    $("#PageProfileLayer").hide();
});

$("#PageProfileLayer").on("click", function (e) {
    e.stopPropagation();
});

$("#btn-login").on("click", function (e) {
    e.stopPropagation();
    $("#PageProfileLayer").slideDown();
});
