File size: 5,937 Bytes
74685fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | // Main JavaScript file for Comboios de Portugal Replica
document.addEventListener('DOMContentLoaded', function() {
// Initialize Feather Icons
if (typeof feather !== 'undefined') {
feather.replace();
}
// Mobile menu toggle
const mobileMenuButton = document.getElementById('mobile-menu-button');
const mobileMenu = document.getElementById('mobile-menu');
if (mobileMenuButton && mobileMenu) {
mobileMenuButton.addEventListener('click', function() {
mobileMenu.classList.toggle('hidden');
mobileMenu.classList.toggle('mobile-menu-enter');
});
}
// Booking widget functionality
initializeBookingWidget();
// Smooth scroll 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'
});
}
});
});
// Sticky navigation on scroll
const nav = document.querySelector('nav');
let lastScroll = 0;
window.addEventListener('scroll', () => {
const currentScroll = window.pageYOffset;
if (currentScroll > 100) {
nav?.classList.add('shadow-md');
} else {
nav?.classList.remove('shadow-md');
}
lastScroll = currentScroll;
});
});
// Booking Widget Logic
function initializeBookingWidget() {
// Swap origin and destination
const swapButton = document.getElementById('swap-stations');
const originInput = document.getElementById('origin');
const destinationInput = document.getElementById('destination');
if (swapButton && originInput && destinationInput) {
swapButton.addEventListener('click', function() {
// Add rotation animation
this.classList.add('swap-animation');
// Swap values
const temp = originInput.value;
originInput.value = destinationInput.value;
destinationInput.value = temp;
// Trigger change events if needed
originInput.dispatchEvent(new Event('change'));
destinationInput.dispatchEvent(new Event('change'));
// Remove animation class after completion
setTimeout(() => {
this.classList.remove('swap-animation');
}, 300);
});
}
// Date validation - ensure return date is after departure
const departureDate = document.getElementById('departure-date');
const returnDate = document.getElementById('return-date');
if (departureDate && returnDate) {
// Set min date to today
const today = new Date().toISOString().split('T')[0];
departureDate.min = today;
returnDate.min = today;
departureDate.addEventListener('change', function() {
returnDate.min = this.value;
if (returnDate.value && returnDate.value < this.value) {
returnDate.value = this.value;
}
});
}
// Search form submission
const searchForm = document.getElementById('booking-form');
if (searchForm) {
searchForm.addEventListener('submit', function(e) {
e.preventDefault();
const searchButton = document.getElementById('search-button');
const originalText = searchButton.innerHTML;
// Show loading state
searchButton.disabled = true;
searchButton.innerHTML = '<i data-feather="loader" class="animate-spin mr-2"></i> A pesquisar...';
searchButton.classList.add('search-button-loading');
feather.replace();
// Simulate API call
setTimeout(() => {
searchButton.disabled = false;
searchButton.innerHTML = originalText;
searchButton.classList.remove('search-button-loading');
feather.replace();
// Show alert (in real app, would redirect to results)
alert('Pesquisa simulada: De ' + originInput.value + ' para ' + destinationInput.value);
}, 1500);
});
}
// Trip type toggle (Ida e volta / Só ida)
const tripTypeRadios = document.querySelectorAll('input[name="trip-type"]');
const returnDateContainer = document.getElementById('return-date-container');
if (tripTypeRadios.length && returnDateContainer) {
tripTypeRadios.forEach(radio => {
radio.addEventListener('change', function() {
if (this.value === 'one-way') {
returnDateContainer.classList.add('hidden');
returnDateContainer.classList.remove('block');
} else {
returnDateContainer.classList.remove('hidden');
returnDateContainer.classList.add('block');
}
});
});
}
}
// Utility function for debouncing
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Station autocomplete simulation
const stations = [
'Lisboa - Oriente',
'Lisboa - Santa Apolónia',
'Porto - Campanhã',
'Porto - São Bento',
'Braga',
'Aveiro',
'Coimbra',
'Faro',
'Évora',
'Guimarães'
];
function filterStations(query) {
return stations.filter(station =>
station.toLowerCase().includes(query.toLowerCase())
);
} |