(function () {
  function getAppRoot() {
    var marker = "/document/";
    var path = window.location.pathname || "";
    var index = path.toLowerCase().indexOf(marker);
    return index === -1 ? "" : path.substring(0, index);
  }

  function siteUrl(path) {
    return (getAppRoot() || "") + path;
  }

  function normalize(path) {
    if (!path) {
      return "/";
    }

    var normalized = path.toLowerCase().replace(/\/+$/, "");
    return normalized || "/";
  }

  function isCurrentPath() {
    var current = normalize(window.location.pathname);

    for (var i = 0; i < arguments.length; i += 1) {
      var candidate = normalize(arguments[i]);
      if (candidate && (current === candidate || current.indexOf(candidate + "/") === 0)) {
        return true;
      }
    }

    return false;
  }

  function createLink(label, href, isActive) {
    var link = document.createElement("a");
    link.href = href;
    link.textContent = label;
    if (isActive) {
      link.classList.add("is-active");
    }
    return link;
  }

  function createSpan(className, text) {
    var span = document.createElement("span");
    if (className) {
      span.className = className;
    }
    span.textContent = text;
    return span;
  }

  function initBrand() {
    var brand = document.querySelector("#autocollapse .navbar-brand");
    if (!brand) {
      return;
    }

    brand.href = siteUrl("/Default.aspx");
    brand.classList.add("au-docs-brand");

    var brandCopy = createSpan("au-docs-brand-copy", "");
    brandCopy.replaceChildren(
      createSpan("au-docs-brand-name", "AjaxUploader"),
      createSpan("au-docs-brand-sub", "ASP.NET Web Forms upload control")
    );

    brand.replaceChildren(
      createSpan("au-docs-brand-mark", "AJ"),
      brandCopy
    );
  }

  function initHeaderLinks() {
    var collapse = document.querySelector("#autocollapse .navbar-collapse");
    var searchForm = document.getElementById("search");
    var searchInput = document.getElementById("search-query");

    if (!collapse || !searchForm) {
      return;
    }

    searchForm.classList.add("au-docs-search");
    if (searchInput) {
      searchInput.placeholder = "Search docs\u2026";
      searchInput.setAttribute("autocomplete", "off");
    }

    // Build nav links
    var nav = collapse.querySelector(".au-docs-site-links");
    if (!nav) {
      nav = document.createElement("nav");
      nav.className = "au-docs-site-links";
      collapse.insertBefore(nav, searchForm);
    }

    nav.replaceChildren(
      createLink("Home", siteUrl("/Default.aspx"), isCurrentPath(siteUrl("/"), siteUrl("/Default.aspx"))),
      createLink("Demos", siteUrl("/Demos.aspx"), isCurrentPath(siteUrl("/Demos.aspx"), siteUrl("/Demos"))),
      createLink("Docs", siteUrl("/Docs.aspx"), isCurrentPath(siteUrl("/Docs.aspx"), siteUrl("/document"))),
      createLink("Pricing", siteUrl("/Pricing.aspx"), isCurrentPath(siteUrl("/Pricing.aspx"))),
      createLink("Company", siteUrl("/About.aspx"), isCurrentPath(siteUrl("/About.aspx")))
    );

    // Build actions (Support + Download Trial)
    var actions = collapse.querySelector(".au-docs-actions");
    if (!actions) {
      actions = document.createElement("div");
      actions.className = "au-docs-actions";
      collapse.appendChild(actions);
    }

    var supportLink = document.createElement("a");
    supportLink.href = siteUrl("/Support.aspx");
    supportLink.textContent = "Support";
    supportLink.className = "au-support-link";

    var downloadBtn = document.createElement("a");
    downloadBtn.href = siteUrl("/Download.aspx");
    downloadBtn.textContent = "Download Trial";
    downloadBtn.className = "au-download-btn";

    actions.replaceChildren(supportLink, downloadBtn);

    // Move search before actions
    collapse.insertBefore(searchForm, actions);
  }

  function initSubnav() {
    var breadcrumb = document.getElementById("breadcrumb");
    if (!breadcrumb) {
      return;
    }

    var shortcuts = breadcrumb.querySelector(".au-docs-shortcuts");
    if (!shortcuts) {
      shortcuts = document.createElement("div");
      shortcuts.className = "au-docs-shortcuts";
      breadcrumb.insertBefore(shortcuts, breadcrumb.firstChild);
    }

    shortcuts.replaceChildren(
      createLink("Class Reference", siteUrl("/document/"), isCurrentPath(siteUrl("/document/"), siteUrl("/document/index.html"), siteUrl("/document/index.htm"))),
      createLink("API Index", siteUrl("/document/api/index.html"), isCurrentPath(siteUrl("/document/api"), siteUrl("/document/api/index.html"))),
      createLink("Quick Start", siteUrl("/document/guides/webforms-quick-start.html"), isCurrentPath(siteUrl("/document/guides/webforms-quick-start.html"))),
      createLink("Upgrade Log", siteUrl("/document/guides/upgrade-log.html"), isCurrentPath(siteUrl("/document/guides/upgrade-log.html"))),
      createLink("Migration Guide", siteUrl("/document/guides/migrating-from-4x.html"), isCurrentPath(siteUrl("/document/guides/migrating-from-4x.html")))
    );
  }

  function initFooter() {
    var footerContainer = document.querySelector("footer .footer .container");
    var docsLink;
    var pricingLink;
    var topLink;
    var footerLinks;

    if (!footerContainer) {
      return;
    }

    docsLink = createLink("Docs Hub", siteUrl("/Docs.aspx"), false);
    pricingLink = createLink("Pricing", siteUrl("/Pricing.aspx"), false);
    topLink = createLink("Back to top", "#top", false);

    footerLinks = createSpan("", "");
    footerLinks.replaceChildren(
      docsLink,
      document.createTextNode(" \u00b7 "),
      pricingLink,
      document.createTextNode(" \u00b7 "),
      topLink
    );

    footerContainer.replaceChildren(
      createSpan("", "AjaxUploader 5.2 \u00b7 ASP.NET Web Forms class reference"),
      document.createTextNode(" "),
      footerLinks
    );
  }

  function init() {
    document.body.id = "top";
    initBrand();
    initHeaderLinks();
    initSubnav();
    initFooter();
  }

  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", init);
  } else {
    init();
  }
})();
