 copyMenuDOM();
//подключаем полосу прокрутки
$("#wh_topic_toc>ul").addClass("scrollbar-outer").scrollbar({
  "disableBodyScroll":true,
  "onScroll":function(y, x){
    if(y.maxScroll - 1 <= y.scroll){
      $("#wh_topic_toc .scroll-wrapper").addClass("hide-bottom-blur");
    }
    else{
      $("#wh_topic_toc .scroll-wrapper").removeClass("hide-bottom-blur");
    }
    if(y.scroll <= 1){
      $("#wh_topic_toc .scroll-wrapper").addClass("hide-top-blur");
    }
    else{
      $("#wh_topic_toc .scroll-wrapper").removeClass("hide-top-blur");
    }
  }
});

function copyMenuDOM(){
  if(document.querySelector("#wh_topic_toc")) {
    const originMenu = document.querySelector("#wh_topic_toc  .wh_topic_toc");
    const originMenuOuter = document.getElementById("wh_topic_toc");
    const menuHtml = originMenu.innerHTML;
    originMenuOuter.innerHTML = menuHtml;
    rightSideMenuInjection();
  }
}
 function rightSideMenuInjection(){
   if(document.querySelector("#wh_topic_toc")){
     let oldRightMenuTitlesNode = document.querySelectorAll("#wh_topic_toc .topic-item, #wh_topic_toc .section-title");
     let oldRightMenuSectionItemNode = document.querySelectorAll("#wh_topic_toc .section-item");
     oldRightMenuTitlesNode.forEach(function (titleElement) {
       let mark = document.createElement("div");
       if(titleElement.querySelector("ul")){
         mark.setAttribute("class", "new-right-side-menu__title-marker_child");
       }
       else{
         mark.setAttribute("class", "new-right-side-menu__title-marker_no-child");
       }
       titleElement.prepend(mark);
     });
     //чистка от пустых элементов
     oldRightMenuTitlesNode.forEach(function (sectionItemElement) {
       if(sectionItemElement.textContent.trim() == ""){
         sectionItemElement.remove();
       }
     });
     oldRightMenuSectionItemNode.forEach(function (sectionItemElement) {
       if(sectionItemElement.textContent.trim() == ""){
         sectionItemElement.remove();
       }
     });
   }
 }
 $(".new-right-side-menu__title-marker_child").on("click",function(){
   let container = $(this).parent().children("ul");
   let parent = $(this).parent();
   if (parent.hasClass("opened-node")){
     container.slideUp(300);
     parent.removeClass("opened-node");
   }
   else{
     container.slideDown(300);
     parent.addClass("opened-node");
   }
 });
 $("#wh_topic_toc a, #wh_topic_toc .new-right-side-menu__title-marker_child, #wh_topic_toc .new-right-side-menu__title-marker_no-child").on("mouseenter", function () {
   if(this.parentNode.nodeName === "LI"){
     this.parentNode.classList.add("hover-node");
   }
   else{
     this.parentNode.parentNode.classList.add("hover-node");
   }
 });
 $("#wh_topic_toc a, #wh_topic_toc .new-right-side-menu__title-marker_child, #wh_topic_toc .new-right-side-menu__title-marker_no-child").on("mouseleave", function () {
   if(this.parentNode.nodeName === "LI"){
     this.parentNode.classList.remove("hover-node");
   }
   else{
     this.parentNode.parentNode.classList.remove("hover-node");
   }
 });

 function showedElement(elem) {
   const docViewTopRegionStart = $(window).scrollTop();
   const docViewTopRegionEnd = docViewTopRegionStart + 145;
   const elemTop = $(elem).offset().top;
   const elemBottom = elemTop + $(elem).height();
   return ((elemTop >= docViewTopRegionStart && elemTop <= docViewTopRegionEnd))||(elemTop <= docViewTopRegionStart && elemBottom >= docViewTopRegionEnd);
 }

 $(document).on("scroll",()=>{
   const menuLinks = document.querySelectorAll("#wh_topic_toc a");
   menuLinks.forEach((link)=>{
     const id = link.getAttribute("data-tocid");
     if(showedElement(`#${id}`)){
       $(".current_node").removeClass("current_node");
       $(link).parents("li").addClass("current_node");
     }
   });
   const currentNode = $("li.current_node");
   let newCurrentOffset = currentNode.length > 0 ? currentNode[0].offsetTop : 0;
   if(newCurrentOffset == 0){
     currentNode.parents("ul").each(function(index){
       if(!$(this).is(":visible")){
         newCurrentOffset = $(this).parent()[0].offsetTop;
       }
     });
   }
   $("#wh_topic_toc .scroll-content").stop(true).animate(
       {scrollTop: (newCurrentOffset - 10) + 'px'},
       300,
       "linear"
   );
 });

 // раскрытие при нажатии
 $("#wh_topic_toc a").on("click", function(){
   let parent = $(this).parent();
   if(!parent.hasClass("opened-node")){
     parent.find(".new-right-side-menu__title-marker_child").trigger("click");
   }
 })

 $('body').on('click', '#wh_topic_toc *[data-tocid]', function () {
   let tocMenu = $(this);
   $([document.documentElement, document.body]).animate({
     scrollTop: $('#' + tocMenu.attr('data-tocid')).offset().top - 100
   }, 400, function () {
     //хак для принудительной подсветки выбранного элемента после завершения действия прокрутки после нажатия на элемент меню
     setTimeout(function(){
       $('#wh_topic_toc li a').removeClass('current_node');
       $(".section-item, .topic-item").removeClass('current_node');
       tocMenu.addClass('current_node');
       tocMenu.parents(".section-item, .topic-item").addClass('current_node');
     },10);
   });
 });

 $(window).bind("load", function () {
   if (window.location.hash !== '') {
     $('#wh_topic_toc *[data-tocid=' + window.location.hash.substr(1) + ']').click();
   }
 });