var Wick = Wick || {};
Wick.topRated = (function () {

    let rangesToggler = $('.ranges-toggle'),
        rangesList = $('.ranges'),
        showReview = $('.product-rated-block__products-item'),
        windowWidth = window.innerWidth;

    $(window).resize(function() {
        windowWidth = $(window).width();
    })

    $(document).mouseup(function(e) {
        if (windowWidth < 768) {
            if (!rangesToggler.is(e.target) && rangesToggler.has(e.target).length === 0)
            {
                rangesToggler.removeClass('border-bottom-rounded_false')
                .find('.icon').removeClass('icon-rotated');
                rangesList.removeClass('d-block border-bottom-rounded');
                console.log(windowWidth);
            }
        }
    });

    rangesToggler.on('click', function (e) {
        e.preventDefault();
        rangesToggler.toggleClass('border-bottom-rounded_false')
        .find('.icon').toggleClass('icon-rotated');
        rangesList.toggleClass('d-block border-bottom-rounded');
    });

    $(function(){
        $('a.ranges-el').on('click', function(e) {
          e.preventDefault(); 
          let sc = $(this).attr("href"),
              dn = $(sc).offset().top - 30;

          $('html, body').animate({scrollTop: dn}, 1000);

          if (windowWidth < 768) {
            rangesToggler.removeClass('border-bottom-rounded_false')
            .find('.icon').removeClass('icon-rotated');
            rangesList.removeClass('d-block border-bottom-rounded');
          }
        });
    });

    $(function(){
        showReview.on('click', function () {
            $(this).parent().find(".product-rated-block__review").removeClass( "index-show" );
            $(this).next().addClass( "index-show" );
            $(this).parent().find(".product-rated-block__review-wrap").removeClass( "show" );
            $(this).next().find(".product-rated-block__review-wrap").addClass( "show" );

            if (windowWidth < 768) {
                $('html, body').animate({
                    scrollTop: $(this).next().offset().top-30
                }, 1000);
            }
        });
    });
})();
