// RoadBuddy Main JavaScript class RoadBuddyApp { constructor() { this.init(); } init() { this.initializeScrollEffects(); this.initializeAnimations(); this.initializeFormHandlers(); } initializeScrollEffects() { // Navbar scroll effect window.addEventListener('scroll', () => { const navbar = document.querySelector('custom-navbar'); if (window.scrollY > 100) { navbar.classList.add('sticky'); } else { navbar.classList.remove('sticky'); } }); // Smooth scrolling for anchor links document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', function (e) { e.preventDefault(); const target = document.querySelector(this.getAttribute('href')); if (target) { target.scrollIntoView({ behavior: 'smooth', block: 'start' }); } }); }); } initializeAnimations() { // Intersection Observer for scroll animations const observerOptions = { threshold: 0.1, rootMargin: '0px 0px -50px 0px' }; const observer = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { entry.target.classList.add('fade-in'); } }); }, observerOptions); // Observe all sections for animation document.querySelectorAll('section').forEach(section => { observer.observe(section); }); } initializeFormHandlers() { // Contact form handler const contactForm = document.querySelector('.contact-form form'); if (contactForm) { contactForm.addEventListener('submit', (e) => { e.preventDefault(); this.handleFormSubmission(contactForm); }); } } handleFormSubmission(form) { const formData = new FormData(form); const data = Object.fromEntries(formData); // Show loading state const submitBtn = form.querySelector('button[type="submit"]'); const originalText = submitBtn.textContent; submitBtn.innerHTML = ' Sending...'; submitBtn.disabled = true; // Simulate API call setTimeout(() => { this.showNotification('Message sent successfully! We\'ll get back to you soon.'); form.reset(); submitBtn.textContent = originalText; submitBtn.disabled = false; }, 2000); } showNotification(message) { // Create notification element const notification = document.createElement('div'); notification.className = 'notification'; notification.innerHTML = ` ${message} `; document.body.appendChild(notification); // Remove notification after 3 seconds setTimeout(() => { notification.remove(); }, 3000); } // Simulate service partner matching findNearbyPartners(location) { return new Promise((resolve) => { setTimeout(() => { resolve([ { name: 'Raj Auto Services', rating: 4.8, distance: '1.2 km', eta: '12 min', services: ['Fuel Delivery', 'Tire Repair', 'Jumpstart'] }, { name: 'City Mechanics', rating: 4.6, distance: '0.8 km', eta: '8 min' } ]); }, 1000); } } // Emergency request simulation async requestEmergencyHelp(issueType, location) { try { const partners = await this.findNearbyPartners(location); return partners[0]; // Return closest partner } catch (error) { console.error('Error finding partners:', error); return null; } } } // Initialize the app when DOM is loaded document.addEventListener('DOMContentLoaded', () => { new RoadBuddyApp(); }); // Service Worker Registration for PWA functionality if ('serviceWorker' in navigator) { window.addEventListener('load', () => { navigator.serviceWorker.register('/sw.js') .then(registration => { console.log('SW registered: ', registration); }) .catch(registrationError => { console.log('SW registration failed: ', registrationError); }); } // Real-time location tracking class LocationTracker { constructor() { this.watchId = null; } startTracking(callback) { if (navigator.geolocation) { this.watchId = navigator.geolocation.watchPosition( (position) => { const location = { latitude: position.coords.latitude, longitude: position.coords.longitude }; callback(location); }, (error) => { console.error('Error getting location:', error); }, { enableHighAccuracy: true, timeout: 5000, maximumAge: 0 }); } } stopTracking() { if (this.watchId) { navigator.geolocation.clearWatch(this.watchId); } }); } } // Emergency SOS functionality class EmergencySOS { constructor() { this.isActive = false; } activate() { this.isActive = true; this.sendEmergencyAlert(); } sendEmergencyAlert() { // In a real app, this would send to backend console.log('Emergency SOS activated!'); // Show emergency UI this.showEmergencyUI(); } showEmergencyUI() { const sosOverlay = document.createElement('div'); sosOverlay.className = 'sos-overlay'; sosOverlay.innerHTML = `
Help is on the way! Sharing your location with nearby partners.'); document.body.appendChild(sosOverlay); } } // Export for module usage if needed if (typeof module !== 'undefined' && module.exports) { module.exports = { RoadBuddyApp, LocationTracker, EmergencySOS }; }