window.addEventListener('DOMContentLoaded', function () {
  var burger = document.querySelector('#menu-burger-main');
  burger.onclick = function () {
    burger.classList.toggle('prikazi');
  }
});

var currentXHR = null, currentURL = null, pendingURL = null, pendingForward = false;

function timeoutHandler() {
  this.abort();
}

function loadPageHandler() {
  if (this.readyState !== 4) return;
  if (this !== currentXHR) return;
  currentXHR = null;
  if (this.status === 200) {
    try {
      var parser = new DOMParser();
      var newdoc = parser.parseFromString(this.responseText, "text/html");
      var oldRoot = document.querySelector(".dialog-off-canvas-main-canvas");
      var newRoot = newdoc.querySelector(".dialog-off-canvas-main-canvas");
      if (oldRoot && newRoot) {
        document.title = newdoc.title;
        document.body.replaceChild(newRoot, oldRoot);
        document.body.className = newdoc.body.className;
        currentURL = pendingURL;
        pendingURL = null;
        pendingForward = false;
        setTimeout(navigationHandlerNextTick, 0);
        return;
      }
    } catch (e) {
      console.error(e);
    }
  }
  pendingURL = null;
  pendingForward = false;
  location.reload();
}

function loadPageAsync() {
  if (currentXHR) {
    var oxhr = currentXHR;
    currentXHR = null;
    oxhr.abort();
  }
  var oldRoot = document.querySelector(".dialog-off-canvas-main-canvas");
  if (!oldRoot) return false;
  if (!pendingURL) {
    pendingForward = false;
    oldRoot.removeAttribute("pending");
    return true;
  }
  try {
    var xhr = new XMLHttpRequest();
    xhr.open("get", pendingURL, true);
    xhr.timeout = 20000;
    xhr.ontimeout = timeoutHandler;
    xhr.onreadystatechange = loadPageHandler;
    currentXHR = xhr;
    xhr.send(null);
  } catch (e) {
    console.error(e);
    currentXHR = null;
    return false;
  }
  oldRoot.setAttribute("pending", "");
  return true;
}

function navigateHandler(fwd) {
  var nextURL = String(location).split("#")[0];
  pendingForward = !!fwd;
  if (nextURL === pendingURL) return;
  if (nextURL === currentURL) pendingURL = null;
  else pendingURL = nextURL;
  if (!loadPageAsync()) location.reload();
}

function popstateHandler() {
  navigateHandler(false);
}

function linkclickHandler(event) {
  if (event.button !== 0 && event.which !== 0 && event.which !== 1) return;
  if (event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) return;
  var link = event.target;
  while (link && link.tagName !== "A") link = link.parentNode;
  if (!link || !link.href || link.target) return;
  try {
    if (pendingForward) history.replaceState(null, "", link.href);
    else history.pushState(null, "", link.href);
  } catch (e) {
    console.error(e);
    return;
  }
  event.preventDefault();
  navigateHandler(true);
}

if ("pushState" in history && "onpopstate" in window && document.body.className.indexOf("user-logged-in") === -1) {
  currentURL = String(location).split("#")[0];
  window.onpopstate = popstateHandler;
  window.onclick = linkclickHandler;
}

function navigationHandlerNextTick() {
  window.dispatchEvent(new CustomEvent("rs:navigate"));
}

window.addEventListener("DOMContentLoaded", function () {
  console.log("LOUDED!");
  navigationHandlerNextTick();
});
