var sectionArray = [1, 2, 3, 6];

$(document).scroll(function() {
    $.each(sectionArray, function(index, value) {
        var $section = $('#section_' + value);
        if ($section.length) {  // Ensure section exists
            var offsetSection = $section.offset().top - 83;
            var docScroll = $(document).scrollTop();
            var docScroll1 = docScroll + 1;

            // Check if current scroll position is within the current section
            if (docScroll1 >= offsetSection) {
                // Remove 'active' from all nav-links
                $('.navbar-nav .nav-item .nav-link').removeClass('active');
                $('.navbar-nav .nav-item .nav-link').addClass('inactive');

                // Add 'active' to the current nav-link
                $('.navbar-nav .nav-item .nav-link').eq(index).addClass('active');
                $('.navbar-nav .nav-item .nav-link').eq(index).removeClass('inactive');

                // Ensure Contact Us retains custom-btn class
                if (value === 6) {
                    $('.navbar-nav .nav-item .nav-link').eq(index).addClass('custom-btn');
                }
            }
        }
    });
});

// Smooth scroll behavior when nav links are clicked
$.each(sectionArray, function(index, value) {
    $('.click-scroll').eq(index).click(function(e) {
        var $section = $('#section_' + value);
        if ($section.length) {  // Ensure section exists
            var offsetClick = $section.offset().top - 83;
            e.preventDefault();
            $('html, body').animate({
                'scrollTop': offsetClick
            }, 300);
        }
    });
});

$(document).ready(function() {
    // Set the initial state
    $('.navbar-nav .nav-item .nav-link').addClass('inactive');
    $('.navbar-nav .nav-item .nav-link').eq(0).addClass('active');
    $('.navbar-nav .nav-item .nav-link').eq(0).removeClass('inactive');

    // Ensure Contact Us always has the custom-btn class
    $('.navbar-nav .nav-item .nav-link[href="#section_6"]').addClass('custom-btn');
});
