$(document).ready(function () {
  const mahamudfutureit_backToTopBtn = document.getElementById("mahamudfutureit_backToTop");
  window.addEventListener("scroll", () => {
    if (window.scrollY > 200) {
      mahamudfutureit_backToTopBtn.style.display = "block";
    } else {
      mahamudfutureit_backToTopBtn.style.display = "none";
    }
  });
  mahamudfutureit_backToTopBtn.addEventListener("click", () => {
    window.scrollTo({
      top: 0,
      behavior: "smooth"
    });
  });

    $('.main-menu-open').click(function() {
      var $overlay = $('.off-canvas-overlay');

      if ($overlay.css('top') === '0px') {
        $overlay.animate({ top: '-100%' }, 500);
        $('.display-overlay').removeClass('active');
      } else {
        $overlay.animate({ top: '0px' }, 500);
        $('.display-overlay').addClass('active').animate({ opacity: 1 }, 500);

      }
    });

  // User Popup Menu Control
  $(".action_userpop").click(function(e){
      $(".profile-nav-pop").show();
       e.stopPropagation();
  });
  $(".profile-nav-pop").click(function(e){
      e.stopPropagation();
  });
  $(document).click(function(){
      $(".profile-nav-pop").hide();
  });

  // TSticky Article Sidebar
  $(window).scroll(function () {
      if ($(this).scrollTop() > 50) {
          $('#leftNav').addClass('');
          $('#RightNav').addClass('');
      } else {
          $('#leftNav').removeClass('header-scrolled');
          $('#RightNav').removeClass('topbar-scrolled');
      }
  });

});


$(document).ready(function() {
  $(document).on('click', '.mahamudfutureit_poll_download', function() {
    // Populate the #downloadPoll div with poll data
    $('#downloadPoll .poll-date').text($(this).data('poll-date'));
    $('#downloadPoll .poll-image').attr('src', $(this).data('poll-image'));
    $('#downloadPoll .poll-title').text($(this).data('poll-title'));
    $('#downloadPoll .poll-option1-text').text($(this).data('poll-option1'));
    $('#downloadPoll .poll-option1-vote').text($(this).data('poll-option1-vote'));
    $('#downloadPoll .poll-option1-bar').css('width', $(this).data('poll-option1-vote-per'));
    $('#downloadPoll .poll-option2-text').text($(this).data('poll-option2'));
    $('#downloadPoll .poll-option2-vote').text($(this).data('poll-option2-vote'));
    $('#downloadPoll .poll-option2-bar').css('width', $(this).data('poll-option2-vote-per'));
    $('#downloadPoll .poll-option3-text').text($(this).data('poll-option3'));
    $('#downloadPoll .poll-option3-vote').text($(this).data('poll-option3-vote'));
    $('#downloadPoll .poll-option3-bar').css('width', $(this).data('poll-option3-vote-per'));
    $('#downloadPoll .poll-total-votes').text($(this).data('poll-total-votes'));
    $('#downloadPoll .poll-download-date').text($(this).data('poll-download'));

    // Show the #downloadPoll div
    $('#downloadPoll').fadeIn();

    // Capture the screenshot after a brief delay to ensure the content is rendered
    setTimeout(function() {
      html2canvas(document.querySelector("#downloadPoll"), { scale: 4 }).then(function(canvas) {
        var link = document.createElement('a');
        link.download = 'POLL_MAHAMUDFUTUREIT.COM_' + Date.now() + '.png';
        link.href = canvas.toDataURL('image/png', 1.0); // max quality
        link.click();
      });
    }, 500);
  });
});

window.addEventListener('scroll', function () {
    let navbar = document.getElementById('pcNavbar');
    if (window.scrollY > 50) {
        navbar.classList.add('sticky-nav'); // Add class when scrolled
    } else {
        navbar.classList.remove('sticky-nav'); // Remove class when at top
    }
});

window.addEventListener('scroll', function () {
    let mobilenavbar = document.getElementById('mobileNavbar');
    if (window.scrollY > 50) {
        mobilenavbar.classList.add('sticky-nav'); // Add class when scrolled
    } else {
        mobilenavbar.classList.remove('sticky-nav'); // Remove class when at top
    }
});
document.addEventListener('DOMContentLoaded', function () {
    const themeIcons = document.querySelectorAll('.theme-toggle');
    let themePreference = localStorage.getItem('themePreference') || 'light';

    function applyTheme(theme) {
        document.documentElement.setAttribute('data-theme', theme);
        setIconTheme(theme);
        switchImages(theme);
    }

    function setIconTheme(theme) {
        const iconClass = theme === 'dark' ? 'bi-brightness-high': 'bi-moon-fill'   ;
        themeIcons.forEach(icon => {
            icon.classList.remove('bi-brightness-high' ,'bi-moon-fill'  );
            icon.classList.add(iconClass);
        });
    }

    function switchImages(theme) {
        document.querySelectorAll('.image-switchable').forEach(img => {
            const newSrc = theme === 'dark' ? img.getAttribute('data-image-dark') : img.getAttribute('data-image-light');
            if (newSrc) img.src = newSrc;
        });
    }

    // Initial theme apply
    applyTheme(themePreference);

    // Toggle between light and dark
    themeIcons.forEach(icon => {
        icon.addEventListener('click', function () {
            themePreference = themePreference === 'light' ? 'dark' : 'light';
            localStorage.setItem('themePreference', themePreference);
            applyTheme(themePreference);
        });
    });
});


document.addEventListener("DOMContentLoaded", function () {
    var lazyImages = document.querySelectorAll("img[data-src]");

    if ("IntersectionObserver" in window) {
        var lazyImageObserver = new IntersectionObserver(function (entries, observer) {
            entries.forEach(function (entry) {
                if (entry.isIntersecting) {
                    var lazyImage = entry.target;
                    lazyImage.src = lazyImage.dataset.src;
                    lazyImageObserver.unobserve(lazyImage);
                }
            });
        });

        lazyImages.forEach(function (lazyImage) {
            lazyImageObserver.observe(lazyImage);
        });
    } else {
        lazyImages.forEach(function (lazyImage) {
            lazyImage.src = lazyImage.dataset.src;
        });
    }
});

function redirectToPage(select) {
    let url = select.value;
    if (url) {
        window.location.href = url;
    }
}

(function () {
'use strict'

// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.querySelectorAll('.needs-validation')

// Loop over them and prevent submission
Array.prototype.slice.call(forms)
  .forEach(function (form) {
    form.addEventListener('submit', function (event) {
      if (!form.checkValidity()) {
        event.preventDefault()
        event.stopPropagation()
      }

      form.classList.add('was-validated')
    }, false)
  })
})();
document.addEventListener("DOMContentLoaded", function () {
    document.querySelectorAll(".copy_tt_link").forEach(function (btn) {
        btn.addEventListener("click", function () {
            let ttLink = this.getAttribute("data-tt-link"); // Get data-tt-link value
            let icon = this.querySelector("i"); // Find the <i> tag inside the button

            // Copy to clipboard
            navigator.clipboard.writeText(ttLink).then(() => {
                // Change the icon to clipboard-check
                icon.classList.replace("bi-clipboard", "bi-clipboard-check");

                // Revert back to clipboard after 2 seconds
                setTimeout(() => {
                    icon.classList.replace("bi-clipboard-check", "bi-clipboard");
                }, 2000);
            }).catch(err => {
                console.error("Failed to copy:", err);
            });
        });
    });
});
// Initial font size
let currentFontSize = 16;
const minFontSize = 12;
const maxFontSize = 24;

// Function to apply font size to all paragraphs
function applyFontSize() {
  document.querySelectorAll('.story-details p').forEach(p => {
    p.style.fontSize = currentFontSize + 'px';
  });
}

// Increase font size
function increaseFontSize() {
  if (currentFontSize < maxFontSize) {
    currentFontSize += 2;
    applyFontSize();
  }
}

// Decrease font size
function decreaseFontSize() {
  if (currentFontSize > minFontSize) {
    currentFontSize -= 2;
    applyFontSize();
  }
}

function lazyLoadImages() {
    var lazyImages = document.querySelectorAll("img[data-src]:not([data-loaded])");

    if ("IntersectionObserver" in window) {
        var lazyImageObserver = new IntersectionObserver(function(entries, observer) {
            entries.forEach(function(entry) {
                if (entry.isIntersecting) {
                    var lazyImage = entry.target;
                    lazyImage.src = lazyImage.dataset.src;
                    lazyImage.setAttribute("data-loaded", "true"); // prevent reprocessing
                    lazyImageObserver.unobserve(lazyImage);
                }
            });
        });

        lazyImages.forEach(function(lazyImage) {
            lazyImageObserver.observe(lazyImage);
        });
    } else {
        // Fallback
        lazyImages.forEach(function(lazyImage) {
            lazyImage.src = lazyImage.dataset.src;
            lazyImage.setAttribute("data-loaded", "true");
        });
    }
}
document.addEventListener("DOMContentLoaded", function () {
    lazyLoadImages();
});
