$(document).ready(function(){	

	$('#home-cycle > div').on('click', function(){
    	window.location.href = $(this).attr('data-cycle-href');
    })


    var index = 0;
    var lines = 0;
    
    $('#slider-btn').on('click', function(){
        if($('#home-overlay').hasClass('show')) {
            $(this).removeClass('active');
            $('#home-overlay').removeClass('show');  
            $('#title-caption-container').show();    
        } else {
            $(this).addClass('active');
            $('#home-overlay').addClass('show');   
            $('#title-caption-container').hide();  
        }
    })
    
    if($('#home-overlay-menu').length) {
        // Calculate overlay menu font size
        var height          = $('#home-overlay-menu').height();
        var defHeight       = $(window).height() - 150;
        var defFontSize     = 5.5;
        var defLineHeight   = 6.5;

        while(height < (defHeight-50)) {
            defFontSize     += 0.1;
            defLineHeight   += 0.1;
            $('#home-overlay-menu a').css({
                'font-size'     : defFontSize+'vh',
                'line-height'   : defLineHeight+'vh'
            })
            height = $('#home-overlay-menu').height();
        }

        while(height > (defHeight+50)) {
            defFontSize     -= 0.1;
            defLineHeight   -= 0.1;
            $('#home-overlay-menu a').css({
                'font-size'     : defFontSize+'vh',
                'line-height'   : defLineHeight+'vh'
            })
            height = $('#home-overlay-menu').height();
        }
    }
    
    if(!$('.sidebar').length) firstNextClick();
    
    $('.sidebar').click(function(){
        $('#overlay').show();
        $('body').css('position', 'fixed');
    })
    
    $('.photo_text:has(iframe)').addClass('ext');
    
    $('#nav-btn').on('click',function(){
        if($(this).hasClass('open')) {
            $(this).removeClass('open');
            $('#nav').removeClass('open');
        } else {
            $(this).addClass('open');
            $('#nav').addClass('open');
        }
    })
    
    $('#slider').swipe( {
        swipeLeft:function(event, direction, distance, duration, fingerCount) {nextClick()},
        swipeRight:function(event, direction, distance, duration, fingerCount) {prevClick()}
    });
    
    $('#start_overlay').on('click', function(){$(this).fadeOut('fast');})
    
    if($('.slide').length > 0) {changeSliderWidth();}
    
    $(window).on('resize', function(){
        if($('.slide').length > 0) {changeSliderWidth();}
    })

    $(window).on('orientationchange',function(){
        if($('.slide').length > 0) {changeSliderWidth();}    
    })

    function changeSliderWidth() {
        if ($(window).width() < 751) {
            var sLeft = 0;
            var sCount = 0;
            $('.slide').each(function(){
                sCount++;
                if (sCount < index) {
                    sLeft -= $(this).width() + 50;
                }
            })
        }
        var wWidth = ($(window).width() < 751) ? ($(window).width() - 50) : 1050;
        $('#slider').css('width', wWidth + $('#slider').offset().left);
        $('#slide_container').css('left', sLeft);
    }

    function prevClick() {
        var startSlide = ($(window).width() < 751) ? 1 : 0;
        
        if(index > 0) {
            index--;
            $('#slide_container').animate({
                left: "+=" + ($('.slide').eq(index - startSlide ).width() + 50)
            }, 500);
            $('.next.slider').show();
        }  
        
        if(index < (startSlide + 1)) {$('.prev.slider').hide();}    
    }

    function nextClick() {
        var startSlide = ($(window).width() < 751) ? 0 : 1;
        if(index < ($('.slide').length - startSlide)) {
            $('#slide_container').animate({
                left: "-=" + ($('.slide').eq(index + startSlide - 1).width() + 50)
            }, 500);
            index++;
            $('.prev.slider').show();
        }  
        
        if(index > ($('.slide').length - 1 - startSlide)) {$('.next.slider').hide();} 
    }

    function firstNextClick() {
        if($(window).width() < 751) $('.prev.slider').hide();
        if(index < $('.slide').length - 1) {
            $('#slide_container').animate({
                left: "-=" + ($('.slide').eq(index).width() + 50)
            }, 0);
            index++;
        }
    }

    $("body").keydown(function(e) {
        if(e.keyCode == 37) { 
            e.preventDefault();
            prevClick();
            if (!$('.prev').hasClass('slider') && $('.prev').length) window.location.href = $('.prev').attr('href');
        } else if(e.keyCode == 39) { 
            e.preventDefault();
            nextClick(); 
            if (!$('.next').hasClass('slider') && $('.next').length) window.location.href = $('.next').attr('href');    
        }
    });

    $('.prev.slider').on('click', function(e){
        e.preventDefault();
        prevClick();
    })

    $('.next.slider').on('click', function(e){
        e.preventDefault();
        nextClick();
    })
    
    $('h4 a').each(function(){
        var href = $(this).attr('href');
        $(this).parent().next().css('cursor', 'pointer');
        
        $(this).parent().next().on('click', function() {
            window.location.href = href;
        }); 
        
        $(this).parent().next().on('mouseover', function() {$(this).css('color','#282828');});
        
        $(this).on('mouseover', function() {$(this).parent().next().css('color','#282828');});
        
        $(this).parent().next().on('mouseout', function() {$(this).css('color','#666666');});
        
        $(this).on('mouseout', function() {$(this).parent().next().css('color','#666666');});
    })
    
    //$('.review').each(function(){
        //$(this).css('height', $(this).height());
    //})
    
    $('.review').on('click', function(e){
        e.preventDefault();
        $('html, body').animate({ scrollTop: 0 }, 0);
        if($(this).find('.review_img img').width() >= $(this).find('.review_img img').height() && $(this).find('.review_img img').width() > 300) {
            overlay = '#overlay2';
        } else {
            overlay = '#overlay';            
        }
        overlay = '#overlay2';
        $(overlay + ' .col1_3 img').attr('src', $(this).find('.review_img img').attr('src')).show();
        $(overlay + ' .col2_3 h1').html($(this).attr('title'));
        $(overlay + ' .col2_3 h2').html($(this).attr('name'));
        $(overlay + ' .col2_3 div').html($(this).find('.review_text').html());
        $(overlay).show();
    })
    
    $('.bgbutton').on('click', function(e){
        e.preventDefault();
        $('html, body').animate({ scrollTop: 0 }, 0);
        overlay = '#overlay2';
        $(overlay + ' .col1_3 img').attr('src', '').hide();
        $(overlay + ' .col2_3 h1').html($(this).attr('title'));
        $(overlay + ' .col2_3 h2').html($(this).attr('name'));
        $(overlay + ' .col2_3 div').html($(this).find('.review_text').html());
        $(overlay).show();
    })
    
    $('.close').on('click', function(e){
        e.preventDefault();
        $(overlay).hide();
        $('body').css('position', 'static');
        
    })  
    
    $('.vimeo_thumb').each(function(){
        var thumb = $(this);
        $.ajax({
            type:'GET',
            url: 'http://vimeo.com/api/v2/video/' + thumb.attr('id') + '.json',
            jsonp: 'callback',
            dataType: 'jsonp',
            success: function(data){             
                var thumbnail_src = data[0].thumbnail_medium;
                thumb.find('img').attr('src', thumbnail_src);
            }
        })
    })
    
    $('.vimeo_thumb').on('click', function(e){
        e.preventDefault();
        $('#vimeo').attr('src', '//player.vimeo.com/video/' + $(this).attr('id') + '?portrait=0');
        $('.col2_3 .col1_3 h1').html($(this).attr('title'));
        $('.col2_3 .col1_3 h2').html($(this).attr('subtitle'));
        $('.col2_3 .col1_3.smaller').html($(this).find('div').html());
    })
    
    $('.vimeo-thumb').on('click', function(e){
        e.preventDefault();
        $('#vimeo').attr('src', '//player.vimeo.com/video/' + $(this).attr('id') + '?portrait=0');
        $('.vimeo-title h1').html($(this).attr('title'));
        $('.vimeo-title h2').html($(this).attr('subtitle'));
        $('.jukebox').html($(this).find('div').html());
    })
    
    // Zet alle externe links op target=_blank
    $('a[href^="http"]').each(function () {
        var linkHost = this.hostname;
        var currentHost = window.location.hostname;

        if (linkHost && linkHost !== currentHost) {
          $(this).attr('target', '_blank');
          $(this).attr('rel', 'noopener noreferrer');
        }
      });
    
    // Hide Header on on scroll down
    var didScroll;
    var lastScrollTop = 0;
    var delta = 5;
    var navbarHeight = $('#header').outerHeight();

    $(window).scroll(function(event){
        didScroll = true;
    });

    setInterval(function() {
        if (didScroll) {
            hasScrolled();
            didScroll = false;
        }
    }, 250);

    function hasScrolled() {
        var st = $(this).scrollTop();

        // Make sure they scroll more than delta
        if(Math.abs(lastScrollTop - st) <= delta)
            return;

        // If they scrolled down and are past the navbar, add class .nav-up.
        // This is necessary so you never see what is "behind" the navbar.
        if (st > lastScrollTop && st > navbarHeight){
            // Scroll Down
            $('#header').removeClass('nav-down').addClass('nav-up');
            $('#nav-btn').removeClass('open');
            $('#nav').removeClass('open');
        } else {
            // Scroll Up
            if(st + $(window).height() < $(document).height()) {$('#header').removeClass('nav-up').addClass('nav-down');}
        }

        lastScrollTop = st;
    }
})