jQuery(function($) {
    "use strict";

    // Keyboard loop function (Fix for ReferenceError)
    function finix_investment_firm_Keyboard_loop(nav) {
        if (!nav.length) return;

        var focusable = nav.find('a, button, input, select, textarea').filter(':visible');
        var first = focusable.first();
        var last = focusable.last();

        nav.on('keydown', function(e) {
            if (e.key === 'Tab') {
                if (e.shiftKey) {
                    if ($(document.activeElement).is(first)) {
                        e.preventDefault();
                        last.focus();
                    }
                } else {
                    if ($(document.activeElement).is(last)) {
                        e.preventDefault();
                        first.focus();
                    }
                }
            }
        });
    }

    // Scroll to top functionality
    $(window).on('scroll', function() {
        if ($(this).scrollTop() >= 50) {
            $('#return-to-top').fadeIn(200);
        } else {
            $('#return-to-top').fadeOut(200);
        }
    });

    $('#return-to-top').on('click', function() {
        $('body,html').animate({ scrollTop: 0 }, 500);
    });

    // Side navigation toggle
    $('.gb_toggle').on('click', function() {
        var nav = $('.side_gb_nav');
        nav.toggleClass('active');

        // Call keyboard accessibility function
        finix_investment_firm_Keyboard_loop(nav);
    });

    // Preloader fade out
    setTimeout(function() {
        $(".loader").fadeOut("slow");
    }, 1000);

});

// Mobile responsive menu
function finix_investment_firm_menu_open_nav() {
    jQuery(".sidenav").addClass('open');
}

function finix_investment_firm_menu_close_nav() {
    jQuery(".sidenav").removeClass('open');
}

// Video Popup
(function($) {
  $(document).ready(function(){

    $('#openBtn').on('click', function(e) {
      e.preventDefault(); // stop normal link

      var videoUrl = $(this).data('video');
      if (!videoUrl) return;

      $('#videoFrame').attr('src', videoUrl);
      $('#videoOverlay').css('display', 'flex');
    });

    $('.close-btn, #videoOverlay').on('click', function(e) {
      // close when clicking on X or dark background
      if (e.target !== this && !$(e.target).closest('.close-btn').length) return;

      $('#videoOverlay').hide();
      $('#videoFrame').attr('src', ''); // stop video
    });

  });
})(jQuery);