/* Merchant Pro — front-end behaviour */

(function () {

  'use strict';



  /* ── Scroll reveal ── */

  const allReveal = document.querySelectorAll('.reveal,.reveal-left,.reveal-right');



  if (allReveal.length && 'IntersectionObserver' in window) {

    const ro = new IntersectionObserver((entries) => {

      entries.forEach((entry, index) => {

        if (entry.isIntersecting) {

          setTimeout(() => entry.target.classList.add('in'), index * 55);

          ro.unobserve(entry.target);

        }

      });

    }, { threshold: 0.08 });



    allReveal.forEach((el) => ro.observe(el));

  }



  /* ── Mobile Accordion Menu ── */

  const menu = document.getElementById('navMenu');



  if (menu) {

    menu.querySelectorAll('li > a').forEach((link) => {

      link.addEventListener('click', function (e) {



        if (window.innerWidth > 960) return;



        const mob = this.parentElement.querySelector('.mob-drop');



        if (!mob) return;



        e.preventDefault();



        const opening = !mob.classList.contains('show');



        menu.querySelectorAll('.mob-drop.show').forEach((el) => {

          el.classList.remove('show');

        });



        menu.querySelectorAll('a.mob-open').forEach((el) => {

          el.classList.remove('mob-open');

        });



        if (opening) {

          mob.classList.add('show');

          this.classList.add('mob-open');

        }

      });

    });

  }



  /* ── Back to top ── */

  const btt = document.getElementById('btt');



  if (btt) {

    window.addEventListener(

      'scroll',

      () => btt.classList.toggle('show', window.scrollY > 500),

      { passive: true }

    );

  }



  /* ── Front-page quote form ── */

  window.doSubmit = function () {

    const ids = ['ff', 'fl', 'fb', 'fp', 'fe'];

    let ok = true;



    ids.forEach((id) => {

      const el = document.getElementById(id);



      if (!el) return;



      el.style.borderColor = '';



      if (!el.value.trim()) {

        el.style.borderColor = '#FF4444';

        ok = false;

      }

    });



    if (!ok) return;



    const btn = document.querySelector('.fsubmit');



    if (btn) {

      btn.disabled = true;

      btn.style.background = '#00C060';

      btn.innerHTML = "✓ Sent! We'll be in touch within one business day.";

    }



    const okPanel = document.getElementById('formOk');



    if (okPanel) {

      okPanel.style.display = 'block';

    }

  };



  /* ── Calculator ── */

  function fmt(n) {

    return '$' + Math.round(n).toLocaleString();

  }



  window.calcUpdate = function () {

    const volSlider = document.getElementById('vol-slider');

    const rateSlider = document.getElementById('rate-slider');



    if (!volSlider || !rateSlider) return;



    const vol = Number(volSlider.value) || 0;

    const rate = Number(rateSlider.value) || 0;



    const volDisplay = document.getElementById('vol-display');

    const rateDisplay = document.getElementById('rate-display');

    const resCost = document.getElementById('res-cost');

    const resMp = document.getElementById('res-mp');

    const resAnnual = document.getElementById('res-annual');

    const calcPh = document.getElementById('calc-ph');

    const calcRb = document.getElementById('calc-rb');



    if (volDisplay) volDisplay.textContent = fmt(vol);

    if (rateDisplay) rateDisplay.textContent = rate.toFixed(2) + '%';



    const current = vol * rate / 100;

    const mp = vol * 0.0014;

    const annual = (current - mp) * 12;



    if (resCost) resCost.textContent = fmt(current);

    if (resMp) resMp.textContent = '~' + fmt(mp);

    if (resAnnual) resAnnual.textContent = fmt(annual);



    if (calcPh) calcPh.style.display = 'none';

    if (calcRb) calcRb.classList.add('active');

  };



  document.addEventListener('DOMContentLoaded', () => {

    calcUpdate();



    const volSlider = document.getElementById('vol-slider');

    const rateSlider = document.getElementById('rate-slider');



    if (volSlider) volSlider.addEventListener('input', calcUpdate);

    if (rateSlider) rateSlider.addEventListener('input', calcUpdate);

  });



})(); 