
/*!
 * enquire.js v2.1.6 - Awesome Media Queries in JavaScript
 * Copyright (c) 2017 Nick Williams - http://wicky.nillia.ms/enquire.js
 * License: MIT */

!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,b.enquire=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){function d(a,b){this.query=a,this.isUnconditional=b,this.handlers=[],this.mql=window.matchMedia(a);var c=this;this.listener=function(a){c.mql=a.currentTarget||a,c.assess()},this.mql.addListener(this.listener)}var e=a(3),f=a(4).each;d.prototype={constuctor:d,addHandler:function(a){var b=new e(a);this.handlers.push(b),this.matches()&&b.on()},removeHandler:function(a){var b=this.handlers;f(b,function(c,d){if(c.equals(a))return c.destroy(),!b.splice(d,1)})},matches:function(){return this.mql.matches||this.isUnconditional},clear:function(){f(this.handlers,function(a){a.destroy()}),this.mql.removeListener(this.listener),this.handlers.length=0},assess:function(){var a=this.matches()?"on":"off";f(this.handlers,function(b){b[a]()})}},b.exports=d},{3:3,4:4}],2:[function(a,b,c){function d(){if(!window.matchMedia)throw new Error("matchMedia not present, legacy browsers require a polyfill");this.queries={},this.browserIsIncapable=!window.matchMedia("only all").matches}var e=a(1),f=a(4),g=f.each,h=f.isFunction,i=f.isArray;d.prototype={constructor:d,register:function(a,b,c){var d=this.queries,f=c&&this.browserIsIncapable;return d[a]||(d[a]=new e(a,f)),h(b)&&(b={match:b}),i(b)||(b=[b]),g(b,function(b){h(b)&&(b={match:b}),d[a].addHandler(b)}),this},unregister:function(a,b){var c=this.queries[a];return c&&(b?c.removeHandler(b):(c.clear(),delete this.queries[a])),this}},b.exports=d},{1:1,4:4}],3:[function(a,b,c){function d(a){this.options=a,!a.deferSetup&&this.setup()}d.prototype={constructor:d,setup:function(){this.options.setup&&this.options.setup(),this.initialised=!0},on:function(){!this.initialised&&this.setup(),this.options.match&&this.options.match()},off:function(){this.options.unmatch&&this.options.unmatch()},destroy:function(){this.options.destroy?this.options.destroy():this.off()},equals:function(a){return this.options===a||this.options.match===a}},b.exports=d},{}],4:[function(a,b,c){function d(a,b){var c=0,d=a.length;for(c;c<d&&b(a[c],c)!==!1;c++);}function e(a){return"[object Array]"===Object.prototype.toString.apply(a)}function f(a){return"function"==typeof a}b.exports={isFunction:f,isArray:e,each:d}},{}],5:[function(a,b,c){var d=a(2);b.exports=new d},{2:2}]},{},[5])(5)});
/**
 * Scoping to NWS-Namespace and jQuery $
 */
(function (NWS, $, undefined) {

	const addAttributeValue = (newAttr) => (i, prevAttr) => ((prevAttr || '') + ' ' + newAttr).trim();

	/**
	 * +++++++++++++++++++++++++++++++++++++++++++
	 * ++ Some Component
	 * +++++++++++++++++++++++++++++++++++++++++++
	 */
	NWS.initSomeComponent = function () {
		// Start writing the code for your component
		// e.g. $(".flexslider").flexslider();
	};

	/**
	 * +++++++++++++++++++++++++++++++++++++++++++
	 * ++ Load random image
	 * ++ Define a candiate by setting attribute data-image-random-candidate
	 * ++ It will then load a random candiate by setting all its image sources.
	 * ++ Define the image sources by attribute data-image-random-src="url" so it doesnt get preloaded
	 * +++++++++++++++++++++++++++++++++++++++++++
	 */
	NWS.loadRandomImageFromCandidates = function () {
		const candidates = document.querySelectorAll('[data-image-random-candidate]');
		const theChosenOneIndex = Math.floor(Math.random() * candidates.length);

		for(let i = 0; i < candidates.length; i++) {
			const candidate = candidates[i];
			// https://media.tenor.com/TxxK-usCnQAAAAAC/star-wars-chosen-one.gif
			const candidateIsTheChosenOne = i === theChosenOneIndex;
			if (!candidateIsTheChosenOne) {
				const group = candidate.dataset.imageRandomCandidate;
				for (const elInGroup of document.querySelectorAll('[data-image-random-group="'+group+'"]')) {
					elInGroup.remove();
				}
				candidate.remove();
				continue;
			}
			const images = candidate.querySelectorAll('[data-image-random-src]');
			for(const image of images) {
				const src = image.dataset.imageRandomSrc;
				if (image.tagName === "SOURCE") {
					image.srcset = src;
				} else {
					image.src = src;
				}
			}
			if (images.length === 0) {
				console.warn("This random image candidate does not appear to have images to show:", candidate);
			}
		}
	};


	/**
	 * +++++++++++++++++++++++++++++++++++++++++++
	 * ++ Adds expiration dates for localStorage
	 * ++ Code is from here: https://www.sohamkamani.com/javascript/localstorage-with-ttl-expiry/
	 * +++++++++++++++++++++++++++++++++++++++++++
	 */
	NWS.initLocalStorageWithExpiry = function () {
		window.setWithExpiry = function(key, value, ttl) {
			const now = new Date()

			// `item` is an object which contains the original value
			// as well as the time when it's supposed to expire
			const item = {
				value: value,
				expiry: now.getTime() + ttl,
			}
			localStorage.setItem(key, JSON.stringify(item))
		}

		window.getWithExpiry = function(key) {
			const itemStr = localStorage.getItem(key)
			// if the item doesn't exist, return null
			if (!itemStr) {
				return null
			}
			const item = JSON.parse(itemStr)
			const now = new Date()
			// compare the expiry time of the item with the current time
			if (now.getTime() > item.expiry) {
				// If the item is expired, delete the item from storage
				// and return null
				localStorage.removeItem(key)
				return null
			}
			return item.value
		}
	};

	/**
	 * +++++++++++++++++++++++++++++++++++++++++++
	 * ++ SVG replace
	 * ++ Convert to inline svg
	 * +++++++++++++++++++++++++++++++++++++++++++
	 */
	NWS.initSvgReplace = function () {
		$("img[src$='.svg']:not(.dnt-replace)").each(function (index) {
			const $img = $(this);
			const imgID = $img.attr('id') || ('svg-img-' + index);
			const imgClass = $img.attr('class');
			const imgURL = $img.attr('src');
			const imgTitle = $img.attr('alt');
			$.get(imgURL, function (data) {
				// Get the SVG tag, ignore the rest
				let $svg = $(data).find('svg');
				// Add replaced image's ID to the new SVG
				if (typeof imgID !== 'undefined') {
					$svg = $svg.attr('id', imgID);
				}
				// Add replaced image's classes to the new SVG
				if (typeof imgClass !== 'undefined') {
					$svg = $svg.attr('class', imgClass + ' replaced-svg');
				}
				// Remove any invalid XML tags as per http://validator.w3.org
				$svg = $svg.removeAttr('xmlns:a');
				// Add Title to SVG
				if (typeof imgTitle !== 'undefined') {
					const titleId = imgID + '-svg-title';
					$svg = $svg.attr('aria-labelledby', addAttributeValue(titleId));
					$svg = $svg.prepend('<title id="' + titleId + '">' + imgTitle + '</title>');
				}
				// Replace image with new SVG
				$img.replaceWith($svg);
			}, 'xml');
		});
	};

	/**
	 * Adds the following functionality:
	 * - There is a fake translation icon, which on click shows a confirmation modal (bc. of sending the data to third party)
	 * - on dismiss nothing happens
	 * - On clicking on the button to grant permission, the real script (see htmlToInjectOnGrant) is loaded into the DOM
	 * - Then the translator initializes and will become available
	 *
	 * And we save the confirmation in localstorage so the user does not have to do that each visit.
	 */
	NWS.init2ClickTranslationSolution = function() {
		const $askForPermissionModal = $('#askTranslatePermissionModal');
		const $initialToggleButtons = $('.gtranslate_fake_globe');
		const $grantPermissionButtons = $('.gtranslate_allow_script_btn');
		const htmlToInjectOnGrant = '<script src="https://cdn.gtranslate.net/widgets/latest/globe.js" defer></script>';
		const storageDecisionKey = 'user_agreed_to_gtranslate';

		const askForTranslationPermission = () => $askForPermissionModal.modal('show')
		const applyGrantedPermission = () => {
			setWithExpiry(storageDecisionKey, "1", 24 * 60 * 60)
			$askForPermissionModal.modal('hide')
			const $scriptEl = $(htmlToInjectOnGrant)
			$initialToggleButtons.remove()
			$(document.body).append($scriptEl)
		}

		const permissionWasAlreadyGranted = getWithExpiry(storageDecisionKey) === "1"

		if (!permissionWasAlreadyGranted) {
			$initialToggleButtons.click(askForTranslationPermission)
			$grantPermissionButtons.click(applyGrantedPermission)
		} else {
			applyGrantedPermission();
		}
	}

	/**
	 * Performs DOM manipulation, triggered by the html elements itself, via data attributes
	 */
	NWS.initDomManipulation = function() {
		$('[data-manip-append-to]').each(function(index, el) {
			const $el = $(el);
			const $target = $($el.attr('data-manip-append-to'));
			$target.append($el);
		})
	}

	/**
	 * +++++++++++++++++++++++++++++++++++++++++++
	 * ++ open Submenu
	 * +++++++++++++++++++++++++++++++++++++++++++
	 */
	NWS.initOpenMenu = function () {
		$(".mega-menu").modal('show');
	};

	/**
	 * +++++++++++++++++++++++++++++++++++++++++++
	 * ++ Close Menu
	 * +++++++++++++++++++++++++++++++++++++++++++
	 */
	NWS.initCloseMenu = function () {
		$(".mega-menu").modal('hide');
	};

	/**
	 * +++++++++++++++++++++++++++++++++++++++++++
	 * SubMenu Toggle
	 * +++++++++++++++++++++++++++++++++++++++++++
	 */

	NWS.initToggleSubmenu = function () {
		var t = $(this).parent();
		if (t.hasClass('expanded')) {
			return true;
		}
		else {
			$("li.hassub", t.parent()).removeClass("expanded");
			t.addClass('expanded');
			return false;
		}
	};

	/**
	 * +++++++++++++++++++++++++++++++++++++++++++
	 * ++ To top smooth scroll
	 * +++++++++++++++++++++++++++++++++++++++++++
	 */
	NWS.initToTopSmoothScroll = function () {
		$(".linkToTop a, #totop-wrapper").click(function () {
			$("html, body").animate({
				scrollTop: 0
			}, "slow");
			return false;
		});
	};

}(window.NWS = window.NWS || {}, jQuery));

(function (NWS, $, undefined) {

	$(function () {
		//NWS.initSomeComponent();
		NWS.initLocalStorageWithExpiry();
		NWS.initSvgReplace();
		NWS.initToTopSmoothScroll();
		NWS.init2ClickTranslationSolution();
		NWS.initDomManipulation();

		var MenuOpenToggler = $(".menu-open");
		MenuOpenToggler.on('click', NWS.initOpenMenu);

		var MenuCloseToggler = $(".menu-close");
		MenuCloseToggler.on('click', NWS.initCloseMenu);
	});

}(window.NWS = window.NWS || {}, jQuery));

// This low level code can be executed directly
NWS.loadRandomImageFromCandidates();

var navbarLis = $('#nav-main li.hassub > a');
enquire.register("screen and (max-width: 767px)", {
	match: function () {
		navbarLis.on('click', NWS.initToggleSubmenu);
	},
	unmatch: function () {
		navbarLis.off('click', NWS.initToggleSubmenu).removeClass('expanded');
	}
}, true);
	$(function() {
		$("a.lightbox")
		.parents(".ce-image, .ce-textpic, .news-img-wrap")
		.each(function() {
			$(this).magnificPopup({
				delegate: 'a.lightbox',
				removalDelay: 150,
				mainClass: 'mfp-fade',
				type: 'image',
				gallery: {
					enabled: true,
					navigateByImgClick: false
				},
				image: {
					titleSrc: function(item) {
						if (item.el[0].nextElementSibling == null) {
							return [item.el.attr('title')];
						}
						else {
							return [item.el.attr('title'), '<small>' + item.el[0].nextElementSibling.innerText + '</small>'];
						}
					}
				}
			});
		});

		// See https://github.com/dimsemenov/Magnific-Popup/issues/291#issuecomment-34992197
		var curLang = $('html').attr('lang');
		switch (curLang) {
			case 'de':
				$.extend(true, $.magnificPopup.defaults, {
					tClose: 'Schließen',
					tLoading: 'Wird geladen...',
					gallery: {
						tPrev: 'Zurück',
						tNext: 'Weiter',
						tCounter: '%curr% von %total%'
					},
					image: {
						tError: 'Das <a href="%url%">Bild</a> konnte nicht geladen werden.'
					},
					ajax: {
						tError: 'Der <a href="%url%">Inhalt</a> konnte nicht geladen werden.'
					}
				});
			break;
			default:
				// english already included
		}
	});