
jQuery(document).ready(function(){

	/* fancybox */
	if($('a.fancybox').length){
		$('a.fancybox[href^="http://"], a.fancybox[href^="https://"]').filter(function(){ return this.hostname && this.hostname !== location.hostname; }).addClass('link-external').removeClass('fancybox');
		$('a.fancybox').each(function(){
			if(! $(this).attr('title')){ $(this).attr('title', $(this).find('img').attr('title')); }
			if(! $(this).attr('data-fancybox')){ $(this).attr('data-fancybox', 'gallery-'+ $(this).closest('div[id]').attr('id')); }
			/* data-caption */
			if(! $(this).attr('data-caption')){ $(this).attr('data-caption', $(this).attr('title')); }
			if($(this).next('figcaption').length){ $(this).attr('data-caption', $(this).next('figcaption').text()); }
			if($(this).parents('.news').length && $(this).closest('.mediaelement').next('.news-img-caption').length){ $(this).attr('data-caption', $(this).closest('.mediaelement').next('.news-img-caption').text()); }
		});
	}
	$.loadScript('/typo3conf/ext/pm_fancybox/Resources/Public/fancybox-master/dist/jquery.fancybox.min.js', function(){
		$('[data-fancybox]').fancybox({
			arrows : true,
			infobar : true,
			smallBtn: "auto",
			toolbar: "auto",
			protect: true,
			buttons: [
				"zoom",
				"slideShow",
				"fullScreen",
				"thumbs",
				"close"
			],
			iframe : {
				preload : false
			}
		});
		$.fancybox.defaults.hash = false;
	});

});
/* eof */

function setCookie(cname, cvalue, exdays) {
	var d = new Date();
	var maxage = 'max-age='+ 24 * 60 * 60 * 365;
	/* document.cookie = cname + '=' + cvalue + 'ts' + Date.now() +   ';' + maxage + ';path=/'; */
	document.cookie = cname + '=' + cvalue + ';' + maxage + ';path=/';
};

function getCookie(cname) {
	var name = cname + '=';
	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);
		}
		if (c.indexOf(name) == 0) {
			return c.substring(name.length, c.length);
		}
	}
	return '';
};

$(document).ready(function(){
	$('.waconcookiemanagement').appendTo('body');
	var wert = getCookie('waconcookiemanagement');
	/* resc = res.split('ts');
	wert = resc[0]; */
	if (wert != '') {
		if (wert == 'max') {
			$('.cookie-on').show();
			$('.cookie-off').hide();
		} else {
			res = wert.split('c');
			jQuery.each( res, function( i, val ) {
				$('.cookie-on.cookie' + val).show();
				$('.cookie-off.cookie' + val).hide();
			});
		}
	} else {
		if(!$('.waconcookiemanagement').hasClass('firsthidden')){
			$('.waconcookiemanagement').show();
			$('.cookieclose').hide();
		}
	}
});
$('.cookie-set').on('click',function(){
	var wert = getCookie('waconcookiemanagement');
	/*resc = res.split('ts');
	wert = resc[0]; */
	if (wert != '') {
		if (wert == 'max') {
			$('.cookie-on').show();
			$('.cookie-off').hide();
		} else {
			res = wert.split('c');
			jQuery.each( res, function( i, val ) {
				$('.cookie-on.cookie' + val).show();
				$('.cookie-off.cookie' + val).hide();
			});
		}
	}
	$('.waconcookiemanagement').show();
	if($(this).hasClass('cookiecontent')){
		$('.box-cookie-management').show();
		$('.cookie-fix').show();
		var varGes = $('#CookieBox').height();
		var varFix = $('.cookie-fix').height();
		var neuHeight = varGes - varFix - 100;
		if(neuHeight >= 300){$('.box-cookie-management').height(neuHeight);}
		if(neuHeight < 300){$('#CookieBox').css('overflow','auto');}
		$('.intro').hide();
		for (i = 0; i < 30; i++) {
			if($(this).hasClass('cookieuid-' + i)){
				$('.cookieinfo-' + i).parent().show();
			}
		}
	}
});
$('.cookie-save').on('click',function() {
	wert = '';
	var i;
	for (i = 0; i < 999; i++) {
		if($('.cookie-on.cookie'+i).css('display') == 'block'){
			wert += 'c' + i;
		}
	}
	if (wert == '') {wert = 'min';}
	setCookie('waconcookiemanagement', wert, Date.now());
	$('.waconcookiemanagement').hide();
	location.reload();
});
$('.cookie-accept a').on('click',function() {
	setCookie('waconcookiemanagement', 'max', Date.now());
	$('.waconcookiemanagement').hide();
	location.reload();
});
$('.cookie-refuse').on('click',function() {
	setCookie('waconcookiemanagement', 'min', Date.now());
	$('.waconcookiemanagement').hide();
	location.reload();
});
$('.cookie-management').on('click',function() {
	$('.box-cookie-management').show();
	$('.cookie-fix').show();
	var varGes = $('#CookieBox').height();
	var varFix = $('.cookie-fix').height();
	var neuHeight = varGes - varFix - 100;
	if(neuHeight >= 300){$('.box-cookie-management').height(neuHeight);}
	if(neuHeight < 300){$('#CookieBox').css('overflow','auto');}
	$('.intro').hide();
});
$('.cookieback').on('click',function() {
	$('.box-cookie-management').hide();
	$('.cookie-fix').hide();
	$('.intro').show();
});
$('.show-n').on('click',function() {
	$('.box-cookie-management').show();
	$('.cookie-n').next().show();
	$('.cookie-n').hide();
	$('.intro').hide();
});
$('.show-m').on('click',function() {
	$('.box-cookie-management').show();
	$('.cookie-m').next().show();
	$('.cookie-m').hide();
	$('.intro').hide();
});
$('.show-s').on('click',function() {
	$('.box-cookie-management').show();
	$('.cookie-s').next().show();
	$('.cookie-s').hide();
	$('.intro').hide();
});
$('.show-e').on('click',function() {
	$('.box-cookie-management').show();
	$('.cookie-e').next().show();
	$('.cookie-e').hide();
	$('.intro').hide();
});
$('.cookie-on.cookiecat').on('click',function() {
	$(this).parent().find('.cookie-off').show();
	$(this).parent().find('.cookie-on').hide();
});
$('.cookie-off.cookiecat').on('click',function() {
	$(this).parent().find('.cookie-on').show();
	$(this).parent().find('.cookie-off').hide();
});
$('.cookie-info .cookie-on').on('click',function() {
	$(this).prev().show();
	$(this).hide();
	var children=$(this).parent().parent().children();
	var cookieoff = 0;
	for(var i=0;i<children.length;i++){
		if(children.eq(i).find('.cookie-off').css('display') == 'none'){
			cookieoff += 1;
		}
	}
	if(cookieoff==0){
		$(this).parent().parent().parent().find('.cookie-off.cookiecat').show();
		$(this).parent().parent().parent().find('.cookie-on.cookiecat').hide();
	}
});
$('.cookie-info .cookie-off').on('click',function() {
	$(this).next().show();
	$(this).hide();
	$(this).parent().parent().parent().find('.cookie-off.cookiecat').hide();
	$(this).parent().parent().parent().find('.cookie-on.cookiecat').show();
});
$('.info-show').on('click',function() {
	$(this).next().show();
	$(this).hide();
});
$('.info-hide').on('click',function() {
	$(this).parent().hide();
	$(this).parent().prev().show();
});
$('.cookieclose').on('click',function() {
	// added: if...
	if(! getCookie('waconcookiemanagement')) {
		setCookie('waconcookiemanagement', 'min', Date.now());
	}
	$('.waconcookiemanagement').hide();
});
// added: .cookie-legal a
$('.cookie-legal a').on('click',function() {
	setCookie('waconcookiemanagement', 'min', Date.now());
});
var $menu = $('#CookieBox');
$('.waconcookiemanagement').on('click', function (e) {
	var wert = getCookie('waconcookiemanagement');
	if (wert != '') {
		// If element is opened and click target is outside it, hide it
		if ($menu.is(':visible') && !$menu.is(e.target) && !$menu.has(e.target).length) {
			$('.waconcookiemanagement').hide();
		}
	}
});

//
///** override core functions **/
//(function(window, document, $, CORE, undefined) {
//	"use strict";
//	CORE.global.forms = function() {
//		if($('form').length){
//			$.loadScript('/typo3conf/ext/pm_theme_uksh/Resources/Public/JavaScript/forms.js');
//		}
//	};
//	return CORE;
//})(window, document, jQuery, window.CORE || {});
//


jQuery(document).ready(function(){

	/* *
	 * Google Maps
	 */
	if($('.map').length){
		$.loadScript('/typo3conf/ext/pm_theme_uksh/Resources/Public/JavaScript/jquery.gmap.js');
	}


	/* *
	 *  Bildunterschriften Verlinkung
	 */

	$( ".frame.frame-layout-7 figure.image" ).each(function( index ) {
		var link = $(this).find("a").attr('href');
		$(this).find("> figcaption p").wrap(function() {
			return "<a href="+link+" target='_blank'></a>";
		});
	});


	/* *
	 * image slider
	 * http://jquery.malsup.com/cycle2/demo/
	 */
	if($('.imageslider .slider-item').length > 1) {
		$.loadScript('/typo3conf/ext/pm_core/Resources/Public/JavaScript/Contrib/jquery.cycle2.min.js', function(){
			//console.log(textStatus);
			$('.imageslider').data('cycle-pause-on-hover', true).data('cycle-pager-template', '<span class="pager" />').append('<div class="cycle-pager" /><span class="cycle cycle-prev" /><span class="cycle cycle-next" />').cycle({
				slides: '> .slider-item',
				//'fx': 'carousel',
				//'carousel-visible': '1',
				//'carousel-fluid': true,
				timeout: 5000,
				speed: 2500
			});
			// bind the swipe callback function to the swipe event on element
			//$('.imageslider').on('swiperight', $('.imageslider .cycle-prev').click() );
			//$('.imageslider').on('swipeleft', $('.imageslider .cycle-next').click() );
		});
	}

	if($('.accordion-content').length) {
		$(this).find(".accordion-content--item[itemno=1]").each(function(){
			$(this).removeClass('closed');
			$(this).find('.accordion-item--header').each(function () {
				$(this).addClass('open');
			});


		});
	}


	/* Digitale Spuren Popup */

	/* *
     * detect viewport dimensions
     */
	var windowHeight, windowWidth;
	function viewport(){
		var e = window, a = 'inner';
		if(!('innerWidth' in window )){
			a = 'client';
			e = document.documentElement || document.body;
		}
		return { height : e[ a+'Height' ], width : e[ a+'Width' ] };
	}
	windowHeight = viewport().height; windowWidth = viewport().width;
	$(window).on('resize', function(){ windowHeight = viewport().height; windowWidth = viewport().width; });
	// end: viewport dimensions


	if( $('#popup').length ){
		$.loadScript('/typo3conf/ext/pm_theme_uksh/Resources/Public/JavaScript/jquery.cookie.js', function(){
			function openOCB(){
				$('#popup').addClass('open').removeClass('closed');
				// $('#overlay').show();
			}
			function closeOCB(){
				$('#popup').removeClass('open').addClass('closed');
			}
			function closeOCBSetCookie(){
				closeOCB();
				$.setCookie('popup_closed', 'yes', 30);
			}
			function toggleOCB(){
				if($.getCookie('popup_closed')){
					closeOCB();
				} else {
					if((viewport().width <= 812) && (viewport().height <= 812)) {
						closeOCB();
					} else {
						openOCB();
					}
				}
			}
			toggleOCB();
			$(window).on('resize', function(){
				toggleOCB();
			});
			$('#popup .ce-textpic').prepend('<div id="closePOPUPLayer" class="toggle" title="schließen"></div>');
			$('#popup header').on('click', function(){
				openOCB();
				$.removeCookie('popup_closed');
			});
			$('#popup .link-page').on('click', function(){
				closeOCB();
				$.setCookie('popup_closed', 'yes', 30);
			});

			$('#closePOPUPLayer').on('click', function(){ closeOCBSetCookie(); });
			// $('#popup .link-page').on('click', function(){
			// 	setTimeout(function(){
			// 		// if( $('#popup .powermail_create').length ) {
			// 			setTimeout(function(){ closeOCBSetCookie(); $('#popup').hide(); }, 5000);
			// 		// }
			// 	}, 5000);
			// });
		});
	}


//	/* *
//	 * slick slider
//	 * https://kenwheeler.github.io/slick/
//	 */
//	if( $('.slick-slider').length ){
//		$.loadScript('/typo3conf/ext/pm_core/Resources/Public/JavaScript/Contrib/slick.min.js', function(){
//			//console.log(textStatus);
//			var $showItems = 1;
//			function getSlickSliderSettings(){
//				return {
//					infinite: true,
//					variableWidth: false,
//					slidesToShow: $showItems,
//					slidesToScroll: 1,
//					speed: 800,
//					autoplay: true,
//					autoplaySpeed: 5000
//				}
//			}
//			function slickSlider(){
//				$('.slick-list').each(function(i, el){
//					$(el).attr({'overflow':'visible'});
//					$(el).find('.slick-list .item').css({'width': ($(el).width() /$showItems) +'px'});
//					$(el).slick(getSlickSliderSettings());
//				});
//			}
//			/* start function */
//			slickSlider();
//		});
//	}


	/* *
	 * animate header images background position on scroll (parallax)
	 */
	function animateHeadPos(elem){
		$(window).on('scroll', function(){
			if( $(window).scrollTop() >= (elem.height() /5 *3) ){ // if 'scroll to top' is more than 'element height' => no scroll
				elem.addClass('fixed');
			} else {
				elem.css({'top': ($(window).scrollTop() /5) +'px'});
				elem.removeClass('fixed');
			}
		});
	}
	/*
	if($('#content-header .imageslider').length){ animateHeadPos($('#content-header .imageslider .slider-item')); }
	if($('#content-header .tx-go-maps-ext').length){ animateHeadPos($('#content-header .tx-go-maps-ext')); }
	 */


	/* *
	 * functions on resize window
	 *
	 * 		same as changing landscape/horizontal
	 */
//	var w = $(window).width();
//	$(window).resize(function(){ // on resize restore default view
//		clearTimeout(window.resizedFinished);
//		window.resizedFinished = setTimeout(function(){
//			// console.log('resize finished ' +$(window).width());
//			if($(window).width()==w) return; // if don't changing window width -> do nothing
//			if($(window).width()<=w && $('#nav-toggle').is(':visible')){ // if resize window and mobile view
//			}
//			if($(window).width()>=w && ! $('#nav-toggle').is(':visible')){ // if enlarge window and no mobile view
//				$('#mainFooter .footer-menu .menu-level').removeClass('open');
//			}
//			w = $(window).width();
//		}, 250);
//	});


});
/* eof */
