const $iframe = $("#toc iframe", parent.document);
if ($iframe.length > 0 && typeof $iframe.iframeResize === "function") {
  $iframe.iframeResize({ license: '1jy4dww5qzv-s54r73oxcn-v59f4kfgfz' });
}

let tocChildsBGcolor = "#f4f5f4";
let tocCurrentBGcolor = "#00a3d9";
let tocCurrentBGcolorBorder = "#c4c7c4";
let tocCurrentItemColor = "#ffffff";
let tocBGcolor = "#fff";
let tocBorderColor = "#424d56";
let tocItemColor = "#3a3a3a";
if ($("html", document).hasClass("dark")) {
  tocChildsBGcolor = "#2a2a2a";
  tocCurrentBGcolor = "#0081c2";
  tocCurrentBGcolorBorder = "#84919a";
  tocCurrentItemColor = "#fff";
  tocBGcolor = "#121212";
  tocBorderColor = "#a7a7a7";
  tocItemColor = "#f0f0f0";
}


function setBGandExpand(currentElement, event) {
  let parnt = null;
  if (currentElement.hasClass("heading1")) {
    parnt = currentElement.parent();
    parentElementA = parnt.find("a");
    parentElementU = parnt.find("ul").not("li.heading2 ul");
  }
  else if (currentElement.hasClass("heading2")) {
    parnt = currentElement.parent().parent().parent();
    parentElementA = parnt.find("a");
    parentElementU = currentElement.parent().find("ul").first();
    parentElementUTOP = parnt.find("ul").first();
    if (parentElementUTOP.css("display") == "none")
      parentElementUTOP.slideToggle(400);
  }
  else if (currentElement.hasClass("heading3")) { 
    let parent2 = currentElement.parent().parent().parent();
    parnt = parent2.parent().parent();
    parentElementA = parnt.find("a");
    parentElementU = currentElement.parent().find("ul").first(); 
    parentElementUTOP = parnt.find("ul").first();
    if (parentElementUTOP.css("display") == "none")
      parentElementUTOP.slideToggle(400); 
    parentElementULEVEL2 = parent2.find("ul").first();
    if (parentElementULEVEL2.css("display") == "none")
      parentElementULEVEL2.slideToggle(400);
    parentElementALEVEL2 = parent2.find("a").first();
    if (parentElementALEVEL2.hasClass(arrowDown))
      parentElementALEVEL2.addClass(arrowUp).removeClass(arrowDown);
  }
  else if (currentElement.hasClass("heading4")) { 
    let parent3 = currentElement.parent().parent().parent();
    let parent2 = parent3.parent().parent();
    parnt = parent2.parent().parent();
    parentElementA = parnt.find("a");
    parentElementU = currentElement.parent().find("ul").first();
    parentElementUTOP = parnt.find("ul").first();
    if (parentElementUTOP.css("display") == "none")
      parentElementUTOP.slideToggle(400); 
    parentElementULEVEL3 = parent3.find("ul").first();
    if (parentElementULEVEL3.css("display") == "none")
      parentElementULEVEL3.slideToggle(400);
    parentElementALEVEL3 = parent3.find("a").first();
    if (parentElementALEVEL3.hasClass(arrowDown))
      parentElementALEVEL3.addClass(arrowUp).removeClass(arrowDown);
    parentElementULEVEL2 = parent2.find("ul").first();
    if (parentElementULEVEL2.css("display") == "none")
      parentElementULEVEL2.slideToggle(400);
    parentElementALEVEL2 = parent2.find("a").first();
    if (parentElementALEVEL2.hasClass(arrowDown))
      parentElementALEVEL2.addClass(arrowUp).removeClass(arrowDown);
  }
  else if (currentElement.hasClass("heading5")) {
    let parent4 = currentElement.parent().parent().parent();
    let parent3 = parent4.parent().parent();
    let parent2 = parent3.parent().parent();
    parnt = parent2.parent().parent();
    parentElementA = parnt.find("a");
    parentElementU = currentElement.parent().find("ul").first();
    parentElementUTOP = parnt.find("ul").first();
    if (parentElementUTOP.css("display") == "none")
      parentElementUTOP.slideToggle(400);
    parentElementULEVEL4 = parent4.find("ul").first();
    if (parentElementULEVEL4.css("display") == "none")
      parentElementULEVEL4.slideToggle(400);
    parentElementALEVEL4 = parent4.find("a").first();
    if (parentElementALEVEL4.hasClass(arrowDown))
      parentElementALEVEL4.addClass(arrowUp).removeClass(arrowDown); 
    parentElementULEVEL3 = parent3.find("ul").first();
    if (parentElementULEVEL3.css("display") == "none")
      parentElementULEVEL3.slideToggle(400);
    parentElementALEVEL3 = parent3.find("a").first();
    if (parentElementALEVEL3.hasClass(arrowDown))
      parentElementALEVEL3.addClass(arrowUp).removeClass(arrowDown);
    parentElementULEVEL2 = parent2.find("ul").first();
    if (parentElementULEVEL2.css("display") == "none")
      parentElementULEVEL2.slideToggle(400);
    parentElementALEVEL2 = parent2.find("a").first();
    if (parentElementALEVEL2.hasClass(arrowDown))
      parentElementALEVEL2.addClass(arrowUp).removeClass(arrowDown);
  }
  else if (currentElement.hasClass("heading6")) {
    let parent5 = currentElement.parent().parent().parent();
    let parent4 = parent5.parent().parent();
    let parent3 = parent4.parent().parent();
    let parent2 = parent3.parent().parent();
    parnt = parent2.parent().parent();
    parentElementA = parnt.find("a");
    parentElementU = currentElement.parent().find("ul").first();
    parentElementUTOP = parnt.find("ul").first();
    if (parentElementUTOP.css("display") == "none")
      parentElementUTOP.slideToggle(400);
    parentElementULEVEL5 = parent5.find("ul").first();
    if (parentElementULEVEL5.css("display") == "none")
      parentElementULEVEL5.slideToggle(400);
    parentElementALEVEL5 = parent5.find("a").first();
    if (parentElementALEVEL5.hasClass(arrowDown))
      parentElementALEVEL5.addClass(arrowUp).removeClass(arrowDown); 
    parentElementULEVEL4 = parent4.find("ul").first();
    if (parentElementULEVEL4.css("display") == "none")
      parentElementULEVEL4.slideToggle(400);
    parentElementALEVEL4 = parent4.find("a").first();
    if (parentElementALEVEL4.hasClass(arrowDown))
      parentElementALEVEL4.addClass(arrowUp).removeClass(arrowDown); 
    parentElementULEVEL3 = parent3.find("ul").first();
    if (parentElementULEVEL3.css("display") == "none")
      parentElementULEVEL3.slideToggle(400);
    parentElementALEVEL3 = parent3.find("a").first();
    if (parentElementALEVEL3.hasClass(arrowDown))
      parentElementALEVEL3.addClass(arrowUp).removeClass(arrowDown);
    parentElementULEVEL2 = parent2.find("ul").first();
    if (parentElementULEVEL2.css("display") == "none")
      parentElementULEVEL2.slideToggle(400);
    parentElementALEVEL2 = parent2.find("a").first();
    if (parentElementALEVEL2.hasClass(arrowDown))
      parentElementALEVEL2.addClass(arrowUp).removeClass(arrowDown);
  }
  // Arrow clicked or chapter w/o text?
  let arrowClicked = false;
  let arrowSize = 28;
  if (window.innerWidth > 480) arrowSize = 42;
  if (event && $("html", document).attr("dir") == "rtl" && currentElement.offset().left + arrowSize > event.pageX)
    arrowClicked = true;
  if (event && $("html", document).attr("dir") != "rtl" && currentElement.offset().left + currentElement.outerWidth() - arrowSize < event.pageX)
    arrowClicked = true;
  if (currentElement.attr("href").includes("void(0)"))
    arrowClicked = true;
  // Background
  parentElementA.css("background-color", tocChildsBGcolor);
  if (currentElement.next("ul").length || !currentElement.hasClass("heading1"))
    parentElementA.css("border-color", tocCurrentBGcolorBorder);
  if (!arrowClicked) {
    currentElement.css("background-color", tocCurrentBGcolor);
    currentElement.css("color", tocCurrentItemColor);
    currentElement.addClass("selected");
  }
  // When clicked, set up arrow
  if (currentElement.hasClass(arrowDown))
    currentElement.addClass(arrowUp).removeClass(arrowDown);
  // When clicked or topic is active, set up arrow for the grandparent topic
  if (parentElementA.first().hasClass(arrowDown))
    parentElementA.first().addClass(arrowUp).removeClass(arrowDown);
  if (!arrowClicked && parentElementU.css("display") == "none") {
    parentElementU.slideToggle(400, function() {parent.adjustFooterPosition();});
    // Change arrow for active subtree
    currentElement.addClass(arrowUp).removeClass(arrowDown);
    // Gray background for current+child topics
    currentElement.css("background-color", tocChildsBGcolor);
  }
  // Expand childs when clicked on arrow
  if (arrowClicked && parentElementU.css("display") == "none") {
    parentElementU.slideToggle(400, function() {
      parent.adjustFooterPosition();
    });
    currentElement.addClass(arrowUp).removeClass(arrowDown);
    reload = false; // do not reload topic
    currentElement.css("background-color", tocChildsBGcolor);
  }
  // Hide when 2 times clicked on the same arrow on same topic
  else if (arrowClicked && parentElementU.css("display") == "block") {
    parentElementU.slideToggle(1000, function() {
      parent.adjustFooterPosition();
    });
    currentElement.addClass(arrowDown).removeClass(arrowUp);
    reload = false;
  }
  if (arrowClicked && latestCurrentElement) {
    latestCurrentElement.css("background-color", tocCurrentBGcolor);
    latestCurrentElement.css("color", tocCurrentItemColor);
    latestCurrentElement.addClass("selected");
  }
  topicName = currentElement.attr("href");
  if (reload) {
    lastTopicName = topicName;
    latestCurrentElement = currentElement;
  }
}

// Always target parent frame
$("#toc a", document).attr("target", "_top");
// Predefined global variables
let topicToReload = "index.html";
let level1Topic = true; 
let validTopicName = false;
// Arrow color
let arrowUp = "bgarrow_up";     let arrowUpWhite = "bgarrow_up_white"; 
let arrowDown = "bgarrow_down"; let arrowDownWhite = "bgarrow_down_white";
if ($("html", document).hasClass("dark")) {
  arrowUp = "bgarrow_up_white";  
  arrowDown = "bgarrow_down_white";
}

// Always hidden, expanded only by click on a specific subtree
$("#toc ul", document).css("display", "none");

// Add arrows to topics with <ul>
$("#toc span", document).filter("[ondblclick]").parent().addClass(arrowDown);

// Set initial topic name based on current parent URL
let lastTopicName = "";
let urlParent = parent.location;
let topicName = urlParent.pathname.substring(urlParent.pathname.lastIndexOf('/')+1);
let queryStringPositon = urlParent.href.indexOf("?");
if (urlParent.href.includes("?")) {
  let topicForceToLoad = urlParent.href.substring(queryStringPositon+1);
  if (topicForceToLoad.match("^[a-zA-Z0-9._-]+\.html$"))
    topicName = topicForceToLoad;
}
if (topicName.length < 5) topicName = "index.html"; // Default is index.html (documentROOT)

let reload = false;
let latestCurrentElement = null;
$("#toc a", document).first().css("border-top", "1px solid " + tocBorderColor);
$("#toc a", document).each(function() {
  if ($( this ).attr("href") == topicName)
    setBGandExpand($( this ));
});

if (history.pushState) {
  function hmLoadTopic(arr) {
    const fadeTimeout = 300;
    $('h1.p_Heading1', parent.document).html($(arr['hmHeader']).contents()).hide().fadeIn(fadeTimeout);
    $('#topicText', parent.document).hide().html(arr['hmBody']).fadeIn(fadeTimeout, function() {
      parent.adjustFooterPosition();
      parent.adjustTablesForMobile();
      parent.sortTables();
      parent.adjustImagesForMobile();
      parent.adjustRTLFloatImages();
      parent.extLinks();
      parent.copyCode();
      
      if (typeof parent.anchorHeading === 'function') { parent.anchorHeading(); }
      
      
      
      
      if (typeof parent.initIframeResizer === 'function') { parent.initIframeResizer(); }
    });
    if (arr['hmPrevLink'].length > 5) {
      $('a.previous', parent.document).attr("href", arr['hmPrevLink']);
      $('a.previous', parent.document).removeClass("hidden");
    } else 
      $('a.previous', parent.document).addClass("hidden");
    if (arr['hmNextLink'].length > 5) {
      $('a.next', parent.document).attr("href", arr['hmNextLink']);
      $('a.next', parent.document).removeClass("hidden");
    } else
      $('a.next', parent.document).addClass("hidden");
    if ($("#breadcrumbs")) {
      let finalBreadcrumb = arr['hmTitle'];
      if (arr['hmBreadCrumbs'].length > 0)
        finalBreadcrumb = arr['hmBreadCrumbs'] + " &gt; " + finalBreadcrumb;
      $('#breadcrumbs span', parent.document).hide().html(finalBreadcrumb).fadeIn(fadeTimeout);
    }
  }
  parent.hmLoadTopic = hmLoadTopic;
  $("#toc a", document).click(function(event) {
    // Reset background
    $("#toc a", document).css("background-color", tocBGcolor);
    $("#toc a", document).css("border-color", tocBorderColor);
    $("#toc a", document).css("color", tocItemColor);
    $("#toc a", document).removeClass("selected");
    // Set global variables to current clicked topic
    topicToReload = $( this ).attr("href").substring(0, $( this ).attr("href").lastIndexOf("."));
    level1Topic = $( this ).hasClass("heading1");
    // Detect layout
    let ifMobileLayout = $("#main", parent.document).css("flex-direction") == "column";
    // Change TOC title
    if (ifMobileLayout)
      $("a#topicSelector", parent.document).text($( this ).text()); 
    // Hide lngselectbar and verselectbar if opened
    $("#languagemenu", parent.document).hide("fast");
    $("#versionSelector", parent.document).hide("fast");
    // If the same topic is clicked, do not reload
    reload = false;
    if (lastTopicName != $( this ).attr("href"))
      reload = true;
    // Set background and expand particular topics + detect if still able to reload
    setBGandExpand($( this ), event);
    // New page
    let newurl = parent.location.protocol + "//" + parent.location.host + parent.location.pathname + '?' + $( this ).attr("href");
    let newurlGA = parent.location.pathname.substring(0, parent.location.pathname.lastIndexOf('/')) + '/' + $( this ).attr("href");
    let newtitle = $( this ).find("span").text() + " | ESET PROTECT On-Prem | ESET Online Help";
    validTopicName = $( this ).attr("href").match("^[a-zA-Z0-9._-]+\.html$");
    parent.document.title = newtitle;
    // Load new page and if failed, reload whole page
    if (reload && validTopicName) {
      let reloadHrefEl = $( this );
      let reloadYmdh = new Date().toISOString().slice(0, 13).replace(/[-T]/g, '');
      // Append loading cursor if page load time is too high
      let reloadTimeout = setTimeout(function() {
        reloadHrefEl.css("cursor", "wait");
      }, 500);
      $.ajax({
        url: "jstopics/" + $(this).attr("href").replace("html", "js") + "?v=" + reloadYmdh,
        dataType: "script",
        cache: true,
        success: function() {
          clearTimeout(reloadTimeout);
          setTimeout(function() {
            $(reloadHrefEl).css("cursor", "pointer");
          }, 250);
        },
        error: function(jqxhr, settings, exception) {
          parent.location.href = newurlGA;
        }
      });
      // If a valid page, push new history
      parent.history.pushState({path:newurl, title:newtitle}, newtitle, newurl);
    }
    // Scroll top if you are below topic heading
    let Theading1 = $("#content h1:first", parent.document);
    let scrollTopOffset = 0;
    if (reload) setTimeout(function () {
      let tocFrameSelector = $("#toc iframe", parent.document); 
      // "Hide" TOC if mobile layout
      if (ifMobileLayout) {
        parent.tocFrameHeight = tocFrameSelector.height();
        parent.hideTOCbyZeroHeight();
        // Scroll to Header1 for mobile skin
        $('html, body', parent.document).animate({ scrollTop: Theading1.offset().top - tocFrameSelector.height() - 20 }, 500);
      } else {
        if (Theading1.offset().top + scrollTopOffset < $(parent.document).scrollTop())
          $('html, body', parent.document).animate({ scrollTop: Theading1.offset().top - 5 }, 500);
      }
    }, 400);

    // Disable local TOC hyperlinks only
    if ($(this).attr("href").startsWith("http://") || $(this).attr("href").startsWith("https://"));
    else
      event.preventDefault();
  });
}
