// source --> https://www.tokati.de/wp-content/themes/tokati/assets/js/custom.js 
jQuery(document).ready(function($) {

      $('#pageUp').click(function() {
        // Animiertes Scrollen nach oben
        $('html, body').animate({ scrollTop: 0 }, 600); // 600ms Dauer
    });

    $('#menubutton_bar .menubutton button').click(function(){
        $(this).toggleClass('active');
        $('#menu-wrapper').toggleClass('active');
        $('header .logo_bar').toggleClass('active');
        $('html, body').toggleClass('noscroll');
        $('main, footer').toggleClass('noscroll');
        $('.sub-menu').removeClass('active');
    });

  $('.menu-item-has-children').each(function() {

    const $li = $(this);
    const $link = $li.children('a');
    const $submenu = $li.children('.sub-menu');

    // eindeutige ID fürs Submenu
    const submenuId = 'submenu-' + Math.random().toString(36).substr(2, 9);
    $submenu.attr('id', submenuId);

    // Link zum Button machen
    $link
      .removeAttr('href')
      .attr({
        role: 'button',
        tabindex: '0',
        'aria-expanded': 'false',
        'aria-controls': submenuId
      });

    // CLICK
    $link.on('click', function(e) {
      e.preventDefault();
      toggleMenu();
    });

    // KEYBOARD (Enter + Space)
    $link.on('keydown', function(e) {
      if (e.key === 'Enter' || e.key === ' ') {
        e.preventDefault();
        toggleMenu();
      }
    });

    function toggleMenu() {
      const isOpen = $submenu.hasClass('active');

      $submenu.toggleClass('active', !isOpen);
      $link.attr('aria-expanded', !isOpen);

      // Fokus beim Öffnen ins Submenu setzen
      if (!isOpen) {
        $submenu.find('a, button').first().focus();
      } else {
        $link.focus();
      }
    }

  });

  $('.menu-item-has-children > .sub-menu').each(function() {

  const $submenu = $(this);
  const $parentLink = $submenu.siblings('a');
  const parentText = $parentLink.text().trim();

  $submenu.prepend(
    '<li class="submenu-close">' +
      '<button type="button" aria-label="Zurück zu ' + parentText + '">' +
        parentText +
      '</button>' +
    '</li>'
  );

});

$(document).on('click', '.submenu-close button', function() {
  const $submenu = $(this).closest('.sub-menu');
  const $parentLink = $submenu.siblings('a');

  $submenu.removeClass('active');
  $parentLink.attr('aria-expanded', 'false').focus();
});


      $('.wp-block-gallery').each(function () {

            var $gallery = $(this);

            $gallery.imagesLoaded(function () {
            $gallery.masonry({
                itemSelector: '.wp-block-image',
                columnWidth: '.wp-block-image',
                percentPosition: true,
                gutter: 16
            });
            });

        });

    /******** details / summary **********/

    // Wrapper erzeugen (falls noch nicht vorhanden)
  $("details").each(function () {
    if ($(this).children(".content").length) return;
    const $summary = $(this).children("summary");
    if (!$summary.length) return;
    $summary.nextAll().wrapAll('<div class="content"></div>');
  });

  // Startzustand: alles zu
  $("details").removeAttr("open");
  $("details .content").hide();

  $("summary").on("click", function (e) {
    e.preventDefault();

    const $details = $(this).parent();
    const $content = $details.children(".content");

    // Wenn offen → schließen
    if ($details.attr("open")) {
      $content.stop(true, true).slideUp(300, function () {
        $details.removeAttr("open");
      });
      return;
    }

    // Andere schließen
    $("details[open]").each(function () {
      const $open = $(this);
      $open.children(".content").stop(true, true).slideUp(300, function () {
        $open.removeAttr("open");
      });
    });

    // Dieses öffnen
    $details.attr("open", true);
    $content.stop(true, true).slideDown(300);
  });


    /******** ticker ********/

 jQuery(function ($) {

    $('#ticker').each(function () {

        var $ticker = $(this);
        var words = $ticker.data('words');

        if (!words || !words.length) return;

        var i = 0;
        var j = 0;
        var typingSpeed = 150;
        var pause = 1000;

        function typeWord() {
            var word = words[i];
            if (j < word.length) {
                $ticker.text(word.substring(0, j + 1));
                j++;
                setTimeout(typeWord, typingSpeed);
            } else {
                setTimeout(deleteWord, pause);
            }
        }

        function deleteWord() {
            var word = words[i];
            if (j > 0) {
                $ticker.text(word.substring(0, j - 1));
                j--;
                setTimeout(deleteWord, typingSpeed);
            } else {
                i = (i + 1) % words.length;
                setTimeout(typeWord, typingSpeed);
            }
        }

        typeWord();
    });
});

});

document.addEventListener("DOMContentLoaded", function () {
    const elements = document.querySelectorAll(".animierbar");

    const observer = new IntersectionObserver((entries, observer) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                entry.target.classList.add("animiert");
                observer.unobserve(entry.target); // nur einmal
            }
        });
    }, {
        threshold: 0.4 // 20% sichtbar reicht
    });

    elements.forEach(el => observer.observe(el));
});