/* Minification of JavaScript failed:
(7,24-25): run-time error JS1003: Expected ':': (
(7,27-28): run-time error JS1100: Expected ',': {
(16,5-6): run-time error JS1002: Syntax error: }
(18,31-32): run-time error JS1195: Expected expression: )
(18,33-34): run-time error JS1004: Expected ';': {
(45,6-7): run-time error JS1195: Expected expression: ,
(47,27-28): run-time error JS1195: Expected expression: )
(47,29-30): run-time error JS1004: Expected ';': {
(74,1-2): run-time error JS1002: Syntax error: }
(75,42-43): run-time error JS1004: Expected ';': {
(97,32-33): run-time error JS1195: Expected expression: >
(145,10-11): run-time error JS1195: Expected expression: )
(146,5-6): run-time error JS1002: Syntax error: }
(149,29-30): run-time error JS1195: Expected expression: )
(149,31-32): run-time error JS1004: Expected ';': {
(152,2-3): run-time error JS1195: Expected expression: )
(154,40-41): run-time error JS1004: Expected ';': {
(76,5,80,7): run-time error JS1018: 'return' statement outside of function: return array.sort(function (a, b) {
        var x = a[key];
        var y = b[key];
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    })
 */
var plpCommon = {
    init: function () {
        this.bindEvents();
        this.initProductPlp();
        window.bindPlpImages(false);
    },
    hideEmptyCategories() {
        $('.product-category').each(function () {
            if (!$(this).find('.product-group-wrapper:not(.outoffilter)').length &&
                !$(this).find('.product-item:not(.outoffilter)').length) {
                $(this).hide();
            } else {
                $(this).show();
            }
        });
    },

    initProductPlp: function () {
        // Gather featured item details
        var featuredItem = $('.qvItem.featured');
        var featuredSku = featuredItem.data('product-id');
        var featuredGroup = $('.qvItem.featured').closest('.product-group-wrapper');

        // Add featured to the entire group for styles, hide the group while we manipulate it.
        featuredGroup.hide();
        featuredGroup.addClass('featured');

        // Hide the siblings for the featured product
        // and display the fatured product
        featuredItem.siblings().each(function () {
            $(this).addClass('outoffilter');
        });
        featuredItem.removeClass('outoffilter');

        // Set the matching color chip to active
        featuredItem.find('plp-grid__color-chips').find('color-chip').each(function () {
            if ($(this).data('product-id') === featuredSku) {
                $(this).addClass('active');
            } else {
                $(this).removeClass('active');
            }
        });

        featuredGroup.show();
    },

    bindEvents: function () {
        $(document).on('filters-processed', function () {
            plpCommon.hideEmptyCategories();
            //Frigidaire.colorChipInit();
        });


        $('#plp-sort').click(function () {
            $('#mobile-filter-options .filterWrapper').slideUp();
            $('#plp-filter').closest('.sortFilter').removeClass('open');
            $(this).closest('.sortFilter').toggleClass('open');
            $('#sort-options').slideToggle();
        });

        $('#plp-filter').click(function () {
            $('#sort-options').slideUp();
            $('#plp-sort').closest('.sortFilter').removeClass('open');
            $(this).closest('.sortFilter').toggleClass('open');
            $('#mobile-filter-options .filterWrapper').slideToggle();
        });

        $('.sort-option').click(function (e) {
            $('#sort-options').slideUp();
            $('.sortFilter').removeClass('open');
            $('#sort').val($(this).data('order')).trigger('change');
        });
    }
}
window.sortArray = function (array, key) {
    return array.sort(function (a, b) {
        var x = a[key];
        var y = b[key];
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    });
}

window.bindPlpImages = function (hideImagesRow) {
    var dataImages = $('.list-products').data('images'),
        parent = $('.list-products #pl');

    if (dataImages != null && dataImages.length > 0) {
        var sortedByRow = window.sortArray(dataImages, 'row');

        console.log("sortedByRow", sortedByRow);

        $('.plpImageContainer', '.list-products #pl').remove();
        $('.plp-image-placeholder:visible:not(.featured)', '.list-products #pl').each(function (index, item) {
            $(this).attr('data-row', Math.ceil((index + 1) / 3));
        });

        sortedByRow.forEach(c => {

            var row = c.row;
            var imageInstance = $('.product-group-wrapper.hidden-instance').clone().removeClass('hidden-instance hide');

            $(imageInstance).addClass('plpImageContainer plp-image-placeholder').attr({
                'data-original-row': row,
                'data-group-product-ids': [],
                'data-row': row
            });

            var innerImage = $('<img/>',
                {
                    'class': 'inner-image',
                    'src': c.imageUrl,
                    'alt': c.imageAlt
                });
            imageInstance.html(innerImage);

            var parent = $('.list-products #pl'),
                skipColumn = $('.plp-image-placeholder:visible[data-row=' + row + ']', parent);

            if (c.alignment && c.alignment.toLowerCase() === "right"
                && (typeof (skipColumn) !== 'undefined' && skipColumn.length > 0)) {
                imageInstance.insertAfter(skipColumn[0]);
            } else {
                imageInstance.insertBefore(skipColumn[0]);
            }

            var lengthIndex = 0;
            $('.plp-image-placeholder:visible:not(.featured)', parent).each(function (index, item) {
                var hasImage = $(this).hasClass('plpImageContainer');
                if (hasImage) {
                    lengthIndex += 2;

                    if (hideImagesRow) {
                        $(this).hide();
                    }
                } else {
                    lengthIndex += 1;
                }

                $(this).attr('data-row', Math.ceil((lengthIndex) / 3));
            });
            var nonVisibleLength = $('.plp-image-placeholder:hidden:not(.plpImageContainer)', parent);
            if (nonVisibleLength.length === 0) {
                $('.plpImageContainer', '.list-products #pl').show();
            }
        });
    }
}

$(document).ready(function () {
    plpCommon.init();
    // SetPositionComingSoonBanner();
});

function SetPositionComingSoonBanner() {

    var comingSoonClasses = $(".flag-comingsoonGrey");

    comingSoonClasses.each(function () {
        var $this = $(this);

        var $elparent = $this.parents(".product-group-wrapper");
        var widthouter = $($elparent).outerWidth(true);
        if ($(window).width() < 667) {
            var widthcalc = (($(window).width() - widthouter) * 2);
            if ($($elparent).hasClass("featured")) {
                $this.css("left", -(widthcalc + 24) + "px");
            }
            else {
                $this.css("left", -(widthcalc - 10) + "px");
            }
        } else if ($(window).width() > 767) {

            if ($($elparent).hasClass("featured")) {
                $this.css("left", -(10) + "px");
            }
            else {
                $this.css("left", -(10) + "px");
            }
        }
        else {
            $this.css("left", -(10) + "px");
            $this.css("min-width", "56%");
        }
    });

    var newClasses = $(".flag-newGrey");

    newClasses.each(function () {
        var $this = $(this);

        var $elparent = $this.parents(".product-group-wrapper");
        var widthouter = $($elparent).outerWidth(true);
        if ($(window).width() < 667) {
            var widthcalc = (($(window).width() - widthouter) * 2);
            if ($($elparent).hasClass("featured")) {
                $this.css("left", -(widthcalc + 24) + "px");
            }
            else {
                $this.css("left", -(widthcalc - 10) + "px");
            }
        } else if ($(window).width() > 767) {

            if ($($elparent).hasClass("featured")) {
                $this.css("left", -(10) + "px");
            }
            else {
                $this.css("left", -(10) + "px");
            }
        }
        else {
            $this.css("left", -(10) + "px");
            $this.css("min-width", "56%");
        }
    });
}
