document.addEventListener('DOMContentLoaded', () => {

// Bulma Navbar Burger Menü
// https://bulma.io/documentation/components/navbar/#navbar-menu
    // Get all "navbar-burger" elements
    const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0);
    
    // Add a click event on each of them
    $navbarBurgers.forEach( el => {
        el.addEventListener('click', () => {
            // Get the target from the "data-target" attribute
            const target = el.dataset.target;
            const $target = document.getElementById(target);
            
            // Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu"
            el.classList.toggle('is-active');
            $target.classList.toggle('is-active');
        });
    });



// Bulma Modal
// https://bulma.io/documentation/components/modal/#javascript-implementation-example
    // Functions to open and close a modal
    function openModal($el) {
       $el.classList.add('is-active');
    }
    
    function closeModal($el) {
      $el.classList.remove('is-active');
    }
    
    function closeAllModals() {
       (document.querySelectorAll('.modal') || []).forEach(($modal) => {
           closeModal($modal);
       });
    }
    
    // Add a click event on buttons to open a specific modal
    (document.querySelectorAll('.js-modal-trigger') || []).forEach(($trigger) => {
        const modal = $trigger.dataset.target;
        const $target = document.getElementById(modal);
    
        $trigger.addEventListener('click', () => {
            openModal($target);
        });
    });
    
    // Add a click event on various child elements to close the parent modal
    (document.querySelectorAll('.modal-background, .modal-close, .modal-card-head .delete, .modal-card-foot .button') || []).forEach(($close) => {
        const $target = $close.closest('.modal');
    
        $close.addEventListener('click', () => {
            closeModal($target);
        });
    });
    
    // Add a keyboard event to close all modals
    document.addEventListener('keydown', (event) => {
        const e = event || window.event;
    
        if (e.keyCode === 27) { // Escape key
            closeAllModals();
        }
    });


// Countdown zur Fasnet
/*    var currentTime = new Date()
    var currDate = new Date(Date.now()); //Todays Date
    var iniPastedDate = "1/6/2023" //End Date
    
    function DateDiff(date1, date2) {
        var datediff = date1.getTime() - date2.getTime(); //store the getTime diff - or +
        return (datediff / (24 * 60 * 60 * 1000)); //Convert values to -/+ days and return value      
    }
    var elem = document.getElementById('countdown');
    if(typeof elem !== 'undefined' && elem !== null) {
        if (new Date(iniPastedDate) > new Date(currDate)) {
            document.getElementById('countdown').innerHTML = "S goht drgega... noch " + DateDiff(new Date(iniPastedDate), new Date(currDate)).toFixed(0) + " Tage!";
            document.getElementById('countdown').style.width = (100 / 365 * (365 - DateDiff(new Date(iniPastedDate), new Date(currDate)))) + "%";
        } else {
            document.getElementById('countdown').style.textAlign = 'center';
            document.getElementById('countdown').style.fontSize = '2.0em';
            document.getElementById('countdown').innerHTML = 'Endlich wieder Fasnet!';
        }
    }
    
    // HTML zum obigen Script, dass im Content platziert werden kann
        <div class="container is-fullhd">
        	<div class="countdown-box">
        		<div id="countdown"></div>
        	</div>
        </div>

*/    
    var currentTime = new Date()
    var currDate = new Date(Date.now());                        // Datum heute
    var iniPastedDate = "1/6/2024"                              // Enddatum
    
    function DateDiff(date1, date2) {
        var datediff = date1.getTime() - date2.getTime();       // Speichere die Differenz von getTime
        return (datediff / (24 * 60 * 60 * 1000));              // Konvertiere die Werte zu Tagen und gib sie zurück     
    }
    var elem = document.getElementById('countdown-text');
    if(typeof elem !== 'undefined' && elem !== null) {
        if (new Date(iniPastedDate) > new Date(currDate)) {
            document.getElementById('countdown-text').innerHTML = "S goht drgega - noch <strong>" + DateDiff(new Date(iniPastedDate), new Date(currDate)).toFixed(0) + " Tage</strong> bis zur Fasnet!";
            document.getElementById('countdown-progress').value = (365 - DateDiff(new Date(iniPastedDate), new Date(currDate)));
            document.getElementById('countdown-progress').innerHTML = (DateDiff(new Date(iniPastedDate), new Date(currDate))) + " Tage";
        } else {
            document.getElementById('countdown-text').innerHTML = 'Endlich wieder Fasnet!';
        }
    }



});


// Filter list elements
// https://codepen.io/cs09g/pen/OGbEMm

// Eingabe in Input-Feld
var input = document.querySelector('#search');
// Suche in der search-list nach den Items
var items = document.querySelector('.search-list').getElementsByTagName('div');

// bei Eingabe starte Funktion
input.addEventListener('keyup', function(ev) {
    
  var text = ev.target.value;
  var pat = new RegExp(text, 'i');
  for (var i=0; i < items.length; i++) {
    var item = items[i];
    if (pat.test(item.innerText)) {
      item.classList.remove("hidden");
    }
    else {console.log(item);
      item.classList.add("hidden");
    }
  }
});
