document.addEventListener('DOMContentLoaded', function () {
    var elems = document.querySelectorAll('.parallax');
    M.Parallax.init(elems, {});

    var elems = document.querySelectorAll('.slider');
    M.Slider.init(elems, {
        indicators: true,
        duration: 500,
        interval: 5000
    });

    var elems = document.querySelectorAll('.sidenav');
    M.Sidenav.init(elems, {});

    var mailcypts = document.querySelectorAll(".cryptmail");
    for (var i = 0; i < mailcypts.length; i++) {
        mailcypts[i].onclick = function () {
            window.location.href = 'mailto:' + this.dataset.name + '@' + this.dataset.domain + '.' + this.dataset.tld;
            return false;
        }
    }

    var hashAnchors = document.querySelectorAll("a[href^='#']:not([href='#'])");
    for (var i = 0; i < hashAnchors.length; i++) {
        hashAnchors[i].onclick = function () {
            var target = this.getAttribute("href");
            scrollToElement(target);
            return false;
        }

    }
});

const navbarScrollOffset = -48;
// native smooth scrolling for Chrome, Firefox & Opera
// @see: https://caniuse.com/#feat=css-scroll-behavior
const nativeSmoothScrollTo = function (elem) {
    window.scroll({
        behavior: 'smooth',
        left: 0,
        top: elem.getBoundingClientRect().top + window.pageYOffset + navbarScrollOffset
    });
};

// polyfilled smooth scrolling for IE, Edge & Safari
const smoothScrollTo = function (to, duration) {
    const element = document.scrollingElement || document.documentElement,
        start = element.scrollTop,
        change = to - start,
        startDate = +new Date();

    // t = current time
    // b = start value
    // c = change in value
    // d = duration
    const easeInOutQuad = function (t, b, c, d) {
        t /= d / 2;
        if (t < 1) return c / 2 * t * t + b;
        t--;
        return -c / 2 * (t * (t - 2) - 1) + b;
    };

    const animateScroll = function () {
        const currentDate = +new Date();
        const currentTime = currentDate - startDate;
        element.scrollTop = parseInt(easeInOutQuad(currentTime, start, change, duration));
        if (currentTime < duration) {
            requestAnimationFrame(animateScroll);
        } else {
            element.scrollTop = to;
        }
    };
    animateScroll();
};

// detect support for the behavior property in ScrollOptions
const supportsNativeSmoothScroll = 'scrollBehavior' in document.documentElement.style;

// smooth scrolling stub
const scrollToElement = function (elemSelector) {
    if (!elemSelector) {
        return;
    }

    const elem = document.querySelector(elemSelector);
    if (elem) {
        if (supportsNativeSmoothScroll) {
            nativeSmoothScrollTo(elem);
        } else {
            smoothScrollTo(elem.offsetTop + navbarScrollOffset, 600);
        }
    }
};