
// Consent class
let ConsentApp = new function ConsentController() {
    //-- global variables ---
    window.dataLayer = window.dataLayer || [];

    //--- public functions ---
    /**
     * Callback function for GoogleTagManager Script to fire the dataLayer trigger
     * @param bool state
     * @param object service
     */
    this.consentChanged = function (state, service) {
        if (state === true) {
            if (service.name.indexOf('google-tagmanager-service') !== -1) {
                let tempObj = {
                    event: service.gtm.trigger
                };
                tempObj[service.gtm.variable] = true;
                window.dataLayer.push(tempObj);

                /*
                //ES6 - https://stackoverflow.com/questions/11508463/javascript-set-object-key-by-variable
                window.dataLayer.push({
                    event: service.name,
                    [service.name]: true
                });
                */
            }
        } else if (state === false) {
            if (service.name.indexOf('google-tagmanager-service') !== -1) {
                let tempObj = {
                    event: service.gtm.trigger
                };
                tempObj[service.gtm.variable] = false;
                window.dataLayer.push(tempObj);
				    }
        }

        //Check if the own callback function is allready defined
        if (typeof window[service.ownCallback] === "function") {
            window[service.ownCallback](state, service);
        } else if (service.ownCallback !== '') {
            console.error('The Callback function ' + service.ownCallback + ' is not yet defined. Please create it first.');
        }
    };

    //--- constructor ---
    (function contruct() {
        $(document).ready(function () {
            //Listener for the button on the privacy page, to edit the consent
            $(document).on('click', '.js-showConsentModal', function (event) {
                event.preventDefault();
                klaro.show();
            });
        });
    })();

    // v3.0.2 - safari -gf20220517
    const isSafari = navigator.vendor && 
           navigator.vendor.indexOf('Apple') > -1 &&
           navigator.userAgent &&
           navigator.userAgent.indexOf('CriOS') == -1 &&
           navigator.userAgent.indexOf('FxiOS') == -1;

    $(function() {
      setTimeout(function() {
        // console.log("isSafari? ("+isSafari+")");
        if (isSafari!=true) {
          $('#klaro').removeClass('safari');
          // $('#klaro').addClass('no-safari');
        } else { 
          $('#klaro').addClass('safari'); 
          // $('#klaro').removeClass('no-safari');
        }
      })
    });
    // v3.0.2 - safari -gf20220517 END.

};


//--- Functions after window.load(): ---
$(function() {
		if($('iframe').length>0) {
				var counterOfIframe = 0;
				var attrDataSrc;
				$('iframe').each(function() {
						attrDataSrc=$(this).attr('src'); 
						if (!attrDataSrc) {
								attrDataSrc=$(this).attr('data-src'); 
						}
						if (attrDataSrc && ( attrDataSrc.indexOf("youtu") > -1 || attrDataSrc.indexOf("vimeo") > -1 )) {
								/* Adjust measures for videoOverlay similar to iframe: */
								$(this).parent().find('.klaro.cm-as-context-notice').css({'width':$(this).width()});
								// $(this).parent().find('.klaro.cm-as-context-notice').css({'height':'100%'});  // Activate if height isn't set to 100% by css.
								if ($(this).height() < $(this).parent().find('.klaro.cm-as-context-notice').height()) {
										$(this).parent().find('.klaro.cm-as-context-notice .cm-buttons').css('margin-top','1em');
								}
						}
						counterOfIframe++;
				});
		}

    /**   Add class for small context-notice box  gf20211115 **/
		$('.klaro.we_cookie_consent.cm-as-context-notice').each(function() {
				if ($(this).width() <= 300) {
						$(this).addClass('notice--minified');
				}
		});

    /** Add class to avoid Google to crawl consent info text  gf20220623 **/
    $('.klaro.we_cookie_consent .cn-body').each(function() {
      $(this).attr('data-nosnippet','data-nosnippet');
    });
});


jQuery(document).ready(function(){
	
	/*********************************************/
	/*** Mobile Toggle  */
	/*********************************************/
    $('#nav-mobile-toggle, .nav__mobile--close').on('click', function() {
    	
    	if ($('#nav-mobile').hasClass('active')) {
    		 $("#nav-mobile").removeClass("active");
    	} else {
    		$("#nav-mobile").addClass("active");
    	}
    	
    	return false;
    });
    
    // close mobile.nav when clicked outside
    $(document).on('click', function (e) {
        if ($(e.target).closest("#nav-mobile").length === 0) {
            $("#nav-mobile").removeClass("active");
        }
    });
    
    /*********************************************/
	/*** Magnific Popup  */
	/*********************************************/
    jQuery('.image-link').magnificPopup({
    	type:'image',
    	image: {
    		titleSrc: function(item) {
				let text = '';
				if (item.el.parents('figure').find('figcaption').html()) {
					text = item.el.parents('figure').find('figcaption').html()
				}
                return text;
            },
            verticalFit: true,
        }, 
    	gallery:{
    	    enabled:true
    	},
    	preload: [1,3],
    	zoom: {
    	    enabled: true, // By default it's false, so don't forget to enable it

    	    duration: 300, // duration of the effect, in milliseconds
    	    easing: 'ease-in-out',
    	}
    });

});

jQuery(document).ready(function(){
    jQuery('.imagearchive-item').magnificPopup({
    	type:'image',
    	image: {
    		titleSrc: function(item) {               
                let text = '';
                
				if (item.el.parents('.alimagearchive-item').find('.alimagearchive-title').html()) {
					text = item.el.parents('.alimagearchive-item').find('.alimagearchive-title').html();
				}
				if (item.el.parents('.alimagearchive-item').find('.alimagearchive-description').html()) {
					text += '<br>' + item.el.parents('.alimagearchive-item').find('.alimagearchive-description').html();
				}
				if (item.el.parents('.alimagearchive-item').find('.alimagearchive-credit').html()) {
					text += '<br>' + item.el.parents('.alimagearchive-item').find('.alimagearchive-credit').html();
				}
                return text;
            },
            verticalFit: true,
        }, 
    	gallery:{
    	    enabled:true
    	},
    	preload: [1,3],
    	zoom: {
    	    enabled: true, // By default it's false, so don't forget to enable it

    	    duration: 300, // duration of the effect, in milliseconds
    	    easing: 'ease-in-out',
    	}
    });
    
    jQuery('.eventimage').magnificPopup({
    	type:'image',
    	image: {
    		titleSrc: function(item) {               
                let text = '';
                text = item.el.attr('title');
				console.log(text);
                return text;
            },
            verticalFit: true,
        }, 
    	gallery:{
    	    enabled:true
    	},
    	preload: [1,3],
    	zoom: {
    	    enabled: true, // By default it's false, so don't forget to enable it

    	    duration: 300, // duration of the effect, in milliseconds
    	    easing: 'ease-in-out',
    	}
    });
    
    $("#alimagearchive-select-sorting").change(function() {
     this.form.submit();
    });
     
    $('.tx-user-ogl .dropdown dt a').on('click', function() {
		
		var visible = true;
		if (jQuery(this).parents('.dropdown').children('dd').children('ul').css("display") == "none" ) {
			visible = false;			
		}
		jQuery('.dropdown').children('dd').children('ul').hide();
		
		if (visible) {
			jQuery(this).parents('.dropdown').children('dd').children('ul').hide();	  
		} else {
			jQuery(this).parents('.dropdown').children('dd').children('ul').show();	  
		}
	    return false;	
	
	});
	
	$('.disabled a').on('click', function() {
			
			return false;
		
	});
	
	// Reponsive Tabs
	$('.eventTab').responsiveTabs({
	    startCollapsed: false,
	    collapsible: false,
	    scrollToAccordion: true,
        scrollToAccordionOnLoad: false,
        //scrollToAccordionOffset: 1000,
	    load: function(event, firstTab){
	    	$('.eventTab').show();	    	
	    }
	});
	
	
	// initialise Main Navigation
	var main_nav = $('.header__inner .nav').superfish({
		onInit: function() {}		
	});
});
