window.addEventListener("scroll", function (e) {
  if (document.querySelector("body").clientWidth > 767) {
    let rightcolumn = document.querySelector(".rightcolumn");
    let mantletitle =
      document.querySelector(".mantletitle") ||
      document.querySelector(".crusttitle") ||
      false;
    if (!mantletitle) {
      return;
    }
    let mantlebar = document.querySelector(".mantlebar");
    let grid = document.querySelector(".grid");
    let gridrect = mantletitle.getBoundingClientRect();

    if (
      document.querySelector("body").clientHeight -
        (grid.clientHeight + mantletitle.clientHeight + 32) <
      window.innerHeight
    ) {
      return;
    }

    if (gridrect.top < 20 && !grid.classList.contains("moved")) {
      grid.classList.add("moved");
      mantlebar.style.display = "inline";
      mantletitle.style.visibility = "hidden";
    } else if (gridrect.top > 21 && grid.classList.contains("moved")) {
      grid.classList.remove("moved");
      mantletitle.style.visibility = "visible";
      mantlebar.style.display = "none";
    }
  } else {
    let rightcolumn = document.querySelector(".rightcolumn");
    let mantletitle =
      document.querySelector(".mantletitle") ||
      document.querySelector(".crusttitle") ||
      false;
    if (!mantletitle) {
      return;
    }
    let mantlebar = document.querySelector(".mantlebar");
    let grid = document.querySelector(".grid");
    let gridrect = mantletitle.getBoundingClientRect();

    if (gridrect.top < 20 && !grid.classList.contains("moved")) {
      grid.classList.add("moved");
      mantlebar.style.display = "inline";
      mantletitle.style.visibility = "hidden";
    } else if (gridrect.top > 21 && grid.classList.contains("moved")) {
      grid.classList.remove("moved");
      mantletitle.style.visibility = "visible";
      mantlebar.style.display = "none";
    }
  }
});

function editPage(repoJsId) {
  let relPath = "../".repeat(relPathDepth - 1);
  var el = document.createElement('script');
  el.onload = ()=> {
    console.log("loading...")
    console.log(repo)
  }
  el.src = `${relPath}js/repo/${repoJsId}.js`
  document.body.appendChild(el)
  document.location.href = `${relPath}edit/index.html?edit=${repoJsId}`
ev}

window.addEventListener("keyup", (e)=> {
  if (e.key == "e" && location.protocol == "file:") {
    editPage(sandpointsPageId)
  } else if (e.key == "E" && location.protocol == "file:") {
    let preview = document.querySelector("#preview");
    if (preview.style.display == "flex") {
      preview.style.display = "none";
    } else {
      preview.style.display = "flex";
    }
    console.log("preview: ", preview);
  }
})

window.addEventListener("DOMContentLoaded", (e) => {
  if (location.protocol == "file:") {
    document.querySelectorAll(".svedit").forEach((i) => {
      i.style.display = "inline";
    })
  }
  
  document.querySelectorAll('.triadlink').forEach((i) => {
    i.addEventListener("click", (ev) => {
      if (ev.ctrlKey) {
        ev.preventDefault()
        editPage(ev.target.getAttribute("repoid"))
      }
    })
  })
  
  let preview = document.querySelector("#preview");
  if (
    location.pathname.split("/").includes("_preview") ||
    location.hash.startsWith("#/_edit")
  ) {
    preview.style.display = "flex";
  }

  let par = document.querySelector(
    "#TableOfContents > ol:first-child > li:first-child"
  );
  if (par) {
    let s = document.createElement("span");
    s.appendChild(par.cloneNode(true));
    par.remove();
    document
      .querySelector("#TableOfContents > ol:first-child")
      .insertBefore(
        s,
        document.querySelector("#TableOfContents > ol:first-child").firstChild
      );
  }

  if (document.querySelector(".soundcite")) {
    let relPath = "../".repeat(relPathDepth - 1);

    var popcorn = document.createElement("script");
    popcorn.src = `${relPath}js/popcorn.min.js`;
    document.body.appendChild(popcorn);

    var soundcite = document.createElement("script");
    soundcite.src = `${relPath}js/soundcite.min.js`;
    document.body.appendChild(soundcite);
  }

  document
    .querySelectorAll(
      "#TableOfContents a, .anchorz, .footnote-backref, .footnote-ref"
    )
    .forEach((i) => {
      i.addEventListener("click", (e) => {
        setTimeout(() => {
          window.scrollBy({
            top: document.querySelector(".header").clientHeight * -1.25,
            behavior: "smooth",
          });
        }, 50);
      });
    });
});

toggleAllLinks = function () {
  document.querySelectorAll("details").forEach((i) => {
    if (i.open) {
      i.removeAttribute("open");
    } else {
      i.setAttribute("open", true);
    }
  });
};
