$(document).ready(function() {

    function scrollToTabContent(tabSelector)
    {
        $(document.body).animate({
            scrollTop: $(tabSelector).offset().top - 220
        }, 1000);
    }

    function activateTravelDatesTab()
    {
        $('[href=\'#travel-dates-content-tab\']').tab('show');
        scrollToTabContent('#travel-dates-content-tab');
    }

    $('.nav-tabs > li > a').click(function() {
        scrollToTabContent($(this).attr('href'));
    });

    $('.st-slider-booking-btn').click(activateTravelDatesTab);

    if (location.hash === '#travel-dates-content-tab')
    {
        activateTravelDatesTab();
    }



    var videos$ = $('a[id^="video_"]');

    videos$.each(function() {

        var el$ = $(this);

        var text = el$.text();
        var length = text.length - 11;
        var caption = text.substring(0, length);
        var expl = this.id.substring(6, this.id.length);

        $('<iframe />')
            .attr({
                width: 680,
                height: 466,
                src: '//www.youtube.com/embed/'+ expl,
                frameborder: 0,
                allowfullscreen: true,
                'data-st-video': this.id
            })
            .addClass('st-collapsed')
            .hide()
            .insertAfter(this)
        ;
        el$
            .css('background-image', 'url(/images/st2/icons/arrowup.gif)')
            .text(caption + ' einblenden')
            .attr('href', 'javascript:void(0);')
        ;
    });

    videos$.click(function() {

        var el$ = $(this);
        var video$ = $('[data-st-video='+ this.id +']');
        var text = el$.text();
        var length = text.length - 11;
        var caption = text.substring(0, length);

        if (el$.hasClass('st-collapsed'))
        {
            video$.slideDown('slow');
            el$.text(caption + ' ausblenden');
            el$.removeClass('st-collapsed');
        }
        else
        {
            video$.slideUp(400);
            el$.text(caption + ' einblenden');
            el$.addClass('st-collapsed');
        }
    });

});