(function( $ ) {

    "use strict";

    /**
     * Bootstrap Bugfix Navi Mobile
     * Open 2nd dropdown
     * try https://www.$script.net/menu/$-Plugin-For-Multilevel-Bootstrap-Dropdown-Navigation.html
     *
     * https://www.$script.net/menu/mobile-multi-level-dropdown-bootstrap.html  not working a second time
     */
    function naviMobile() {

        // 1) The $ script to prevent closing from click inside the dropdown
        $(document).on('click', '.dropdown-menu', function (e) {
            e.stopPropagation();
        });
        // 2) Convert the multi-level dropdown into an accordion when running on a small screen device
        if ($(window).width() < 767) {
            $('.dropdown a').click(function(e) {
                if($(this).attr('href') === '#') {
                    e.preventDefault();
                }
                $(this).parent('.dropdown').toggleClass('hwk-open');
            });


            $('.dropdown').on('click.bs.dropdown', function (e) {

                if( $(e.currentTarget).parents('.dropdown').length > 0) {
                    e.stopPropagation();
                    //console.log( e.currentTarget, $(e.currentTarget).parents('.dropdown').length);
                }
            });
        }
    }

    /**
     * div.slider-text-container
     *      div.slider-text-h1
     *          span.slider-text-h1-deco
     *          span.slider-text-h1-text
     *
     * @type {never|$}
     */
    function checkCarouselHeader() {

        var carouselHeader =  $('#carousel-header');
        var itemTextOnTop =  $('#carousel-header .item.text-on-top');

        $(itemTextOnTop).each(function(i, slide) {
            setCarouselHeaderTextOnTopDeco( slide );
        });
    }

    /**
     * Set Header Slider text on top deco height
     * (reqired jQuery actual plugin for hidden elements)
     * @param slide
     */
    function setCarouselHeaderTextOnTopDeco( slide ) {
        var h1Height = $(slide).find('.slider-text .slider-text-h1').actual('outerHeight');
        var h1Pt = parseInt($(slide).find('.slider-text-h1 > *').css('paddingTop'));
        var h1Pb = parseInt($(slide).find('.slider-text-h1 > *').css('paddingBottom'));
        $(slide).find('.slider-text .slider-text-h1-deco').css({'height': h1Height + h1Pt + h1Pb + 'px'});

        //console.log('setCarouselHeaderTextOnTopDeco', $(slide), h1Height );
    }

    /**
     * Ceck if arrows to horizontal swipe container necessary
     */
    function checkArrowsHorizontalSwipe(reset) {

        // @todo not used yet as problem on resize not found yet
        reset = reset || false;
        
        var container = $('.swipe-horizontal-container');
        var maxWidth = container.outerWidth(true);
        var actualWidth = 0;

        // if(reset) {
        //     $(container).animate({scrollLeft: 0}, 800);
        // }
        
        $.each($('.swipe-horizontal >'), function(i, item) {
            actualWidth += $(item).outerWidth(true);
        });

        // needs scrolling
        if( maxWidth < actualWidth ) {
            addArrowsHorizontalSwipe( maxWidth, actualWidth );
        }
        else {
            removeArrowsHorizontalSwipe();
        }
    }

    function addArrowsHorizontalSwipe( maxWidth, actualWidth ) {
        
        var container = $('.swipe-horizontal-container');

        var wrapper = $('.swipe-horizontal-wrapper');
        var left = $('.swipe-horizontal-left');
        var right = $('.swipe-horizontal-right');

        // -- create new --------------- //
        if( $(wrapper).length == 0 ) {
            $(container).wrap('<div class="swipe-horizontal-wrapper" />');
            wrapper = $('.swipe-horizontal-wrapper');
        }
        // Inserting Buttons
        if( $(left).length == 0 ) {
            wrapper.prepend('<a class="swipe-horizontal-arrow swipe-horizontal-left"><span class="glyphicon glyphicon-einfacher-pfeil-links"></span></a>');
            left = $('.swipe-horizontal-left');
        }
        if( $(right).length == 0) {
            wrapper.append('<a class="swipe-horizontal-arrow swipe-horizontal-right"><span class="glyphicon glyphicon-einfacher-pfeil-rechts"></span></a>');
            right = $('.swipe-horizontal-right');
        }
        // ---------------------------- //
        
        var speed = 240;
        $(wrapper).addClass('has-swipe');
        $(left).removeClass('invisible'); //.addClass('disabled');
        $(right).removeClass('invisible');
        
        $(left).click(function() {
            var leftPos = container.scrollLeft();
            container.animate({
                scrollLeft: leftPos - speed
            }, 800/*, function() {
                if ($(container).scrollLeft() <= 0) {
                    $(left).addClass('disabled');
                }
            }*/);
        });

        $(right).click(function() {
            $(left).removeClass('disabled');
            var leftPos = container.scrollLeft();
            container.animate({
                scrollLeft: leftPos + speed
            }, 800/*, function() {
                if(maxWidth + $(container).scrollLeft() >= actualWidth) {
                    $(right).addClass('disabled');
                }
            }*/);
        });
    }

    function removeArrowsHorizontalSwipe() {
        $('.swipe-horizontal-wrapper').removeClass('has-swipe');
        $('.swipe-horizontal-left').addClass('invisible');
        $('.swipe-horizontal-right').addClass('invisible');
    }

    function initNew() {
        naviMobile();
        checkCarouselHeader();
        checkArrowsHorizontalSwipe();

        $('#myScrollspy .nav-tabs, .scrollspy .nav-tabs').affix({
            offset: {
                top: 100,
                bottom: function () {
                    return (this.bottom = $('footer').outerHeight(true) + 40)
                }
            }
        })
    }

    $(window).resize(function() {
        checkCarouselHeader();
        //checkArrowsHorizontalSwipe(true); // still not working
    });

    initNew();

    // if html was adjusted set deco of header slider textOnTop
    $(document).on('adjust.carouselheader.textontop', function(e) {
        setCarouselHeaderTextOnTopDeco( e.slide );
    });

}( jQuery ));