var $ = jQuery.noConflict();
$(document).ready(function(){
    let hasSelected = false, clickedSize = false;
    const colorWrapper = $( 'ul[data-attribute_name="attribute_pa_color"]' );
    if( colorWrapper.length ){
        colorWrapper.find( '.variable-item-contents' ).on( 'click', function(){
            hasSelected = false;
            const sizeElem =  $( 'ul[data-attribute_name="attribute_pa_sizes"] li' );
            sizeElem.each( function() {
                if( $( this ).hasClass( 'selected' ) ) {
                    hasSelected = true;
                }
            });
            if( hasSelected )  return;
            clickedSize = ( sizeElem.eq( 0 ).hasClass( 'disabled' ) ) ? sizeElem.eq( 1 ) : sizeElem.eq( 0 );
            clickedSize.find('.variable-item-contents').trigger( 'click' );

        });
    }
    var slider        = $(".simple-slider-wrapper");
    var item_length   = $('.simple-slider-wrapper > div').length - 2;

    var loop      = slider.attr('data-loop');
    var interval  = slider.attr('data-interval');

    slider.slick({
        'infinite' : (loop == 1) ? 'true' : 'false',
        'dots' : false,
        'vertical': true,
        'arrows' : false,
        'autoplay': true,
        'autoplaySpeed': (interval) ? parseInt(interval) : 3000,
        'speed': 1

    });

//    slider.on('beforeChange', function(event, slick, currentSlide, nextSlide){
//        if( item_length === nextSlide ){
//            slider.slick('slickSetOption', 'autoplaySpeed', 10000);
//
//        };
//    });


    $(window).bind('mousewheel DOMMouseScroll', function(event){
        if (event.originalEvent.wheelDelta > 0 || event.originalEvent.detail < 0) {
            // scroll up
            slider.slick('slickPrev');
        }
        else {
            slider.slick('slickNext');
        }
    });

    $('.simple-burger').on('click', function(){
        if ( $('.simple-menu ul').hasClass('opened-simple-menu') ) {
            $('.simple-menu ul').hide();    
            $('.simple-menu ul').removeClass('opened-simple-menu');
        } else {
            $('.simple-menu ul').addClass('opened-simple-menu');
            $('.simple-menu ul').show();
        }
    });

    document.addEventListener('touchstart', handleTouchStart, false);
    document.addEventListener('touchmove', handleTouchMove, false);

    var xDown = null;
    var yDown = null;

    function getTouches(evt) {
    return evt.touches ||             // browser API
            evt.originalEvent.touches; // jQuery
    }

    function handleTouchStart(evt) {
        const firstTouch = getTouches(evt)[0];
        xDown = firstTouch.clientX;
        yDown = firstTouch.clientY;
    };

    function handleTouchMove(evt) {
        if ( ! xDown || ! yDown ) {
            return;
        }

        var xUp = evt.touches[0].clientX;
        var yUp = evt.touches[0].clientY;

        var xDiff = xDown - xUp;
        var yDiff = yDown - yUp;

        if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {/*most significant*/
            if ( xDiff > 0 ) {
                /* left swipe */ 
                slider.slick('slickPrev');
            } else {
                /* right swipe */
                slider.slick('slickNext');
            }
        } else {
            if ( yDiff > 0 ) {
                /* up swipe */ 
                slider.slick('slickNext');
            } else { 
                /* down swipe */
                slider.slick('slickPrev');
            }
        }
        /* reset values */
        xDown = null;
        yDown = null;
    };

    var archive_slider = $('.collection-slider');

    archive_slider.each(function(){
        var loop        = $(this).attr('data-loop');
        var interval    = $(this).attr('data-interval');

        $(this).slick({
            'infinite' : (loop) ? 'true' : 'false',
            'dots' : false,
            'vertical': false,
            'arrows' : false,
            'autoplay': true,
            'autoplaySpeed': (interval) ? parseInt(interval) : 3000,
            'speed': 1
        });

    });

    if (document.querySelector('.erfolg-custom-slider')) {
        var media_slider = $('.erfolg-custom-slider .gallery');


        media_slider.each(function(){
            var loop        = $(this).parent().attr('data-loop');
            var interval    = $(this).parent().attr('data-interval');

            $(this).slick({
                'infinite' : (loop) ? 'true' : 'false',
                'dots' : false,
                'vertical': false,
                'arrows' : false,
                'autoplay': true,
                'autoplaySpeed': (interval) ? parseInt(interval) : 3000,
                'speed': 1
            });

        });

    }


});


