

jQuery('document').ready(function ($) {

    var showAllRezepte = true;
    var blockRezeptScroll = false;

    // -------------------------------------------------------------------------------------------------------------------- Rezeptzutaten  ----------
    if ($('.tx-steinhaus-rezepte-zutaten').length > 0) {

        $('.tx-steinhaus-rezept-personen.col-md-3').hide();

        if ($('.table.ingredients-table > table').length === 1) {
            // $(this).find('h4').hide();
        }

        $list = $('.tx-steinhaus-rezepte-zutaten');

        var $portions = parseInt($list.attr("data-portions"));
        var $originalPortions = $portions;

        $('.tx-steinhaus-rezept-portions__modify-minus').click(function () {
            $portions -= 1;
            calculatePortions();

        });
        $('.tx-steinhaus-rezept-portions__modify-plus').click(function () {
            $portions += 1;
            calculatePortions();
        });
        //
        var truncateDecimals = function (number, digits) {
            var multiplier = Math.pow(10, digits),
                adjustedNum = number * multiplier,
                truncatedNum = Math[adjustedNum < 0 ? 'ceil' : 'floor'](adjustedNum);
            return truncatedNum / multiplier;
        };
        //
        var calculatePortions = function () {
            if ($portions < 1) {
                $portions = 1
            } if ($portions > 8) {
                $portions = 8
            }
            //
            $('.portions').text($portions);
            //
            $('td.text-right').each(function () {
                var $amount = ($(this).attr('data-original-amount') / $originalPortions) * $portions;
                $(this).attr('data-amount', $amount);
                if ($amount > 0) {
                    if ($(this).parent().hasClass('einheit-uid-2')) {
                        $amountNice = Math.round($amount);
                    } else {
                        $amountNice = truncateDecimals($amount, 2).toString().replace(".", ",");
                    }
                    $(this).text($amountNice);
                }
            });
        }

    }


    // -------------------------------------------------------------------------------------------------------------------- Rezeptliste  ----------

    $('.tx-steinhaus .tx-steinhaus-selector h5').click(function () {
        $(this).next().slideToggle(100);
        $(this).toggleClass('opened');
    });

    // store filter for each group
    var buttonFilters = {};
    var buttonFilter;
    // quick search regex
    var qsRegex;

    // init Isotope
    var $grid = $('#rezeptliste').isotope({
        itemSelector: '.rezept',
        layoutMode: 'fitRows' ,
        filter: function () {
            var $this = $(this);
            var searchResult = qsRegex ? $this.text().match(qsRegex) : true;
            var buttonResult = buttonFilter ? $this.is(buttonFilter) : true;
            var tierResult = '*';
            return searchResult && buttonResult && tierResult;
        }
    });

    $('.wizard-options').on('click', 'li', function () {
        var $this = $(this);
        // get group key
        var $buttonGroup = $this.parents('.wizard-options');
        var filterGroup = $buttonGroup.attr('data-filter-group');
        // set filter for group
        buttonFilters[filterGroup] = $this.attr('data-filter');
        // combine filters
        buttonFilter = concatValues(buttonFilters);
        // Isotope arrange
        $grid.isotope();
    });

    // use value of search field to filter
    var $quicksearch = $('.quicksearch').keyup(debounce(function () {
        qsRegex = new RegExp($quicksearch.val(), 'gi');
        $grid.isotope();
    }));

    // change is-checked class on buttons
    $('.button-group').each(function (i, buttonGroup) {
        var $buttonGroup = $(buttonGroup);
        $buttonGroup.on('click', 'button', function () {
            $buttonGroup.find('.is-checked').removeClass('is-checked');
            $(this).addClass('is-checked');
        });
    });

    // flatten object by concatting values
    function concatValues(obj) {
        var value = '';
        for (var prop in obj) {
            value += obj[prop];
        }
        return value;
    }

    // debounce so filtering doesn't happen every millisecond
    function debounce(fn, threshold) {
        var timeout;
        threshold = threshold || 100;
        return function debounced() {
            clearTimeout(timeout);
            var args = arguments;
            var _this = this;
            function delayed() {
                fn.apply(_this, args);
            }
            timeout = setTimeout(delayed, threshold);
        };
    }

    // ----------------------------------------------------------------------------------    custom tweaks

    $('.wizard-options li:not(.reset)').click(function () {
        $('div.rezept.hidden').removeClass('hidden');
        var $this = $(this);
        var str = $this.text();
        $this.parent().prev().find("span").text(str);
        //
        var timeoutID;
        function delayedAlert() {
            timeoutID = window.setTimeout(slowAlert, 100);
        }
        function slowAlert() {
            $('.wizard-options').hide();
            $('.tx-steinhaus h5').removeClass('opened');
        }
        delayedAlert();
        // get group key
        var $buttonGroup = $this.parents('.wizard-options');
        var filterGroup = $buttonGroup.attr('data-filter-group');
        $('.wizard-options[data-filter-group=' + filterGroup + '] li').removeClass("active");
        $this.addClass("active");
    });

    $('#isotope-veg').change(function () {

    });

    $('.isotope-reset').click(function () {
        location.reload();
        $(this).addClass('waiting');
    });

    // Reset in Filterliste
    $('.wizard-options li.reset').click(function () {
        $('ul.wizard-options[data-filter-group=' + $(this).parent().attr('data-filter-group') + '] li').removeClass('active');
        $(this).addClass("active");
        $('.wizard-options').hide();
        $('.tx-steinhaus h5').removeClass('opened');
        var str = $(this).text();
        $(this).parent().prev().find("span").text(str);
    });

    // -------------------------------------------------------------------------------------------------------------------- Rezeptvideos  ----------

    if (jQuery("#rezeptvideos").length) {
        if ($(window).width() >= 1280) {
            jQuery("#rezeptvideos").unitegallery({
                gallery_width: 900,
                gallery_height: 450,
                thumb_width: 90,
                thumb_height: 90,
                thumb_border_effect: true,
                thumb_border_width: 0,
                //
                gridpanel_padding_border_top: 0,
                gridpanel_padding_border_left: 50,
                gridpanel_padding_border_bottom: 0,
                gridpanel_arrows_padding_vert: 0,
                gridpanel_arrows_always_on: false,
                gridpanel_space_between_arrows: 0,
                grid_num_cols: 3,
                //
                thumb_color_overlay_effect: true,
                thumb_overlay_opacity: 0.1,
                grid_space_between_cols: 15,
                grid_space_between_rows: 15
            });
        } else if ($(window).width() < 1280) {
            var Breite = window.screen.availWidth;
            jQuery("#rezeptvideos").unitegallery({
                gallery_height: Breite,
                gallery_width: "100%",
                gallery_min_width: "100%",
                //
                thumb_width: 0,
                thumb_height: 0
            });
        }
    }


});


