/* ============================================================
   Globinex Research — pages.js
   Generic animated network background for inner pages.
   Runs alongside custom.js (which handles navbar, scroll-reveal,
   count-ups). Initializes every <canvas data-net> element.
   ============================================================ */
(function () {
  "use strict";

  function initNet(canvas) {
    var ctx = canvas.getContext("2d");
    var color = canvas.getAttribute("data-net-color") || "0,135,62";
    var linkColor = canvas.getAttribute("data-net-link") || color;
    var density = parseFloat(canvas.getAttribute("data-net-density")) || 26000;
    var linkDist = parseFloat(canvas.getAttribute("data-net-dist")) || 150;
    var DPR = Math.min(window.devicePixelRatio || 1, 2);
    var pts = [], W = 0, H = 0, running = false, raf = null;

    function resize() {
      var r = canvas.getBoundingClientRect();
      W = r.width; H = r.height;
      if (!W || !H) return;
      canvas.width = W * DPR; canvas.height = H * DPR;
      ctx.setTransform(DPR, 0, 0, DPR, 0, 0);
      build();
    }
    function build() {
      pts = [];
      var count = Math.max(24, Math.round((W * H) / density));
      for (var i = 0; i < count; i++) {
        pts.push({
          x: Math.random() * W, y: Math.random() * H,
          vx: (Math.random() - 0.5) * 0.35,
          vy: (Math.random() - 0.5) * 0.35,
          r: 1.3 + Math.random() * 2.1
        });
      }
    }
    function frame() {
      ctx.clearRect(0, 0, W, H);
      for (var i = 0; i < pts.length; i++) {
        var p = pts[i];
        p.x += p.vx; p.y += p.vy;
        if (p.x < 0 || p.x > W) p.vx *= -1;
        if (p.y < 0 || p.y > H) p.vy *= -1;
      }
      for (var a = 0; a < pts.length; a++) {
        for (var b = a + 1; b < pts.length; b++) {
          var dx = pts[a].x - pts[b].x, dy = pts[a].y - pts[b].y;
          var d = Math.sqrt(dx * dx + dy * dy);
          if (d < linkDist) {
            ctx.strokeStyle = "rgba(" + linkColor + "," + (1 - d / linkDist) * 0.16 + ")";
            ctx.lineWidth = 1;
            ctx.beginPath();
            ctx.moveTo(pts[a].x, pts[a].y);
            ctx.lineTo(pts[b].x, pts[b].y);
            ctx.stroke();
          }
        }
      }
      for (var k = 0; k < pts.length; k++) {
        ctx.beginPath();
        ctx.fillStyle = "rgba(" + color + ",0.32)";
        ctx.arc(pts[k].x, pts[k].y, pts[k].r, 0, Math.PI * 2);
        ctx.fill();
      }
      raf = requestAnimationFrame(frame);
    }
    var io = new IntersectionObserver(function (entries) {
      entries.forEach(function (e) {
        if (e.isIntersecting && !running) { running = true; raf = requestAnimationFrame(frame); }
        else if (!e.isIntersecting && running) { running = false; cancelAnimationFrame(raf); }
      });
    }, { threshold: 0 });
    window.addEventListener("resize", resize);
    resize();
    io.observe(canvas);
  }

  document.querySelectorAll("canvas[data-net]").forEach(initNet);

  /* ---------- Counter fallback (in case custom.js order differs) ---------- */
  // custom.js already animates [data-count]; nothing else needed here.

  /* ---------- Mobile nav: close on link click ---------- */
  document.querySelectorAll("#gxMenu .nav-link").forEach(function (l) {
    l.addEventListener("click", function () {
      var menu = document.getElementById("gxMenu");
      if (menu && menu.classList.contains("show")) {
        try { bootstrap.Collapse.getOrCreateInstance(menu).hide(); } catch (e) {}
      }
    });
  });
})();
