function setCookie(cname, cvalue, exdays) {
  const d = new Date();
  d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
  let expires = "expires=" + d.toUTCString();
  document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
};

function getCookie(cname) {
  let name = cname + "=";
  let decodedCookie = decodeURIComponent(document.cookie);
  let ca = decodedCookie.split(';');
  for (let i = 0; i < ca.length; i++) {
    let c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
};

function show(content) {
    var cont = document.getElementById(content);
    cont.style.transition = "height 1s ease";
    cont.style.height = "auto";
    var contheight = cont.offsetHeight;
    cont.style.height = "0px";
    setTimeout(function () {
        cont.style.height = contheight + "px";
        cont.style.overflow = "none";
        }, 1);
};

function showcontent(content) {
    show(content);
    var contbutton = document.getElementById(content + "button");
    contbutton.style.background = "var(--mtf-accent-color)";
    contbutton.style.color = "var(--mtf-background-color)";
};

function hidecontent(content) {
    var cont = document.getElementById(content);
    var contbutton = document.getElementById(content + "button");
    cont.style.transition = "height 1s ease";
    setTimeout(function () {
        cont.style.height = "0px";
        cont.style.overflow = "none";
        }, 1);
    contbutton.style.background = "var(--mtf-base-color)";
    contbutton.style.color = "var(--mtf-font-color)";
};

function startup() {
// var srch = document.getElementById("search");
// srch.style.height = "auto";
// srch.style.overflow = "auto";
// var srchh = srch.offsetHeight;
// var srchh = srchh + "px";
// srch.style.height = srchh;
    setTimeout(function () {
        document.getElementById('searchbox').focus();
    }, 200);
    if (getCookie("contentlist") == "") {
        setCookie("contentlist", "search", 10000);
    }
    var darklight = getCookie("darklightc");
    if (darklight == 0) {
        button_lightmode();
    }
    else if (darklight == 1) {
        button_darkmode();
    }
    else {
        return "";
    };
    var k = getCookie("contentlist").split(' ').length;
    for (var l = 0; l < k; l++)
    {
        showcontent(getCookie("contentlist").split(' ')[l]);
    };

    show("header");
    show("linksrow");
    show("footer");
};

function shiftcontent(content) {
    var contentlist = getCookie("contentlist");
    let containcontent = contentlist.includes(content);
    if (containcontent == false) {
        newcontentlist = getCookie("contentlist") + " " + content;
        setCookie("contentlist", newcontentlist, 10000);
        showcontent(content);
        var cont = document.getElementById(content);
        var position = cont.getBoundingClientRect().top;
        window.scrollTo({
            top: position,
            left: 0,
            behavior: "smooth",
        });
        setTimeout(function () {
            window.scrollTo({
                top: position,
                left: 0,
                behavior: "smooth",
            });
        }, 300);
    }
    else if (containcontent == true) {
        newcontentlist = getCookie("contentlist").replace(content,'');
        setCookie("contentlist", newcontentlist, 10000);
        hidecontent(content);
    };
};


//function shiftcontent(content) {
// var x = document.getElementsByClassName("box-shower");
// var cont = document.getElementById(content);
// cont.style.height = "auto";
// var contheight = cont.offsetHeight;
// cont.style.height = "0px";
// for (var i = 0; i < x.length; i++)
// {
//   x[i].style.height = "0px";
//   x[i].style.overflow = "hidden";
// };
// setTimeout(function() {
//   contheight = contheight + "px";
//   cont.style.height = contheight;
//   cont.style.overflow = "auto";
// }, 500);
// if (content == "search") {
//   setTimeout(function ()
//   {
//     document.getElementById('searchbox').focus();
//   }, 700);
// }
// else {
//    return "";
//  }
//};

function toggleld(ld) {
  let clrs = document.body.style;
  if (ld == light) {
    document.getElementById("darkmode").style.display = "inline";
    document.getElementById("lightmode").style.display = "none";
    clrs.setProperty('--mtf-background-color', "#eff1f5");
    clrs.setProperty('--mtf-font-color', "#4c4f69");
    clrs.setProperty('--mtf-background-2-color', "#e6e9efdf");
    clrs.setProperty('--mtf-accent-color', "#dc8a78");
    clrs.setProperty('--mtf-background-image', "url('src/Clearnight.jpg')");
    setCookie("darklightc", 0, 30);
  }
  else {
    document.getElementById("darkmode").style.display = "none";
    document.getElementById("lightmode").style.display = "inline";
    clrs.setProperty('--mtf-background-color', "#1e1e2e");
    clrs.setProperty('--mtf-font-color', "#cdd6f4");
    clrs.setProperty('--mtf-background-2-color', "#181825df");
    clrs.setProperty('--mtf-accent-color', "#f5e0dc");
    clrs.setProperty('--mtf-background-image', "url('src/Clearday.jpg')");
    setCookie("darklightc", 1, 30);
  }
};

function button_lightmode() {
  document.getElementById("darkmode").style.display = "inline";
  document.getElementById("lightmode").style.display = "none";
  document.body.style.setProperty('--mtf-background-color', "#eff1f5");
  document.body.style.setProperty('--mtf-font-color', "#4c4f69");
  document.body.style.setProperty('--mtf-background-2-color', "#e6e9efdf");
  document.body.style.setProperty('--mtf-accent-color', "#dc8a78");
  document.body.style.setProperty('--mtf-background-image', "url('src/Clearnight.jpg')");
  setCookie("darklightc", 0, 30);
};


function button_darkmode() {
  document.getElementById("darkmode").style.display = "none";
  document.getElementById("lightmode").style.display = "inline";
  document.body.style.setProperty('--mtf-background-color', "#1e1e2e");
  document.body.style.setProperty('--mtf-font-color', "#cdd6f4");
  document.body.style.setProperty('--mtf-background-2-color', "#181825df");
  document.body.style.setProperty('--mtf-accent-color', "#f5e0dc");
  document.body.style.setProperty('--mtf-background-image', "url('src/Clearday.jpg')");
  setCookie("darklightc", 1, 30);
};

function bookmarks_open() {
  let bmks = document.getElementById("bookmarks");
  bmks.style.display = "block";
  bmks.style.opacity = "1"
};

function bookmarks_close() {
  let bmks = document.getElementById("bookmarks");
  bmks.style.display = "none";
  bmks.style.opacity = "0";
};


function bmks_exp(bmrk) {
  var bmk = document.getElementById(bmrk);
  var disp = window.getComputedStyle(bmk).display;
  if (disp == "none") {
    bmk.style.display = "block";
  }
  else {
    bmk.style.display = "none";
  }
}
