jQuery(document).ready(function ($) {

     // Search focus handler
  function news_current_affair_searchFocusHandler() {
    const searchFirstTab = $('.inner_searchbox input[type="search"]');
    const searchLastTab = $('button.search-close');

    $(".open-search").click(function(e) {
      e.preventDefault();
      e.stopPropagation();
      $('body').addClass("search-focus");
      searchFirstTab.focus();
    });

    $("button.search-close").click(function(e) {
      e.preventDefault();
      e.stopPropagation();
      $('body').removeClass("search-focus");
      $(".open-search").focus();
    });

    // Redirect last tab to first input
    searchLastTab.on('keydown', function(e) {
      if ($('body').hasClass('search-focus') && e.which === 9 && !e.shiftKey) {
        e.preventDefault();
        searchFirstTab.focus();
      }
    });

    // Redirect first shift+tab to last input
    searchFirstTab.on('keydown', function(e) {
      if ($('body').hasClass('search-focus') && e.which === 9 && e.shiftKey) {
        e.preventDefault();
        searchLastTab.focus();
      }
    });

    // Allow escape key to close menu
    $('.inner_searchbox').on('keyup', function(e) {
      if ($('body').hasClass('search-focus') && e.keyCode === 27) {
        $('body').removeClass('search-focus');
        searchLastTab.focus();
      }
    });
  }

  // Call the search focus handler
  news_current_affair_searchFocusHandler();

    // Preloader
    jQuery('#preloader').delay(1500).fadeOut();
    jQuery('#loader').delay(1500).fadeOut("slow");

    // Scroll to Top Button
    if ($('.go-to-top').length) {
        var scrollTrigger = $('body').position(); // px
        var goToTop = function () {
            var scrollTop = $(window).scrollTop();
            if (scrollTop > 150) {
                $('.footer-go-to-top').addClass('show');
            } else {
                $('.footer-go-to-top').removeClass('show');
            }
        };
        goToTop();
        $(window).on('scroll', function () {
            goToTop();
        });
        $('.go-to-top').on('click', function (e) {
            e.preventDefault();
            $('html, body').animate({
                scrollTop: scrollTrigger.top
            }, 700);
        });
    }

    // Mobile Menu Focus
    $(window).resize(news_current_affair_navTrapp);

    function news_current_affair_navTrapp() {
        var width = $(window).width();
        if (width < 992) {
            $('.main-navigation').on('keydown', function (e) {
                if ($('.main-navigation').hasClass('toggled')) {
                    var focusableEls = $('.main-navigation a[href]:not([disabled]), .main-navigation button');
                    var firstFocusableEl = focusableEls[0];
                    var lastFocusableEl = focusableEls[focusableEls.length - 1];
                    var KEYCODE_TAB = 9;
                    var KEYCODE_ESC = 27;
                    if (e.key === 'Tab' || e.keyCode === KEYCODE_TAB) {
                        if (e.shiftKey) { // shift + tab
                            if (document.activeElement === firstFocusableEl) {
                                lastFocusableEl.focus();
                                e.preventDefault();
                            }
                        } else { // tab
                            if (document.activeElement === lastFocusableEl) {
                                firstFocusableEl.focus();
                                e.preventDefault();
                            }
                        }
                    }
                    if (e.keyCode === KEYCODE_ESC) {
                        $('.main-navigation').removeClass('toggled');
                        return;
                    }
                }
            });
        }
    }

    news_current_affair_navTrapp();

});

// tab js
jQuery(document).ready(function () {
    jQuery( ".tablinks" ).first().addClass( "active" );
});

function news_current_affair_projetcs_tab(evt, cityName) {
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  jQuery('#'+ cityName).show()
  evt.currentTarget.className += " active";
}

jQuery(document).ready(function () {
    jQuery('.tabcontent').hide();
    jQuery('.tabcontent:first').show();
});

// toggle topbar js
jQuery(function($) {
  function news_current_affair_accountDropdownHandler() {
    const toggleBtn = $('#account-toggle');
    const dropdown = $('#account-dropdown');
    const firstLink = $('#account-dropdown a:first');
    const lastLink = $('#account-dropdown a:last');

    // Toggle dropdown on click or Enter/Space key
    toggleBtn.on('click keydown', function(e) {
      if (e.type === 'click' || (e.type === 'keydown' && (e.which === 13 || e.which === 32))) {
        e.preventDefault();
        const isOpen = toggleBtn.attr('aria-expanded') === 'true';

        toggleBtn.attr('aria-expanded', !isOpen);
        dropdown.prop('hidden', isOpen);
        if (!isOpen) {
          firstLink.focus();
        }
      }
    });

    // Focus loop
    lastLink.on('keydown', function(e) {
      if (!dropdown.prop('hidden') && e.which === 9 && !e.shiftKey) {
        e.preventDefault();
        toggleBtn.focus();
      }
    });

    firstLink.on('keydown', function(e) {
      if (!dropdown.prop('hidden') && e.which === 9 && e.shiftKey) {
        e.preventDefault();
        toggleBtn.focus();
      }
    });

    // ESC to close
    dropdown.on('keyup', function(e) {
      if (e.keyCode === 27 && !dropdown.prop('hidden')) {
        toggleBtn.attr('aria-expanded', 'false');
        dropdown.prop('hidden', true);
        toggleBtn.focus();
      }
    });

    // Click outside to close
    $(document).on('click', function(e) {
      if (!dropdown.prop('hidden') && !$(e.target).closest('.topbar-menu').length) {
        toggleBtn.attr('aria-expanded', 'false');
        dropdown.prop('hidden', true);
        toggleBtn.focus();
      }
    });
  }

  // Initialize
  news_current_affair_accountDropdownHandler();
});

// share topbar js
jQuery(function($) {
  function news_current_affair_shareDropdownHandler() {
    const shareToggle = $('.share-box');
    const shareBox = $('.share-options');
    const firstTab = $('.share-options a:first');
    const lastTab = $('.share-options a:last');

    // Toggle dropdown on click or keyboard (Enter/Space)
    shareToggle.on('click keydown', function(e) {
      if (e.type === 'click' || (e.type === 'keydown' && (e.which === 13 || e.which === 32))) {
        e.preventDefault();
        e.stopPropagation();

        if ($('body').hasClass('share-open')) {
          $('body').removeClass('share-open');
          shareBox.hide();
        } else {
          $('body').addClass('share-open');
          shareBox.show();
          firstTab.focus();
        }
      }
    });

    // Focus loop
    lastTab.on('keydown', function(e) {
      if ($('body').hasClass('share-open') && e.which === 9 && !e.shiftKey) {
        e.preventDefault();
        shareToggle.focus();
      }
    });

    firstTab.on('keydown', function(e) {
      if ($('body').hasClass('share-open') && e.which === 9 && e.shiftKey) {
        e.preventDefault();
        shareToggle.focus();
      }
    });

    // ESC to close
    shareBox.on('keyup', function(e) {
      if (e.keyCode === 27 && $('body').hasClass('share-open')) {
        $('body').removeClass('share-open');
        shareBox.hide();
        shareToggle.focus();
      }
    });

    // Click outside to close
    $(document).on('click', function(e) {
      if ($('body').hasClass('share-open') && !$(e.target).closest('.share-icon').length) {
        $('body').removeClass('share-open');
        shareBox.hide();
        shareToggle.focus();
      }
    });
  }

  // Initialize
  news_current_affair_shareDropdownHandler();
});

document.addEventListener('DOMContentLoaded', function() {
  const header = document.querySelector('.sticky-header');
  if (header) { // Check if header exists
      window.addEventListener('scroll', function() {
      if (window.scrollY > 0) {
          header.classList.add('stickyon');
      } else {
          header.classList.remove('stickyon');
      }
      });
  }
});