/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


jQuery(document).ready(function() {
      $('a[href$=".pdf"]').each(function(){
        $(this).attr("target", "_blank");
      });

      $('a#toplink').on( "click", function(e) { 
          e.stopPropagation();
          e.preventDefault();
          $('html, body').animate({ scrollTop: 0 }, 'fast');
      });

    /*notice for IE*/
    //userAgent in IE7 WinXP returns: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)
    //userAgent in IE11 Win7 returns: Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko

    if (navigator.userAgent.indexOf('MSIE') != -1)
        var detectIEregexp = /MSIE (\d+\.\d+);/ //test for MSIE x.x
    else // if no "MSIE" string in userAgent
        var detectIEregexp = /Trident.*rv[ :]*(\d+\.\d+)/ //test for rv:x.x or rv x.x where Trident string exists

    if (detectIEregexp.test(navigator.userAgent)) { //if some form of IE        
          //document.html.classList.add("outdated-ie");      
          var root = document.getElementsByTagName( 'html' )[0];
          root.setAttribute( 'class', 'outdated-ie' );
          document.getElementById("ienotice").style.display = "block";     
                              
        // hinweis über esc ausblenden        
        $(document).keyup(function(e) {
          if (e.keyCode == 27) { // escape key maps to keycode `27`                  
            document.getElementById("ienotice").style.display = "none";           
          }
        });
    }
    else {
        //console.log("n/a");
    }

      //insert bootstrap5 classes for image ratio and embed ratio
$(".th-pic.ratio[class*='th-pic-']").each(function() {  
  this.className = this.className.replace("th-pic-", "ratio-");
  this.className = this.className.replace("by", "x");  
});

$(".ratio[class*='embed-']").each(function() {  
  this.className = this.className.replace("embed-responsive-", "ratio-");
  this.className = this.className.replace("by", "x");  
});

    // Open all urls that don't belong to our domain in a new window or tab
      $("a[href^='http:']:not([href*='" + window.location.host + "'])").each(function(){
        $(this).attr("target", "_blank");
      });

      /**mega-slider**/
      $(".th-slider-mega .frame-type-shortcut").addClass("th-slider-wrapper");
      $(".th-slider-mega .frame-type-shortcut .frame-type-header h3").addClass("th-hdl h2");
      $(".th-slider-mega .frame-type-shortcut div[data-ce-columns=3]").addClass("th-gallery row");
      $(".th-slider-mega .frame-type-shortcut div[data-ce-columns=3] figure").addClass("col-4");
        

      /*pfeile im bilder-teaser-slider*/
      $(".th-slider.th-tiles-slider .slick-prev").prepend( "<i class='fas fa-angle-left' aria-hidden='true'></i>" );
      $(".th-slider.th-tiles-slider .slick-next").prepend( "<i class='fas fa-angle-right' aria-hidden='true'></i>" );


        $(window).on("resize, scroll",function(){       
          if ($(window).scrollTop()==0) {
            $(".th-nav-sticky button").attr("aria-expanded","false");
            $(".th-nav-main").attr("aria-expanded","false");
            $(".th-nav-sticky button").removeClass("th-shownav");          
            $(".th-nav-main").removeClass("collapse show");  
            $(".sticky-top").removeClass("navbar-transition");
            $(".th-nav-main").show();
            $(".frame-type-menu_section").removeClass("sticky-top-100");
            if( ($(".th-nav-main").attr("aria-expanded")=="false")) {
              $( ".th-nav-flap" ).css("height", "inherit");
              $( ".th-nav-flap" ).css("max-height", "inherit");
              $( ".th-nav-main > .th-container" ).css("height", "inherit");
              $( ".th-nav-main > .th-container" ).css("max-height", "inherit");
            } 
            if( ($(".sticky-top button").attr("aria-expanded")=="false") && (window.innerWidth < 880) ) {
                $(".th-nav-main").hide();
            }
          } else {        
            if ($(window).scrollTop()>0) {
              $(".sticky-top").addClass("navbar-transition");
              $(".frame-type-menu_section").addClass("sticky-top-100");
              if( ($(".th-nav-main").attr("aria-expanded")=="false") && $(".sticky-top").hasClass("navbar-transition")) {
                $(".th-nav-main").hide();
                $( ".th-nav-flap" ).css("height", "inherit");                
                $( ".th-nav-flap" ).css("max-height", "inherit");
              }
            } else {
              $(".sticky-top").removeClass("navbar-transition");
              $(".th-nav-main").hide();
              $(".frame-type-menu_section").removeClass("sticky-top-100");
            }
           }
        });      

        //calc window-height and set height to dropdown menu
        $( "#th-nav-main .th-lvl1-btn" ).on( "click", function() {
          var win_height = $( window ).height()-300;
          $( ".th-nav-main > .th-container" ).css("height", "inherit");
          if ($(".th-header").is('.navbar-transition')){
            //hamburger            
            $( ".th-nav-flap" ).css("max-height", "inherit");
          } else {
            //megamenu            
            $( ".th-nav-flap" ).css("max-height", win_height);
          }
        });

        //calc window-height and set height to hamburger dropdown menu
        $( ".navbar-toggler" ).on( "click", function() {
          $( ".th-nav-flap" ).css("height", "inherit");
          if ($(".th-header").is('.navbar-transition')){
            //hamburger                   
            var win_height_ham = $( window ).height()-200;
            $( ".navbar-transition .th-nav-main > .th-container" ).css("max-height", win_height_ham);
          }

          if ($(".th-header").is('.mobile')){
            //hamburger mobile                  
            var win_height_ham = $( window ).height()-100;
            $( ".mobile .th-nav-main > .th-container" ).css("height", win_height_ham);
          }          
        });        


        $(window).on('orientationchange', function() {
              

              $( ".th-nav-flap" ).css("height", "inherit");
              $( ".th-nav-main > .th-container" ).css("height", "inherit");

              if ($(".th-header").is('.navbar-transition')){
                //hamburger                   
                var win_height_ham = $( window ).height()-200;
                $( ".navbar-transition .th-nav-main > .th-container" ).css("max-height", win_height_ham);
              }

              if ($(".th-header").is('.mobile')){
                //hamburger mobile                  
                var win_height_ham = $( window ).height()-100;
                $( ".mobile .th-nav-main > .th-container" ).css("height", win_height_ham);
              } 

               $(window).one('resize', function() {

                if (window.innerWidth < 880) {
                  $(".th-header").addClass("mobile");
                  $(".th-nav-main").hide();  
                } else {
                  $( ".th-nav-main > .th-container" ).css("height", "inherit");
                  $(".th-header").removeClass("mobile");
   
                  $(".th-nav-main").show();   
                }
              });       
        });


        // Menü über Nav-sticky-Button einblenden/ausblenden
          $(".th-nav-sticky button").click(function() {
            if ($(this).attr("aria-expanded") == "true") {
              $(".th-nav-main").show();
              $(".th-nav-main").attr("aria-expanded","true");
              $(this)
                .attr("aria-expanded","true")
                .addClass("th-shownav");
            } else {
              $(".th-nav-main").hide();
              $(".th-nav-main").attr("aria-expanded","false");
              $(this)
                .attr("aria-expanded","false")
                .removeClass("th-shownav");
            }
          });      

        if (window.innerWidth < 880) {
          $(".th-header").addClass("mobile");
        }
      
        if($(".frame-default").hasClass("frame-type-uploads")){
            $(this).find("h2").addClass("th-lst-head");
        } 

        var headlines = [  'H6', 'H5', 'H4', 'H1', 'H2', 'H3'];
        //set external link layout
        $('.th-content a[target="_blank"]:not(:has(img, i)), .th-content .ce-uploads a:not(:has(img)), .th-content .target-blank a:not(:has(img)), .th-slider .article a[target="_blank"].more').each(function(){
                $(this).attr('target','_blank');
                var nodeName = $(this).parent().prop("tagName");
                
                if(jQuery.inArray(nodeName, headlines) == -1) {
                var href= $(this).attr("href");
                var extension = href.substr( (href.lastIndexOf('.') +1) ).toLowerCase();

                    switch(extension) {
                        case 'zip':
                        case 'rar':
                            $(this).prepend('<i class="fas fa-download" aria-hidden="true"></i>');
                            break;                                                  
                        case 'xls':
                        case 'xlsx':
                            $(this).prepend('<i class="fas fa-file-excel" aria-hidden="true"></i>');
                            break;                                                 
                        case 'doc':
                        case 'docx':
                            $(this).prepend('<i class="fas fa-file-word" aria-hidden="true"></i>');
                            break;   
                        case 'ppt':
                        case 'pptx':
                            $(this).prepend('<i class="fas fa-file-powerpoint" aria-hidden="true"></i>');
                            break;                                                                                                        
                        case 'pdf':
                            $(this).prepend('<i class="fas fa-file-pdf" aria-hidden="true"></i>');
                            break;
                        case 'jpg':
                        case 'jpeg':
                        case 'png':
                        case 'gif':
                        case 'JPG':
                        case 'JPEG':
                            $(this).prepend('<i class="fas fa-file-image" aria-hidden="true"></i>');
                            break;  
                        case 'mp3':
                        case 'wma':
                        case 'wav':
                            $(this).prepend('<i class="fas fa-file-audio" aria-hidden="true"></i>');
                            break;      
                        case 'mov':
                        case 'mp4':
                        case 'wmv':
                        case 'avi':                        
                            $(this).prepend('<i class="fas fa-file-video" aria-hidden="true"></i>');                            
                            break;                                                                                
                        default:                        
                           $(this).prepend('<i class="fas fa-external-link-alt" aria-hidden="true"></i>');
                    }
                }
         });  

        //set mailto-link layout
        $('.th-content a[href^="mailto:"]').prepend('<i class="fas fa-envelope" aria-hidden="true"></i>');         
    

        $("a.fancybox").fancybox();

        if($(".powermail_fieldwrap_type_text").hasClass("alert-info")){
            $(".powermail_fieldwrap_type_text").addClass("alert layout1");
        }     

        //replace data-aria attributes from f.link with correct aria
        $("[data-aria-label]").each(function () {
                var label = $(this).attr("data-aria-label");
                $(this).attr("aria-label" , label);
                $(this).removeAttr("data-aria-label");
            });    

        $("[data-aria-current]").each(function () {
                var current = $(this).attr("data-aria-current");
                $(this).attr("aria-current" , current);
                $(this).removeAttr("data-aria-current");
            });                 

        if( $(".th-kvsl h1")) $(".th-kvsl img").attr("aria-labelledby", "h1");        


        if( $( '.th-video .ratio' ).length > 0 ) {

            $('.video-config').click(function() {                  
                $('.video-config-box').toggle();

                $(".video-config-box input:checkbox").change(function() {
                    var ischecked= $(this).is(':checked');
                    if(!ischecked) {
                      var cname = 'th-youtube-video';
                      var d = new Date(); //Create an date object
                      d.setTime(d.getTime() - (1000*60*60*24)); //Set the time to the past. 1000 milliseonds = 1 second
                      var expires = "expires=" + d.toGMTString(); //Compose the expirartion date                      
                      window.document.cookie = cname+"="+"; "+expires+"; path=/"; //Set the cookie with name and the expiration date

                      setTimeout(function(){
                        location.reload();
                      },1000);
                    }
                });

            }); 



            if($.cookie("th-youtube-video")=="1") {
                
                $( '.th-video .ratio' ).each( function() {
                    v_wrapper = $( this );
                    v_trigger = $( this ).find( '.video_trigger');
                    v_trigger.hide();
        
                    v_wrapper.children( '.video_layer' ).show().children( 'iframe' ).attr( 'src', 'https://www.youtube-nocookie.com/embed/' + v_trigger.attr( 'data-source' ) + '?rel=0&controls=1&showinfo=0&autoplay=0' ).attr('title', v_trigger.attr( 'data-title' ));
                    $('.video-config' ).show();
                    $('.video-config-box input:checkbox').prop('checked', true);                    
                });

            } else {
                
                $( '.th-video .ratio' ).each( function() {
                    _wrapper = $( this );
                    
                    _wrapper.children( '.video_trigger' ).children('.th-video-hinweis').children( 'input[type="button"]' ).click( function() {                  
                                                                   
                        $.cookie("th-youtube-video", "1", { path: '/', expires: 356 });      
                                
                        $( '.th-video .ratio' ).each( function() {
                            v_wrapper = $( this );
                            v_trigger = $( this ).find( '.video_trigger');
                            v_trigger.hide();
                
                            v_wrapper.children( '.video_layer' ).show().children( 'iframe' ).attr( 'src', 'https://www.youtube-nocookie.com/embed/' + v_trigger.attr( 'data-source' ) + '?rel=0&controls=1&showinfo=0&autoplay=0' ).attr('title', v_trigger.attr( 'data-title' ));

                            $('.video-config' ).show();
                            $('.video-config-box input:checkbox').prop('checked', true);                            
                        });                                      

                    });
                });
             }
        }    
    
    $('form.date-menu').on('submit', function(e) {
        e.preventDefault;
        
        //console.log('switching month');
        
        var url = $(this).find('select').val();
        if(url) {
            console.log('    success');
            location.href = url;
        }
        
        return false;
    });
    
    // On every re-initialization
//    $('.th-slider-special-sub').on('init, reInit', function(event, slick){
//        var maxWidth = $(slick).width();
//        maxWidth = Math.ceil((maxWidth - 100) / 5);
//        $(slick).find('.slick-slide').css('max-width', maxWidth);
//        console.info('~~~~~ maxWidth=', maxWidth);
//    });
    $('.th-slider-special-sub').on('breakpoint', function(event, slick, breakpoint){
        var maxWidth = $(slick).width();
        maxWidth = Math.ceil((maxWidth - 100) / 5);
        $(slick).find('.slick-slide').css('max-width', maxWidth);
        //console.info('~~~~~ maxWidth=', maxWidth);
    });
    
    $('.accordion.hide .collapse').collapse("hide");
    $('.accordion.show li:first-child .collapse').collapse("show");

/*var buttonsContainer = $('.shariff');
new Shariff(buttonsContainer, {
    mailUrl: 'mailto:info@XXX.de',
});
*/

  
  if($('#contentOnePage').length) {
      var onePageContents = $('#contentOnePage section.th-box').length;
      var menuLinks = $('#th-nav-main li > a');
      var url = window.location.href.split("#")[0];
      $('#contentOnePage section.th-box').each(function(index) {
          var link1 = '';
          var link2 = '';
         if(index==0) {
             link1 = '<a class="th-nav-page-link" href="'+url+'#th-top" aria-label="einen Abschnitt nach oben"><i class="fas fa-arrow-up" aria-hidden="true"></i></a>';
         } else {
             link1 = '<a class="th-nav-page-link" href="'+$(menuLinks[index-1]).attr('href')+'" aria-label="einen Abschnitt nach oben"><i class="fas fa-arrow-up" aria-hidden="true"></i></a>';
         }
         var new_id = $(this).attr('aria-label').toLowerCase().trim().replace(/\s+/g, "-").replace(/ä/g, 'ae').replace(/ö/g, 'oe').replace(/ü/g, 'ue').replace(/ß/g, 'ss');
         var content = '<nav class="th-nav-page" aria-label="Sprunglinks zu Abschnitten" id="'+new_id+'"><ul><li>'+link1+'</li>';
         for (i = 0; i < onePageContents; i++) {
             if(i==index) {
                 content += '<li><a href="'+$(menuLinks[i]).attr('href')+'" aria-label="Abschnitt '+(i+1)+'" class="th-nav-page-indicator th-current"></a></li>';
             } else {
                 content += '<li><a href="'+$(menuLinks[i]).attr('href')+'" aria-label="Abschnitt '+(i+1)+'" class="th-nav-page-indicator"></a></li>';
             }
             
         }
         if(index < (onePageContents-1)) {
             link2 = '<a class="th-nav-page-link" href="'+$(menuLinks[index+1]).attr('href')+'" aria-label="einen Abschnitt nach unten"><i class="fas fa-arrow-down" aria-hidden="true"></i></a>';
         } else {
             link2 = '<span class="th-nav-page-link" disabled=""><i class="fas fa-arrow-down" aria-hidden="true"></i></span>';
         }
         content += '<li>'+link2+'</li>';
         
         content += '</ul></nav>';
         
         $(this).before(content);
      });
  }

});
