// ba-dotimeout
(function($){var a={},c="doTimeout",d=Array.prototype.slice;$[c]=function(){return b.apply(window,[0].concat(d.call(arguments)))};$.fn[c]=function(){var f=d.call(arguments),e=b.apply(this,[c+f[0]].concat(f));return typeof f[0]==="number"||typeof f[1]==="number"?this:e};function b(l){var m=this,h,k={},g=l?$.fn:$,n=arguments,i=4,f=n[1],j=n[2],p=n[3];if(typeof f!=="string"){i--;f=l=0;j=n[1];p=n[2]}if(l){h=m.eq(0);h.data(l,k=h.data(l)||{})}else{if(f){k=a[f]||(a[f]={})}}k.id&&clearTimeout(k.id);delete k.id;function e(){if(l){h.removeData(l)}else{if(f){delete a[f]}}}function o(){k.id=setTimeout(function(){k.fn()},j)}if(p){k.fn=function(q){if(typeof p==="string"){p=g[p]}p.apply(m,d.call(n,i))===true&&!q?o():e()};o()}else{if(k.fn){j===undefined?e():k.fn(j===false);return true}else{e()}}}})(jQuery);

/* @SplashScroll */

var splash_attached = true;
var splash_lock    = false;

;(function($) {

  $.fn.SplashScroll = function(settings) {
     
    var config = {};
 
    if (settings) $.extend(config, settings);
  
    return this.each(function() { 
      
      var $preroll = $('#preroll'),
          $win = $(window),
          $a = $('a.next'),
          $body = $('body'),
          $navigation = $('nav#main'),
          $utility = $('#navigation-utility'),
          $wrapper = $('#content-wrap').css({position: 'fixed', top: $win.height()}),
          $splashScroll = $('#splash-scroll'),
          $enterButton = $('#enter-button'),
          $bigImg = $('#preroll>img'),
          $bigImgWidth = $bigImg.attr('width'),
          $bigImgHeight = $bigImg.attr('height'),
          $stripes = $('#stripes'),
          $pattern = $('#pattern'),
          $overlay = $('#overlay'),
          //$enterSite = $('#enter-site'),
          $scroll = ($.browser.mozilla || $.browser.msie) ? $('html') : $body,
          $cta = $('#cta'),
          $featureHeadline = $('#feature-headline'),
          $featureCta = $('#feature-cta'),
          _featureCtaBottom = $featureCta.css('bottom'),
          _ctaHeight = $cta.height(),
          $backButton = $('<div id="back-button"/>').appendTo($wrapper);
          $backLink = $('<a href="#home" id="back-home">Terug naar home</a>').appendTo($backButton),
          _scrollPoints = [200,2600],
          _offsetTop = 175,
          _entered = false,
          _isIE7 = ($.browser.msie && parseFloat($.browser.version) <= 7.0),
          
          $carousel = $('.carousel'),
          pCarousel = null,
          carouselChecker = -1,
          
          $pane1 = $('<div class="intro-pane pane-plus"/>').html('<div class="inner-wrap"></div>'), 
          $pane2 = $('<div class="intro-pane pane-drops"/>').html('<div class="inner-wrap"></div>'), 
          $pane3 = $('<div class="intro-pane pane-plus"/>').html('<div class="inner-wrap"></div>'); 
      
      $wrapper.css({overflow: 'hidden'});
      on_resize();
      init();
              
      function init(){
        $pane3.prependTo($splashScroll);
        $pane2.prependTo($splashScroll);
        $pane1.prependTo($splashScroll);

          if(_isIE7){
              $('body').addClass('force-height');
              $('html').addClass('force-height');
          }
        
        // Do the resize to get the splash proportioned correctly.
        $win.bind('resize', on_resize);

    $a.each(function(){
            $(this).unbind('click');
      $(this).bind('click',function(e){
        e.preventDefault();

                var href =$(this).attr('href');
                _gaq.push(['_trackPageview', href]);

                Global.currentBranch = href;

        var getContents = $(this).attr('href');
        
        $.ajax({
          url: getContents,
          success: (function(data) { 
            $('#content',$wrapper).html(data);
            Global.afterAjaxInit();
          })
        });
      if(/*!_isIE7*/true) {
        // WISH: scrolling panes start in success function ajax call, but that doesn't work right away because the scope is different after succes. We are not in the splash scroll scope anymore...
        $scroll.scrollTop(0);
        _entered = true;
        $win.unbind('resize', on_resize);
      
        var inc = 300,
          dur = 800,
          delay = 0,
          winH = $win.height(),
          ease = 'easeInExpo',
          finalEase = 'easeInOutExpo';
        
        $preroll.animate({top: -$win.height()}, dur, ease);
        
        $.doTimeout(200, function(){
          $a.animate({bottom: -$a.height()}, 600, 'easeInOutExpo');
        });
        
        delay += inc;
        $.doTimeout(delay, function(){ $pane3.animate({top: -winH}, dur, ease); });
        
        delay += inc;
        $.doTimeout(delay, function(){ $pane2.animate({top: -winH}, dur, ease); });
        
        delay += inc*2;
        $.doTimeout(delay, function(){
          $pane1.animate({top: -winH}, dur, finalEase);
          $wrapper.css({top: winH}).animate({top: 0}, dur, finalEase, function(){
            $wrapper.css({overflowY: 'hidden', overflowX: 'hidden', height:'auto', position: 'static'});
            
            if(splash_attached && !splash_lock){
              $splashScroll.detach();
              splash_attached = false;
              splash_lock = true;
            }

                        if(_isIE7) {

                            $.doTimeout(delay, function(){
                                $('body').removeClass('force-height');;
                                $('html').removeClass('force-height');
                                $('body').css({overflowX: 'hidden'});
                                $('body').css({overflowY: 'visible'});
                            });
                        }
          });
        
          splash_lock = false;
          $.doTimeout(dur * .5, revealBackButton);
        
        });
            } /*else {
                $('.intro-pane').remove();
                var inc = 300,
                    dur = 1200,
                    delay = 0,
                    winH = $win.height(),
                    ease = 'easeInExpo',
                    finalEase = 'easeInOutExpo';

                $preroll.animate({top: -$win.height()}, dur, ease);
                $.doTimeout(dur * .5, revealBackButton);
                //$.doTimeout(dur * .5, function(){$splashScroll.detach();});
                delay += inc*2;

                $.doTimeout(delay, function(){
                    $wrapper.css({overflowY: 'hidden', overflowX: 'hidden', height:'auto', position: 'static'});

                        $splashScroll.detach();
                        splash_attached = false;
                        splash_lock = true;
                    $('body').removeClass('force-height');
                    $('html').removeClass('force-height');
                    $('html').css({overflow: 'hidden'});
                    $('body').css({overflowX: 'hidden'});


                });
            }*/
        
        e.preventDefault();
        $(this).unbind('click');
        
      }); // End click() bind
    }); // End each()
        
        
      }; // End init()
      
      function revealBackButton(){
        $backButton.css({display: 'block'});
        
        $backLink
            .css({top: -$backLink.height()})
            .animate({top:0}, 600, 'easeInOutExpo', function(){
        $backLink.unbind('click');
        $backLink.bind('click',onBackLinkClick);
            });

          if($('#all-vacancies-back-home').length > 0) {
              $('#all-vacancies-back-home').unbind('click');
              $('#all-vacancies-back-home').bind('click',onBackLinkClick);
          }
        
    if($('#nav').length > 0) {
      $('#nav').fadeIn('fast');
    }
      };


      function onBackLinkClick(e) {
    if($('#nav').length > 0) {
      $('#nav').fadeOut('fast', function() {
        $(this).remove();
      });
    }

          _gaq.push(['_trackPageview', '/home']);
          Global.currentBranch = '';
          Global.currentTarget = '';

          var inc = 300,
              dur = 800,
              delay = 0,
              winH = $win.height(),
              ease = 'easeInExpo',
              finalEase = 'easeInOutExpo';
          _entered = false;

          if(_isIE7){
              $('body').addClass('force-height');
              $('html').addClass('force-height');
          }

          // Get the panels and the preroll off-screen.
          $pane1.css({top: winH});
          $pane2.css({top: winH});
          $pane3.css({top: winH});
          $preroll.css({top: winH});

          on_resize();

          $splashScroll.prependTo($body);
          splash_attached = true;


      $.doTimeout(200, function(){
        $backLink.animate({bottom: -$backLink.height()}, 600, 'easeInOutExpo');
      });


          $pane1.animate({top: 0}, dur, ease);

          delay += inc;
          $.doTimeout(delay, function(){ $pane2.animate({top: 0}, dur, ease); });

          delay += inc;
          $.doTimeout(delay, function(){ $pane3.animate({top: 0}, dur, ease); });

          delay += inc * 2;
          $.doTimeout(delay, function(){
            $preroll.animate({top: 0}, dur, finalEase);
            $wrapper.css({overflow: 'hidden', height: winH + 'px', position: 'fixed', top: 0});
            $.doTimeout(dur * .5, function(){
              $enterButton.animate({bottom: 0}, 600, 'easeInOutExpo', function(){
                $scroll.scrollTop(0);
                init();
              });
            });
          });

          e.preventDefault();
          $backLink.unbind('click');


      }; // End onBackLinkClick()

      function on_resize(){
                    
        if(!_entered) {
          var win_w = $win.width(),
              win_h = $win.height();
        
          // Make the wrapper's content hidden and height be the window height.
          if($wrapper.css('overflow') == 'hidden')
            $wrapper.css({height: win_h});
        
          // Lets position that bottom CTA when the browser win goes below a certain point.
          if(win_h < 680) {
            $featureCta.css('bottom', win_h - 560);
          } else {
            $featureCta.css('bottom', _featureCtaBottom);
          }
          
          
          // Basic idea is resize the image to fit.
          // First scale based on the width.
          
          var dest_w = win_w;
          var dest_h = dest_w * ($bigImgHeight / $bigImgWidth);
          if(dest_h < win_h) {
            // need to scale it back up.
            dest_h = win_h;
            dest_w = dest_h * ($bigImgWidth / $bigImgHeight);
          }
        
          $bigImg.attr('width', dest_w);
          $bigImg.attr('height', dest_h);
        
          // Offset the margin.
          if(dest_w > win_w) {
            $bigImg.css({marginLeft: Math.floor((dest_w - win_w) / -2)});
          } else {
            $bigImg.css({marginLeft: 0});
          }
          if(dest_h > win_h) {
            $bigImg.css({marginTop: Math.floor((dest_h - win_h) / -2)});
          } else {
            $bigImg.css({marginTop: 0});
          }
        
        }
        
      }; // End on_resize()
      
    }); // End jQuery.each() loop
      
  }; // End jQuery.SplashScroll plugin

})(jQuery);
