// Check for hash on load.

window.addEventListener('load', (event) => {
    const preclicked = window.location.hash.slice(1);
    if (! preclicked) return;
    const albumheader = document.getElementById(preclicked);
    if (albumheader){
        albumheader.classList.add('open');
    }
});

// Scroll to search result album

function albumselect(code){
    const albumheader = document.getElementById(code);
    const searchbar = document.getElementById('search-input');
    for (const other of document.querySelectorAll('.sort-header.album.open')){
        other.classList.remove('open');
    }
    albumheader.classList.add('open');
    searchbar.value = '';
    searchbar.dispatchEvent(new Event('input'));
    document.getElementById('search-results-header').replaceChildren();
}

// Toggle header when clicked

function albumHeaderClick(clickevent){
    const albumheader = clickevent.target;
    if (albumheader.classList.contains('open')){
        albumheader.classList.remove('open');
        history.replaceState(null, document.title, `${window.location.origin}${window.location.pathname}${window.location.search}`);
    }
    else{
        for (const other of document.querySelectorAll('.sort-header.album.open')){
            other.classList.remove('open');
        }
        albumheader.classList.add('open');
        window.location.hash = albumheader.id;
    }
}

// Fill out form with suggest search result
function suggestform(id, desc, clickevent){
    const searchresults = clickevent.target.parentElement.parentElement;
    const form = searchresults.dataset.form;
    const searchbar = document.getElementById(`search-input-suggest-${form}`)
    document.getElementById(`connection-${form}`).innerHTML = clickevent.target.innerHTML;
    document.getElementById(`connection-${form}-id`).value = id;
    document.getElementById(`connection-${form}-desc`).value = desc;
    searchbar.value = '';
    searchbar.dispatchEvent(new Event('input'));
    searchresults.replaceChildren();
}

// Mutually exclusive audio playback
function onlyPlayOneIn(container) {
    container.addEventListener("play", function(event) {
        audio_elements = container.getElementsByTagName("audio")
        for(i=0; i < audio_elements.length; i++) {
            audio_element = audio_elements[i];
            if (audio_element !== event.target) {
                audio_element.pause();
            }
        }
    }, true);
}
  
document.addEventListener("DOMContentLoaded", function() {
    onlyPlayOneIn(document.body);
});

// Search clear button
function searchIconChange(event, buttonid, barid) {
    const searchbutton = document.getElementById(buttonid);
    const searchicon = document.getElementById(barid);

    if (event.target.value.length !== 0) {
        searchbutton.classList.remove('hidden');
        searchicon.classList.remove('search-empty');
    }
    else {
        searchbutton.classList.add('hidden');
        searchicon.classList.add('search-empty');
    }
}

function clearSearch(searchid, resultid) {
    const searchbar = document.getElementById(searchid);
    const searchresults = document.getElementById(resultid);
    searchbar.value = '';
    searchresults.replaceChildren();
    searchbar.dispatchEvent(new Event('input'));
}