// Tuning Calculator Logic
let calculatorForm, resultsContainer;
let selectedStage = 'stage1';
// Quick calculator elements
let quickPowerInput, quickEngineType, quickResultsContainer;
let quickCalculateBtn, stage1Result, stage2Result, customResult;
// Tuning multipliers for different stages and engine types
const tuningData = {
stage1: {
'petrol-na': { power: 0.15, torque: 0.20, fuel: -0.05, basePrice: 399 },
'petrol-turbo': { power: 0.25, torque: 0.30, fuel: -0.12, basePrice: 299 },
'diesel': { power: 0.30, torque: 0.35, fuel: -0.15, basePrice: 349 },
'hybrid': { power: 0.20, torque: 0.25, fuel: -0.08, basePrice: 449 }
},
stage2: {
'petrol-na': { power: 0.25, torque: 0.35, fuel: -0.08, basePrice: 699 },
'petrol-turbo': { power: 0.40, torque: 0.50, fuel: -0.05, basePrice: 599 },
'diesel': { power: 0.45, torque: 0.60, fuel: -0.10, basePrice: 649 },
'hybrid': { power: 0.35, torque: 0.45, fuel: -0.12, basePrice: 799 }
},
custom: {
'petrol-na': { power: 0.40, torque: 0.55, fuel: 0.05, basePrice: 1299 },
'petrol-turbo': { power: 0.60, torque: 0.80, fuel: 0.10, basePrice: 999 },
'diesel': { power: 0.65, torque: 0.85, fuel: 0.00, basePrice: 1199 },
'hybrid': { power: 0.50, torque: 0.70, fuel: -0.05, basePrice: 1499 }
}
};
// Brand multipliers (some brands are more expensive to tune)
const brandMultipliers = {
'porsche': 1.5,
'mercedes': 1.3,
'bmw': 1.2,
'audi': 1.1,
'volkswagen': 1.0,
'toyota': 0.9,
'ford': 0.9,
'peugeot': 0.85,
'citroen': 0.85,
'renault': 0.85,
'opel': 0.8,
'volvo': 1.1,
'other': 1.0
};
// Age multipliers (older cars might need more work)
const ageMultipliers = {
'2024': 1.0,
'2023': 1.0,
'2022': 1.0,
'2021': 1.05,
'2020': 1.1,
'2019': 1.1,
'2018': 1.15,
'2017': 1.2,
'2016': 1.25,
'2015': 1.3,
'older': 1.4
};
// Quick Calculator Functions
function handleQuickCalculate() {
const currentPower = parseInt(quickPowerInput.value);
const engineType = quickEngineType.value;
if (!currentPower || !engineType) {
alert('Vul beide velden in voor de berekening.');
return;
}
if (currentPower < 50 || currentPower > 1000) {
alert('Voer een realistisch vermogen in tussen 50 en 1000 PK.');
return;
}
// Calculate power gains for all stages
const stage1Data = tuningData.stage1[engineType];
const stage2Data = tuningData.stage2[engineType];
const customData = tuningData.custom[engineType];
if (stage1Data && stage2Data && customData) {
const stage1Gain = Math.round(currentPower * stage1Data.power);
const stage2Gain = Math.round(currentPower * stage2Data.power);
const customGain = Math.round(currentPower * customData.power);
// Update results
stage1Result.textContent = `+${stage1Gain} PK`;
stage2Result.textContent = `+${stage2Gain} PK`;
customResult.textContent = `+${customGain} PK`;
// Enable results with animation
quickResultsContainer.style.opacity = '1';
quickResultsContainer.style.transform = 'scale(1.02)';
setTimeout(() => {
quickResultsContainer.style.transform = 'scale(1)';
}, 200);
// Add some visual feedback
quickCalculateBtn.innerHTML = 'Berekend!';
quickCalculateBtn.classList.add('bg-green-500');
setTimeout(() => {
quickCalculateBtn.innerHTML = 'Bereken Direct';
quickCalculateBtn.classList.remove('bg-green-500');
}, 2000);
}
}
function handleQuickInputChange() {
// Reset results to default when inputs change
quickResultsContainer.style.opacity = '0.5';
const currentPower = parseInt(quickPowerInput.value) || 150;
const engineType = quickEngineType.value || 'petrol-turbo';
// Show default values based on current input
if (currentPower >= 50) {
const stage1Data = tuningData.stage1[engineType];
const stage2Data = tuningData.stage2[engineType];
const customData = tuningData.custom[engineType];
if (stage1Data && stage2Data && customData) {
const stage1Gain = Math.round(currentPower * stage1Data.power);
const stage2Gain = Math.round(currentPower * stage2Data.power);
const customGain = Math.round(currentPower * customData.power);
stage1Result.textContent = `+${stage1Gain} PK`;
stage2Result.textContent = `+${stage2Gain} PK`;
customResult.textContent = `+${customGain} PK`;
}
}
}
// Main Calculator Functions (existing)
function handleFormSubmit(e) {
e.preventDefault();
// Get form values
const brand = document.getElementById('car-brand').value;
const year = document.getElementById('car-year').value;
const engineType = document.getElementById('engine-type').value;
const currentPower = parseInt(document.getElementById('current-power').value);
const currentTorque = parseInt(document.getElementById('current-torque').value) || Math.round(currentPower * 1.4);
const fuelConsumption = parseFloat(document.getElementById('fuel-consumption').value) || 8.0;
// Validate required fields
if (!brand || !year || !engineType || !currentPower) {
alert('Vul alle verplichte velden in om de berekening uit te voeren.');
return;
}
// Get selected tuning stage
const stageRadios = document.querySelectorAll('input[name="tuning-stage"]');
for (const radio of stageRadios) {
if (radio.checked) {
selectedStage = radio.value;
break;
}
}
// Calculate results
calculateTuningResults(brand, year, engineType, currentPower, currentTorque, fuelConsumption, selectedStage);
}
function calculateTuningResults(brand, year, engineType, currentPower, currentTorque, fuelConsumption, stage) {
// Get tuning data for selected stage and engine type
const tuning = tuningData[stage][engineType];
if (!tuning) return;
// Calculate new power and torque
const powerGainPercent = Math.round(tuning.power * 100);
const torqueGainPercent = Math.round(tuning.torque * 100);
const fuelSavingPercent = Math.round(Math.abs(tuning.fuel) * 100);
const newPower = Math.round(currentPower * (1 + tuning.power));
const newTorque = Math.round(currentTorque * (1 + tuning.torque));
const newFuelConsumption = (fuelConsumption * (1 + tuning.fuel)).toFixed(1);
// Calculate price with brand and age multipliers
const brandMultiplier = brandMultipliers[brand] || 1.0;
const ageMultiplier = ageMultipliers[year] || 1.0;
const estimatedPrice = Math.round(tuning.basePrice * brandMultiplier * ageMultiplier);
// Update results display
updateResultsDisplay(
currentPower, newPower, powerGainPercent,
currentTorque, newTorque, torqueGainPercent,
fuelConsumption, newFuelConsumption, fuelSavingPercent,
estimatedPrice
);
// Enable and highlight results
resultsContainer.style.opacity = '1';
resultsContainer.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
function updateResultsDisplay(currentPower, newPower, powerGain, currentTorque, newTorque, torqueGain, currentFuel, newFuel, fuelSaving, price) {
// Update power section
document.getElementById('power-gain').textContent = `+${powerGain}%`;
document.getElementById('current-power-display').textContent = `${currentPower} PK`;
document.getElementById('new-power-display').textContent = `${newPower} PK`;
document.getElementById('power-bar').style.width = `${Math.min(powerGain, 100)}%`;
// Update torque section
document.getElementById('torque-gain').textContent = `+${torqueGain}%`;
document.getElementById('current-torque-display').textContent = `${currentTorque} Nm`;
document.getElementById('new-torque-display').textContent = `${newTorque} Nm`;
document.getElementById('torque-bar').style.width = `${Math.min(torqueGain, 100)}%`;
// Update fuel section
document.getElementById('fuel-saving').textContent = `-${fuelSaving}%`;
document.getElementById('current-fuel-display').textContent = `${currentFuel} L/100km`;
document.getElementById('new-fuel-display').textContent = `${newFuel} L/100km`;
document.getElementById('fuel-bar').style.width = `${Math.min(fuelSaving, 50)}%`;
// Update price
document.getElementById('estimated-price').textContent = `€${price}`;
// Add animation to bars
setTimeout(() => {
const bars = document.querySelectorAll('#results-container [id$="-bar"]');
bars.forEach(bar => {
bar.style.transition = 'width 1.5s ease-out';
});
}, 100);
}
function handleStageSelection() {
// Handle visual feedback for stage selection
const stageOptions = document.querySelectorAll('.tuning-stage-option');
const stageRadios = document.querySelectorAll('input[name="tuning-stage"]');
stageRadios.forEach((radio, index) => {
radio.addEventListener('change', function() {
// Reset all options
stageOptions.forEach(option => {
option.classList.remove('border-[var(--primary-color)]', 'border-[var(--accent2-color)]', 'border-[var(--accent4-color)]', 'bg-white/10');
option.classList.add('border-white/20', 'bg-white/5');
});
// Highlight selected option
if (this.checked) {
const option = stageOptions[index];
option.classList.remove('border-white/20', 'bg-white/5');
option.classList.add('bg-white/10');
if (this.value === 'stage1') {
option.classList.add('border-[var(--primary-color)]');
} else if (this.value === 'stage2') {
option.classList.add('border-[var(--accent2-color)]');
} else if (this.value === 'custom') {
option.classList.add('border-[var(--accent4-color)]');
}
}
});
});
// Set initial selection
const initialRadio = document.querySelector('input[name="tuning-stage"]:checked');
if (initialRadio) {
initialRadio.dispatchEvent(new Event('change'));
}
}
function init() {
// Initialize main calculator elements
calculatorForm = document.getElementById('tuning-calculator');
resultsContainer = document.getElementById('results-container');
// Initialize quick calculator elements
quickPowerInput = document.getElementById('quick-power-input');
quickEngineType = document.getElementById('quick-engine-type');
quickResultsContainer = document.getElementById('quick-results');
quickCalculateBtn = document.getElementById('quick-calculate-btn');
stage1Result = document.getElementById('stage1-result');
stage2Result = document.getElementById('stage2-result');
customResult = document.getElementById('custom-result');
// Quick calculator event listeners
if (quickCalculateBtn) {
quickCalculateBtn.addEventListener('click', handleQuickCalculate);
}
if (quickPowerInput && quickEngineType) {
quickPowerInput.addEventListener('input', handleQuickInputChange);
quickEngineType.addEventListener('change', handleQuickInputChange);
// Set initial default values
quickPowerInput.value = 150;
quickEngineType.value = 'petrol-turbo';
handleQuickInputChange();
}
// Main calculator initialization
if (calculatorForm) {
// Add form submit handler
calculatorForm.addEventListener('submit', handleFormSubmit);
// Handle stage selection visual feedback
handleStageSelection();
// Add input change handlers for real-time validation
const requiredInputs = calculatorForm.querySelectorAll('[required]');
requiredInputs.forEach(input => {
input.addEventListener('change', function() {
if (this.checkValidity()) {
this.style.borderColor = 'var(--accent2-color)';
} else {
this.style.borderColor = 'var(--accent-color)';
}
});
});
}
}
function teardown() {
// Clean up event listeners
if (calculatorForm) {
calculatorForm.removeEventListener('submit', handleFormSubmit);
}
if (quickCalculateBtn) {
quickCalculateBtn.removeEventListener('click', handleQuickCalculate);
}
if (quickPowerInput && quickEngineType) {
quickPowerInput.removeEventListener('input', handleQuickInputChange);
quickEngineType.removeEventListener('change', handleQuickInputChange);
}
const stageRadios = document.querySelectorAll('input[name="tuning-stage"]');
stageRadios.forEach(radio => {
radio.removeEventListener('change', handleStageSelection);
});
}
export { init, teardown };