jQuery('body').addClass('js');

// visiblity plugin https://raw.githubusercontent.com/customd/jquery-visible/master/jquery.visible.min.js

(function (e) {
    e.fn.visible = function (t, n, r) {
        var i = e(this).eq(0),
            s = i.get(0),
            o = e(window),
            u = o.scrollTop(),
            a = u + o.height(),
            f = o.scrollLeft(),
            l = f + o.width(),
            c = i.offset().top,
            h = c + i.height(),
            p = i.offset().left,
            d = p + i.width(),
            v = t === true ? h : c,
            m = t === true ? c : h,
            g = t === true ? d : p,
            y = t === true ? p : d,
            b = n === true ? s.offsetWidth * s.offsetHeight : true,
            r = r ? r : "both";
        if (r === "both")
            return !!b && m <= a && v >= u && y <= l && g >= f;
        else if (r === "vertical")
            return !!b && m <= a && v >= u;
        else if (r === "horizontal")
            return !!b && y <= l && g >= f;
    };
})(jQuery);
// visiblity end



jQuery('document').ready(function ($) {

    // -------------------------------------------------------------------------------------------------------------------- Kontaktformular  ----------

    $(window).click(function () {
        $('#c2085 .frame-type-wst3bootstrap_card').removeClass('active');
    });

    $('#c2085 .frame-type-wst3bootstrap_card').on('click', function () {
        event.stopPropagation();
        $(this).toggleClass('active');
    });

    // -------------------------------------------------------------------------------------------------------------------- Kontaktformular ENDE  ----------


    //

    var movementTimer = null;

    $(".vid-cont").mousemove(function (e) {
        $('.playpause').fadeIn();
        clearTimeout(movementTimer);
        movementTimer = setTimeout(function () {
            $('.playpause').fadeOut();
        }, 1000);
    });

    // --------------------------------------------------------------- Video: Modal Schließen beendet Abspielen

    $('#modal-video').on('hidden.bs.modal', function () {
        $("#modal-video iframe").attr("src", $("#modal-video iframe").attr("src"));
    });


    // --------------------------------------------------------------- Video: Click auf Play-Button startet
    $('video').parent().click(function () {
        if ($(this).children("video").get(0).paused) {
            $(this).children("video").get(0).play();
            $(this).children(".playpause").fadeOut().text('▌▌').removeClass('lg');
        } else {
            $(this).children("video").get(0).pause();
            $(this).children(".playpause").fadeIn().text('▶').addClass('lg');
        }
    });
    // --------------------------------------------------------------- Reklamationsformular 
    $('#tx-steinhaus-reklamationsformular').on('change', function () {
        if (!$('#tx-reklamation-name-des-artikels').val()) {
            var anzahl = $("#flex0").val();
            var eanwert = $("#flex0").val();
            if (eanwert === '400933740093') { // STEINHAUS-Kennummer mit eingegeben
                $("#flex0").val('4009337');
                $("#hinweis-ean").fadeIn(1000);
            }
            if (anzahl.length >= 13) { // EAN vollständig ausgefüllt 
                $("#hinweis-ean").hide();
                $('#beschwerde-container').show();
                var eanummer = $("#flex0").val().substr(0, 13);
                console.log(eanummer);
                var artikelname = $("#flex0").val().substr(16);
                $('.img-' + eanummer).fadeIn(500);
                $('.produktname-' + eanummer).fadeIn(500);
                $('.reklamation-neueingabe').fadeIn(1500);
                $('.reklamation-tipp').fadeOut(500);
                $('#tx-reklamation-name-des-artikels').val(artikelname);
                //                $('#tx-reklamation-name-des-artikels').prop("readonly", true);
                //                $('#tx-reklamation-name-des-artikels').addClass("disabled normal-cursor");
                $('#flex0').prop("readonly", true);
                $('#flex0').addClass("disabled");
                $('#flex0').val(eanummer);
            }
        }
    });
    var reklamationReset = function () {
        $('#beschwerde-container').fadeOut(500);
        $('#beschwerde-container img').fadeOut(500);
        $('#beschwerde-container h3').fadeOut(500);
        $('.reklamation-neueingabe').fadeOut(50);
        $('.reklamation-tipp').fadeIn(2000);
        $('#tx-reklamation-name-des-artikels').prop("readonly", false);
        $('#tx-reklamation-name-des-artikels').removeClass("disabled");
        $('#flex0').prop("readonly", false);
        $('#flex0').removeClass("disabled");
        $('#tx-reklamation-name-des-artikels').val('');
        $('#flex0').val('4009337');
    };
    $(".reklamation-neueingabe").click(function () {
        reklamationReset();
    });
    $("#tx-steinhaus-reklamationsformular").validate({
        rules: {
            "tx_form[name]": "required",
            "tx_form[reklamation-grund]": "required",
            "tx_form[kundenname]": "required",
            "tx_form[datenschutz]": "required",
            "tx_form[eadresse]": {
                required: true,
                email: true
            }
        },
        messages: {
            "tx_form[name]": "Bitte geben Sie an, welchen Artikel Sie reklamieren m&ouml;chten.",
            "tx_form[reklamation-grund]": "Bitte geben Sie den Grund Ihrer Reklamation an.",
            "tx_form[datenschutz]": "Bitte bestätigen Sie, dass Sie unsere Datenschutzerklärung gelesen haben und dieser zustimmen.",
            "tx_form[kundenname]": "Bitte geben Sie Ihren Namen an.",
            "tx_form[eadresse]": {
                required: "Bitte geben Sie eine Ihre E-Mail-Adresse ein.",
                email: "Bitte geben Sie eine g&uuml;ltige E-Mail-Adresse ein."
            }
        }
    });

    $("#tb").click(function () {
        $('html, body').animate({
            scrollTop: $("#c1343").offset().top
        }, 2000);
    });


    // --------------------------------------------------------------------------------- Auswahl Kontaktformular & Setzen Cookie
    $('#formularauswahl a').click(function () {
        $(".accordion .card .collapse").removeClass('show');
        $.cookie("formularwahl", $(this).parent().index(), { expires: 1 });
    });
    if ($.cookie("formularwahl")) {
        var showForm = $.cookie("formularwahl");
        ++showForm;
        $(".accordion .card:nth-of-type(" + showForm + ") .collapse").addClass('show');
    }
    // --------------------------------------------------------------------------------- Auswahl Kontaktformular & Setzen Cookie ENDE

    //
    jQuery(".nav-link:contains('und mehr')").html(function (_, html) {
        return html.replace(/(und mehr)/g, '<small>$1</small>');
    });
    //
    jQuery('.megamenu')
        .css("display", "flex")
        .hide()
        .fadeIn(1000);
    //
    jQuery('.carousel').carousel({
        interval: 9000,
        pause: "null"
    });
    jQuery('video').on('play', function (e) {
        jQuery(".carousel").carousel('pause');
    });
    jQuery('video').on('stop pause ended', function (e) {
        jQuery(".carousel").carousel();
        setTimeout(function () {
            jQuery(".carousel").carousel('next');
        }, 1000);
    });
    //
    jQuery('#nav-primary').removeClass('fixed-top');
    //


    // ------------------------------------------------------------------ Animation Content-Elemente

    var $animation_elements = $('.animation-element');
    var $window = $(window);

    // im Content-Element definiert
    //var $loop = 1;

    function check_if_in_view() {
        var window_height = $window.height();
        var window_top_position = $window.scrollTop();
        var window_bottom_position = (window_top_position + window_height);

        $.each($animation_elements, function () {
            var $element = $(this);

            var element_height = $element.outerHeight();
            var element_top_position = $element.offset().top;
            var element_bottom_position = (element_top_position + element_height);

            //check to see if this current container is within viewport
            if ((element_bottom_position >= window_top_position) &&
                (element_top_position <= window_bottom_position)) {
                $element.addClass('in-view');
            } else {
                if ($loop === 1) {
                    $element.removeClass('in-view');
                }
            }

        });
    }
    $window.on('scroll resize load', check_if_in_view);
    $window.trigger('scroll');

    // ------------------------------------------------------------------ Animation Content-Elemente ENDE

});

//// --------------------------------------------------------------------------------------------------------------------- modals ----------

$(window).on('load', function () {
    if ($("#page_1").length > 0) {
        if ($("#werksverkauf-modal").length > 0) {
            $('#werksverkauf-modal').modal('show');

        }
    }
});

//// --------------------------------------------------------------------------------------------------------------------- modals ----------