jQuery(document).ready(function () {

    disabilities();

    var wapoMainWindowWidth = $(window).width();
    $('.sf-menu ul li').mouseover(function () {
        // checks if third level menu exist         
        var subMenuExist = $(this).find('.dropdown').length;
        if (subMenuExist > 0) {
            var subMenuWidth = $(this).find('.dropdown').width();
            var subMenuOffset = $(this).find('.dropdown').parent().offset().left + subMenuWidth;
            // if sub menu is off screen, give new position
            if ((subMenuOffset + subMenuWidth) > wapoMainWindowWidth) {
                var newSubMenuPosition = subMenuWidth + 3;
                $(this).find('.dropdown').css({
                    left: -newSubMenuPosition,
                    top: '0',
                });
            }
        }
    });

    enquire.register("screen and (min-width: 768px)", function () {
        $('.sf-menu').superfish();
    });

    enquire.register("screen and (max-width: 992px)", function () {
        $(".position-side-left").detach().insertAfter(".content");
    });

    enquire.register("screen and (min-width: 992px)", function () {
        $(".position-side-left").detach().insertBefore(".content");
    });

});



$(window).bind("load", function () {

    if ($('.slider-wrapper').length) {
        $('#slider').nivoSlider({
            effect: 'sliceDown,sliceDownLeft,sliceUp,sliceUpLeft,sliceUpDown,sliceUpDownLeft,fold,boxRandom',
            animSpeed: 500,
            pauseTime: getSlidePauseTime(),
            controlNav: false,
            directionNav: false,
            pauseOnHover: false
        });
    }

    const addPaddingToFooter = () => {
        const footer = document.querySelector('footer');
        const cookiesBox = document.querySelector('.cookies');
        const footerBottomPadding = cookiesBox.clientHeight + 10;
        footer.setAttribute('style', `padding-bottom: ${footerBottomPadding}px`);
    }
  
    const removePaddingFromFooter = () => document.querySelector('footer').removeAttribute('style');
  
    if (WHReadCookie('cookies_accepted') !== 'T') {
        document.querySelector('.cookies').hidden = false;
        addPaddingToFooter();
    }
  
    document.querySelector('.cookies__btn-agree').addEventListener('click', () => {
        WHCreateCookie('cookies_accepted', 'T', 365);
        document.querySelector('.cookies').hidden = true;
        removePaddingFromFooter();
    });

    if (WHReadCookie("return") != "T") {
        $.ajax({ url: "//" + document.domain + "/addviewer" }).done(function (
            data
        ) {
            WHCreateCookie("return", "T", 1);
        });
    }


    $('.go-to-search').click(function () {
        $('#search-input').focus();
    });

    $('.sf-menu li a').focusin(function () {
        $(this).parent('li').addClass('focused');
    });
    $('.sf-menu li a').focusout(function () {
        $(this).parent('li').removeClass('focused');
    });


    var $container = $('.masonry');
    $container.imagesLoaded(function () {
        $container.masonry();
    });

    $.each($('.sf-menu li a'), function (index, value) {
        if ($(this).attr('href') == window.location.pathname) {
            $(this).parent().addClass('focused');
        }
    });

    if (document.querySelector('.lightbox')) {
        new Tobii({
            captionsSelector: 'self',
            captionAttribute: 'title',
            navLabel: ['Poprzednie zdjęcie', 'Następne zdjęcie'],
            closeLabel: 'Zamknij galerię',
        });
    }
  
    if (document.querySelector('#popup') && !WHReadCookie('interPop')) {
        const dialog = new A11yDialog(document.querySelector('#popup'));
        WHCreateCookie('interPop', 'popup', 1);
        dialog.on('show', () => (document.documentElement.style.overflowY = 'hidden')).on('hide', () => (document.documentElement.style.overflowY = ''));
        dialog.show();
    }

});



function WHCreateCookie(name, value, days) {
    var now = new Date();
    var expire = new Date();
    expire.setFullYear(now.getFullYear());
    expire.setMonth(now.getMonth());
    expire.setDate(now.getDate() + days);
    expire.setHours(0);
    expire.setMinutes(0);
    document.cookie = name + "=" + value + "; expires=" + expire.toString() + "; path=/";
}

function WHReadCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}



const updateCalendar = (curmonth, curyear) => {
    const eventscalendar = document.getElementById('eventscalendar');
    $.ajax({ url: "//" + document.domain + "/eventsajax?month=" + curmonth + "&year=" + curyear }).done(function (data) {
        eventscalendar.innerHTML = data;
    });
}

function getSlidePauseTime() {
    if ($('.slider-img').length > 1) {
        return 4000;
    } else {
        return 9999999999999;
    }
}























function disabilities() {
    $('body .go-to-content:eq(1)').after(createDisabilitiesContainer());
    makeContrast();
    makeFont();
}

function createDisabilitiesContainer() {
    return '<div style="position:absolute;top:0;left:0;z-index:9999;width:100%;background:#fff;color:#000;text-align:center; padding:3px; border-bottom: 2px solid #000;" class="hidden-print" id="disabilities">' +
        '<span>Rozmiar czcionek: <a id="fonta" href="#" title="Standardowa wielkość czcionek" style="font-size:12px;padding-left:5px;color:#000;">A</a><a id="fontaa" href="#" title="Powiększone czcionki" style="font-size:14px;padding-left:5px;color:#000;">A+</a><a id="fontaaa" href="#" title="Maksymalna wielkość i pogrubienie czcionek" style="font-size:16px;font-weight:600;padding-left:5px;color:#000;">A++</a></span>' +
        '<span style="padding:0 15px">|</span>' +
        '<span><a id="contrast" href="#" style="color:#000;">' + contrastTypeText() + '</a></span>' +
        '</div>';
}

function makeContrast() {
    if (isContrastSet()) {
        $('body').addClass('contrast');
        $('head').append('<link rel="stylesheet" type="text/css" href="/css/contrast.css" />')
    }
}

function isContrastSet() {
    if (WHReadCookie('contrast') != null) { return true; }
    return false;
}

function contrastTypeText() {
    if (isContrastSet()) { return 'Ustaw standardowy kontrast'; }
    return 'Zwiększ kontrast';
}

$(document).on('click', '#contrast', function (e) {
    e.preventDefault();
    if (isContrastSet()) {
        document.cookie = 'contrast' + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
        location.reload();
    }
    else {
        WHCreateCookie('contrast', 'T', 365);
        location.reload();
    }
});

function makeFont() {
    if (isFontSet()) {
        $('body').addClass('font' + WHReadCookie('font'));
        $('head').append('<link rel="stylesheet" type="text/css" href="/css/font' + WHReadCookie('font') + '.css" />')
    }
}

function isFontSet() {
    if (WHReadCookie('font') != null) { return true; }
    return false;
}

$(document).on('click', '#fonta', function (e) {
    e.preventDefault();
    document.cookie = 'font' + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
    location.reload();
});

$(document).on('click', '#fontaa', function (e) {
    e.preventDefault();
    document.cookie = 'font' + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
    WHCreateCookie('font', 'aa', 365);
    location.reload();
});

$(document).on('click', '#fontaaa', function (e) {
    e.preventDefault();
    document.cookie = 'font' + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
    WHCreateCookie('font', 'aaa', 365);
    location.reload();
});