window.onload = function () {
    document.querySelectorAll('#sideNav .nav-link')[0].classList.add('active');
    fixedTopMenu();
    miniMenuPosition();
    var elementExists = document.querySelector('#navbar-scrollspy-proevent > a.active');
    if (elementExists != null) {
        document.querySelector('#navbar-scrollspy-proevent > a.active').classList.remove('active');
    }
    parent = document.querySelector('.content-holder');
    var bannerContent = document.querySelectorAll('.content-holder>.banner-content');
    var activeContent = bannerContent[0];
    var activeMenuKey = 0;
    for (var i = 0; i < bannerContent.length; i++) {
        if ((Math.abs(bannerContent[i].getBoundingClientRect().top)) < (Math.abs(Math.abs(activeContent.getBoundingClientRect().top)))) {
            activeContent = bannerContent[i];
            activeMenuKey = i;
        }
        if (document.querySelector('#navbar-scrollspy-proevent > a.active') != undefined) {
            document.querySelector('#navbar-scrollspy-proevent > a.active').classList.remove('active');
        }
        document.querySelector('#navbar-scrollspy-proevent').getElementsByTagName('a')[activeMenuKey].classList.add('active');
    }
}

// Minimenu
if (window.location.pathname === '/' || window.location.pathname.includes('teamevents.html') || window.location.pathname.includes('themenwelten.html')) {
    window.addEventListener('scroll', function () {
        miniMenuPosition();
    })
}

function miniMenuPosition() {
    if (window.location.pathname == '/' || window.location.href.indexOf("themenwelten") > -1) {
        x = window.scrollY;
        if (x < document.querySelector('.content-holder').offsetTop || x == 0) {
            document.querySelectorAll('#navbar-scrollspy-proevent a')[0].classList.add('active');
        }
        headerHeight = document.querySelector('header').offsetHeight;
        if (window.location.href.indexOf("themenwelten") > -1) {
            var contentHolder = document.querySelector('.compact-menu');
        } else {
            var contentHolder = document.querySelector('.content-holder');
        }
        var contentHolderTop = contentHolder.getBoundingClientRect().top
        var minimenu = document.querySelector('.mini-menu');
        if (minimenu) {
            if (x < headerHeight) {
                minimenu.style.top = contentHolderTop + window.scrollY + 'px';
                minimenu.style.position = 'absolute';
            } else if (225 > x > headerHeight) {
                minimenu.style.top = 265 - x + 'px';
                minimenu.style.position = 'fixed';
                if (window.width < 992) {
                    minimenu.style.top = 210 - x + 'px';
                }
            } else {
                minimenu.style.top = 50 + 'px';
                minimenu.style.position = 'fixed';
            }
        }
        var footerTop = document.querySelector('.footer').getBoundingClientRect().top;
        if (window.location.pathname === '/') {
            if (footerTop <= minimenu.offsetHeight + Number((minimenu.style.top).slice(0, -2))) {
                minimenu.style.top = footerTop - 740 + 'px';
                if (window.width < 992) {
                    minimenu.style.top = footerTop - 790 + 'px';
                }
            }
        } else if (window.location.pathname.includes('teamevents')) {
            if (footerTop <= minimenu.offsetHeight + Number((minimenu.style.top).slice(0, -2))) {
                minimenu.style.top = footerTop - 340 + 'px';
                if (window.width < 992) {
                    minimenu.style.top = footerTop - 390 + 'px';
                }
            }
        } else if (window.location.pathname.includes('themenwelten')) {
            if (footerTop <= minimenu.offsetHeight + Number((minimenu.style.top).slice(0, -2))) {
                minimenu.style.top = footerTop - 400 + 'px';
                if (window.width < 992) {
                    minimenu.style.top = footerTop - 450 + 'px';
                }
            }
        }
    }
}

// kontaktformular
function privatRuf() {
    document.getElementById("firmenIdRuf").children[1].children[0].disabled = true;
    document.getElementById("privatIdRuf").children[1].children[0].disabled = false;
    document.getElementById("privatIdRuf").children[2].children[0].disabled = false;
}

function firmenRuf() {
    document.getElementById("firmenIdRuf").children[1].children[0].disabled = false;
    document.getElementById("privatIdRuf").children[1].children[0].disabled = true;
    document.getElementById("privatIdRuf").children[2].children[0].disabled = true;
}

function privatMail() {
    document.getElementById("firmenIdMail").children[1].children[0].disabled = true;
    document.getElementById("privatIdMail").children[1].children[0].disabled = false;
    document.getElementById("privatIdMail").children[2].children[0].disabled = false;
}

function firmenMail() {
    document.getElementById("firmenIdMail").children[1].children[0].disabled = false;
    document.getElementById("privatIdMail").children[1].children[0].disabled = true;
    document.getElementById("privatIdMail").children[2].children[0].disabled = true;
}

// Unterkategorie

var buttons = document.getElementsByClassName('unterkat');
for (let i = 0; i < buttons.length; i++) {
    buttons[i].addEventListener('mouseenter', function () {
        this.classList.add('shadow')
    })
    buttons[i].addEventListener('mouseleave', function () {
        this.classList.remove('shadow')
    })
}

// Dienstleistungen

var buttons = document.getElementsByClassName('dinst-hover');
for (let i = 0; i < buttons.length; i++) {
    buttons[i].addEventListener('mouseenter', function () {
        this.classList.add('shadow')
    })
    buttons[i].addEventListener('mouseleave', function () {
        this.classList.remove('shadow')
    })
}

// anzahl
if (document.querySelector('.zahl')) {
    document.querySelector('.zahl').addEventListener('click', function () {
        var anzahl = this.options[this.selectedIndex].value;
        this.nextElementSibling.childNodes[0].setAttribute("data-anzahl", anzahl);
    });
}

// Indikator
const parentObject = document.getElementsByClassName('indicator');

[...parentObject].forEach((parent, i) => {
    if (parent.classList.contains('good')) {
        const childElement = document.createElement('i');
        childElement.className = 'ci-check-circle text-success';
        parent.appendChild(childElement)

    } else if (parent.classList.contains('bad')) {
        const childElement = document.createElement('i');
        childElement.className = 'ci-close-circle text-danger';
        parent.appendChild(childElement)
    } else {
        const childElement = document.createElement('i');
        parent.appendChild(childElement)
    }
});

function indi() {
    [...parentObject].forEach((parent, i) => {
        if (parent.classList.contains('good')) {
            parent.childNodes[0].classList.remove('ci-close-circle');
            parent.childNodes[0].classList.remove('text-danger');
            parent.childNodes[0].classList.add('ci-check-circle');
            parent.childNodes[0].classList.add('text-success');
        } else if (parent.classList.contains('bad')) {
            parent.childNodes[0].classList.remove('ci-check-circle');
            parent.childNodes[0].classList.remove('text-success');
            parent.childNodes[0].classList.add('ci-close-circle');
            parent.childNodes[0].classList.add('text-danger');
        }
    });
}

// mietzeitraum_hidden
if (window.location.href.match(/(neuer-auftrag|warenkorb)/)) {
    var mietzeitraum = document.querySelector('#mietzeitraum_hidden').getAttribute('value');
    var optionValue = document.getElementById('mietzeitraum_hidden').options;
    for (var i = 0; i < optionValue.length; i++) {
        if (optionValue[i].value == mietzeitraum) {
            optionValue[i].selected = 'selected'
        }
    }
}

//Product Caousel
var s = document.querySelectorAll(".product-gallery");
if (s.length) for (var e = 0; e < s.length; e++) !function (r) {
    for (var a = s[r].querySelectorAll(".product-gallery-thumblist-item:not(.video-item)"),
             n = s[r].querySelectorAll(".product-gallery-preview-item"),
             e = s[r].querySelectorAll(".product-gallery-thumblist-item.video-item"),
             t = 0; t < a.length; t++) a[t].addEventListener("click", o);

    function o(e) {
        e.preventDefault();
        for (var t = 0; t < a.length; t++) {
            if (typeof n[t] != 'undefined') {
                n[t].classList.remove("active");
                a[t].classList.remove("active")
            }
        }

        this.classList.add("active"), s[r].querySelector(this.getAttribute("href")).classList.add("active")
    }

    for (var l = 0; l < e.length; l++) lightGallery(e[l], {
        selector: "this",
        download: !1,
        videojs: !0,
        youtubePlayerParams: {modestbranding: 1, showinfo: 0, rel: 0, controls: 0},
        vimeoPlayerParams: {byline: 0, portrait: 0, color: "fe696a"}
    })
}(e)
var thumbnails = document.querySelectorAll('.product-gallery .product-gallery-thumblist-item');
for (var i = 0; i < thumbnails.length; i++) {
    thumbnails[i].addEventListener('click', function () {
        var siblings = this.parentNode.childNodes;
        for (var j = 0; j < siblings.length; j++) {
            siblings[j].classList.remove('active')
        }
        this.classList.add('active');
    })
}
// lazy loading
document.addEventListener("DOMContentLoaded", function () {
    var lazyloadImages;
    if ("IntersectionObserver" in window) {
        lazyloadImages = document.querySelectorAll(".lazy");
        var imageObserver = new IntersectionObserver(function (entries, observer) {
            entries.forEach(function (entry) {
                if (entry.isIntersecting) {
                    var image = entry.target;
                    image.src = image.dataset.src;
                    image.classList.remove("lazy");
                    imageObserver.unobserve(image);
                }
            });
        });

        lazyloadImages.forEach(function (image) {
            imageObserver.observe(image);
        });
    } else {
        var lazyloadThrottleTimeout;
        lazyloadImages = document.querySelectorAll(".lazy");

        function lazyload() {
            if (lazyloadThrottleTimeout) {
                clearTimeout(lazyloadThrottleTimeout);
            }

            lazyloadThrottleTimeout = setTimeout(function () {
                var scrollTop = window.pageYOffset;
                lazyloadImages.forEach(function (img) {
                    if (img.offsetTop < (window.innerHeight + scrollTop)) {
                        img.src = img.dataset.src;
                        img.classList.remove('lazy');
                    }
                });
                if (lazyloadImages.length == 0) {
                    document.removeEventListener("scroll", lazyload);
                    window.removeEventListener("resize", lazyload);
                    window.removeEventListener("orientationChange", lazyload);
                }
            }, 20);
        }

        document.addEventListener("scroll", lazyload);
        window.addEventListener("resize", lazyload);
        window.addEventListener("orientationChange", lazyload);
    }
})
document.querySelector('.aside-nav-btn').addEventListener('click', function () {
    toogleNav();
})
document.querySelector('.modal-backdrop').addEventListener('click', function () {
    toogleNav();
})
navItems = document.querySelectorAll('.tab-content .widget-list-item');
for (var i = 0; i < navItems.length; i++) {
    navItems[i].addEventListener('click', function () {
        toogleNav();
    })
}

function toogleNav() {
    var sideNav = document.getElementById('sideNav');
    if (sideNav.classList.contains('side-nav-expand')) {
        sideNav.classList.remove('side-nav-expand');
        document.querySelector('.modal-backdrop').classList.add('fade');
        document.querySelector('.modal-backdrop').classList.remove('show');
    } else {
        sideNav.classList.add('side-nav-expand');
        document.querySelector('.modal-backdrop').classList.remove('fade');
        document.querySelector('.modal-backdrop').classList.add('show');
    }
}

document.addEventListener("DOMContentLoaded", function () {
    window.addEventListener('scroll', function () {
        fixedTopMenu()
    });
});

function fixedTopMenu() {
    if (window.scrollY > 50) {
        document.querySelector('header').classList.add('fixed-top');
        if(document.getElementsByClassName("topbar") == true){
            document.querySelector('.topbar').classList.add('d-none');
        }
        document.querySelector('.navbar').classList.add('p-0');
        document.querySelector('.navbar-brand').classList.add('margin-padding');
        document.querySelector('.navbar-brand-holder').classList.add('navbar-brand-holder-margin');
        document.querySelector('.navbar-brand-holder').classList.add('navbar-brand-holder-scroll');
        navbar_height = document.querySelector('.header-top').offsetHeight;
        document.body.style.paddingTop = navbar_height - 20 + 'px';
        if (document.querySelector('.mini-menu') != null) {
            document.querySelector('.mini-menu').style.marginTop = navbar_height + 'px';
        }
    } else {
        document.querySelector('header').classList.remove('fixed-top');
        if(document.getElementsByClassName("topbar") == true){
            document.querySelector('.topbar').classList.remove('d-none');
        }
        document.querySelector('.navbar').classList.remove('p-0');
        document.querySelector('.navbar-brand').classList.remove('margin-padding');
        document.querySelector('.navbar-brand-holder').classList.remove('navbar-brand-holder-margin');
        document.querySelector('.navbar-brand-holder').classList.remove('navbar-brand-holder-scroll');
        document.body.style.paddingTop = '0';
        if (document.querySelector('.mini-menu') != null) {
            document.querySelector('.mini-menu').style.marginTop = '0';
        }
    }
}

if (window.location.pathname.includes('themenwelt')) {
    document.querySelectorAll('.navbar-mega-nav>li')[2].classList.add('active');
} else if (/*window.location.pathname == '/' ||*/ window.location.pathname.includes('/kategorie') || window.location.href.includes('?kat')) {
    document.querySelectorAll('.navbar-mega-nav>li')[1].classList.add('active');
} else if (window.location.pathname == '/'){
    document.querySelectorAll('.navbar-mega-nav>li')[0].classList.add('active');
}

// var elements = document.querySelectorAll('.widget-list-item');
// var contents = document.querySelectorAll('.scroll-margin')
// for (var i = 0; i < elements.length; i++) {
//     elements[i].addEventListener('click', function () {
//         for (var j = 0; j < contents.length; j++) {
//             headerHeight = document.querySelector('header').clientHeight;
//             console.log(headerHeight)
//             const myStyles = `
//     scroll-margin-top: ` + headerHeight + `px;
// `;
//             contents[j].style.cssText = myStyles;
//             console.log(document.querySelector('header').clientHeight)
//         }
//     })
// }