//<![CDATA[
/* obsługa paginacji, dodawanie elementów podczas reqestu complete */
function addFocusOnPagination(callback) {
	var perPageElement = [].slice.call(document.querySelectorAll('[role="navigation"] > select'));
	var firstPaginationFocus = [].slice.call(document.querySelectorAll('[role="navigation"] > span a'));
	ariaRoleNeed();
	perPageElement.forEach(function (element) {
		element.classList.add('wcag_focus');

		element.addEventListener('change', function () {
			setTimeout(function () {
				var newEle = [].slice.call(document.querySelectorAll('table.wcag_focus tr td a'));
				if (newEle.length > 0 && newEle.length <= 5) {
						newEle[0].focus();
						console.log(newEle, 1)
				}
				if (newEle.length >= 10) {
						newEle[4].focus();
						console.log(newEle, 2)
				}
				if (newEle.length >= 15) {
						newEle[9].focus();
						console.log(newEle, 3)
				}
				if (newEle.length >= 20) {
						newEle[14].focus();
						console.log(newEle, 4);
				}
			}, 450);
			setTimeout(function () {
				ariaRoleNeed();
			}, 450);
		});
	});

	firstPaginationFocus.forEach(function (pag) {
		pag.classList.add('wcag_focus');
		pag.addEventListener('keydown', function (e) {
			if (e.key === 'Enter') {
				var newEle = [].slice.call(document.querySelectorAll('table.wcag_focus tr td a'));
				if (newEle.length > 0) {
					setTimeout(function () {
						newEle[0].focus();
					}, 400);
				}
				var check = setInterval(function () {
					newEle = [].slice.call(document.querySelectorAll('table.wcag_focus tr td a'));
					firstPaginationFocus = [].slice.call(document.querySelectorAll('[role="navigation"]:last-child span a'));
					firstPaginationFocus.forEach(function (el) {
						if (!el.classList.contains('wcag_focus')) {
							ariaRoleNeed();
							removeRole();
							clearInterval(check);
						}
					});
				}, 250);
			}
		});
	});
}

function removeRole() {
	[].slice.call(document.querySelectorAll('[role="grid"], [role="row"]')).forEach(function (value) {
		value.removeAttribute('role');
	});

	[].slice.call(document.querySelectorAll('[role="gridcell"]')).forEach(function (value) {
		value.classList.add('gridcell');
		value.removeAttribute('role');
	});
}

function ariaRoleNeed() {
	[].slice.call(document.querySelectorAll('span.ui-paginator-pages')).forEach(function (el) {
		el.setAttribute('role', 'list');
	});

	var aria = [].slice.call(document.querySelectorAll('[role="navigation"] span a'));
	aria.forEach(function(el) {
		el.classList.add('wcag_focus');
		el.setAttribute('role', 'listitem');
		el.setAttribute('tabindex', '-1');
		el.removeAttribute('aria-label');
		el.removeAttribute('href');
		if (!el.querySelector('span')) {
			var span = document.createElement('span');
			span.textContent = 'Strona ' + el.textContent;
			span.setAttribute('tabindex', '0');
			span.setAttribute('role', 'link');
			span.style.height = '0';
			span.style.width = '0';
			span.style.position = 'absolute';
			span.style.opacity = '0';
			span.style.overflow = 'hidden';
			el.appendChild(span);

			span.addEventListener('focus', function () {
				el.style.outline = '2px solid #1A8AD5';
			});

			span.addEventListener('focusout', function () {
				el.style.outline = '';
			});

			span.addEventListener('keydown', function (e) {
				if (e.key === 'Enter' || e.key === ' ') {
					el.click();
				}
			})
		}
	});
}

window.addEventListener('load', function() {
	(function () {
		var clickGear = 0;
		var sliceTab = function sliceTab(arr) {
			return [].slice.call(document.querySelectorAll(arr));
		};
		var setAttr = function(arr, index) {
			arr.forEach(function(element) {
				return element.setAttribute('tabindex', index);
			});
		};

		function setAttributes(el, attrs) {
			try {
				for (var key in attrs) {
					el.setAttribute(key, attrs[key]);
				}
			} catch (err) {
				console.error('have a problem with ' + el + ' must have a single element ' + err.message);
			}
		}

		function eventOnEnter(tab, agrr, chech, boolen) {
			var eventTab = sliceTab(tab);

			eventTab.forEach(function(el) {
				var nazwa = el.parentElement.parentElement.parentElement.querySelector('label');
				if (nazwa !== null) {
					el.setAttribute('aria-label', nazwa.textContent);
				}

				if (!el.hasAttribute('aria-checked') ||
					!el.hasAttribute('aria-expanded')) {
					setAttributes(el, agrr);
					el.classList.add('wcag_focus');
					if (el.classList.value.indexOf('ui-icon-check') !== -1) {
						el.setAttribute('aria-checked', 'true');
					}

					el.addEventListener('keydown', function(event) {
						if (event.key === 'Enter' || event.key === ' ') {
							event.stopPropagation();
							event.preventDefault();
							var value = el.getAttribute(chech);
							if (boolen === true) {
								el.click();
								setTimeout(function () {
									usuwanieAtrybutow();
								}, 250);
							}
							value === 'false' ?
								el.setAttribute(chech, 'true') :
								el.setAttribute(chech, 'false');
							setTimeout(function () {
								usuwanieAtrybutow();
							}, 250);
						}
					}, false);

					el.addEventListener('click', function() {
							var value = el.getAttribute(chech);
							value === 'false' ?
								el.setAttribute(chech, 'true') :
								el.setAttribute(chech, 'false');
						setTimeout(function () {
							usuwanieAtrybutow();
						}, 250);
					});
				}
			});
		}

		function addSimpleAria(ele, aria, tres) {
			el = document.querySelector(ele);
			if (el !== null) {
				el.setAttribute(aria, tres);
			}
		}

		function addDynamicClass(arr, arrCla) {
			arr.forEach(function(el) {
				el.classList.add(arrCla);
			});
		}


		function addingAlert() {
			var int = setInterval(function () {
				var ale = document.querySelector('#form\\:advances_to_accept_datatable_data tr.ui-datatable-empty-message td');
				if (ale) {
					ale.parentElement.parentElement.parentElement.removeAttribute('role');
					ale.parentElement.parentElement.parentElement.setAttribute('role', 'presentation');
					ale.setAttribute('role', 'alert');
					clearInterval(int);
				}
			}, 350);
		}

		/* wysz zaaw */

		function wyszZaa() {
			if (location.pathname.match(/wyszukiwanieZaawansowane/i) !== null) {
				if (document.querySelector('.contentSearch')) {
					document.querySelector('.contentSearch').classList.add('wcag_focus');
				}

				var index0 = document.getElementById('form:scales_input');
				if (index0) {
					index0.setAttribute('tabindex', '-1');
				}

				function wybierzStatus() {
					function cLabel() {
						var label = document.createElement('label');
						label.setAttribute('for', 'form:multiple1_focus');
						label.classList.add('wcag_hide2');
						label.innerText = 'Wybierz status';

						document.querySelector('.statusDiv1 .ui-helper-hidden-accessible').insertAdjacentElement('afterbegin', label);
					}

					function changePosition() {
						setTimeout(function() {
							var menu = document.getElementById('form:multiple1_panel');
							document.querySelector('body').insertAdjacentElement('afterbegin', menu);
						}, 500);
					}

					eventOnEnter('.contentSearch .ui-chkbox-icon', {
						'tabindex': '0',
						'role': 'checkbox',
						'aria-checked': 'false'
					}, 'aria-checked', true);

					function setAria() {
						var allOther = sliceTab('#form\\:multiple1_panel .ui-selectcheckboxmenu-items-wrapper .ui-chkbox span');
						var all = document.querySelector('#form\\:multiple1_panel .ui-chkbox span');
						var allLi = sliceTab('#form\\:multiple1_panel .ui-selectcheckboxmenu-items-wrapper li');

						all.classList.add('wcag_focus');
						addDynamicClass(allOther, 'wcag_focus');
						addDynamicClass(allLi, 'wcag_focus');

						allOther[allOther.length - 1].addEventListener('focusout', function() {
							document.getElementById('form:multiple1_panel').style.display = 'none';
							document.querySelector('#form\\:scales span').focus();
						});

						setAttributes(all, {
							'role': 'checkbox',
							'tabindex': '0',
							'aria-label': 'Zaznacz wszystko',
							'aria-checked': 'false'
						});

						[].forEach.call(allOther, function(el) {
							setAttributes(el, {
								'role': 'checkbox',
								'tabindex': '0',
								'aria-checked': 'false'
							});
						});
					}
					setAria();
					changePosition();
					cLabel();
				}

				wybierzStatus();
				addingAlert();
			}
		}

		/* edycja treści */

		function edycjaTesci() {
			if (location.pathname.match(/edycjaTresci/i) !== null ||
				location.pathname.match(/wyszukiwanieZaawansowane./i) !== null) {
				var allOther = sliceTab('#form\\:multiple_panel .ui-selectcheckboxmenu-items-wrapper .ui-chkbox span');

				function createLabel() {
					var label = document.createElement('label');
					label.setAttribute('for', 'form:multiple_focus');
					label.classList.add('wcag_hide2');
					label.innerText = 'Słowa kluczowe';
					document.querySelector('.firstButtons .ui-helper-hidden-accessible').insertAdjacentElement('afterbegin', label);
				}

				setInterval(function () {
					sliceTab('.notHere').forEach(function (e) {
						e.setAttribute('role', 'presentation');
					})
				}, 1000);

				function changePosition() {
					setTimeout(function() {
						var menu = document.getElementById('form:multiple_panel');
						if (menu) {
							document.querySelector('body').insertAdjacentElement('afterbegin', menu);
						}
					}, 500);
				}

				function setPolishWords() {
					var deleteRead = document.getElementById('form:multiple_focus');
					var selectAtt = document.querySelector('#form\\:multiple_panel .ui-chkbox input');
					var search = document.querySelector('#form\\:multiple_panel .ui-selectcheckboxmenu-filter-container input');
					var close = document.querySelector('#form\\:multiple_panel a.ui-selectcheckboxmenu-close');


					if (document.querySelector('.ui-tree-container') !== null) {
						document.querySelector('.ui-tree-container').addEventListener('DOMSubtreeModified', function() {
							var multiFocus = sliceTab('.klasaNajnizsza span .ui-treenode-label');
							setTimeout(function() {
								multiFocus.forEach(function(el) {
									el.classList.add('wcag_focus');
								});
							}, 200);
						});
					}

					search.classList.add('wcag_focus');
					close.classList.add('wcag_focus');

					deleteRead.removeAttribute('readonly');
					selectAtt.setAttribute('tabindex', '-1');
					addSimpleAria('#form\\:multiple_panel .ui-selectcheckboxmenu-filter-container input', 'aria-label', 'Pole wyszukiwania');
					addSimpleAria('#form\\:multiple_panel a.ui-selectcheckboxmenu-close', 'aria-label', 'Zamknij okno');
				}

				function setAria() {
					var all = document.querySelector('#form\\:multiple_panel .ui-chkbox span');
					var allLi = sliceTab('#form\\:multiple_panel .ui-selectcheckboxmenu-items-wrapper li');

					all.classList.add('wcag_focus');
					addDynamicClass(allOther, 'wcag_focus');
					addDynamicClass(allLi, 'wcag_focus');


					setAttributes(all, {
						'role': 'checkbox',
						'tabindex': '0',
						'aria-label': 'Zaznacz wszystko',
						'aria-checked': 'false'
					});

					[].forEach.call(allOther, function(el) {
						setAttributes(el, {
							'role': 'checkbox',
							'tabindex': '0'
						});
					});
				}

				function chekcBox() {
					allOther[allOther.length - 1].addEventListener('focusout', function() {
						var zaa = document.getElementById('form:zrodlo');
						var tres = document.querySelector('#panelGrupUprawnien h2');
						document.getElementById('form:multiple_panel').style.display = 'none';
						tres ? tres.focus() : zaa.focus();
					});

					allOther.forEach(function(el) {
						var parEle = el.parentElement.parentElement.querySelector('input');
						parEle.removeAttribute('readonly');
						parEle.setAttribute('tabindex', '-1');
						var checked = parEle.getAttribute('aria-checked');
						if (checked === 'true') {
							el.setAttribute('aria-checked', 'true');
							setAttributes(el, {
								'tabindex': '0',
								'role': 'checkbox'
							});
						} else {
							setAttributes(el, {
								'tabindex': '0',
								'role': 'checkbox',
								'aria-checked': 'false'
							});
						}
						el.addEventListener('keydown', function(e) {
							if (e.key === 'Enter') {
								var value = el.getAttribute('aria-checked');
								el.click();
								value === 'false' ?
									el.setAttribute('aria-checked', 'true') :
									el.setAttribute('aria-checked', 'false');
							}
						});
					});
				}
				changePosition();
				chekcBox();
				setAria();
				setPolishWords();
				createLabel();
			}
		}

		/* baza wiedzy */

		function edycjaBW() {
			if (location.pathname.match(/edycjafolderu/i) !== null ||
				location.pathname.match(/edycjaTresci/i) !== null) {

				var dropDown = sliceTab('.ui-selectcheckboxmenu-item');

				eventOnEnter('.ui-chkbox-box:not(.ui-state-disabled) .ui-chkbox-icon:not([tabindex])', {
					'tabindex': '0',
					'role': 'checkbox',
					'aria-checked': 'false'
				}, 'aria-checked', true);

				sliceTab('.ui-treenode-label').forEach(function (el) {
					el.classList.add('wcag_focus');
				});

				dropDown.forEach(function (val) {
					var text = val.querySelector('label').textContent;
					val.querySelector('span').setAttribute('aria-label', text);
				});

				dropDown[dropDown.length - 1].addEventListener('focusout', function () {
					document.querySelector('.viewGroups span[tabindex]').focus()
				})
			}
		}

		/* szczegóły folderu */
		function szczegolyFodleru() {
			if (location.pathname.match(/szczegolyFolderu/i) !== null) {
				function hangePosition() {
					setTimeout(function() {
						var menu = document.getElementById('form:multiple2_panel');
						if (menu) {
							document.querySelector('body').insertAdjacentElement('afterbegin', menu);
						}
					}, 500);
				}

				function checkDynamic() {
					var read = document.querySelector('.statusDiv1 input#form\\:multiple2_focus');
					if (read) {
						read.removeAttribute('readonly');
					}

					var arr = [].slice.call(document.querySelectorAll('#form\\:multiple2_panel .ui-chkbox-icon'));
					if (arr.length > 0) {
						arr[arr.length - 1].addEventListener('focusout', function() {
							document.getElementById('form:multiple2_panel').style.display = 'none';
							document.querySelector('#form\\:advances_to_accept_datatable table a.wcag_focus:not([tabindex="-1"])').focus()
						});
					}
				}

				var loop = 0;

				if (!!document.documentMode) {
					setInterval(function () {
						var input = [].slice.call(document.querySelectorAll('#form\\:multiple2_panel li'));
						input.forEach(function(element) {
							element.classList.add('wcag_focus');
							element.setAttribute('tabindex', '0');
							element.querySelector('input').setAttribute('tabindex', '-1');
							element.querySelector('input').removeAttribute('readonly');
							if (loop === 0) {
								loop++;
								checkDynamic();
							}
						});
						setTimeout(function() {
							input[0].focus();
						}, 150);
					}, 1000);
				} else {
					var observer = new MutationObserver(function(mutations) {
						var input = [].slice.call(document.querySelectorAll('#form\\:multiple2_panel li'));
						input.forEach(function(element) {
							element.classList.add('wcag_focus');
							element.setAttribute('tabindex', '0');
							element.querySelector('input').setAttribute('tabindex', '-1');
							element.querySelector('input').removeAttribute('readonly');
							if (loop === 0) {
								loop++;
								checkDynamic();
							}
						});
						setTimeout(function() {
							input[0].focus();
						}, 150);

						setTimeout(function() {
							var target = document.getElementById('form:multiple2_panel');
							observer.observe(target, {
								attributes: true,
								attributeFilter: ['style']
							});
						}, 350);
					});
				}

				hangePosition();
				lostFocus();
				checkDynamic();
			}

		}

		/* strona głowna BW */

		function detectDynamicMenu() {
			function hangePosition() {
				setTimeout(function() {
					var menu = document.getElementById('form:multiple_panel');
					if (menu) {
						document.querySelector('body').insertAdjacentElement('afterbegin', menu);
					}
				}, 500);
			}

			setInterval(function () {
				addFocusOnPagination();
			}, 1000);

			function removeReadonly() {
				sliceTab('[readonly]').forEach(function (el) {
					el.removeAttribute('readonly');
				});
			}

			function setTableCaption() {
				var capNeed = document.querySelector('table.wcag_focus');
				if (capNeed) {
					var cap = document.createElement('caption');
					cap.textContent = 'Foldery';
					cap.classList.add('wcag_hide2');
					capNeed.insertAdjacentElement('afterbegin', cap);
				}
			}

			function checkDynamic() {
				document.querySelector('.statusDiv input#form\\:multiple_focus').removeAttribute('readonly');

				var arr = [].slice.call(document.querySelectorAll('#form\\:multiple_panel .ui-chkbox-icon'));
				arr[arr.length - 1].addEventListener('focusout', function() {
					document.getElementById('form:multiple_panel').style.display = 'none';
					document.querySelector('table[role="presentation"] table a:not([tabindex])').focus();
				});

				sliceTab('.nietypowydd').forEach(function (el) {
					el.querySelector('input').addEventListener('keydown', function () {
						sliceTab('.ui-selectcheckboxmenu-panel').forEach(function (el) {
							if (el.style.display === 'block') {
								el.querySelector('li .ui-chkbox-icon').focus();
							}
						});
					});
				});
			}

			var loop = 0;

			if (!!document.documentMode) {
				setInterval(function () {
					var input = [].slice.call(document.querySelectorAll('#form\\:multiple_panel li'));
					input.forEach(function(element) {
						element.classList.add('wcag_focus');
						//element.setAttribute('tabindex', '0');
						element.querySelector('input').setAttribute('tabindex', '-1');
						element.querySelector('input').removeAttribute('readonly');
						if (loop === 0) {
							loop++;
							checkDynamic();
						}
					});
					setTimeout(function() {
						input[0].focus();
					}, 150);
				}, 1000)
			} else {
				var observer = new MutationObserver(function(mutations) {
					var input = [].slice.call(document.querySelectorAll('#form\\:multiple_panel li'));
					input.forEach(function(element) {
						element.classList.add('wcag_focus');
						element.querySelector('input').setAttribute('tabindex', '-1');
						element.querySelector('input').removeAttribute('readonly');
						if (loop === 0) {
							loop++;
							checkDynamic();
						}
					});
					setTimeout(function() {
						input[0].querySelector('span.ui-chkbox-icon').focus();
					}, 150);
				});
				setTimeout(function() {
					var target = document.getElementById('form:multiple_panel');
					if (target) {
						observer.observe(target, {
							attributes: true,
							attributeFilter: ['style']
						});
					}
				}, 350);
			}


			hangePosition();
			lostFocus();
			removeReadonly();
			addingAlert();
			setTimeout(function () {
				setTableCaption();
			}, 2000);
		}

		/* mutation */

		function muta() {
			if (!!document.documentMode) {
				setInterval(function () {
					edycjaBW();
					setPolishWords();
					ariaLabelSet();
					setAttributeOnElement();
					clickBaza();
				}, 1000);
			} else {
				var target = location.pathname.match(/edycjaTresci/i) !== null ?
					document.querySelector('#panelGrupUprawnien') :
					document.querySelector('body');
				var observer = new MutationObserver(function(mutations) {
					edycjaBW();
					setPolishWords();
					ariaLabelSet();
					setAttributeOnElement();
					clickBaza();
				});

				var config = {
					attributes: true,
					childList: true,
					characterData: true
				};

				observer.disconnect();
				observer.observe(target, config);
			}
		}

		function tablee() {
			var target = document.querySelector('head');
			target.addEventListener('DOMSubtreeModified', function() {
				setTimeout(function() {
					setPolishWords();
					addEventsAria();
					setAttributeOnElement();
					createCapAll();
				}, 200);
			});
		}

		/* dodanie focus i attr do checkbox */

		function setCheckState() {
			if (location.pathname.match(/zarzadzanieUzytkownikamiIKontemPT/i) !== null) {
				var checkAgr = document.querySelector('#form\\:zgodaElektronicznePisma input').getAttribute('aria-checked');
				var info = document.querySelector('.opisNeed')
					?  document.querySelector('.opisNeed').textContent
					: 'brak';
				var agr = sliceTab('.agree .ui-chkbox-icon');
				agr.forEach(function(elemnt) {
					elemnt.classList.add('wcag_focus');
					elemnt.setAttribute('aria-label', info);
					checkAgr === 'true' ?
						setAttributes(elemnt, {
							'tabindex': '0',
							'role': 'checkbox',
							'aria-checked': 'true'
						}) : setAttributes(elemnt, {
							'tabindex': '0',
							'role': 'checkbox',
							'aria-checked': 'false'
						});
					elemnt.addEventListener('keydown', function(event) {
						if (event.key === 'Enter'
							|| event.key === ' ') {
							var atr = elemnt.getAttribute('aria-checked');
							elemnt.click();
							event.preventDefault();
							event.stopPropagation();
							atr === 'false' ?
								setAttributes(elemnt, {
									'aria-checked': 'true'
								}) :
								setAttributes(elemnt, {
									'aria-checked': 'false'
								});
							elemnt.focus();
						}
					});
					elemnt.addEventListener('click', function() {
						var value = elemnt.getAttribute('aria-checked');
						value === 'false' ?
							elemnt.setAttribute('aria-checked', 'true') :
							elemnt.setAttribute('aria-checked', 'false');
					});
				});
			}
		}

		/* dodanie focus na paginacji */

		function clickBaza() {
			var help = document.querySelector('.helpCss');
			setTimeout(function() {
				if (help) {
					help.addEventListener('keydown', function(e) {
						if (e.key === 'Enter') {
							document.querySelector('.ui-treenode-content .ui-tree-toggler').click();
						}
					});
				}
			}, 150);
		}

		function dynamincAria() {
			sliceTab('span.ui-paginator-pages').forEach(function (el) {
				el.setAttribute('role', 'list');
			});

			setInterval(function () {
				sliceTab('.sekcjaStopka a[tabindex]').forEach(function (el) {
					el.removeAttribute('tabindex');
				});
			}, 2000);

			setAttributeOnElement();
			usuwanieAtrybutow();

			var aria = sliceTab('[role="navigation"]:last-child span a');
			aria.forEach(function(el) {
				el.classList.add('wcag_focus');
				el.setAttribute('role', 'listitem');
				el.setAttribute('tabindex', '-1');
				el.removeAttribute('aria-label');
				el.removeAttribute('href');
				if (!el.querySelector('span')) {
						var span = document.createElement('span');
						span.textContent = 'Strona ' + el.textContent;
						span.setAttribute('tabindex', '0');
						span.setAttribute('role', 'link');
						span.style.height = '0';
						span.style.width = '0';
						span.style.position = 'absolute';
						span.style.opacity = '0';
						span.style.overflow = 'hidden';
						el.appendChild(span);

						span.addEventListener('focus', function () {
							el.style.outline = '2px solid #1A8AD5';
						});

						span.addEventListener('focusout', function () {
							el.style.outline = '';
						});

						span.addEventListener('keydown', function (e) {
							if (e.key === 'Enter' || e.key === ' ') {
								el.click();
							}
						})
				}
			});
		}


		function ariaLabelSet() {
			var aria_req = sliceTab('.reqAria input, .capReq');
			var helpCss = sliceTab('.ui-treenode-content span.ui-treenode-label');
			var tree = sliceTab('.ui-treenode-label');
			var regulamin = sliceTab('.goodFonts a[target="_blank"]');
			var p1 = document.querySelector('.p1');
			var p2 = document.querySelector('.p2');
			var disIcon = sliceTab('.disIcon');
			var polishCalendar = sliceTab('button.ui-datepicker-trigger');
			var specjalAria = document.getElementById('form:kwotaRoszczenInput_input');
			if (specjalAria) {
				specjalAria.setAttribute('aria-label', 'Kwota roszczeń');
			}

			sliceTab('input[aria-autocomplete="list"]').forEach(function (el) {
				var list = el.parentElement.parentElement;
				if (location.pathname.match(/e-deklaracja.xhtml/i) === null
					&& !el.parentElement.querySelector('label')
					&& list.querySelector('label') !== null) {
					var label = document.createElement('label');
					label.setAttribute('for', el.id);
					label.classList.add('wcag_hide2');
					label.innerText = list.querySelector('label').textContent;

					el.insertAdjacentElement('beforebegin', label);
				}

				if (!el.classList.contains('eventClick123')) {
					var wartosc = false;
					el.classList.add('eventClick123');
					el.addEventListener('keydown', function (e) {
						if (e.key === ' ' || e.key === 'Enter') {
							if (wartosc === false) {
								list.querySelector('label.ui-selectonemenu-label').click();
								wartosc = true;
							} else {
								wartosc = false;
							}
						}
					});
				}


				if (list.hasAttribute('aria-owns')) {
					el.setAttribute('role', 'combobox');
					setAttributes(el, {
						'role': 'combobox',
						'aria-haspopup': 'true',
						'aria-owns': list.getAttribute('aria-owns')

					});

					list.removeAttribute('role');
					list.removeAttribute('aria-haspopup');
					list.removeAttribute('aria-owns');
					list.removeAttribute('aria-expanded');
				}

			});

			sliceTab('div[role="combobox"]').forEach(function (el) {
				[].slice.call((el.querySelectorAll('input[type="text"]'))).forEach(function (em) {
					em.removeAttribute('type');
				});
			});

			sliceTab('input[type="file"][aria-hidden]').forEach(function (el) {
				el.removeAttribute('aria-hidden');
			});

			sliceTab('.gear').forEach(function (el) {
				el.setAttribute('aria-label', 'Ustawienia');
			});

			polishCalendar.forEach(function (el) {
				el.setAttribute('aria-label', 'Pokaż kalendarz');
				el.classList.add('wcag_focus');
			});

			setAttributes(p1, {
				'aria-label': 'Powiększ czcionkę'
			});
			sliceTab('.ui-dialog-titlebar-icon.ui-dialog-titlebar-close.ui-corner-all').forEach(function (el) {
				el.setAttribute('aria-label', "Zamknij okno");
				el.classList.add('wcag_focus');
			});
			setAttributes(p2, {
				'aria-label': 'Pomniejsz czcionkę'
			});
			disIcon.forEach(function(el) {
				setAttributes(el, {
					'aria-hidden': "true"
				});
			});
			aria_req.forEach(function (el) {
				el.setAttribute('aria-required', 'true');
			});
			addDynamicClass(helpCss, 'helpCss');
			addDynamicClass(regulamin, 'wcag_focus');
			addDynamicClass(tree, 'wcag_focus');
			addSimpleAria('.searchInput .goSearch', 'aria-label', "Wyszukaj frazę");
			addSimpleAria('.ui-selectcheckboxmenu-close ui-corner-all', 'aria-label', "Zamknij okno");
		}

		function replaceTag() {
			sliceTab('h3').filter(function (el) {
				return el.textContent === '';
			}).forEach(function (el) {
				var span = document.createElement('span');
				span.innerHTML = el.innerHTML;
				span.classList.add('replaceH3');
				el.parentElement.appendChild(span);
				el.parentElement.removeChild(el);
			});

			addSimpleAria('#form\\:notifications-all', 'aria-label', 'zobacz wszystkie Nieprzeczytane powiadomienia');
			addSimpleAria('#form\\:declarations-all:not(.zmianahasla123)', 'aria-label', 'zobacz wszystkie Deklaracje');

			var dek1 = document.getElementById('form:declarations-latest_header');
			if (dek1) {
				var newDek1 = document.createElement('h2');
				newDek1.innerHTML = dek1.innerHTML;
				newDek1.classList.add('ui-panel-titlebar', 'ui-widget-header', 'ui-helper-clearfix', 'ui-corner-all');
				newDek1.style.paddingTop = '32px';
				dek1.parentNode.replaceChild(newDek1, dek1);
			}
			var dek2 = document.getElementById('form:notifications_header');
			if (dek2) {
				var newDek2 = document.createElement('h2');
				newDek2.innerHTML = dek2.innerHTML;
				newDek2.classList.add('ui-panel-titlebar', 'ui-widget-header', 'ui-helper-clearfix', 'ui-corner-all');
				newDek2.style.paddingTop = '32px';
				dek2.parentNode.replaceChild(newDek2, dek2);
			}

			var tab1 = document.getElementById('form:declarations_grid');
			if (tab1 && location.pathname.match(/rejestracjaPT/i) === null) {
				tab1.setAttribute('role', 'presentation');
				var cap = document.createElement('caption');
				cap.innerText = 'Ostatnie deklaracje';
				cap.classList.add('wcag_hide2', 'tab1');
				tab1.querySelector('table.wcag_focus').appendChild(cap);
				tab1.querySelector('table.wcag_focus').insertAdjacentElement('afterbegin', document.querySelector('.tab1'));
			}

			/* baza wiedzy */

			var folder = document.querySelector('#form\\:folders_header .ui-panel-title');
			if (folder) {
				var newDek1 = document.createElement('h2');
				newDek1.innerHTML = folder.innerHTML;
				newDek1.classList.add('ui-panel-title');
				folder.parentNode.replaceChild(newDek1, folder);
			}
		}

		function greenMenu() {
			greenTab = sliceTab('[class*="MainTab"] > a');
			greenTab.forEach(function(el) {
				setAttributes(el, {
					'role': 'tab',
					'aria-expanded': 'false'
				});
				el.addEventListener('keydown', function(e) {
					if (e.key === 'Enter') {
						var value = el.getAttribute('aria-expanded');
						el.parentNode.click();
						value === 'false' ?
							el.setAttribute('aria-expanded', 'true') :
							el.setAttribute('aria-expanded', 'false');
					}
				});
			});
		}


		function addEventsAria() {
			/* menu boczne */
			eventOnEnter('.wcagTab', {
				'tabindex': '0',
				'aria-expanded': 'false'
			}, 'aria-expanded', true);
			if (location.pathname.match(/edycjafolderu/i) === null &&
				location.pathname.match(/zarzadzanieUzytkownikamiIKontemPT/i) === null &&
				location.pathname.match(/edycjaTresci/i) === null &&
				location.pathname.match(/stronaGlowna/i) === null &&
				location.pathname.match(/wyszukiwanieZaawansowane/i) === null &&
				location.pathname.match(/tworzenieEdycjaKontaUzytkownika/i) === null) {
				eventOnEnter('.ui-chkbox-icon:not([tabindex])', {
					'tabindex': '0',
					'role': 'checkbox',
					'aria-checked': 'false'
				}, 'aria-checked', true);
			}

			sliceTab('.ui-selectbooleancheckbox').forEach(function (el) {
				var label = el.querySelector('.ui-chkbox-label');
				if (label && label.textContent.trim() !== '') {
					el.querySelector('.ui-chkbox-icon').setAttribute('aria-label', label.textContent.trim());
				}
			});

		}

		function teku() {
			if (location.pathname.match(/tworzenieEdycjaKontaUzytkownika/i) !== null) {
				var lista = sliceTab('.ui-picklist-item');
				var listaBtn = sliceTab('.ui-picklist-buttons-cell button');

				addDynamicClass(lista, 'wcag_focus');
				addDynamicClass(listaBtn, 'wcag_focus');

				function addAll() {
					eventOnEnter('.ui-chkbox-box:not(.ui-state-disabled) .ui-chkbox-icon:not([tabindex])', {
						'tabindex': '0',
						'role': 'checkbox',
						'aria-checked': 'false'
					}, 'aria-checked', true);
				}

				function stayFocus() {
						var arr = [];
						sliceTab('span[role="checkbox"]').forEach(function (el, index) {
							if (!el.classList.contains('test-' + index)) {
								el.classList.add('test-' + index);
							} else if (el.classList.contains('test-' + index) && !el.classList.contains('works')) {
								el.classList.add('works');
								el.addEventListener('keypress', function (event) {
									console.log(event);
									if(event.key === 'Enter' || event.key === ' ') {
										arr.push('.test-' + index);
										var lastElement = arr[arr.length - 1];
										setTimeout(function () {
											document.querySelector(lastElement).focus();
											console.error(document.querySelector(lastElement));
										}, 100);
									}
								});
							}
						});
				}

				document.getElementById('form:basic_content').addEventListener('DOMSubtreeModified', function (e){
					stayFocus();
					addAll();
				}, false);
			}
		}

		function perPage() {
			if (location.pathname.match(/tworzenieKorespondencji/i) !== null) {
				setInterval(function () {
					addHover();
					var zalacznik = sliceTab('.zalaczniki span.ui-fileupload-choose');
					zalacznik.forEach(function (el) {
						el.classList.add('button--primary');
					});
				}, 300);
			}

			if (location.pathname.match(/parametryzacjaPowiadomien/i) !== null) {
				sliceTab('table.wcag_focus tr[data-ri]').forEach(function (element) {
					element.setAttribute('role', 'group');
					var id = element.querySelector('.opisKonfig span');
					element.setAttribute('aria-labelledby', id.id);

					if (element.querySelector('.smsGroup > span.mobileHidden')) {
						var czySms = element.querySelector('.smsGroup > span.mobileHidden');
						var span1 = element.querySelector('.sms span.ui-chkbox-icon');
						span1.setAttribute('aria-labelledby', czySms.id);
					}

					if (element.querySelector('.emailGroup > span.mobileHidden')) {
						var czyEmail = element.querySelector('.emailGroup > span.mobileHidden');
						var span2 = element.querySelector('.email span.ui-chkbox-icon');
						span2.setAttribute('aria-labelledby', czyEmail.id);
					}
				});
			}

			if (location.pathname.match(/szczegolyKorespondencji/i) !== null) {
				var inter = setInterval(function () {
					var needClick = sliceTab('.clickMe');
					needClick.forEach(function (el) {
						el.addEventListener('keydown', function (e) {
							if (e.key === 'Enter') {
								el.click();
							}
						})
					});
				}, 1000);
			}

			if (document.querySelector('table')) {
				setInterval(function () {
					sliceTab('table tr.ui-widget-content.ui-datatable-empty-message td').forEach(function (el) {
						if (el.textContent.match(/Brak wynik.w.wyszukiwania/i) !== null ||
							el.textContent.match(/Nie.znaleziono.deklaracji/i) !== null ||
							el.textContent.match(/Brak.wynik.w.dla..adanych.parametr.w/i) !== null) {
							el.setAttribute('role', 'alert');
						}
					});
				}, 500);
			}

			if (location.pathname.match(/tworzenieKorespondencji/i) !== null) {
				setInterval(function () {
					var adresat = document.getElementById('form:adresaci');
					if (adresat) {
						adresat.classList.add('withoutfocus');
					}
					lostFocus();
					setAttributeOnElement();
				}, 500);
			}

			if (location.pathname.match(/listaslownikow/i) !== null) {
				document.querySelector('body').insertAdjacentElement('afterbegin', document.getElementById('form:multiple_panel'));
				document.querySelector('body').insertAdjacentElement('afterbegin', document.getElementById('form:nazwy_panel'));

				var firstArr = document.querySelector('#form\\:multiple_panel div ul li.ui-selectcheckboxmenu-item:last-child span');
				firstArr.addEventListener('focusout', function () {
					document.getElementById('form:multiple_panel').style.display = 'none';
				});

				var secondArr = document.querySelector('#form\\:nazwy_panel div ul li.ui-selectcheckboxmenu-item:last-child span');
				secondArr.addEventListener('focusout', function () {
					document.getElementById('form:nazwy_panel').style.display = 'none';
					document.querySelector('input#form\\:opis').focus();
				});
			}

			if (location.pathname.match(/tworzenieNowejPlatnosci/i) !== null) {
				sliceTab('.ui-confirmdialog-yes, .ui-confirmdialog-no, .ui-dialog-titlebar-icon').forEach(function (el) {
					el.addEventListener('keydown', function (e) {
						if (e.key === 'Enter') {
							setTimeout(function () {
								document.querySelector('.dalejButton').focus();
							}, 200);
						}
					});
				});
			}

			if (location.pathname.match(/autoryzacjaAdministratora/i) !== null) {
				sliceTab('.ui-chkbox-icon').forEach(function (el, index) {
					el.setAttribute('aria-labelledby', 'form:autoryzacjaAdm_zgoda' + (index + 1));
				})
			}


		}

		/* zebatka */

		// function gearShowIndex(callback) {
		// 	var mobileMenu = sliceTab('.nav__open, .menuWWW');
		// 	mobileMenu.forEach(function (el) {
		// 		el.addEventListener('keydown', function (e) {
		// 			if (e.key === 'Enter' || e.key === ' ') {
		// 				el.click();
		// 			}
		// 		});
		// 	});
		//
		// 	var geraII = sliceTab('.zebatka');
		// 	geraII.forEach(function (el) {
		// 		setAttributes(el, {
		// 			'aria-expanded': 'false',
		// 			'role': 'tab',
		// 			'tabindex': '0'
		// 		});
		// 		el.addEventListener('keydown', function(e) {
		// 			if (e.key === 'Enter' || e.key === ' ') {
		// 				if (clickGear === 0) {
		// 					el.click();
		// 					clickGear++;
		// 				} else {
		// 					el.click();
		// 					if (el.querySelector('span.replaceH3 span.ui-icon-triangle-1-s') !== null) {
		// 						setTimeout(function() {
		// 							el.setAttribute('aria-expanded', 'true');
		// 						}, 100);
		// 					} else {
		// 						el.setAttribute('aria-expanded', 'false');
		// 					}
		// 				}
		// 			}
		// 		});
		//
		// 		/* zamyka menu po straceniu focusa z ostatniego elemntu */
		//
		// 		$('.wcag_open').last().focus()
		// 			.focusout(function() {
		// 				$('.wcag_open').closest('li')
		// 					.css('display', 'none');
		// 			});
		// 	});
		// }

		/* chowanie menu głownego po clicku */

		function uglyFunction() {
			if (location.pathname.match(/kontrahent/) === null) {
				/* chowa liste jak straci focus  */
				$('.MainTab1:last-child ul li:last-child a.Tab1').focus().focusout(function() {
					$('.MainTab1 ul.ui-menu-child').hide();
				});
			}
		}

		/* globalne dodanie tabindex */

		function setAttributeOnElement() {
			var arrMenu = sliceTab('.Tab');
			var boczneMenu = sliceTab('.menuWWW');
			var wygaButton = sliceTab('#form\\:j_idt58 button');
			var arrBadIndex = sliceTab('.ui-helper-hidden-accessible');
			var gearI = sliceTab('.wcagZ');
			var arrTopNav = sliceTab('[class*="MainTab"] a');
			var stopka = sliceTab('.sekcjaStopka a');
			var pickList = sliceTab('.pickLista .ui-picklist ul li');
			var bazaEdycja = sliceTab('.ui-g .ui-chkbox input');
			var inputF = sliceTab('[name="form:zgodaElektronicznePisma_input"]');
			var meesageGrowl = sliceTab('.ui-confirm-dialog-message');
			var table = sliceTab('div.ui-datatable-tablewrapper > table');
			var komorkiA = sliceTab('tr.ui-widget-content a.ui-link');
			var folder = sliceTab('.folderIcon');
			var fileIcon = sliceTab('.fileIcon');
			var upload = sliceTab('.ui-fileupload-choose');
			var editor = sliceTab('.ql-editor');
			var inputFocus = sliceTab('.tabelka input');
			var zamykanie = sliceTab('.ui-selectcheckboxmenu-close ui-corner-all');
			var szczegoly = sliceTab('td.toggle div, td.toggler div');
			var specjalLi = sliceTab('.withoutfocus ul li');
			var e_deklaracje = sliceTab('.wcag_need_focus ul li');
			var nrEwi = sliceTab('#form\\:nrEwidencyjny input');
			var e_deklaracje_input = sliceTab('#form\\:panelZakladek\\:mainPanel_content input, #form\\:panelZakladek\\:mainPanel_pu_content input, #form\\:panelZakladek\\:podsumowanieNaleznosci input')
			var inputNumer = sliceTab('.inputNum input');

			addDynamicClass(wygaButton, 'wcag_focus');
			addDynamicClass(upload, 'wcag_focus_btn');
			addDynamicClass(komorkiA, 'wcag_focus');
			addDynamicClass(table, 'wcag_focus');
			addDynamicClass(stopka, 'wcag_focus');
			addDynamicClass(inputFocus, 'wcag_focus');
			addDynamicClass(zamykanie, 'wcag_focus');
			addDynamicClass(szczegoly, 'wcag_focus');
			addDynamicClass(specjalLi, 'wcag_focus');
			addDynamicClass(e_deklaracje, 'wcag_focus');
			addDynamicClass(e_deklaracje_input, 'wcag_focus');
			addDynamicClass(nrEwi, 'wcag_focus');
			addDynamicClass(inputNumer, 'wcag_focus');

			setAttr(folder, '-1');
			setAttr(editor, '-1');
			setAttr(fileIcon, '-1');
			setAttr(meesageGrowl, '0');
			setAttr(stopka, '0');
			setAttr(boczneMenu, '0');
			setAttr(bazaEdycja, '-1');
			setAttr(inputF, '-1');
			setAttr(arrTopNav, '0');
			setAttr(arrMenu, '0');
			setAttr(arrBadIndex, '-1');
			setAttr(gearI, '0');
			setAttr(pickList, '0');
			setAttr(e_deklaracje, '0');
		}

		function lostFocus() {
			var stronaGlownaEdycja = document.getElementById('form:multiple_focus');
			var stronaWysz = document.getElementById('form:multiple1_focus');
			var szczegolyFolderu = document.getElementById('form:multiple2_focus');
			var stronaGlownaDiv = document.querySelector('.statusDiv');
			var szczegolyFolderuDiv = document.querySelector('.statusDiv1');
			var stronaEdycjiTresci = document.querySelector('.firstButtons');
			var styleCss = '0.4em solid #1A8AD5';

			/* szczegoly kores 23.04.2020 */
			sliceTab('input[type="file"]').forEach(function (el) {
				var atr = el.getAttribute('tabindex');
				var parEle = el.parentElement;
				if (atr !== null && (atr === '-1' || atr === '0') || el.classList.contains('dodajMifocus')) {
					el.removeAttribute('tabindex');
					el.classList.add('dodajMifocus');
					el.addEventListener('focus', function () {
						parEle.style.outline = '4px solid #1A8AD5';
					});

					el.addEventListener('focusout', function () {
						parEle.style.outline = '';
					});
				}
				if (parEle !== null) {
					parEle.removeAttribute('tabindex');

				}
			});

			sliceTab('.withoutfocus').forEach(function (ele) {
				ele.querySelector('input').addEventListener('focus', function () {
					if (document.querySelector('html[data-whatinput="keyboard"]') !== null) {
						ele.style.outline = styleCss;
					}
				});
				ele.querySelector('input').addEventListener('focusout', function () {
					ele.style.outline = '';
				});
			});

			/* nietypowy dropdown */
			sliceTab('.nietypowydd').forEach(function (el) {
				el.querySelector('input').addEventListener('keydown', function () {
					sliceTab('.ui-selectcheckboxmenu-panel').forEach(function (el) {
						if (el.style.display === 'block') {
							el.querySelector('.ui-chkbox-icon').focus();
						}
					});
				});
			});

			if (stronaGlownaEdycja && stronaGlownaDiv) {
				stronaGlownaEdycja.addEventListener('focus', function (event) {
					if (event.relatedTarget !== null) {
						stronaGlownaDiv.style.outline = styleCss;
					}
				});
				stronaGlownaEdycja.addEventListener('focusout', function () {
					stronaGlownaDiv.style.outline = '';
				});
			}

			if (stronaWysz && szczegolyFolderuDiv) {
				stronaWysz.addEventListener('focus', function (event) {
					if (event.relatedTarget !== null) {
						szczegolyFolderuDiv.style.outline = styleCss;
					}
				});
				stronaWysz.addEventListener('focusout', function () {
					szczegolyFolderuDiv.style.outline = '';
				});
			}

			if (szczegolyFolderu && szczegolyFolderuDiv) {
				szczegolyFolderu.addEventListener('focus', function (event) {
					if (event.relatedTarget !== null) {
						szczegolyFolderuDiv.style.outline = styleCss;
					}
				});
				szczegolyFolderu.addEventListener('focusout', function () {
					szczegolyFolderuDiv.style.outline = '';
				});
			}

			if (stronaGlownaEdycja && stronaEdycjiTresci) {
				stronaGlownaEdycja.addEventListener('focus', function (event) {
					if (event.relatedTarget !== null) {
						stronaEdycjiTresci.style.outline = styleCss;
					}
				});
				stronaGlownaEdycja.addEventListener('focusout', function () {
					stronaEdycjiTresci.style.outline = '';
				});
			}
		}

		function usuwanieAtrybutow() {
			var remove = function remove (selector) {
				if (selector) {
					selector.removeAttribute('role');
				}
			};
			function removeArr (arr) {
				try {
					if (!Array.isArray(arr)) {
						throw new TypeError(arr + 'is not a array');
					}
					arr.forEach(function (rev) {
						rev.removeAttribute('role');
					});
				} catch (error) {
					console.error(err.message);
					console.error(err.stack);
				}
			}

			sliceTab('[role="tablist"], [role="tab"]').forEach(function (el) {
				el.removeAttribute('role');
			});

			sliceTab('span.fa-plus').forEach(function (el) {
				el.setAttribute('role', 'img');
				el.setAttribute('aria-hidden', 'true');
			});

			sliceTab('input[role="textbox"], textarea[role="textbox"]').forEach(function (el) {
				el.removeAttribute('role');
			});

			sliceTab('td[role="gridcell"]').forEach(function (el) {
				el.classList.add('gridcell');
				el.removeAttribute('role');
			});

			sliceTab('[role="menubar"], [role="menu"]').forEach(function (rol) {
				rol.removeAttribute('role');
			});

			sliceTab('table[role="grid"]').forEach(function (re) {
				re.removeAttribute('role');
			});

			sliceTab('th[aria-label]').forEach(function (el) {
				el.removeAttribute('aria-label');
			});

			sliceTab('[role="menuitem"]').forEach(function (rev) {
				rev.removeAttribute('role');
			});

			sliceTab('h1,h2,h3,h4,h5,h6').filter(function (el) {
				return el.hasAttribute('tabindex');
			}).forEach(function (el) {
				return el.removeAttribute('tabindex');
			});

			var thRole = sliceTab('th[role="columnheader"]');
			var trRole = sliceTab('tr[role="row"]');
			var aButton = sliceTab('a[role="button"]');
			var bButton = sliceTab('button[role="button"]');
			var button = document.getElementById('form:przedluzButton');
			var button1 = document.getElementById('form:wygas');
			var button2 = document.getElementById('form:sessionTimeButton');
			var button3 = document.getElementById('form:confirm');

			removeArr(trRole);
			removeArr(thRole);
			removeArr(aButton);
			removeArr(bButton);
			remove(button);
			remove(button1);
			remove(button2);
			remove(button3);
		}

		function refCode() {
			var h1Top = document.querySelector('h1.topN');
			if (h1Top) {
				document.querySelector('body').insertAdjacentElement('afterbegin', h1Top);
			}

			var inputReq = sliceTab('input');
			inputReq.forEach(function (el) {
				if (/\*/.test(el.placeholder)) {
					el.setAttribute('aria-required', 'true');
				}
			});

			sliceTab('.meclick').forEach(function (el) {
				el.addEventListener('keydown', function (e) {
					if (e.key === 'Enter') {
						el.click();
					}
				});
			});

			var code = document.querySelector('.refreshIcon');
			if (code !== null) {
				code.addEventListener('keydown', function(e) {
					if (e.key === 'Enter') {
						code.click();
					}
				});
			}
		}

		/* dodanie hover do przycisków  */
		function addHover() {
			$('.wcag-ui').mouseover(function() {
				e = $(this);
				e.addClass('ui-state-hover');
			}).mouseout(function() {
				$(this).removeClass('ui-state-hover');
			});
		}

		function wydanieW3() {
			if (location.pathname.match(/listaRaportow/i) !== null ||
				location.pathname.match(/listaRaportowAnomalii/i) !== null) {

				function usunReadonly() {
					sliceTab('input[readonly]').forEach(function (el) {
						el.removeAttribute('readonly');
					})
				}
				var count = 0;
				var i = setInterval(function () {
					usunReadonly();
					count++;
					var raport1 = document.getElementById('form:typRaportu_panel');
					var raport2 = document.getElementById('form:statusRaportu_panel');
					if (raport1) {
						raport1.querySelector('.ui-widget-header .ui-state-default .ui-chkbox-icon').setAttribute('aria-label', 'Zaznacz wszystko');
						document.querySelector('body').insertAdjacentElement('afterbegin', raport1);
						[].slice.call(raport1.querySelectorAll('li span')).pop().addEventListener('focusout', function () {
							document.querySelector('.nietypowydd1 input').focus();
							raport1.style.display = 'none';
						});
					}

					if (raport2) {
						raport2.querySelector('.ui-widget-header .ui-state-default .ui-chkbox-icon').setAttribute('aria-label', 'Zaznacz wszystko');
						document.querySelector('body').insertAdjacentElement('afterbegin', raport2);
						[].slice.call(raport2.querySelectorAll('li span')).pop().addEventListener('focusout', function () {
							document.querySelector('.buttons button:first-child').focus();
							raport2.style.display = 'none';
						});
					}

					if (count === 8) {
						clearInterval(i);
					}

				}, 1000);


			}

			if (location.pathname.match(/listaRaportow.xhtml/i) !== null ||
				location.pathname.match(/listaRaportowAnomalii/i) !== null ||
				location.pathname.match(/nowyRaportAnomalii.xhtml/i) !== null ||
				location.pathname.match(/nowyRaport/i) !== null) {
				setInterval(function () {
					setAttributeOnElement();
					lostFocus();
					growl();
					addHover();
					usuwanieAtrybutow();
					createCapAll();
					eventOnEnter('.ui-chkbox-box:not(.ui-state-disabled) .ui-chkbox-icon:not([tabindex])', {
						'tabindex': '0',
						'role': 'checkbox',
						'aria-checked': 'false'
					}, 'aria-checked', true);
				}, 1000);
			}

			var zalacznik = sliceTab('.zalaczniki span.ui-fileupload-choose');
			zalacznik.forEach(function (el) {
				el.classList.add('button--primary');
			});
			if (location.pathname.match(/szczegolyListyRoszczen/i) !== null) {
				var tr = sliceTab('tr div.ui-row-toggler');

				tr.forEach(function (el) {
					var imie = el.parentElement.parentElement.querySelector('td span.imiee').textContent;
					var nazwisko = el.parentElement.parentElement.querySelector('td span.nazwiskoo').textContent;
					var nrumowy = el.parentElement.parentElement.querySelector('td span.numerUmowyy').textContent;

					el.setAttribute('aria-label','Rozwiń wiersz ' + imie + ' ' + nazwisko + ' numer umowy ' + nrumowy);

					el.addEventListener('focusout', function () {
						if ((/ui-icon-circle-triangle-s/).test(el.classList.value)) {
							el.click();
						}
					});
				});
			}

			if ((/tworzenieNowychDyspozycjiZaliczek.xhtml/i).test(location.pathname) || (/tworzenieNowegoRozliczeniaZaliczki.xhtml/i.test(location.pathname))) {
				setTimeout(function () {
					sliceTab('[aria-describedby="form:j_idt145_content"] .ui-dialog-buttonpane button').forEach(function (el) {
						el.classList.add('button--primary');
					});

					sliceTab('[aria-describedby="form:j_idt143_content"] .ui-dialog-buttonpane button').forEach(function (el) {
						el.classList.add('button--primary');
					});
				}, 800);
			}
		}

		function replaceTags() {
			var replace = document.querySelector('.replaceTag');
			if (replace) {
				var h2C = document.createElement('h2');
				h2C.classList.add('ui-outputlabel', 'ui-widget', 'ui-outputlabel', 'ui-widget','title__label');
				h2C.innerHTML = replace.innerHTML;
				replace.parentNode.insertBefore(h2C, replace);
				replace.parentNode.removeChild(replace);
			}
		}

		function setPolishWords() {
			var paginacja = document.querySelectorAll('.ui-paginator[aria-label="Pagination"]');
			var wybierzSE = document.querySelector('.wybierz span.ui-c.ui-button-text');
			var badWords = sliceTab('.ui-paginator-next');
			var badWords1 = sliceTab('.ui-paginator-prev');
			var badWords2 = sliceTab('.ui-paginator-rpp-label.ui-helper-hidden');
			var selectAtt = document.querySelector('#form\\:multiple1_panel .ui-chkbox input');
			var search = document.querySelector('#form\\:multiple1_panel .ui-selectcheckboxmenu-filter-container input');
			var close = document.querySelector('#form\\:multiple1_panel a.ui-selectcheckboxmenu-close');
			var close1 = document.querySelector('#form\\:multiple_panel a.ui-selectcheckboxmenu-close');
			var miesiac = sliceTab('select.ui-datepicker-month');
			var rok = sliceTab('select.ui-datepicker-year');
			var wylaczenieEwi = sliceTab('.nrEwiSpr');

			if (search) {
				search.classList.add('wcag_focus');
			}
			if (close1) {
				close1.classList.add('wcag_focus');
			}
			if (close) {
				close.classList.add('wcag_focus');
			}
			if (selectAtt) {
				selectAtt.setAttribute('tabindex', '-1');
			}

			miesiac.forEach(function (el) {
				el.classList.add('wcag_focus');
				el.setAttribute('tabindex', '0');
				el.setAttribute('aria-label', 'Wybierz miesiąc');
			});

			rok.forEach(function (el) {
				el.classList.add('wcag_focus');
				el.setAttribute('tabindex', '0');
				el.setAttribute('aria-label', 'Wybierz rok');
			});

			wylaczenieEwi.forEach(function (el) {
				if (el.textContent.trim() === '') {
					sliceTab('.nrEwiJest').forEach(function (off) {
						off.style.display = 'none';
					});
				}
			});

			sliceTab('.ui-selectcheckboxmenu-filter-container input').forEach(function (el) {
				el.setAttribute('aria-label', 'Pole wyszukiwania');
			});

			sliceTab('a.ui-selectcheckboxmenu-close').forEach(function (el) {
				el.setAttribute('aria-label', 'Zamknij okno');
				el.classList.add('wcag_focus');
			});

			badWords.forEach(function(el) {
				el.setAttribute('aria-label', 'Następna strona');
			});
			badWords1.forEach(function(el) {
				el.setAttribute('aria-label', 'Poprzednia Strona');
			});
			badWords2.forEach(function(el) {
				el.textContent = 'Rzędy na stronach: zmiana wartości spowoduje automatyczną zmianę listy wyników wyszukiwania';
			});

			[].forEach.call(paginacja, function (el) {
				el.setAttribute('aria-label', 'numeracja stron');
			});

			if (wybierzSE) {
				wybierzSE.innerText = 'Wybierz';
			}
		}

		function growl() {
			function simpleAddRequier() {
				sliceTab('input').forEach(function (el) {
					if ((/\*/).test(el.placeholder)) {
						el.setAttribute('aria-required', 'true');
					}
				});
			}
			var loop = 0;
			var lop = 0;
			var inter1 = setInterval(function () {
				var target = document.querySelector('.ui-growl');
				if (target) {
					clearInterval(inter1);
					document.querySelector('body').insertAdjacentElement('afterbegin', target);
					if (!!document.documentMode) {
						setInterval(function () {
							usuwanieAtrybutow();
							simpleAddRequier();
							lostFocus();
							if (document.querySelector('.ui-growl .ui-growl-item-container .ui-growl-image-error') !== null) {
								document.querySelector('.ui-growl.ui-widget').setAttribute('role', 'alert');
							} else {
								document.querySelector('.ui-growl.ui-widget').setAttribute('role', 'status');
							}
							var allGrowl = sliceTab('.ui-growl-item-container');
							allGrowl.forEach(function (par, index) {
								par.querySelector('.ui-growl-icon-close').classList.add('wcag_focus');
								par.querySelector('.ui-growl-icon-close').setAttribute('role', 'button');
								par.querySelector('.ui-growl-icon-close').setAttribute('tabindex', '0');
								par.querySelector('.ui-growl-icon-close').setAttribute('aria-label', 'Zamknij komunikat');
								par.querySelector('.ui-growl-icon-close').addEventListener('keydown', function (e) {
									if (e.key === 'Enter' || e.key === ' ') {
										loop = 0;
										this.click();
									}
								});
								par.setAttribute('tabindex', index);
							});
							setTimeout(function () {
								if (allGrowl.length && loop === 0) {
									allGrowl[0].focus();
									loop++;
								}
							}, 250);
						}, 1000)
					} else {
						sliceTab('.ui-growl').forEach(function (el) {
							el.addEventListener('DOMSubtreeModified', function () {
								usuwanieAtrybutow();
								simpleAddRequier();
								lostFocus();
								if (el.querySelector('.ui-growl-item-container .ui-growl-image-error') !== null) {
									el.setAttribute('role', 'alert');
								} else {
									el.setAttribute('role', 'status');
								}
								var allGrowl = sliceTab('.ui-growl-item-container');
								allGrowl.forEach(function (par, index) {
									par.querySelector('.ui-growl-icon-close').classList.add('wcag_focus');
									par.querySelector('.ui-growl-icon-close').setAttribute('role', 'button');
									par.querySelector('.ui-growl-icon-close').setAttribute('tabindex', '0');
									par.querySelector('.ui-growl-icon-close').setAttribute('aria-label', 'Zamknij komunikat');
									par.querySelector('.ui-growl-icon-close').addEventListener('keydown', function (e) {
										if (e.key === 'Enter' || e.key === ' ') {
											loop = 0;
											this.click();
										}
									});
									par.setAttribute('tabindex', index);
								});
								setTimeout(function () {
									if (allGrowl.length && loop === 0) {
										allGrowl[0].focus();
										loop++;
									}
								}, 300);
							});
						});
					}
				}
				lop++;
				if (lop === 10) {
					clearInterval(inter1);
					console.error('Brak elementu growl');
				}
			}, 1000);

		}

		function changeMenu() {
			setTimeout(function() {
				var menu = document.getElementById('wcag');
				if (menu) {
					document.querySelector('body').insertAdjacentElement('afterbegin', menu);
				}
			}, 500);
		}

		function loopFocus() {
			var sejsa = document.querySelector('.popu');
			if (sejsa) {
				sejsa.addEventListener('DOMSubtreeModified', function () {
					document.querySelector('body').insertAdjacentElement('afterbegin', sejsa);
					var foc = document.querySelectorAll('.popu .wcag_focus');
					if (foc.length > 2) {
						foc[2].addEventListener('focusout', function () {
							setTimeout(function () {
								foc[0].focus();
							}, 100);
						});
					}
				});
			}
		}

		function e_dek() {
			if(window.location.pathname.match(/e-deklaracja/i) !== null) {
				function addRoleTooltop() {
					sliceTab('.ui-tooltip').forEach(function (el) {
						if (!el.hasAttribute('role')) {
							el.setAttribute('role', 'tooltip');
						}
					});
					sliceTab('.tooltipWCAG').forEach(function (el) {
						if (!el.hasAttribute('aria-describedby'))
							el.setAttribute('aria-describedby', el.id + 'Tooltip');
					});
				}

				function removeAttOnLi() {
					sliceTab('li.ui-tabs-header').forEach(function (el) {
						el.removeAttribute('aria-expanded');
						el.removeAttribute('aria-selected');
					});
				}
				
				function addLabelOnSearch() {
					var zabe = document.getElementById('form:panelZakladek:rodzaj_filter');
					var podmiot = document.getElementById('form:panelZakladek:podmiot_udzielajcy_filter');
					var waluta = document.getElementById('form:panelZakladek:waluta_edycj_filter');
					var rZabe = document.getElementById('form:panelZakladek:rodzaj_focus');
					var puz = document.getElementById('form:panelZakladek:podmiot_udzielajcy_focus');
					var walutaInput = document.getElementById('form:panelZakladek:waluta_edycj_focus');

					function crlb(text, miejsce, id) {
						var lbe = document.createElement('label');
						lbe.classList.add('wcag_hide2');
						lbe.textContent = text;
						lbe.setAttribute('for', id);
						document.querySelector(miejsce).insertAdjacentElement('afterbegin', lbe);
					}

					if (walutaInput && document.querySelector('#form\\:panelZakladek\\:waluta_edycj > .ui-helper-hidden-accessible > label') === null) {
						crlb('Waluta *', '#form\\:panelZakladek\\:waluta_edycj > .ui-helper-hidden-accessible', walutaInput.id);
					}

					if (puz && document.querySelector('#form\\:panelZakladek\\:podmiot_udzielajcy > .ui-helper-hidden-accessible > label') === null) {
						crlb('Podmiot udzielający zabezpieczeń *', '#form\\:panelZakladek\\:podmiot_udzielajcy > .ui-helper-hidden-accessible', puz.id);
					}

					if (rZabe && document.querySelector('#form\\:panelZakladek\\:rodzaj > .ui-helper-hidden-accessible > label') === null) {
						crlb('Rodzaj zabezpieczenia *', '#form\\:panelZakladek\\:rodzaj > .ui-helper-hidden-accessible', rZabe.id);
					}

					if (zabe && document.querySelector('#form\\:panelZakladek\\:rodzaj_panel > .ui-selectonemenu-filter-container > label') === null) {
						crlb('Wybierz Zabepiezpieczenie', '#form\\:panelZakladek\\:rodzaj_panel > .ui-selectonemenu-filter-container', zabe.id);
					}

					if (podmiot && document.querySelector('#form\\:panelZakladek\\:podmiot_udzielajcy_panel > .ui-selectonemenu-filter-container > label') === null) {
						crlb('Wybierz podmiot', '#form\\:panelZakladek\\:podmiot_udzielajcy_panel > .ui-selectonemenu-filter-container', podmiot.id)
					}

					if (waluta && document.querySelector('#form\\:panelZakladek\\:waluta_edycj_panel > .ui-selectonemenu-filter-container > label') === null) {
						crlb('Wybierz walute','#form\\:panelZakladek\\:waluta_edycj_panel > .ui-selectonemenu-filter-container', waluta.id);
					}
				}

				function replaceTagE() {
					sliceTab('label.replaceLabel').forEach(function (el) {
						var span = document.createElement('span');
						span.innerHTML = el.innerHTML;
						el.parentElement.appendChild(span);
						el.insertAdjacentElement('beforebegin', span);
						el.parentElement.removeChild(el);
					});
				}

				function oneInput(){
					var zablokujInput = [].slice.call(document.querySelectorAll('div.ui-selectonemenu'));

					zablokujInput.forEach(function(el){
						el.addEventListener("keydown", function(event) {
								if (event.which == 13) {
									var inputy = [].slice.call(document.querySelectorAll('input.ui-selectonemenu-filter'));
									inputy.forEach(function(el){
										el.setAttribute("disabled","true")
									})
								}

							}
						)
					});
					function odblokuj(){
						var inputy = [].slice.call(document.querySelectorAll('input.ui-selectonemenu-filter'));

						inputy.forEach(function(el){
							el.removeAttribute("disabled")
						})
					}

					var odblokujInput = [].slice.call(document.querySelectorAll('input.eventClick123'));
					odblokujInput.forEach(function(el,index){
						if (clickGear === 0) {
							clickGear++;
							el.addEventListener("keydown", function(event) {
								if (event.which >= 65 && event.which <= 90 ) {
									odblokuj();
									document.querySelectorAll('.ui-selectonemenu-filter')[index].focus()
								} else {
									odblokuj();
								}
							})
						}
					});
				}

				setInterval(function () {
					var wysokosc = document.getElementById('form:panelZakladek:wysokosc_zabezpieczenia_edycja_input');
					if (wysokosc) {
						wysokosc.classList.add('wcag_focus');
					}

					sliceTab('.ui-tabs-panel').forEach(function (el) {
						if (el.style.display !== 'none'){
							var title = el.querySelector('.eTytyl');
							if (title) {
								document.getElementsByTagName('title')[0].textContent = title.textContent.trim();
							}
						}
					});

					lostFocus();
					ariaLabelSet();
					replaceTagE();
					addRoleTooltop();
					usuwanieAtrybutow();
					createCapAll();
					removeAttOnLi();
					addLabelOnSearch();
					infoDate();
					oneInput();
				}, 1500);
			}
		}

		function createCapAll() {
			setTimeout(function () {
				var tabAll = sliceTab('table.wcag_focus');
				tabAll.forEach(function (cap, index) {
					if (cap.querySelector('caption') === null
						&& document.querySelectorAll('.capTable .ui-panel-title').length > 0) {
						var capTitle = document.querySelectorAll('.capTable .ui-panel-title')[index];
						var capTio = document.createElement('caption');
						capTio.classList.add('wcag_hide2');
						capTio.innerText = capTitle.textContent;
						cap.insertAdjacentElement('afterbegin', capTio);
					}
				});
			}, 2500);

			sliceTab('.ui-panel-titlebar').forEach(function (el) {
				if (location.pathname.match(/weryfikacjaZPesel/i) === null &&
					location.pathname.match(/autoryzacjaAdministratora/i) === null &&
					location.pathname.match(/ekranZmianyHasla/i) === null &&
					location.pathname.match(/weryfikacjaZPesel/i) === null &&
					location.pathname.match(/potwierdzenieAktywacjiKonta/i) === null &&
					location.pathname.match(/stronaGlownaPT/i) === null &&
					document.querySelector('.KST') === null &&
					document.querySelector('.wyszarz') === null) {
					if (location.pathname.match(/kontoUzytkownikaPT/i) !== null) {
						var creatt = document.createElement('h2');
					} else {
						var creatt = document.createElement('h3');
					}
					creatt.classList.add('specjalneH3');
					el.insertAdjacentElement('afterbegin', creatt);
					creatt.insertAdjacentElement('afterbegin', el.querySelector('.ui-panel-title'));
				}
			});

			sliceTab('h3.specjalneH3').forEach(function (remo) {
				if (remo.textContent.trim() === '') {
					remo.parentNode.removeChild(remo);
				}
			});
		}

		function infoDate() {
			var date = sliceTab('.dataOd input');
			var dateDo = sliceTab('.dataDo input');
			dateDo.forEach(function (od) {
				if (document.querySelector('label[for="' + od.id + '"]') === null) {
					var spanDate = document.createElement('span');
					spanDate.textContent = 'dd-mm-rrrr';
					var labelFor = document.createElement('label');
					labelFor.setAttribute('for', od.id);
					labelFor.innerText = 'Data utworzenia do dnia';
					labelFor.classList.add('wcag_hide2');
					labelFor.insertAdjacentElement('beforeend', spanDate);
					od.insertAdjacentElement('beforebegin', labelFor);
					od.classList.add('wcag_focus')
				}
			});
			date.forEach(function (od) {
				if (document.querySelector('label[for="' + od.id + '"]') === null) {
					var spanDate = document.createElement('span');
					spanDate.textContent = 'dd-mm-rrrr';
					var labelFor = document.createElement('label');
					labelFor.setAttribute('for', od.id);
					labelFor.innerText = 'Data utworzenia od dnia';
					labelFor.classList.add('wcag_hide2');
					labelFor.insertAdjacentElement('beforeend', spanDate);
					od.insertAdjacentElement('beforebegin', labelFor);
					od.classList.add('wcag_focus');
				}
			});
		}
		
		function specjalCap() {
			if (location.pathname.match(/rejestracjaPT/) !== null) {
				setInterval(function () {
					sliceTab('table').forEach(function (el) {
						var caption = document.createElement('caption');
						caption.textContent = 'Tabela z adresami e-mail';
						caption.classList.add('wcag_hide2');
						if (el.querySelector('caption') === null) {
							el.insertAdjacentElement('afterbegin', caption);
							usuwanieAtrybutow();
						}
					})
				}, 1000);
			}
		}

		if (window.location.pathname.match(/bazawiedzy\/stronaGlowna/i) !== null ||
			window.location.pathname.match(/szczegolyFolderu/i) !== null ||
			document.querySelector('.KST') !== null) {
			detectDynamicMenu();
		}

		function pKore() {
			if (window.location.href.match(/parametryzacjaKorespondencji/i) !== null) {
				document.getElementById('form:parametryzacjaKorespondencji_email').setAttribute('aria-label', 'Adres e-mail');
				document.getElementById('form:checkValue_input').setAttribute('aria-label', 'Automatyczne przekazywanie na skrzynkę pocztową');
			}
		}

		teku();
		wyszZaa();
		szczegolyFodleru();
		edycjaTesci();
		lostFocus();
		tablee();
		refCode();
		muta();
		edycjaBW();
		setPolishWords();
		dynamincAria();
		addFocusOnPagination(greenMenu());
		ariaLabelSet();
		// gearShowIndex();
		addHover();
		setCheckState();
		setAttributeOnElement();
		perPage();
		uglyFunction();
		clickBaza();
		changeMenu();
		usuwanieAtrybutow();
		wydanieW3();
		replaceTags();
		growl();
		replaceTag();
		addEventsAria();
		loopFocus();
		e_dek();
		createCapAll();
		infoDate();
		specjalCap();
		pKore();
	})()

});

// ]]>
