document.addEventListener("DOMContentLoaded", function() {
function attachChfLogic(chfId, totalId, textId, switchId) {
const chfSpan = document.getElementById(chfId);
const totalSpan = document.getElementById(totalId);
const normalText = document.getElementById(textId);
const switchText = document.getElementById(switchId);
if (!chfSpan || !totalSpan || !normalText || !switchText) return;
function updateChfValue() {
const totalValue = parseFloat(totalSpan.textContent.replace(/[^\d.]/g, "")) || 0;
let newChfValue = 250 - totalValue;
if (newChfValue < 0) newChfValue = 0;
chfSpan.textContent = newChfValue.toFixed(2);
// Toggle text visibility
if (totalValue >= 250) {
normalText.style.display = "none";
switchText.style.display = "block";
} else {
normalText.style.display = "block";
switchText.style.display = "none";
}
console.log(`[${chfId}] total=${totalValue} -> chf=${newChfValue}`);
}
updateChfValue();
const observer = new MutationObserver(updateChfValue);
observer.observe(totalSpan, { childList: true, characterData: true, subtree: true });
}
// Proveri screen širinu
if (window.innerWidth > 768) {
// Desktop
attachChfLogic("chf-span", "total-span", "shipping-text", "shipping-text-switch");
} else {
// Telefon
attachChfLogic("chf-span-1", "total-span-1", "shipping-text-1", "shipping-text-1-switch");
}
});