let nav = $('.nav-open');
let SwiperSubMenu = document.querySelector('.swiper-sub-menu');
if (SwiperSubMenu) {
    let start = 6;
    let start_1024 = 5;
    let start_768 = 4;
    let elements = SwiperSubMenu.querySelectorAll('.swiper-slide');
    if (elements.length < 6) {
        start = elements.length;
        start_1024 = start;
        if (start < 4) {
            start_768 = start;
        }
    }
    let swiper = new Swiper('.swiper-sub-menu', {
        slidesPerView: start,
        loop: false,
        preventClicks: true,
        preventClicksPropagation: false,
        breakpoints: {
            1024: {
                slidesPerView: start_1024,
            },
            768: {
                slidesPerView: start_768,
            },
            640: {
                slidesPerView: 3,
            },
            480: {
                slidesPerView: 2,
            }
        },
        //loopFillGroupWithBlank: true,
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
        onClick: function(sw, e) {
            e.preventDefault();
        },
        onTap: function(sw, e) {
            e.preventDefault();
            sw.allowClick = false;

        }
    });
    let articles = $('article')
        , sticky = $('.swiper-sub-menu')
        , nav_height = nav.outerHeight();

    $(window).on('scroll', function () {
        let cur_pos = $(this).scrollTop();
        articles.each(function() {
            let top = $(this).offset().top - nav_height,
                bottom = top + $(this).outerHeight();
            if (cur_pos + 150  >= top && cur_pos + 150 <= bottom) {
                swiper.slideTo($(this).data('index'));
                sticky.find('a').removeClass('active');
                sticky.find('a[href="#'+$(this).attr('id')+'"]').addClass('active ');
            }
        });
    });
}
let  swiperCottage= new Swiper('.swiper-sub-menu-cottage', {
    slidesPerView: 5,
    loop: false,
    preventClicks: true,
    preventClicksPropagation: false,
    breakpoints: {
        1024: {
            slidesPerView: 4,
        },
        768: {
            slidesPerView: 3,
        },
        640: {
            slidesPerView: 3,
        },
        480: {
            slidesPerView: 2,
        }
    },
    //loopFillGroupWithBlank: true,
    navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
    },
    onClick: function(sw, e) {
        e.preventDefault();
    },
    onTap: function(sw, e) {
        e.preventDefault();
        sw.allowClick = false;
    }
});

new Swiper('.swiper-header', {
    spaceBetween: 30,
    effect: 'fade',
    speed: 1000,
    pagination: false,
    navigation: false,
    autoplay: {
        delay: 6000,
    }
});


new Swiper('.swiper-room', {
    spaceBetween: 30,
    effect: 'fade',
    speed: 1000,
    pagination: false,
    navigation: false,
    autoplay: {
        delay: 4000,
    }
});

let el = document.querySelector('.shop-isotope');
if (el) {
    let numItems = $('.swiper-slide').length;
    let numSlide = 4;
    if (numItems < 4)
    {
        numSlide = numItems;
    }
    let swiperCategory = new Swiper('.swiper-category', {
        slidesPerView: numSlide,
        loop: false,
        preventClicks: true,
        preventClicksPropagation: false,
        breakpoints: {
            1024: {
                slidesPerView: numSlide-1,
            },
            768: {
                slidesPerView: numSlide-1,
            },
            640: {
                slidesPerView: numSlide-2,
            },
            480: {
                slidesPerView: numSlide-2,
            }
        },
        //loopFillGroupWithBlank: true,
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
        pagination: {
            el: '.swiper-pagination',
            dynamicBullets: true,
        },
        onClick: function(sw, e) {
            e.preventDefault();
        },
        onTap: function(sw, e) {
            e.preventDefault();
            sw.allowClick = false;
        }
    });
}



if (document.querySelector('.swiper-sub-menu-cottage')){
    let articles = $('article')
        , sticky = $('.swiper-sub-menu-cottage')
        , nav_height = nav.outerHeight();

    $(window).on('scroll', function () {
        let cur_pos = $(this).scrollTop();
        articles.each(function() {
            let top = $(this).offset().top - nav_height,
                bottom = top + $(this).outerHeight();
            if (cur_pos + 150  >= top && cur_pos + 150 <= bottom) {
                swiperCottage.slideTo($(this).data('index'));
                sticky.find('a').removeClass('active');
                sticky.find('a[href="#'+$(this).attr('id')+'"]').addClass('active ');
            }
        });
    });
}

if (window.innerWidth < 970) {
    let swiperGallery = new Swiper('.swiper-gallery-horizontal', {
        slidesPerView: 2,
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
        breakpoints: {
            970: {
                slidesPerView: 4,
            },
            560: {
                slidesPerView: 3,
            },
        },
    });
}else{
    let swiperGallery = new Swiper('.swiper-gallery', {
        direction: 'vertical',
        slidesPerView: 4,
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev'
        },
    });
}

$(window).on('resize', function ()
{
    if (window.innerWidth < 970) {
        let swiperGallery = new Swiper('.swiper-gallery-horizontal', {
            slidesPerView: 2,
            navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
            },
            breakpoints: {
                970: {
                    slidesPerView: 4,
                },
                560: {
                    slidesPerView: 3,
                },
            },
        });
    }else{
        let swiperGallery = new Swiper('.swiper-gallery', {
            direction: 'vertical',
            slidesPerView: 4,
            navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev'
            },
        });
    }

});

$( ".gallery" ).on( "click", ".swiper-slide", function() {
    $('.large-image').find('img').attr('src', $( this ).data('image'));
    $('.legend').html($( this ).data('legend'));
    $( ".swiper-slide" ).each(function( index ) {
        $(this).css('opacity',0.8);
    });
    $(this).css('opacity',1);
});

$( ".slide-images" ).each(function( index ) {
    new Swiper($(this), {
        loop: false,
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
        pagination: {
            el: '.swiper-pagination',
            dynamicBullets: true,
        },
    });
});

/*

let swiperGallery = new Swiper('.swiper-salons', {
    direction: 'vertical',
    slidesPerView: 4,
    navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev'
    },
});*/
