
/* ssa : 18-02-04 : delete after update 8.7 accordion is not working properly ..
var hash = $(window.location.hash);

if (hash.length && hash.hasClass("accordion-item")) {
    hash.addClass("is-active");
} else {
    //Replace #panel1 with the one that should be open by default
    $("#panel0").addClass("is-active");
}
*/


jQuery(document).ready(function(){

    // foundation
    jQuery(document).foundation();


    // start :: general
    // Open every Link to PDF into an new Window!
    jQuery('a[href$=".pdf"], a[href$=".txt"]').attr('target','_blank');


    // write value from "figcaption" into "data-caption" (for fancybox 3)
    jQuery('figcaption').each(function() {
        var imgCaption = jQuery(this).html();
        jQuery(this).prev('a').attr('data-caption', imgCaption);
    });

    // end :: general

    // start :: cookie consent user centrics geht nicht, cookie consent wird unter dem scripttag eingebunden
    //jQuery('#uc-btn-deny-banner').removeAttr('style');
    //jQuery('#uc-btn-accept-banner').removeAttr('style');
    //jQuery('#uc-btn-more-info-banner').removeAttr('style');


    // start :: mobile navigation

    // initialize plugin : http://www.smartmenus.org/
    jQuery(function() {
        jQuery('#main-menu').smartmenus();
    });
    // end :: mobile navigation



    // start :: main navigation

    // special width for APPLICATIONS at
    //jQuery('#nav4 > .mzr-content > div').attr('class', 'mzr-level2 w31');

    // change flyout panel  to dropdown menu at COMPANY
    jQuery('#nav6').attr('class', 'mzr-drop');

    // change flyout panel  to dropdown menu at CONTACT
    jQuery('#nav7').attr('class', 'mzr-drop');

    // change flyout panel  to dropdown menu at CAREER
    jQuery('#nav38').attr('class', 'mzr-drop');

    // change last product group in flyout
    jQuery('#nav15').attr('class', 'mzr-level2 w48');

    // remove vertical from Tunable diode lasers
    jQuery('#nav15 > ul').removeClass('vertical').addClass('displayB');
    jQuery('#nav55 > ul').removeClass('vertical');
    jQuery('#nav56 > ul').removeClass('vertical');
    jQuery('#nav57 > ul').removeClass('vertical');

    // end :: main navigation




    // start :: manipulate DOM

    // Re-Order Sub Menu at 'Tunable Diode Lasers'
    // Get the outerHTML : http://www.computerhowtoguy.com/how-to-get-the-outerhtml-of-an-element-using-jquery/
    var myString = jQuery('#subnav56').clone().wrap('<div>').parent().html();
    var myElement = jQuery('#subnav57');
    myElement.before(myString);
    myElement.next().remove();

    // careers list view, initiativ and bachelor at the bottom
    /*$( ".job-link-42, .job-link-174" ).insertAfter( ".careers-list:last-child" );
    $( ".job-line-42" ).hide();*/

    // end :: manipulate DOM



    // start :: coloring columns and rows at hover : https://codepen.io/chriscoyier/pen/pvwmaR
    if (screen.width < 500) {
        jQuery("body").addClass("nohover");
        jQuery("td, th")
            .attr("tabindex", "1")
            .on("touchstart", function() {
                $(this).focus();
        });
    }
    // end :: coloring columns and rows at hover : https://codepen.io/chriscoyier/pen/pvwmaR


    // start :: initial thumbnail slider : ssa: deleted april 2021

    // https://sachinchoolur.github.io/lightslider/examples.html
    /*
    jQuery('#imageGallery').lightSlider({
        gallery:true,
        item: 1,
        loop: true,
        thumbItem: 6,
        onSliderLoad: function(el) {
            el.lightGallery({
                selector: '#imageGallery .lslide'
            });
        }
    });
    */
    // end  :: initial thumbnail slider




    // start :: Smoooth scroll to top / arrow at page bottom, see PartialFooter.html
    var scrollTopButton = jQuery('#scrollTop');
    var offset = 200;
    var duration = 500;

    jQuery(window).scroll(function() {

        if (jQuery(this).scrollTop() > offset) {
            scrollTopButton.fadeIn(duration);
        } else {
            scrollTopButton.fadeOut(duration);
        }
    });

    scrollTopButton.click(function(event) {
        event.preventDefault();
        jQuery('html, body').animate({scrollTop: 0}, duration);
        return false;
    });
    // end :: Smoooth scroll to top / arrow at page bottom





    // start :: add figcaption into the data-title from lightbox_a
    /* http://bit.ly/2dRXFmT */
    jQuery('figure').find('figcaption').each(function () {
        //var desc = $(this).text();
        jQuery('a.lightbox2').attr('data-title', function() {
            return $(this).next('figcaption').text();
        });
    });
    // end :: add figcaption into the data-title from lightbox_a



    // start :: lazyload : https://appelsiini.net/projects/lazyload/
    jQuery("img.lazyload").lazyload();
    // end ::  lazyload : https://appelsiini.net/projects/lazyload/





    // start :: FORMS : https://colorlib.com/wp/template/contact-form-v3/
    jQuery('.powermail_input, .powermail_textarea, .powermail_captcha').each(function(){
        jQuery(this).on('blur', function(){
            if(jQuery(this).val().trim() != "") {
                jQuery(this).addClass('has-val');
            }
            else {
                jQuery(this).removeClass('has-val');
            }
        })
    });

    // https://select2.org/dropdown
    //jQuery(".powermail_select, .powermail_country").select2({
    //    minimumResultsForSearch: 20,
    //    dropdownParent: $('#dropDownSelect1')
    //});

    // end :: FORMS : https://colorlib.com/wp/template/contact-form-v3/




    // start ::  Hide Header on on scroll down + show it on scroll up
    /* http://jsfiddle.net/mariusc23/s6mLJ/31/ or http://bit.ly/2dU4F22 */
    var didScroll;
    var lastScrollTop = 0;
    var delta = 5;
    var navbarHeight = jQuery('#head').outerHeight();

    jQuery(window).scroll(function(event){
        didScroll = true;
    });

    setInterval(function() {
        if (didScroll) {
            hasScrolled();
            didScroll = false;
        }
    }, 250);

    function hasScrolled() {
        var st = jQuery(this).scrollTop();

        // Make sure they scroll more than delta
        if(Math.abs(lastScrollTop - st) <= delta)
            return;

        // If they scrolled down and are past the navbar, add class .nav-up.
        // This is necessary so you never see what is "behind" the navbar.
        if (st > lastScrollTop && st > navbarHeight){
            // Scroll Down
            jQuery('#head').removeClass('nav-down').addClass('nav-up');
        } else {
            // Scroll Up
            if(st + jQuery(window).height() < jQuery(document).height()) {
                jQuery('#head').removeClass('nav-up').addClass('nav-down');
            }
        }

        lastScrollTop = st;
    }
    // END ::  Hide Header on on scroll down + show it on scroll up




    // BEGIN // careers button
    jQuery('#initiativ span').addClass('big-button');
    setTimeout( function(){
        jQuery('#initiativ span').addClass('retracted');
    }  , 4000 ); // was 2000
    setTimeout( function(){
        jQuery('#initiativ span').removeClass('big-button');
        jQuery('#initiativ span').removeClass('retracted');
        jQuery('#initiativ span').addClass('small-button');
    }  , 4500 ); // was 2500

    jQuery('#initiativ').hover( function(){
        jQuery('#initiativ span').addClass('initiativ-hover');
        console.log('d');
    }, function(){
        jQuery('#initiativ span').removeClass('initiativ-hover');
    });

    // https://foundation.zurb.com/sites/docs/reveal.html#ajaxq
    /*
    var $modalcareers = $('#reveal-careers');

    $.ajax('/url')
        .done(function(resp){
            $modalcareers.html(resp).foundation('open');
    });
    */
    // END // careers button


    // BEGIN // ckeditor insert empty <p></p> before tables - I don't know why?
    jQuery('p').each(function() {
        var $this = jQuery(this);
        if($this.html().length == 0)
            $this.remove();
    });

    // https://stackoverflow.com/questions/7823914/remove-empty-paragraphs-with-jquery
    /*$("p:empty").remove();
    $('p').filter(function () { return this.innerHTML == "" }).remove();
    */
    // END // ckeditor insert empty <p></p> before tables



    // start ::  Slick SLider, see also : https://github.com/kenwheeler/slick/issues/3059#issuecomment-458895726
    jQuery('.toptica-news-slider').slick({
        arrows: true,
        dots: false,
        infinite: true,
        speed: 300,
        autoplay: true,
        autoplaySpeed: 7000,
        //centerMode: true,
        //centerPadding: '50px',
        lazyLoad: 'ondemand', // 'progressive'
        slidesToShow: 3,
        slidesToScroll: 1,
        responsive: [
            {
                breakpoint: 1024,
                settings: {
                    infinite: true
                }
            },
            {
                breakpoint: 600,
                settings: {
                    infinite: false,
                    arrows: false,
                    slidesToShow: 1
                }
            },
            {
                breakpoint: 480,
                settings: {
                    infinite: false,
                    arrows: false,
                    slidesToShow: 1
                }
            }
        ]
    });

    // start ::  Slick SLider, see also : https://github.com/kenwheeler/slick/issues/3059#issuecomment-458895726
    jQuery('.mask-headerslider').slick({
        arrows: true,
        dots: false,
        infinite: true,
        speed: 300,
        autoplay: true,
        autoplaySpeed: 7000,
        lazyLoad: 'ondemand', // 'progressive'
        slidesToShow: 1,
        slidesToScroll: 1,
        responsive: [
            {
                breakpoint: 1024,
                settings: {
                    infinite: true,
                    arrows: true,
                    dots: false
                }
            },
            {
                breakpoint: 600,
                settings: {
                    infinite: false,
                    arrows: false,
                    dots: true
                }
            },
            {
                breakpoint: 480,
                settings: {
                    infinite: false,
                    arrows: false,
                    dots: true
                }
            }
        ]
    });


    jQuery('.toptica-product-slider').slick({
        arrows: true,
        dots: false,
        //asNavFor: '.toptica-product-thumbs',
        infinite: true,
        speed: 300,
        autoplay: true,
        autoplaySpeed: 7000,
        lazyLoad: 'ondemand', // 'progressive'
        slidesToShow: 1,
        slidesToScroll: 1,
        responsive: [
            {
                breakpoint: 1024,
                settings: {
                    infinite: true,
                    arrows: true,
                    dots: false
                }
            },
            {
                breakpoint: 600,
                settings: {
                    infinite: false,
                    arrows: false,
                    dots: true
                }
            },
            {
                breakpoint: 480,
                settings: {
                    infinite: false,
                    arrows: false,
                    dots: true
                }
            }
        ]
    });

    var plyrOptions = {
        iconUrl: '/typo3conf/ext/toptica/Resources/Public/css/plyr.svg',
        youtube: { noCookie: true, rel: 0, showinfo: 0, iv_load_policy: 3, modestbranding: 1 }
    }

    const youtubePlayers = Plyr.setup('.embed-container-youtube', plyrOptions);

    const videoPlayers = Plyr.setup('.embed-container-video video', plyrOptions);

    // Stop slider when playing youtube and html videos
    $.each(youtubePlayers, function( index, player ) {
        player.on('playing', event => {
          jQuery('.toptica-product-slider').slick('slickPause');
        });
    });
    $.each(videoPlayers, function( index, player ) {
        player.on('playing', event => {
          jQuery('.toptica-product-slider').slick('slickPause');
        });
    });


    // On before slide change
    $('.toptica-product-slider').on('afterChange', function(event, slick, currentSlide){

        // Stop youtube and html videos when changing slides
        $('.toptica-product-slider .slick-slide').not('.slick-current').each(function( index ) {

            $.each(youtubePlayers, function( index, player ) {
                player.pause();
            });

            $.each(videoPlayers, function( index, player ) {
                player.pause();
            });

        });

    });

    // Pause slick slider autoPlay when clicking play on a video
    $('.toptica-product-slider video').on('play', function(event){
        jQuery('.toptica-product-slider').slick('slickPause');
    });

    jQuery('.toptica-product-thumbs').slick({
        slidesToShow: 10,
        slidesToScroll: 1,
        asNavFor: '.toptica-product-slider',
        arrow: false,
        dots: false,
        centerMode: true,
        focusOnSelect: true
    });

    jQuery('.mask-headerslider-thumbs').slick({
        slidesToShow: 10,
        slidesToScroll: 1,
        asNavFor: '.mask-headerslider',
        arrow: false,
        dots: false,
        centerMode: true,
        focusOnSelect: true
    });
    // END ::  Slick SLider



    // START ::  Sidenote Icon ckeditor, see _type.scss
    jQuery( "a.sidenote-icon" ).each(function() {
        jQuery( this ).wrapAll( "<span class='sidenote'><cite class='linkicon'></cite></span>" );

        var title = jQuery( this ).attr('title');
        var linkaim = jQuery( this ).attr('href');
        var linktarget = jQuery( this ).attr('target');
        var linktext = jQuery( this ).html();
        var titlewrap = "<span class='sidenote-span colorPrimary'>" + linktext + "</span>";

        jQuery( this ).parent().after(titlewrap);
        jQuery( this ).html(title);
        // also add link to sidenote-span
        jQuery( this ).parent().next("span").wrapAll( "<a href='" + linkaim + "' target='" + linktarget + "'></a>" );

        // also hover and show sidenote link
        jQuery('.sidenote-span').hover(function(){
            jQuery(this).parentsUntil(".sidenote").siblings("cite").children().addClass("displayB");
            jQuery(this).parentsUntil(".sidenote").siblings("cite").addClass("hover");
        }, function () {
            jQuery(this).parentsUntil(".sidenote").siblings("cite").children().removeClass("displayB");
            jQuery(this).parentsUntil(".sidenote").siblings("cite").removeClass("hover");
        });
    });

    // Add link to sidenote `cite`
    jQuery(".linkicon").click((function (e) {
        if (!jQuery(e.target).is("a")) {
            var t = jQuery(this)
                .find("a:first")
                .attr("href");
            window.open(t)
        }
    }));
    // END ::  Sidenote Icon ckeditor

}); // DOM ready
/*

i()((function() {
    function e() {
        var e = -1,
            t = null;
        i()(".sidenote .icon:not(.sidenotes-corrected):visible").each((function(n, r) {
            var o = i()(r),
                a = o.offset().top;
            e == a && (t.css("margin-top", "-16px").addClass("sidenotes-corrected"), o.css("margin-top", "16px").addClass("sidenotes-corrected")), e = a, t = o
        }))
    }
    e(), window.onhashchange ? window.addEventListener("hashchange", e, !1) : window.setInterval(e, 1e3)
}))*/



window.accordionCeToggle = function(hash) {
    if (hash !== undefined && hash.length > 0) {
        $accordionElement = $(hash).closest('.FceAccordion');
        // Iterate accordions.
        $('.accordion-item').each(function() {
            $accordion = $(this);
            // Search for CE with hash in per accordion scope.
            $ceSearch = $accordion.find(hash);
            if ($ceSearch.length > 0 && $accordionElement.length > 0) {
                $ce = $($ceSearch.get(0));
                $accordionContent = $ce.closest('.accordion-content');
                $accordionElement.foundation('down', $accordionContent);
                // Scroll to.
                $('html,body').animate({scrollTop: $accordionContent.offset().top}, 'slow');
            }
        });
    }
}

// If anchor inside accordion, find appropiate accordion, jump to it and open it.
$(document).ready(function() {
    setTimeout(function() {
        window.accordionCeToggle(window.top.location.hash);
    }, 2000);
});

$(document).on('click', 'a.link-page', function(){
    window.accordionCeToggle(this.hash);
    return true;
});
$(document).ready(function() {

    // Specifications
    $('.tx-topticaproducts-additional-specifications').click(function(){
        $('#additional-speci-dropdown').toggleClass('hide-show')
        $('.tx-topticaproducts-additional-specifications').toggleClass('hide-show')
        $('.tx-topticaproducts-additional-specifications-hide').toggleClass('hide-show')
    });
    $('.tx-topticaproducts-additional-specifications-hide').click(function(){
        $('#additional-speci-dropdown').toggleClass('hide-show')
        $('.tx-topticaproducts-additional-specifications').toggleClass('hide-show')
        $('.tx-topticaproducts-additional-specifications-hide').toggleClass('hide-show')
    });

    // Top seller
    $('.tx-topticaproducts-additional-topseller').click(function(){
        $('#additional-topseller-dropdown').toggleClass('hide-show')
        $('.tx-topticaproducts-additional-topseller').toggleClass('hide-show')
        $('.tx-topticaproducts-additional-topseller-hide').toggleClass('hide-show')
    });
    $('.tx-topticaproducts-additional-topseller-hide').click(function(){
        $('#additional-topseller-dropdown').toggleClass('hide-show')
        $('.tx-topticaproducts-additional-topseller').toggleClass('hide-show')
        $('.tx-topticaproducts-additional-topseller-hide').toggleClass('hide-show')
    });

});