const loader = document.querySelector('.preloader');
var mailLinkClicked = false;
if (loader) {
	let timeout = setTimeout(() => {
		loader.classList.remove('show');
		setTimeout(function () {
			document.body.classList.add('preloader-off');
			window.dispatchEvent(new CustomEvent('preloader-off'))
		}, 500);
	}, 10000);

	window.addEventListener('load', function () {
		'use strict';
		loader.classList.remove('show');
		setTimeout(function () {
			document.body.classList.add('preloader-off');
			window.dispatchEvent(new CustomEvent('preloader-off'))
		}, 500);
		clearTimeout(timeout);
	});

	var unloadTriggered = false;

	if (!document.body.classList.contains('preloader-loading-only')) {
		window.addEventListener('beforeunload', function (event) {
			'use strict';

			if (unloadTriggered) {
				return true;
			}
			setTimeout(function () {
				if (mailLinkClicked) {
					mailLinkClicked = false;
					return;
				}
				document.body.classList.remove('preloader-off');
				setTimeout(function() {
					loader.classList.add('show');
				}, 100);

				unloadTriggered = true;
				setTimeout(function () {
					window.dispatchEvent(new Event('beforeunload'));
				}, 400);
				return undefined;
			}, 100);

		});
	}

	const links = document.querySelectorAll('a[href*="mailto:"], a[href$=".zip"]');
	links.forEach(function (link) {
		link.addEventListener('click', function (event) {
			mailLinkClicked = true;
		});
	})
}
