

// <!-- blog page section start -->
document.addEventListener("DOMContentLoaded", function(){
  const searchInput = document.getElementById("searchInput");

// 🔥 ALL DATA (you can expand anytime)
const searchData = [
    {title:"Drilling Equipment", link:"product.html"},
    {title:"Oil & Gas Conference", link:"blog1.html"},
    {title:"Rigless Technology", link:"services.html"},
    {title:"Energy Solutions", link:"services.html"},
    {title:"Industrial Work", link:"blog2.html"},
    {title:"Project Completion", link:"blog3.html"},
    {title:"About Us", link:"about.html"},
    {title:"Contact Page", link:"contact.html"}
];

// RESULT BOX
let resultBox = document.createElement("div");
resultBox.style.background="#fff";
resultBox.style.border="1px solid #ccc";
resultBox.style.position="absolute";
resultBox.style.width="100%";
resultBox.style.zIndex="999";
searchInput.parentNode.appendChild(resultBox);

// SEARCH EVENT
searchInput.addEventListener("keyup", function(){
    let value = this.value.toLowerCase();
    resultBox.innerHTML = "";

    if(value === "") return;

    let results = searchData.filter(item =>
        item.title.toLowerCase().includes(value)
    );

    results.forEach(item=>{
        let div = document.createElement("div");
        div.style.padding="10px";
        div.style.cursor="pointer";
        div.innerText = item.title;

        div.onclick = () => {
            window.location.href = item.link;
        };

        div.onmouseover = ()=> div.style.background="#f1f1f1";
        div.onmouseout = ()=> div.style.background="#fff";

        resultBox.appendChild(div);
    });
});


  });

// <!-- blog page section End -->

// <!-- R and d page our impact start  -->
document.addEventListener("DOMContentLoaded", function(){

const counters = document.querySelectorAll(".r-d-our-impact-number");
let started = false;

function startCounter(){
  counters.forEach(counter => {
    const target = +counter.getAttribute("data-target");
    let count = 0;
    const increment = target / 100;

    function update(){
      count += increment;
      if(count < target){
        counter.innerText = format(count);
        requestAnimationFrame(update);
      }else{
        counter.innerText = format(target);
      }
    }

    update();
  });
}

function format(num){
  if(num >= 1000){
    return "+" + Math.floor(num/1000) + "K";
  }
  return Math.floor(num);
}

window.addEventListener("scroll", () => {
  const section = document.querySelector(".r-d-our-impact-section");
  const trigger = section.offsetTop - window.innerHeight + 100;

  if(window.scrollY > trigger && !started){
    startCounter();
    started = true;
  }
});


/* VIDEO BUTTON */
const btn = document.querySelector(".r-d-our-impact-play");
const video = document.querySelector(".r-d-our-impact-video");

btn.addEventListener("click", () => {
  if(video.paused){
    video.play();
    btn.style.opacity = "0";
  }else{
    video.pause();
    btn.style.opacity = "1";
  }
});

});
// <!-- R and d page our impact End  -->

/* .drill-rig-explore-more section Start */

document.addEventListener("DOMContentLoaded", function(){

  const swiper = new Swiper(".drill-rig-explore-more-slider", {
    slidesPerView: 2,
    spaceBetween: 30,
    loop: true,

    navigation: {
      nextEl: ".next",
      prevEl: ".prev",
    },

    breakpoints:{
      0:{
        slidesPerView:1
      },
      768:{
        slidesPerView:1.5
      },
      1024:{
        slidesPerView:2
      }
    }

  });

});
/* .drill-rig-explore-more section End */


// <!-- home Map section Start -->

document.addEventListener("DOMContentLoaded", function(){

  new Swiper(".home-world-map-swiper", {
    slidesPerView: 4,
    spaceBetween: 20,
    centeredSlides: true,
    loop: true,

    navigation: {
      nextEl: ".home-world-map-next",
      prevEl: ".home-world-map-prev",
    },

    breakpoints:{
      0:{ slidesPerView:2 },
      480:{ slidesPerView:3 },
      768:{ slidesPerView:4 },
      1024:{ slidesPerView:5 }
    }

  });

});


// <!-- home  Map section End -->

  /* Footer page  start */


    /* Footer page  End */
    /* Home Our Products & Services section start */

    /* Home Our Products & Services section End */

    /* home why choose us section Start */ 

    /* home why choose us section End */ 


/* home brand swipe section Start */ 
document.addEventListener("DOMContentLoaded", function () {

    new Swiper(".brand-home-swiper", {
        loop:true,
        slidesPerView:6,
        spaceBetween:80,
        speed:4500,

        autoplay:{
            delay:0,
            disableOnInteraction:false
        },

        allowTouchMove:false,

        breakpoints:{
            0:{ slidesPerView:2, spaceBetween:30 },
            768:{ slidesPerView:4 },
            1024:{ slidesPerView:6 }
        }
    });

});
/* home brand swipe section end */ 


document.addEventListener("DOMContentLoaded", function () {

  const btn = document.getElementById("menuBtn");
  const menu = document.getElementById("mobileMenu");

  // EXISTING listener
  btn.addEventListener("click", function (e) {
    e.stopPropagation();
    menu.classList.toggle("active");
    btn.classList.toggle("active");
  });

  // ✅ ADD THIS RIGHT HERE - backup listener
  document.querySelector('.right-section').addEventListener('click', function(e) {
    if (e.target.closest('#menuBtn') || e.target.closest('.menu-toggle')) {
      menu.classList.toggle('active');
      btn.classList.toggle('active');
    }
  });

  // Close mobile menu when a non-trigger link is clicked
  menu.querySelectorAll("a").forEach(function(link) {
    link.addEventListener("click", function() {
      menu.classList.remove("active");
      btn.classList.remove("active");
    });
  });

  // Toggle submenu
  window.toggleSubmenu = function (id) {
    const sub = document.getElementById(id);
    sub.classList.toggle("open");
  };

  // Market ticker
  function updateMarket(id) {
    const priceEl = document.querySelector("#" + id + " .price");
    const arrowEl = document.querySelector("#" + id + " .arrow");
    const changeEl = document.querySelector("#" + id + " .change");
    const change = (Math.random() * 1).toFixed(2);
    const direction = Math.random() > 0.5 ? "up" : "down";
    let price = parseFloat(priceEl.innerText.replace("$", ""));
    if (direction === "up") {
      price += parseFloat(change);
      arrowEl.innerHTML = "▲";
      arrowEl.className = "arrow up";
      changeEl.className = "change up";
      changeEl.innerHTML = "+" + change;
    } else {
      price -= parseFloat(change);
      arrowEl.innerHTML = "▼";
      arrowEl.className = "arrow down";
      changeEl.className = "change down";
      changeEl.innerHTML = "-" + change;
    }
    priceEl.innerHTML = "$" + price.toFixed(2);
  }

  setInterval(() => {
    updateMarket("brent");
    updateMarket("gas");
  }, 4000);

});
