
/* NAVBAR COLOR TOGGLE WHILE SCROLLING */
function navbarToggleOnScroll() {
    var el = document.getElementById("navbar");
    if (window.pageYOffset >= 500) {
        el.classList.add("scroll");
    } else {
        el.classList.remove("scroll");
    }
 }; 
window.addEventListener("scroll", navbarToggleOnScroll);

/* SIDEBAR TOGGLE */
function hasClass(ele, cls) {
    return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}

function addClass(ele, cls) {
    if (!hasClass(ele, cls)) ele.className += " " + cls;
}

function removeClass(ele, cls) {
    if (hasClass(ele, cls)) {
        var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
        ele.className = ele.className.replace(reg, ' ');
    }
}
function toggleMenu() {
    var ele = document.getElementsByTagName('body')[0];
    if (!hasClass(ele, "menu-open")) {
        addClass(ele, "menu-open");
    } else {
        removeClass(ele, "menu-open");
    }
}
//Add event from js the keep the marup clean
function init() {
    document.getElementById("menutoggle").addEventListener("click", toggleMenu);
    document.getElementById("body-overlay").addEventListener("click", toggleMenu);
}


//Prevent the function to run before the document is loaded
document.addEventListener('readystatechange', function() {
    if (document.readyState === "complete") {
        init();
    }
});