/* =============================================
YarnMellow – vlastní skripty
============================================= */
document.addEventListener('DOMContentLoaded', function()
{
/* --- Přepis nadpisů related / alternative (jQuery) --- */
if (typeof $ !== 'undefined')
{
$('.products-related-header').text('Mohlo by se vám líbit');
$('.products-alternative-header').text('Ostatní zákazníci nakoupili také');
}
/* --- Homepage ratings flag --- */
window.homepageRatings = true;
/* --- Slevový kód – toggle v košíku --- */
(function()
{
var heading = document.querySelector('.cart-summary h4');
var wrapper = document.querySelector('.cart-summary .extras-wrapper');
if (!heading || !wrapper) return;
heading.textContent = 'Máte slevový kód?';
var arrow = document.createElement('span');
arrow.classList.add('coupon-arrow');
arrow.textContent = '▼';
heading.appendChild(arrow);
heading.addEventListener('click', function()
{
var isOpen = wrapper.classList.contains('is-open');
if (isOpen)
{
wrapper.classList.remove('is-open');
heading.classList.remove('is-open');
}
else
{
wrapper.classList.add('is-open');
heading.classList.add('is-open');
}
});
})();
/* --- Přepis "Doručení možné do" (košík / listing) --- */
(function()
{
var el = document.querySelector('[data-testid="deliveryTime"]');
if (!el) return;
var strong = el.querySelector('strong');
if (!strong) return;
var rawDate = strong.textContent.trim();
var parts = rawDate.split('.');
if (parts.length < 3) return;
var day = parseInt(parts[0], 10);
var month = parseInt(parts[1], 10) - 1;
var year = parseInt(parts[2], 10);
var deliveryDate = new Date(year, month, day);
var today = new Date();
today.setHours(0, 0, 0, 0);
var workdays = countWorkdays(today, deliveryDate);
var formattedDate = day + '. ' + (month + 1) + '. ' + year;
var prefix = getDeliveryPrefix(workdays, deliveryDate);
el.innerHTML = prefix + ' ' + formattedDate + '';
el.style.fontSize = '1.2em';
})();
/* --- Detail produktu – skrytí dostupnosti + dynamický text doručení --- */
(function()
{
var label = document.querySelector('[data-testid="labelAvailability"]');
if (label) label.style.display = 'none';
var amount = document.querySelector('[data-testid="numberAvailabilityAmount"]');
if (amount) amount.style.display = 'none';
var deliveryEl = document.querySelector('.delivery-time[data-testid="deliveryTime"]');
if (!deliveryEl) return;
var span = deliveryEl.querySelector('span');
if (!span) return;
var rawDate = span.textContent.trim();
var parts = rawDate.split('.');
if (parts.length < 3) return;
var day = parseInt(parts[0], 10);
var month = parseInt(parts[1], 10) - 1;
var year = parseInt(parts[2], 10);
var deliveryDate = new Date(year, month, day);
var today = new Date();
today.setHours(0, 0, 0, 0);
var workdays = countWorkdays(today, deliveryDate);
var formattedDate = day + '. ' + (month + 1) + '. ' + year;
var prefix = getDeliveryPrefix(workdays, deliveryDate);
var deliveryLine = deliveryEl.closest('.delivery-line');
if (deliveryLine)
{
var shippingLink = deliveryLine.querySelector('.shipping-options');
var linkHTML = shippingLink ? shippingLink.outerHTML : '';
deliveryLine.innerHTML =
'' +
prefix + ' ' + formattedDate + '' +
'' +
'' + linkHTML + '';
}
})();
/* --- Taby: rozbalit všechny + smooth scroll --- */
(function()
{
function initTabs()
{
/* Rozbalíme všechny panely */
document.querySelectorAll('.tab-pane').forEach(function(pane)
{
pane.style.display = 'block';
pane.style.opacity = '1';
pane.style.visibility = 'visible';
});
/* Scroll po kliknutí na záložku */
document.querySelectorAll('.shp-tab-link').forEach(function(link)
{
link.addEventListener('click', function(e)
{
e.preventDefault();
var targetId = this.getAttribute('href').replace('#', '');
var target = document.getElementById(targetId);
if (target)
{
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
});
});
}
/* Zkusíme hned */
var tabs = document.querySelectorAll('.shp-tab-link');
if (tabs.length > 0)
{
initTabs();
return;
}
/* Pokud taby ještě neexistují, sledujeme DOM */
var observer = new MutationObserver(function(mutations, obs)
{
var tabs = document.querySelectorAll('.shp-tab-link');
if (tabs.length > 0)
{
obs.disconnect();
initTabs();
}
});
observer.observe(document.body, { childList: true, subtree: true });
/* Pojistka – po 5 s přestaneme sledovat */
setTimeout(function()
{
observer.disconnect();
}, 5000);
})();
/* --- Změna textu nadpisu newsletteru --- */
(function()
{
var heading = document.querySelector('h3.pageElement__heading.topic span');
if (heading && heading.textContent.trim() === 'Odebírat newsletter')
{
heading.innerHTML = 'Chcete vědět o všem jako první?
Nechte nám na sebe kontakt!';
}
})();
/* --- Nadpisy a přesun produktových bloků --- */
(function()
{
var relatedHeader = document.querySelector('.products-related-header');
if (relatedHeader)
{
relatedHeader.textContent = 'Mohlo by se vám líbit';
}
var altHeader = document.querySelector('.products-alternative-header');
if (altHeader)
{
altHeader.textContent = 'Zákazníci zakoupili také';
}
var altBlock = document.querySelector('.products-alternative');
var gallery = document.querySelector('.product-gallery, .detail-gallery, .gallery-wrapper, [data-testid="productGallery"]');
if (altBlock && altHeader && gallery)
{
gallery.parentNode.insertBefore(altHeader, gallery.nextSibling);
gallery.parentNode.insertBefore(altBlock, altHeader.nextSibling);
}
})();
/* --- Video grid v detailu produktu --- */
(function()
{
var container = document.getElementById('productVideos');
if (!container) return;
var headings = container.querySelectorAll('h3');
var iframes = container.querySelectorAll('iframe');
if (headings.length === 0 || iframes.length === 0) return;
var grid = document.createElement('div');
grid.className = 'video-grid';
for (var i = 0; i < iframes.length; i++)
{
var item = document.createElement('div');
item.className = 'video-item';
var wrapper = document.createElement('div');
wrapper.className = 'video-wrapper';
iframes[i].removeAttribute('width');
iframes[i].removeAttribute('height');
iframes[i].removeAttribute('style');
if (headings[i])
{
item.appendChild(headings[i]);
}
wrapper.appendChild(iframes[i]);
item.appendChild(wrapper);
grid.appendChild(item);
}
container.innerHTML = '';
container.appendChild(grid);
})();
});
/* === Sdílené pomocné funkce === */
function countWorkdays(from, to)
{
var count = 0;
var current = new Date(from);
current.setDate(current.getDate() + 1);
while (current <= to)
{
var dow = current.getDay();
if (dow !== 0 && dow !== 6) count++;
current.setDate(current.getDate() + 1);
}
return count;
}
function getDeliveryPrefix(workdays, deliveryDate)
{
if (workdays <= 1)
{
return 'Doručíme zítra';
}
else if (workdays === 2)
{
return 'Doručíme pozítří';
}
else
{
var dayNames = ['neděli', 'pondělí', 'úterý', 'středu', 'čtvrtek', 'pátek', 'sobotu'];
return 'Doručíme do ' + dayNames[deliveryDate.getDay()];
}
}
/* === Tabs → scroll navigace === */
(function()
{
function scrollTabClick(e)
{
e.preventDefault();
e.stopPropagation();
var targetId = this.getAttribute('href');
if (!targetId) return;
var targetEl = document.querySelector(targetId);
if (!targetEl) return;
var offset = 100;
var top = targetEl.getBoundingClientRect().top + window.pageYOffset - offset;
window.scrollTo({ top: top, behavior: 'smooth' });
var allTabs = document.querySelectorAll('#p-detail-tabs .shp-tab');
for (var j = 0; j < allTabs.length; j++)
{
allTabs[j].classList.remove('active');
}
this.parentElement.classList.add('active');
}
function initScrollTabs()
{
var tabList = document.getElementById('p-detail-tabs');
if (!tabList) return false;
var links = tabList.querySelectorAll('.shp-tab-link[data-toggle="tab"]');
if (links.length === 0) return false;
/* Rozbalíme všechny panely */
var panes = document.querySelectorAll('#tab-content .tab-pane');
for (var k = 0; k < panes.length; k++)
{
panes[k].classList.remove('fade');
panes[k].classList.add('active', 'in');
}
/* Klonujeme tab list – zruší Bootstrap eventy */
var cloned = tabList.cloneNode(true);
tabList.parentNode.replaceChild(cloned, tabList);
var newLinks = cloned.querySelectorAll('.shp-tab-link');
for (var m = 0; m < newLinks.length; m++)
{
newLinks[m].removeAttribute('data-toggle');
newLinks[m].addEventListener('click', scrollTabClick);
}
return true;
}
if (document.readyState === 'loading')
{
document.addEventListener('DOMContentLoaded', function()
{
if (!initScrollTabs()) setTimeout(initScrollTabs, 500);
});
}
else
{
if (!initScrollTabs()) setTimeout(initScrollTabs, 500);
}
})();
/* === Video grid === */
(function()
{
function rebuildVideos(container)
{
var iframes = container.querySelectorAll('iframe');
if (iframes.length === 0) return;
var headings = container.querySelectorAll('h3');
var grid = document.createElement('div');
grid.className = 'video-grid';
for (var i = 0; i < iframes.length; i++)
{
var item = document.createElement('div');
item.className = 'video-item';
var wrapper = document.createElement('div');
wrapper.className = 'video-wrapper';
iframes[i].removeAttribute('width');
iframes[i].removeAttribute('height');
iframes[i].removeAttribute('style');
if (headings[i]) item.appendChild(headings[i]);
wrapper.appendChild(iframes[i]);
item.appendChild(wrapper);
grid.appendChild(item);
}
container.innerHTML = '';
container.appendChild(grid);
}
function tryRebuild()
{
var container = document.getElementById('productVideos');
if (!container) return false;
if (container.querySelectorAll('iframe').length === 0) return false;
rebuildVideos(container);
return true;
}
function watchForVideos()
{
var target = document.getElementById('productVideos');
if (!target) return;
var observer = new MutationObserver(function(mutations, obs)
{
if (tryRebuild()) obs.disconnect();
});
observer.observe(target, { childList: true, subtree: true });
setTimeout(function() { observer.disconnect(); }, 5000);
}
if (document.readyState === 'loading')
{
document.addEventListener('DOMContentLoaded', function()
{
if (!tryRebuild()) watchForVideos();
});
}
else
{
if (!tryRebuild()) watchForVideos();
}
})();