/* ============================================
   Sterling & Associates Law Firm Template
   Custom JavaScript
   ============================================ */

(function() {
    'use strict';

    // Update current year in footer
    document.addEventListener('DOMContentLoaded', function() {
        var yearElement = document.getElementById('current-year');
        if (yearElement) {
            yearElement.textContent = new Date().getFullYear();
        }
    });

    // Smooth scroll for anchor links
    document.querySelectorAll('a[href^="#"]').forEach(function(anchor) {
        anchor.addEventListener('click', function(e) {
            var targetId = this.getAttribute('href');
            if (targetId === '#') return;

            var targetElement = document.querySelector(targetId);
            if (targetElement) {
                e.preventDefault();
                var navbarHeight = document.querySelector('.rsl-navbar').offsetHeight || 0;
                var topBarHeight = document.querySelector('.top-bar')?.offsetHeight || 0;
                var offset = navbarHeight + topBarHeight + 20;

                window.scrollTo({
                    top: targetElement.offsetTop - offset,
                    behavior: 'smooth'
                });

                // Update focus for accessibility
                targetElement.setAttribute('tabindex', '-1');
                targetElement.focus();
            }
        });
    });

    // Form submission handler (demo)
    var consultationForm = document.getElementById('consultation-form');
    if (consultationForm) {
        consultationForm.addEventListener('submit', function(e) {
            e.preventDefault();

            // Get form data
            var formData = new FormData(this);
            var data = {};
            formData.forEach(function(value, key) {
                data[key] = value;
            });

            // Show success message (in production, send to server)
            alert('Thank you for your inquiry! Our team will contact you within 24 hours.');

            // Reset form
            this.reset();
        });
    }

    // Counter animation for trust badges
    function animateCounters() {
        var counters = document.querySelectorAll('[data-count-to]');

        counters.forEach(function(counter) {
            var target = parseInt(counter.getAttribute('data-count-to'), 10);
            var current = 0;
            var increment = target / 50;
            var duration = 2000;
            var stepTime = duration / 50;

            var timer = setInterval(function() {
                current += increment;
                if (current >= target) {
                    counter.textContent = target + '+';
                    clearInterval(timer);
                } else {
                    counter.textContent = Math.floor(current) + '+';
                }
            }, stepTime);
        });
    }

    // Intersection Observer for counter animation
    var trustSection = document.querySelector('.trust-section');
    if (trustSection) {
        var observer = new IntersectionObserver(function(entries) {
            entries.forEach(function(entry) {
                if (entry.isIntersecting) {
                    animateCounters();
                    observer.unobserve(entry.target);
                }
            });
        }, { threshold: 0.5 });

        observer.observe(trustSection);
    }

    // Navbar scroll effect
    var navbar = document.querySelector('.law-navbar');
    var topBar = document.querySelector('.top-bar');

    if (navbar && topBar) {
        window.addEventListener('scroll', function() {
            if (window.scrollY > 50) {
                navbar.classList.add('scrolled');
                topBar.style.transform = 'translateY(-100%)';
            } else {
                navbar.classList.remove('scrolled');
                topBar.style.transform = 'translateY(0)';
            }
        });
    }

    // Add scrolled style
    var style = document.createElement('style');
    style.textContent = `
        .top-bar {
            transition: transform 0.3s ease;
        }
        .law-navbar.scrolled {
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
        }
    `;
    document.head.appendChild(style);

})();
