window.addEventListener("load", privacy, false);

	$(".cookie-panel__button").click(function() {
		setTimeout(function () {
					location.reload(true);
					},1800)			
	}); 



            function privacy (){
                var cookiestatus = getCookie('omCookieConsent');

                $('iframe.om-cookie-iframe').each(function(){
                    if((cookiestatus != undefined && cookiestatus.indexOf(".0") >= 0) || (cookiestatus == undefined)) {
                        $(this).wrap('<div class="om-cookie-iframe-wrapper"></div>');
                        $(this).after($('.om-cookie-iframe-wrap').html());
                        $(this).attr('data-src', $(this).attr('src'));
                        $(this).attr('src', '');
                    }else{
                        $('.om-cookie-iframe').each(function(){
                            $(this).attr('src', $(this).attr('data-src'));
                        });
                    }
                });
                $("body").delegate(".om-cookie-info-opencookiebar", "click", function(){
                    $('.om-cookie-panel').addClass('active');
                });
				
				
				// console.log(cookie);
				// document.cookie = 'omCookieConsent' + '=' + 'group-19.1,group-20.1,group-22.1,group-21.1,group-23.1,group-24.1,dismiss' + ';path=/;expires='; 
				
				
            }
			

		
	
			
	/**
	 * Adds a consent for iFrames, which needs to be accepted.
	 *
	 * @param {dom} iframe
	 *
	 * @return {void}
	 */
	function replaceIFrameWithConsent(iframe) {  
  		var protectedIFrames = [];
      var SETTINGS = {"iframe_enabled":true,"cookie_lifetime":cookie_expires_time};
      //  var TEXT_ENTRIES = {"header":"Datenschutzeinstellungen","description":"Auf unserer Webseite werden Cookies verwendet. Einige davon werden zwingend ben\u00f6tigt, w\u00e4hrend es uns andere erm\u00f6glichen, Ihre Nutzererfahrung auf unserer Webseite zu verbessern.","accept_all_text":"Alle akzeptieren","accept_specific_text":"Auswahl speichern","accept_essential_text":"Nur essentielle Cookies akzeptieren","extend_box_link_text":"Weitere Informationen anzeigen","extend_box_link_text_close":"Weitere Informationen verstecken","extend_table_link_text":"Cookie-Informationen anzeigen","extend_table_link_text_close":"Cookie-Informationen verstecken","cookie_name_text":"Name","cookie_provider_text":"Anbieter","cookie_purpose_text":"Zweck","cookie_lifetime_text":"Laufzeit","iframe_button_allow_all_text":"Alle externen Inhalte erlauben","iframe_button_allow_one_text":"Einmalig erlauben","iframe_button_load_one_text":"Externen Inhalt laden","iframe_open_settings_text":"Einstellungen anzeigen"};
      var TEXT_ENTRIES = {"iframe_button_load_one_text":"Externen Inhalt laden","iframe_open_settings_text":""};
      
      // noinspection EqualityComparisonWithCoercionJS
  		if (iframe.getAttribute('data-iframe-allow-always') == true) {
  			return;
  		}
  
  		var parent = iframe.parentElement;
  		if (!parent) {
  			return;
  		}
  
  		if (!iframe.src || iframe.src.indexOf('chrome-extension') >= 0) {
  			return;
  		}
  
  		// Got problems with the zero.
  		// var iframeId = protectedIFrames.length + 1;
      var iframeId = iframe.getAttribute('data-iframe-id');
  		if (iframeId === 0) {
  			iframeId = 1;
  		} 
  		protectedIFrames[iframeId] = iframe;    
      
  		var button = document.createElement('BUTTON');
  		button.appendChild(document.createTextNode(TEXT_ENTRIES.iframe_button_load_one_text));
  		button.addEventListener('click', function() {
  			acceptIFrame(iframeId,protectedIFrames);
  		});
      
      /*** Hinweis hizufügen ***/
      var settingsNotice = document.createElement('DIV');
      settingsNotice.className ='settings-notice';
      var settingsIframeHost = iframe.getAttribute('data-iframe-host');
      if (settingsIframeHost != null){
            switch (settingsIframeHost) {
              case "Google Maps":
                var NOTICE_ENTRIES = {"iframe_settings_notice":"Wir verwenden externe Inhalte von Google Maps, um Ihnen zusätzliche Informationen anzubieten."};
                break;
              case "Youtube":
                var NOTICE_ENTRIES = {"iframe_settings_notice":"Wir verwenden externe Inhalte von Youtube, um Ihnen zusätzliche Informationen anzubieten."};
                break;
              case "unidentified":
              default:
                var NOTICE_ENTRIES = {"iframe_settings_notice":"Wir verwenden externe Inhalte, um Ihnen zusätzliche Informationen anzubieten."};
            }  
      }else{
           var NOTICE_ENTRIES = {"iframe_settings_notice":"Externer Inhalt unbekannter Herkunft"};
      }          
  		settingsNotice.appendChild(document.createTextNode(NOTICE_ENTRIES.iframe_settings_notice));
  
      /*** Überschrift hizufügen ***/
      if (settingsIframeHost != null){ 
            switch (settingsIframeHost) {
              case "Google Maps":
                var HEADER_ENTRIES = {"iframe_host_header":"Externer Inhalt von Google Maps"};
                break;
              case "Youtube":
                var HEADER_ENTRIES = {"iframe_host_header":"Externer Inhalt von Youtube"};
                break;
              case "unidentified":
              default:
                var HEADER_ENTRIES = {"iframe_host_header":""};
            }
           /* console.log(HEADER_ENTRIES.iframe_host_header);  */
           if (HEADER_ENTRIES.iframe_host_header != ''){
               var settingsIframeHeader = document.createElement('DIV');
               settingsIframeHeader.className ='iframe-host-header';       
               settingsIframeHeader.appendChild(document.createTextNode(HEADER_ENTRIES.iframe_host_header));
           }       
      }      
      
      
      /*** Link alle Externen Inhalte zulassen hizufügen ***/
      var settingsAllElementsFromHost = document.createElement('a');
      settingsAllElementsFromHost.className ='accept-hosts';
  		// settingsAllElementsFromHost.appendChild(document.createTextNode('Alle externen Inhalte erlauben'));
  		settingsAllElementsFromHost.addEventListener('click', function() {
  			acceptAllIframes(iframeId,protectedIFrames);
  		}); 
          
  		var container = document.createElement('DIV');
  		container.setAttribute('data-iframe-id', iframeId);
  		container.setAttribute('data-src', iframe.src);
  		container.setAttribute('style', 'height: ' + iframe.offsetHeight + 'px;' + ' width: ' + iframe.offsetWidth + 'px;');
  		container.classList.add('pp-cookie-optin-iframe-consent');
  		if (HEADER_ENTRIES.iframe_host_header != ''){
          container.appendChild(settingsIframeHeader);
      }
      container.appendChild(button);
      container.appendChild(settingsAllElementsFromHost);
      container.appendChild(settingsNotice);		 
  		parent.appendChild(container);
  
  		// Because of the IE11 no .remove();
  		parent.removeChild(iframe);
	}  

	/**
	 * Replaces a iFrame consent container with the iframe.
	 *
	 * @param {int} iframeId
	 *
	 * @return {void}
	 */
	function acceptIFrame(iframeId,protectedIFrames) {    
      if (!iframeId) {
  			iframeId = parent.getAttribute('data-iframe-id');
  			if (!iframeId) {
  				return;
  			}
  		}
      
  		var container = document.querySelector('div[data-iframe-id="' + iframeId + '"]');
  		var iframe = protectedIFrames[iframeId];
     
  		if (!iframe || !container) {
  			return;
  		}     
  		iframe.setAttribute('data-iframe-allow-always', 1);  
        
  		// Because of the IE11 no .replaceWith();
  		var parentNode = container.parentNode;
  		parentNode.removeChild(container);
  		parentNode.appendChild(iframe);        
	}  

	/**
	 * Replaces a iFrame consent container with the iframe for this host and set Cookie.
	 *
	 * @param {int} iframeId
	 *
	 * @return {void}
	 */
	function acceptAllIframes(iframeId,protectedIFrames) {        
      if (!iframeId) {
  			iframeId = parent.getAttribute('data-iframe-id');
  			if (!iframeId) {
  				return;
  			}
  		}
      
  		var container = document.querySelector('div[data-iframe-id="' + iframeId + '"]');
  		var iframe = protectedIFrames[iframeId];
     
  		if (!iframe || !container) {
  			return;
  		}     
  		iframe.setAttribute('data-iframe-allow-always', 1);  
        
  		// Because of the IE11 no .replaceWith();
  		var parentNode = container.parentNode;
  		parentNode.removeChild(container);
  		parentNode.appendChild(iframe);
      setCookie('iframe_allow_always', 'allow', cookie_expires_time);
      if(enableIframesOptin == true && reloadAfterConfirm == true ){
          location.reload(); 
      }       
	}
    
	/**
	 * Adds the cookie buttons.
	 *
	 * @return {void}
	 */
	function addIFrameButtons(parentDOM) {
  		var acceptAllButton = document.createElement('BUTTON');
  		acceptAllButton.classList.add('pp-cookie-optin-box-button-accept-all');
  		acceptAllButton.appendChild(document.createTextNode(TEXT_ENTRIES.iframe_button_allow_all_text));
  		acceptAllButton.addEventListener('click', function() {
  			acceptAllIFrames();
  			hideAndReloadCookieOptIn();
  		});
  
  		var acceptSpecificButton = document.createElement('BUTTON');
  		acceptSpecificButton.classList.add('pp-cookie-optin-box-button-accept-specific');
  		acceptSpecificButton.appendChild(document.createTextNode(TEXT_ENTRIES.iframe_button_allow_one_text));
  		acceptSpecificButton.addEventListener('click', function() {
  			acceptIFrame(lastOpenedIFrameId);
  			hideAndReloadCookieOptIn();
  		});
  
  		parentDOM.appendChild(acceptAllButton);
  		parentDOM.appendChild(acceptSpecificButton);
	}
  
	/**
	 * todo Optimize the cookie handling with hasSetting, addSetting, removeSetting and use it everywhere.
	 *
	 * Returns the cookie, found with the given name, or null.
	 *
	 * @param {string} name
	 * @return {string}
	 */
	function getCookie(name) {
		var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
		return v ? v[2] : null;
	} 
  
	/**
	 * Sets the given cookie with the given value for X days.
	 *
	 * @param {string} name
	 * @param {string} value
	 * @param {string} days
	 */
	function setCookie(name, value, days) {
		var d = new Date;
		d.setTime(d.getTime() + 24 * 60 * 60 * 1000 * days);
		document.cookie = name + '=' + value + ';path=/;expires=' + d.toGMTString();
	}  

/***  Cookie-Einstellungen prüfen  ***/  
window.onload = function() {  
       /*** Cookie iframe_allow_always löschen, wenn Cookie-Einstellungen zückgesetzt werden ***/
       if(document.getElementById('btnRevoke') !==null){
           document.getElementById('btnRevoke').onclick = function(){
              setCookie('iframe_allow_always', '', '0');
              if(enableIframesOptin == true && reloadAfterConfirm == true ){
                  location.reload();
              }           
           }
       }
       
       // /*** Abhängig vom cookieconsent_status ***/       
       // var cookieConsentStatus = getCookie('cookieconsent_status');     
       // if(getCookie('cookieconsent_status') != 'allow'){
         // /*** Cookie löschen, wenn Tracking verweigert wurde ***/
            // setCookie('iframe_allow_always', '', '0');
       // }
      // /*  console.log('Test');  */       
};  			