jQuery(document).ready(function($) {
const customContainerId = '#sm-variation-desc';
const $customContainer = $(customContainerId);
const isVariableProduct = $('.variations_form').length > 0;
const originalContent = $customContainer.html();
// --- KEŠIRAJ SVE OPISE NA POČETKU ---
const variationDescriptions = {};
const $form = $('.variations_form');
const allVariations = $form.data('product_variations');
if (allVariations && Array.isArray(allVariations)) {
allVariations.forEach(v => {
// Sačuvaj description (ili prazan string ako nema)
variationDescriptions[v.variation_id] = v.variation_description || '';
});
}
// --- FUNKCIJA: Selektuj prvu DOSTUPNU varijaciju kao default ---
// Bira prvu varijaciju koja je na lageru (is_in_stock), pa primenjuje
// SVE njene atribute odjednom. Tako nikad ne završimo u nemogućoj
// kombinaciji koja kvari greying script.
function selectFirstVariation() {
if (!Array.isArray(allVariations) || !allVariations.length) return;
// Nađi prvu IN-STOCK varijaciju; fallback na prvu uopšte ako nijedna nije na lageru
const target = allVariations.find(v => v && v.is_in_stock && v.attributes) || allVariations[0];
if (!target || !target.attributes) return;
Object.keys(target.attributes).forEach(attrName => {
const val = target.attributes[attrName];
if (!val) return;
const $select = $form.find('select[name="' + attrName + '"]');
if ($select.length && !$select.val()) {
$select.val(val).trigger('change');
}
});
}
// --- UPDATE SOCIAL SHARE LINKS ---
function updateSocialLinks() {
const newUrl = window.location.href;
const baseUrl = window.location.origin + window.location.pathname;
$('.wd-social-icon').each(function() {
let $btn = $(this);
let originalHref = $btn.attr('href');
if (!originalHref) return;
if (!$btn.data('original-share-url')) {
$btn.data('original-share-url', originalHref);
}
let templateHref = $btn.data('original-share-url');
let finalHref = templateHref
.replace(encodeURIComponent(baseUrl), encodeURIComponent(newUrl))
.replace(baseUrl, newUrl);
$btn.attr('href', finalHref);
});
}
// --- UPDATE URL ---
function updateBrowserUrl() {
const currentUrl = new URL(window.location);
let needsUpdate = false;
$('.variations_form select').each(function() {
const key = $(this).attr('name');
const val = $(this).val();
if (!key || !key.startsWith('attribute_')) return;
const currentUrlVal = currentUrl.searchParams.get(key);
if (val && val !== currentUrlVal) {
currentUrl.searchParams.set(key, val);
needsUpdate = true;
}
else if (!val && currentUrl.searchParams.has(key)) {
currentUrl.searchParams.delete(key);
needsUpdate = true;
}
});
if (needsUpdate) {
window.history.replaceState({}, '', currentUrl);
updateSocialLinks();
}
}
// --- EVENT 1: Variation Found ---
$(document).on('found_variation', '.variations_form', function(event, variation) {
// Uzmi description iz KEŠA (ne iz DOM-a!)
const descHtml = variationDescriptions[variation.variation_id];
if (descHtml && descHtml.trim() !== '') {
$customContainer.html('
' + descHtml + '
').show();
} else {
// Varijacija NEMA description - prikaži originalni sadržaj
$customContainer.html(originalContent).show();
}
});
// --- EVENT 2: Variation Reset ---
$(document).on('reset_variation', '.variations_form', function() {
$customContainer.html(originalContent);
if (isVariableProduct) {
$customContainer.hide();
} else {
$customContainer.show();
}
const hasValues = $('.variations_form select').filter((i, el) => $(el).val()).length > 0;
if (!hasValues) {
const currentUrl = new URL(window.location);
let changed = false;
Array.from(currentUrl.searchParams.keys()).forEach(key => {
if (key.startsWith('attribute_')) {
currentUrl.searchParams.delete(key);
changed = true;
}
});
if (changed) {
window.history.replaceState({}, '', currentUrl);
updateSocialLinks();
}
}
});
// --- EVENT 3: User Interaction ---
$(document).on('change', '.variations_form select', function() {
updateBrowserUrl();
});
// --- ON LOAD ---
setTimeout(function() {
$('.wd-social-icon').each(function() {
if (!$(this).data('original-share-url')) {
$(this).data('original-share-url', $(this).attr('href'));
}
});
const urlParams = new URLSearchParams(window.location.search);
const hasDeepLink = Array.from(urlParams.keys()).some(k => k.startsWith('attribute_'));
var noPreselectBooster = document.body.className.match(/(^|s)postid-(430891|431960|432054)(?=s|$)/);
if (!hasDeepLink && !noPreselectBooster) {
// AUTO-SELECT PRVA DOSTUPNA VARIJACIJA
selectFirstVariation();
} else {
updateSocialLinks();
}
}, 500);
// --- FIX: For non-variable products ---
if (!isVariableProduct) {
$('head').append('');
}
});