function getCookie(cname) {
    let name = cname + "=";
    let decodedCookie = decodeURIComponent(document.cookie);
    let ca = decodedCookie.split(';');
    for (let i = 0; i < ca.length; i++) {
        let c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return true;
        }
    }
    return false;
}

function displayCookieBanner(link) {
    const div = document.createElement('div');
    const h4 = document.createElement('h4');
    const p = document.createElement('p');
    const buttonJa = document.createElement('button');
    const buttonNein = document.createElement('button');

    div.classList.add('cookieBanner');
    h4.classList.add('cookieBanner-title');
    p.classList.add('cookieBanner-text');
    buttonJa.classList.add('cookieBanner-btn', 'cookieBanner-btn-primary');
    buttonNein.classList.add('cookieBanner-btn', 'cookieBanner-btn-secondary');

    h4.innerHTML = "Cookie Hinweis";
    p.innerHTML = "Die Seite, zu welcher dieser Link führt, nutzt einen technisch notwendigen Cookie. Es werden keine Cookies von Drittanbietern verwendet.";
    div.append(h4, p);

    const buttonsDiv = document.createElement('div');
    buttonsDiv.classList.add('cookieBanner-buttons');

    buttonJa.innerHTML = "Fortfahren"; 
    buttonNein.innerHTML = "Abbrechen";

    buttonJa.addEventListener('click', (el) => {
        window.location.assign(link);
    });

    buttonNein.addEventListener('click', (el) => {
        deleteCookieBanner();
    });

    buttonsDiv.append(buttonNein, buttonJa);
    div.append(buttonsDiv);

    $('body').append(div);

    setTimeout(() => {
        div.classList.add('show');
    }, 10);
}

function deleteCookieBanner() {
    const $div = $('.cookieBanner');

    if ($div.length === 0) { return; }

    $div.removeClass('show');
    setTimeout(() => {
        $div.remove();
    }, 400);
}
