(function () {
    var _czasWejsciaNaStrone = (new Date()).getTime() / 1000;
    var _czasSesji = 30 * 60; //30min w sekundach;
    var _minuty = 2;
    var _czasOdliczania = _minuty * 60;
    var _rozpoczacOdliczanie = false;

    function addElement(callback) {
        document.body.insertAdjacentHTML('afterbegin', '<div class="displayPopup"></div>');

        if (callback) {
            return callback;
        }
    }

    function dodajListenerDoPrzedluz() {
        var extanded = document.getElementById('form:przedluz');
        var extendMenuBoczne = document.getElementById('form:przedluz');
        if (extanded !== null) {
            document.getElementById('form:przedluz').addEventListener("click", resetujLicznik);
        }

        if (extendMenuBoczne !== null) {
            document.getElementById('form:przedluz').addEventListener("click", resetujLicznik);
        }
    };

    function closeDialog() {
        [].slice.call(document.querySelectorAll('.popu .ui-icon.ui-icon-closethick, .popu button:first-child')).forEach(function (el)  {
            el.addEventListener('click', function () {
                var classDelete = document.querySelector('.displayPopup');
                classDelete.classList.remove('popuBackground');
            })
        });

        [].slice.call(document.querySelectorAll('.popu .ui-dialog-titlebar-icon')).forEach(function (ah) {
            ah.classList.add('wcag_focus');
            ah.addEventListener('keydown', function (e) {
                if (e.keyCode === 13 || e.keyCode === 32) {
                    this.click();
                    var classDelete = document.querySelector('.displayPopup');
                    classDelete.classList.remove('popuBackground');
                }
            })
        });
        setTimeout(function () {
            var needFocus = document.querySelector('#form\\:header_dialog a.ui-dialog-titlebar-icon');
            if (needFocus) {
                needFocus.style.outline = '0.2em solid #1A8AD5';
                needFocus.focus();
                needFocus.addEventListener('focusout', function () {
                    needFocus.style.outline = '';
                });
            }
        }, 250);
    }

    function resetujLicznik() {
        _czasWejsciaNaStrone = (new Date()).getTime() / 1000;
        _rozpoczacOdliczanie = false;
        var popup = PF("wygasSesjePopup");
        popup.hide();
        var popupMenu = PF("wygasSesjePopup1");
        popupMenu.hide();
    };

    function addClassButton() {
        var popu = [].slice.call(document.querySelectorAll('.popu button'));
        var popuHideElement = [].slice.call(document.querySelectorAll('.popu .ui-resizable-handle'));

        popuHideElement.forEach(function (value) {
            value.style.display = 'none';
        });

        [].slice.call(document.querySelectorAll('.popu .ui-dialog-titlebar-icon')).forEach(function (ah) {
            ah.classList.add('wcag_focus');
            ah.addEventListener('keydown', function (e) {
                if (e.keyCode === 13 || e.keyCode === 32) {
                    this.click();
                }
            })
        });

        popu.forEach(function(el) {
            el.classList.add('ui-button', 'ui-widget', 'ui-state-default', 'ui-corner-all', 'ui-button-text-icon-right', 'button--primary', 'backButton', 'wcag_focus');
        });


    }

    var _sprawdzZakonczenieSesji = function() {
        dodajListenerDoPrzedluz();
        var teraz = (new Date()).getTime() / 1000;
        var roznica = parseInt(teraz - _czasWejsciaNaStrone);
        var licznikStrona = document.getElementById('form:timer');
        var licznikStronMenuBoczne = document.getElementById('form:timer1');
        var liczbaSekund = parseInt(_czasSesji - roznica);
        var minuty = ("0" + Math.floor(liczbaSekund / 60)).slice(-2);
        var sekundy = ("0" + (liczbaSekund - minuty * 60)).slice(-2);

        if (licznikStrona !== null) {
            licznikStrona.innerText = minuty + ":" + sekundy;
        }

        if (licznikStronMenuBoczne !== null) {
            licznikStronMenuBoczne.innerText = minuty + ":" + sekundy;
        }

        if (minuty == 0 && sekundy == 0) {
            // resetujLicznik();
            var popup = PF("wygasSesjePopup");
            var popupMenuBoczne = PF("wygasSesjePopup1");
            if (popup != null) {
                popup.hide();
            }
            if(popupMenuBoczne != null){
                popupMenuBoczne.hide();
            }
            document.getElementById("form:sessionTimeButton").click();
        }

        if (roznica > _czasSesji) {
            return;
        } else if (roznica > (_czasSesji - _czasOdliczania) && !_rozpoczacOdliczanie) {
            document.querySelector('body').appendChild(document.querySelector('.popu'));
            addElement(addClassButton());
            var popupBack = document.querySelector('.displayPopup');
            popupBack.classList.add('popuBackground');
            var popup = PF("wygasSesjePopup");
            var popupMenuBoczne = PF("wygasSesjePopup1");
            closeDialog();
            if (popup != null) {
                popup.show();
            }
            if(popupMenuBoczne != null){
                popupMenuBoczne.show();
            }

            document.getElementById("form:przedluzButton").addEventListener("click", resetujLicznik);
            document.getElementById("form:przedluzButton").removeAttribute('role');
            document.getElementById("form:przedluzButton1").addEventListener("click", resetujLicznik);
            document.getElementById("form:przedluzButton1").removeAttribute('role');
            _rozpoczacOdliczanie = true
        }

    };


    window.setInterval(_sprawdzZakonczenieSesji, 1000);
})();
