
nsec = {};

nsec.isFirstLoad = function(namesp) {
  var isFirst = namesp.firstLoad === undefined;
  namesp.firstLoad = false;
  return isFirst;
}

window.onload = function() {
  if (!nsec.isFirstLoad(nsec)) {
	  return;
  }
  var ents = document.querySelectorAll("entry.nsec>top-g>*:first-child");
  for (let i = 0; i < ents.length; i++) {
    var ent = ents[i];
    if (ent.closest("entry").getElementsByTagName("x-gs").length > 0) {
      ent.style.color = "#752a2a";
      ent.addEventListener("click", modeSwitch);
    }
  }
  var olis = document.querySelectorAll("entry.nsec ol>li.collapsible");
  for (let i = 0; i < olis.length; i++) {
    var oli = olis[i];
    oli.addEventListener("click", showLiExamples);
  }
  var drs = document.querySelectorAll("entry.nsec dr");
  for (let i = 0; i < drs.length; i++) {
    var dr = drs[i];
    if (dr.closest("dr-g-blk").getElementsByTagName("x-gs").length > 0) {
      dr.style.color = "#752a2a";
      dr.addEventListener("click", showDrExamples);
    }
  }
}

function modeSwitch() {
  var xgs = this.closest("entry").getElementsByTagName("x-gs");
  var flag = true;
  for (let i = 0; i < xgs.length; i++) {
    var xg = xgs[i];
    if (xg.style.display === "block") {
      xg.style.display = "none";
      flag = false;
    }
  }
  if (flag) {
    for (let i = 0; i < xgs.length; i++) {
      var xg = xgs[i];
      xg.style.display = "block";
    }
  }
}


function showLiExamples(event) {
  if (event.target.nodeName === "LI") {
    var xgs = this.getElementsByTagName("x-gs");
    for (let i = 0; i < xgs.length; i++) {
      var xg = xgs[i];
      if (xg.style.display !== "block") {
        xg.style.display = "block";
      } else {
        xg.style.display = "none";
      }
    }
  }
}


function showDrExamples() {
  var xgs = this.closest("dr-g-blk").getElementsByTagName("x-gs");
  for (let i = 0; i < xgs.length; i++) {
    var xg = xgs[i];
    if (xg.style.display !== "block") {
      xg.style.display = "block";
    } else {
      xg.style.display = "none";
    }
  } 
}





(function () {
    const oldOnload = window.onload || function(){};
    window.onload = function () {
        oldOnload();
        bindPosClick();
    };

    function bindPosClick() {
        // 所有词性标签绑定点击事件
        const posList = document.querySelectorAll("entry.nsec pos");
        posList.forEach(posEl => {
            posEl.addEventListener("click", function () {
                // 获取当前词条根节点
                const entry = this.closest("entry");
                // 切换类名，控制eng显示/隐藏
                entry.classList.toggle("show-eng");
            });
        });
    }
})();
