(function($) {
    "use strict";

    /* ==============================================
    HEADER STICKY -->
    =============================================== */

    $(window).scroll(function() {
        if ($(this).scrollTop() > 1){  
            $('header').addClass("sticky");
        }
        else{
            $('header').removeClass("sticky");
        }
    });
    

     /* ==============================================
    nav-tabs -->
    =============================================== */
    var hidWidth;
    var scrollBarWidths = 40;
    var windowW = $(window).width();

    var widthOfList = function(){
      var itemsWidth = 0;
      $('.list-nav-tabs li').each(function(){
        var itemWidth = $(this).outerWidth();
        itemsWidth+=itemWidth;
      });
      return itemsWidth;
    };

    var widthOfHidden = function(){
      return (($('.wrapper-nav-tabs').outerWidth())-widthOfList()-getLeftPosi())-scrollBarWidths;
    };

    var getLeftPosi = function(){
        if($('.list-nav-tabs').length){
            return $('.list-nav-tabs').position().left;
        }else{
            return 0;
        }
    };

    var reAdjust = function(){
      if (($('.wrapper-nav-tabs').outerWidth()) < widthOfList()) {
        $('.scroller-right-nav-tabs').show();
      }
      else {
        $('.scroller-right-nav-tabs').hide();
      }
      
      if (getLeftPosi()<0) {
        $('.scroller-left-nav-tabs').show();
      }
      else {
        $('.item').animate({left:"-="+getLeftPosi()+"px"},'slow');
        $('.scroller-left-nav-tabs').hide();
      }
    }

    if(windowW <= 992){    
        reAdjust();
    }
    $(window).on('resize',function(e){  
         var windowW = $(window).width();
         if(windowW <= 992){    
            reAdjust();
        }else{
            $('.scroller-right-nav-tabs').hide();
            $('.scroller-left-nav-tabs').hide();
        }
    });


    $('.scroller-right-nav-tabs').click(function() {
   
      if(((getLeftPosi()*-1) + $('.wrapper-nav-tabs').outerWidth()) < (widthOfHidden()*-1)){
        $('.scroller-left-nav-tabs').fadeIn('slow');
        $('.list-nav-tabs').animate({left:"+="+($('.wrapper-nav-tabs').outerWidth()*-1)+"px"},'slow',function(){

        });
      }else{
        $('.scroller-left-nav-tabs').fadeIn('slow');
        $('.scroller-right-nav-tabs').fadeOut('slow');
        $('.list-nav-tabs').animate({left:"+="+widthOfHidden()+"px"},'slow',function(){

        });
      }
    
    });

    $('.scroller-left-nav-tabs').click(function() {



    if(((getLeftPosi()) + $('.wrapper-nav-tabs').outerWidth()) < 0){
        $('.scroller-left-nav-tabs').fadeIn('slow');
        $('.list-nav-tabs').animate({left:"-="+($('.wrapper-nav-tabs').outerWidth()*-1)+"px"},'slow',function(){

        });
      }else{
        $('.scroller-right-nav-tabs').fadeIn('slow');
        $('.scroller-left-nav-tabs').fadeOut('slow');
        $('.list-nav-tabs').animate({left:"-="+getLeftPosi()+"px"},'slow',function(){
           $('.list-nav-tabs').css({'left': 0});
        });
      }

       
    });    

    /* ==============================================
    MENU HOVER -->
    =============================================== */

    $(".header .dropdown:not(.openClick)").hover(            
        function() {
            $(".dropdown.openClick").removeClass('open');
            $('.dropdown-menu', this).stop( true, true ).slideDown("fast");
            $(this).toggleClass('open');        
        },
        function() {
            if(!$(".dropdown-toggle").hasClass('selectpicker')){
                $(".dropdown-toggle").dropdown('toggle');
            }
            $('.dropdown-menu', this).stop( true, true ).slideUp("fast");
             $(this).toggleClass('open');       
        }
    );


  


    /* ==============================================
    collapse_open -->
    =============================================== */

    function collapse_open(){
        var windowW = $(window).width();
        if(windowW <= 768){
            $('.collapse-widget').addClass('collapsed');
            $('.collapse-widget').parents('.widget').find('.collapse').removeClass('in');
        }else{
            $('.collapse-widget').removeClass('collapsed');
            $('.collapse-widget').parents('.widget').find('.collapse').addClass('in');
        }

    } 

    var $frist = false;
    $(window).resize(function() {
        if(!$first){
            var $first = true;
            collapse_open();
        }
    });
    collapse_open();

    
    /* ==============================================
    nav-tabs -->
    =============================================== */
    $('.nav-tabs li a').click(function (e) {     
        //get selected href
        var href = $(this).attr('href');    
    
        //set all nav tabs to inactive
        $('.nav-tabs li').removeClass('active');
        
        //get all nav tabs matching the href and set to active
        $('.nav-tabs li a[href="'+href+'"]').closest('li').addClass('active');

        //active tab
        $('.tab-pane').removeClass('active');
        $('.tab-pane'+href).addClass('active');

        if($(this).parents('.nav-tabs-bottom').length){
            $('html, body').animate({
            scrollTop: ($(".content-copy").offset().top + 200)
        }, 500);
        }
    
    })

    /* ==============================================
    tooltip -->
    =============================================== */

    $(function () {
      $('[data-toggle="tooltip"]').tooltip()
    })

    /* ==============================================
    carousel -->
    =============================================== */
    
    $('.carousel').carousel({
        interval: 5000 //changes the speed
    })
    
    /* ==============================================
    ACCORDION -->
    =============================================== */

    function toggleChevron(e) {
        console.log(e.target);
        var $i = $(e.target)
            .parent('.panel-group')
            .find("i.indicator");
        
        $i.toggleClass('icon-minus icon-plus');

        if($i.hasClass('icon-minus')){
            $i.parent('h3').find('span').html('Reiseleistungen ausblenden');
        }
        if($i.hasClass('icon-plus')){
            $i.parent('h3').find('span').html('Reiseleistungen einblenden');
        }   
    }
    $('#accordion').on('hidden.bs.collapse', toggleChevron);
    $('#accordion').on('shown.bs.collapse', toggleChevron);

   
    /* ==============================================
    LIGHTBOX -->
    =============================================== */

    /*
    jQuery('a[data-gal]').each(function() {
        jQuery(this).attr('rel', jQuery(this).data('gal'));
        });     
    jQuery("a[data-gal^='prettyPhoto']").prettyPhoto({animationSpeed:'slow',slideshow:false,overlay_gallery: false,theme:'light_square',social_tools:false,deeplinking:false});
    */
    /* ==============================================
    CAROUSEL -->
    =============================================== */

        $('.owl-fullwidth').owlCarousel({
            loop:true,
            margin:0,
            nav:true,
            dots:false,
            navText: [
                "<i class='fa fa-angle-right'></i>",                
                "<i class='fa fa-angle-left'></i>"],
                responsive:{
                0:{
                    items:1
                },
                600:{
                    items:2
                },
                1000:{
                    items:3
                }
            }
        })

      /* ==============================================
    BOX LINK -->
    =============================================== */

    $('.get-box-link').click(function(){
        location.href = $(this).find('.is-box-link').attr('href');
    });

      function resize_box (){
            $('.owl-fullwidth:not(.noresize)').each(function(){
                var boxes = $(this).find('.hl5');
                var maxHeight = Math.max.apply(
                Math, boxes.map(function() {
                    return $(this).height();
                }).get());
                boxes.height(maxHeight);
            });
            
        }
    resize_box();

    $(window).resize(function() {
       resize_box();
    });

    /* ==============================================
    DATAPICKER -->
    =============================================== */

        $(".datepicker").each(function(){
            var windowW = $(window).width();
            if(windowW <= 768){
                $(this).attr('readonly', 'true');
            }
        });
        $( ".datepicker" ).datepicker({ 
            dateFormat: 'dd.mm.yy', prevText: '&#x3c;zurück', prevStatus: '',
            prevJumpText: '&#x3c;&#x3c;', prevJumpStatus: '',
            nextText: 'Vor&#x3e;', nextStatus: '',
            nextJumpText: '&#x3e;&#x3e;', nextJumpStatus: '',
            currentText: 'heute', currentStatus: '',
            todayText: 'heute', todayStatus: '',
            clearText: '-', clearStatus: '',
            closeText: 'schließen', closeStatus: '',
            monthNames: ['Januar','Februar','März','April','Mai','Juni',
            'Juli','August','September','Oktober','November','Dezember'],
            monthNamesShort: ['Jan','Feb','Mär','Apr','Mai','Jun',
            'Jul','Aug','Sep','Okt','Nov','Dez'],
            dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
            dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
            dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
            firstDay: 1
         });

    })(jQuery);