/******* Do not edit this file *******
Simple Custom CSS and JS - by Silkypress.com
Saved: May 20 2026 | 18:00:34 */
(function() {
  if (!document.body.classList.contains('home')) return;

  function timeAgo(dateStr) {
    var diff = (Date.now() - new Date(dateStr)) / 1000;
    if (diff < 3600) return Math.floor(diff / 60) + ' min';
    if (diff < 86400) return Math.floor(diff / 3600) + ' h';
    if (diff < 172800) return 'ontem';
    return Math.floor(diff / 86400) + ' d';
  }

  function readingTime(html) {
    var words = html.replace(/<[^>]*>/g, '').split(/\s+/).filter(Boolean).length;
    return Math.max(1, Math.round(words / 200)) + ' min';
  }

  function decodeHtml(str) {
    var d = document.createElement('div');
    d.innerHTML = str;
    return d.textContent || d.innerText || str;
  }

  function updateSection(posts) {
    var posters = document.querySelectorAll('.fk-cinema .fk-poster');
    posts.forEach(function(post, i) {
      var poster = posters[i];
      if (!poster) return;

      poster.href = post.link;

      var titleEl = poster.querySelector('.fk-poster-title');
      if (titleEl) titleEl.textContent = decodeHtml(post.title.rendered);

      var tagEl = poster.querySelector('.fk-poster-tag');
      if (tagEl) {
        var terms = (post._embedded && post._embedded['wp:term']) ? [].concat.apply([], post._embedded['wp:term']) : [];
        var tag = terms.find(function(t) { return t.taxonomy === 'post_tag'; }) ||
                  terms.find(function(t) { return t.taxonomy === 'category'; });
        if (tag) tagEl.textContent = tag.name;
      }

      var metaEl = poster.querySelector('.fk-poster-meta');
      if (metaEl) metaEl.textContent = timeAgo(post.date) + ' · ' + readingTime(post.content ? post.content.rendered : '');

      var phEl = poster.querySelector('.fk-poster-ph');
      if (phEl) {
        var media = post._embedded && post._embedded['wp:featuredmedia'] && post._embedded['wp:featuredmedia'][0];
        if (media && media.source_url) {
          phEl.style.cssText = 'background: url(' + media.source_url + ') center/cover no-repeat !important;';
        }
      }
    });
  }

  function load() {
    var posters = document.querySelectorAll('.fk-cinema .fk-poster');
    if (!posters.length) return;
    var xhr = new XMLHttpRequest();
    xhr.open('GET', '/wp-json/wp/v2/posts?categories=4&per_page=5&status=publish&_embed=1&orderby=date&order=desc', true);
    xhr.timeout = 8000;
    xhr.onload = function() {
      if (xhr.status !== 200) return;
      try {
        var posts = JSON.parse(xhr.responseText);
        if (Array.isArray(posts) && posts.length) updateSection(posts);
      } catch(e) {}
    };
    xhr.send();
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', load);
  } else {
    setTimeout(load, 500);
  }
})();