/*
 *
 *  Author:  Rafaël Blais Masson
 *             <http://twitter.com/rafBM>
 *             <https://github.com/rafBM>
 *
 *  Company: iXmédia <http://ixmedia.com>
 *  
 * --------------------------------------------- */

(function($) {
$(function() {

/* debugging {{{
******************************************************************************/

  if ( !('console' in window) ) {
    window.console = {
      log: function() {}
    };
  }

  function functionLog( functionName, theThis ) {
    var callerArguments = arguments.callee.caller.arguments,
      argumentsArray = [];
  
    for ( var i = 0; i < callerArguments.length; i++ ) {
      argumentsArray.push( callerArguments[ i ] );
    }
  
    if ( theThis ) {
      console.log( functionName+'(', argumentsArray, ')', 'this =', theThis );
    } else {
      console.log( functionName+'(', argumentsArray, ')' );
    }
  }

/* }}} */

/* jQuery extensions {{{
******************************************************************************/

  $.fn.percentageWindowOffset = function () {
    var elementScrollOffset = $(this[0]).offset().top - $window.scrollTop();
    return Math.round( elementScrollOffset * 100 / $window.height() );
  };
  
  $.fn.fadeInContentBlock = function() {
    // functionLog( '$.fn.fadeInContentBlock', this );
    
    this.each(function() {
      var $this = $(this);
      
      if ( $.browser.msie || ($.browser.mozilla && parseInt(GECKO_VERSION,10) < 191 && GECKO_VERSION.charAt(0) != '2') || $.browser.opera ) {
        $this.fadeIn( 600 );
        
      } else {
        $this.find('.fade-in-block').stop().clearQueue();

        $this.fadeIn( 600 );

        var delay = 200;
        $this.find('.fade-in-block').each(function() {
          var $this = $(this);
          var timeout = setTimeout(function() {
            $this.animate({ 'opacity': 1 }, 600);
          }, delay);
          delay += 400;
        });
      }
      
      if ( $this.is('#content-2, #content-4') ) {
        window.arrowKeys.$arrows4.animate({ 'opacity': 1 });
      } else {
        window.arrowKeys.$arrows4.animate({ 'opacity': 0 });
      }
    });
  };
  
  $.fn.fadeOutContentBlock = function() {
    // functionLog( '$.fn.fadeOutContentBlock', this );
    
    this.each(function() {
      var $this = $(this);
      
      if ( $.browser.msie || ($.browser.mozilla && parseInt(GECKO_VERSION,10) < 191 && GECKO_VERSION.charAt(0) != '2') || $.browser.opera ) {
        
        $this.fadeOut( 600 );
        
      } else {
        
        $this.find('.fade-in-block').stop().clearQueue();

        $this.fadeOut(600, function() {
          $this.find('.fade-in-block').css( 'opacity', 0 );
        });
      }
    });
  };

/* }}} */

/* browsa stuff {{{
******************************************************************************/

  window.IE8 = !!($.browser.msie && $.browser.version == 8);
  window.GECKO_VERSION = $.browser.version.replace(/\./g,'').substr(0,3);
  if (GECKO_VERSION.length < 3) {
    (function() {
      var numbersToPad = 3 - GECKO_VERSION.length;
      while (numbersToPad--) GECKO_VERSION += '0';
    })();
  }
  window.IPAD = (navigator.platform == 'iPad');

  if ( $.browser.mozilla ) {
    $('#banner > div[role="navigation"] > ul > li > a').css('margin-top', -11); 
    $('#content-3').css( 'padding-top', 14 );
  }
  
  if ( $.browser.msie ) {
    if ( $('body.fr').length ) {
      $('#content-1').css( 'background-image', "url('../images/interface/bande-contenu-334-ie.png')" );
      $('#content-2').css( 'background-image', "url('../images/interface/bande-contenu-334-ie.png')" );
      $('#content-3').css( 'background-image', "url('../images/interface/bande-contenu-321-ie.png')" );
      $('#content-4').css( 'background-image', "url('../images/interface/bande-contenu-334-ie.png')" );
      $('#content-5').css( 'background-image', "url('../images/interface/bande-contenu-297-ie.png')" );
    } else {
      $('#content-1').css( 'background-image', "url('./images/interface/bande-contenu-334-ie.png')" );
      $('#content-2').css( 'background-image', "url('./images/interface/bande-contenu-334-ie.png')" );
      $('#content-3').css( 'background-image', "url('./images/interface/bande-contenu-321-ie.png')" );
      $('#content-4').css( 'background-image', "url('./images/interface/bande-contenu-334-ie.png')" );
      $('#content-5').css( 'background-image', "url('./images/interface/bande-contenu-297-ie.png')" );
    }
  }
  
  if ( $.browser.mobile || IPAD ) {
    $('body').css( 'background-color', '#2b2b2b' );
    $('#overlay, #bottom-mask').hide();
    $('#header').css({
      'height': 130,
      'margin-bottom': 139,
      'background-color': '#fff'
    });
  } else {
    $('.back-to-top').hide();
    $('body').css( 'background-image', "url('/site/images/interface/body.jpg')" );
  }

/* }}} */

/* basic {{{
******************************************************************************/

  var $window   = $(window),
    $document = $(document);
  
  $('a').keyup(function(e) {
    if ( e.which === 9 ) {
      $(this).addClass('focus');
    }
  }).blur(function() {
    $(this).removeClass('focus');
  });
  
  var dummyImage = new Image();
  dummyImage.src = '/site/images/bloc-1-lazy-raiders-over.png';
  dummyImage.src = '/site/images/fr-hover.png';
  dummyImage.src = '/site/images/en-hover.png';

/* }}} */

/* Carousel {{{
******************************************************************************/

  if ( !($.browser.msie && $.browser.version < 9) ) {
    
    $('span.pulse').css( 'opacity', 0 );
    // PULSE IS THE NEW BLINK
    function pulse() {
      $('span.pulse').animate({ 'opacity': 1 }, 1000, function() {
        $(this).animate({ 'opacity': 0 }, 1000);
      });
    }
    window.setInterval( pulse, 2200 );
    
  } else {
    $('span.pulse').remove();
  }

  window.Carousel = function( args ) { this.initialize( args ); };

  Carousel.prototype = {
    
    initialize: function( args ) {
      // functionLog( 'Carousel.initialize' );
      var this_ = this;
      
      this_.boutonsActifs = true;
      this_.carouselType = args.carouselType;
      
      this_.$ul = $( args.ulId );
      this_.$divContent = this_.$ul.parents('div.content').eq(0);
      this_.$boutonPrev = this_.$divContent.find('a.previous-carousel-item').eq(0);
      this_.$boutonNext = this_.$divContent.find('a.next-carousel-item').eq(0);
      
      this_.$boutonPrev.click(function() {
        this_.slidePrev();
        return false;
      });
      this_.$boutonNext.click(function() {
        this_.slideNext();
        return false;
      });
      
      var $liGroup = this_.$ul.find('li'),
        liGroupLength = $liGroup.length,
        $liGroupToAppend = this_.$ul.find('li:lt(4)'),
        $liGroupToPrepend = (liGroupLength == 5) ? this_.$ul.find('li').add(this_.$ul.find('li').eq(0)) : this_.$ul.find('li:gt('+ (liGroupLength-6) +')');
      
      this_.ITEM_COUNT = $liGroup.length;
      
      this_.INITIAL_ITEM = 0;
      this_.currentItem = this_.INITIAL_ITEM;
      this_.previousCurrentItem = 0;
      
      this_.liMarginLeft = parseInt( $liGroup.css('margin-left'), 10 );
      this_.liWidth = $liGroup.width();
      this_.liWidthWithMargin = this_.liWidth + (this_.liMarginLeft * 2);
      
      var ulWidth = this_.liWidthWithMargin * (liGroupLength + 10);
      this_.INITIAL_MARGIN_LEFT = -(ulWidth/3) - args.initialOffset;
      
      
      this_.$ul
        .prepend( $liGroupToPrepend.clone() )
        .append( $liGroupToAppend.clone() )
        .css( 'width', ulWidth )
        .css( 'margin-left', this_.INITIAL_MARGIN_LEFT );
      
      this_.$li = this_.$ul.find('li');
      
      this_.groups = args.groups;
      if ( this_.groups ) {
        this_.currentGroup = this_.groups[ this_.INITIAL_ITEM ];
        $('#game-descriptions > .game-description').eq(this_.currentGroup-1).siblings().hide();
      }
      
      if ( this_.carouselType == 'images' ) {
        
        this_.$li.each(function() {
          var $this = $(this),
            $img  = $this.find('img'),
            $newImg = $img.clone().attr( 'src', $img.attr('src').replace('grey','color') );

          $this.prepend( $newImg );
        });
        
        this_.$li.eq( this_.currentItem+5 ).find('img[src*="grey"]').css({ 'opacity': 0 });
        
      } else {
        
        this_.$li.css({ 'opacity': 0.3 });
        
        this_.$li.eq( this_.currentItem+5 ).css({ 'opacity': 1 });
      }
    },
    
    slideNext: function() {
      // functionLog( 'Carousel.slideNext' );
      var this_ = this;
      
      if ( this_.boutonsActifs && this_.$divContent.is(':visible') ) {
        
        if ( !($.browser.mobile || IPAD) ) {
          window.arrowKeys.showArrow('right');
        }
        
        this_.boutonsActifs = false;
        this_.$boutonNext.addClass('active');
        var timer1 = setTimeout(function() {
          this_.$boutonNext.removeClass('active');
        }, 100);
        this_.previousCurrentItem = -1;
        this_.currentItem++;
        if ( this_.currentItem > this_.ITEM_COUNT-1 ) {
          this_.currentItem = 0;
        }
        this_.slideNextOrPrev();
      }
    },
    
    slidePrev: function() {
      // functionLog( 'Carousel.slidePrev' );
      var this_ = this;
      
      if ( this_.boutonsActifs && this_.$divContent.is(':visible') ) {
        
        if ( !($.browser.mobile || IPAD) ) {
          window.arrowKeys.showArrow('left');
        }
        
        this_.boutonsActifs = false;
        this_.$boutonPrev.addClass('active');
        var timer1 = setTimeout(function() {
          this_.$boutonPrev.removeClass('active');
        }, 100);
        this_.previousCurrentItem = 1;
        this_.currentItem--;
        if ( this_.currentItem < 0 ) {
          this_.currentItem = this_.ITEM_COUNT-1;
        }
        this_.slideNextOrPrev();
      }
    },
    
    slideNextOrPrev: function() {
      // functionLog( 'Carousel.slideNextOrPrev' );
      var this_ = this;
      var timer = setTimeout(function() {
        this_.boutonsActifs = true;
      }, 400);
      
      if ( this_.groups && this_.currentGroup != this_.groups[ this_.currentItem ] ) {
        $('#game-descriptions > .game-description').eq(this_.currentGroup-1).fadeOut(function() {
          this_.currentGroup = this_.groups[ this_.currentItem ];
          $('#game-descriptions > .game-description').eq(this_.currentGroup-1).fadeIn();
        });
      }
      
      this_.$ul.clearQueue().animate({ 'margin-left': this.INITIAL_MARGIN_LEFT - (this_.currentItem * this_.liWidthWithMargin) }, 400, function() {
        
        if ( this_.carouselType == 'images' ) {
          
          this_.$li.eq( this_.currentItem+5 ).find('img[src*="grey"]').animate({ 'opacity': 0 });
          
          if ( this_.currentItem+5+this_.previousCurrentItem >= 5 + this_.ITEM_COUNT ) {
            this_.$li.eq( this_.currentItem+5-this_.ITEM_COUNT+this_.previousCurrentItem ).find('img[src*="grey"]').animate({ 'opacity': 1 });
          } else if ( this_.currentItem+5+this_.previousCurrentItem <= 4 ) {
            this_.$li.eq( this_.currentItem+5+this_.ITEM_COUNT+this_.previousCurrentItem ).find('img[src*="grey"]').animate({ 'opacity': 1 });
          } else {
            this_.$li.eq( this_.currentItem+5+this_.previousCurrentItem ).find('img[src*="grey"]').animate({ 'opacity': 1 });
          }
          
        } else {
          
          this_.$li.eq( this_.currentItem+5 ).animate({ 'opacity': 1 });
          
          if ( this_.currentItem+5+this_.previousCurrentItem >= 5 + this_.ITEM_COUNT ) {
            this_.$li.eq( this_.currentItem+5-this_.ITEM_COUNT+this_.previousCurrentItem ).animate({ 'opacity': 0.3 });
          } else if ( this_.currentItem+5+this_.previousCurrentItem <= 4 ) {
            this_.$li.eq( this_.currentItem+5+this_.ITEM_COUNT+this_.previousCurrentItem ).animate({ 'opacity': 0.3 });
          } else {
            this_.$li.eq( this_.currentItem+5+this_.previousCurrentItem ).animate({ 'opacity': 0.3 });
          }
        }
          
        if ( this_.currentItem == 0 ) {
          this_.$ul.css( 'margin-left', this_.IITIAL_MARGIN_LEFT);
          this_.currentItem = this_.INITIAL_ITEM;
        }
      });
    }
    
  };
  
  if (window.location.pathname.match(/\/fr\/?$/))
    var carousel1 = new Carousel({
      ulId: '#carousel-1',
      initialItem: 0,
      carouselType: 'images',
      initialOffset: -730,
      groups: [
        1, 1, 1, 1,
        2, 2, 2,
        3, 3, 3,
        4, 4, 4,
        5, 5
      ]
    });
    
  else
    var carousel1 = new Carousel({
      ulId: '#carousel-1',
      initialItem: 0,
      carouselType: 'images',
      initialOffset: -1630,
      groups: [
        1, 1, 1, 1, 1,
        2, 2, 2,
        3, 3, 3, 3,
        4, 4, 4,
        5, 5, 5,
        6, 6, 6,
        7, 7
      ]
    });
  
  var carousel2 = new Carousel({
    ulId: '#carousel-2',
    initialItem: 0,
    carouselType: 'text',
    initialOffset: 240
  });

/* }}} */

/* anchors navigation {{{
******************************************************************************/

  $('a[href="#"], a[href="#Games-People-Play"], a[href="#Bite-Size-AAA-Games"], a[href="#The-Crew-Behind-The-Creative"], a[href="#Whats-Going-On"], a[href="#Get-In-Touch"]').click(function() {
    
    if ( !CHANGING_SECTION ) {
      
      var hrefId = $(this).attr('href'),
        newScrollTop;

      if ( !($.browser.mobile || IPAD) ) {

        if ( parseInt($(this).attr('data-numero-bande'),10) > window.currentSection ) {
          window.arrowKeys.showArrow('down');
        } else if ( parseInt($(this).attr('data-numero-bande'),10) < window.currentSection ) {
          window.arrowKeys.showArrow('up');
        }
      }

      if ( $.browser.mobile || IPAD ) {

        if ( hrefId != '#' && hrefId != '#Games-People-Play' ) {
          newScrollTop = $(hrefId).offset().top + 250;
        } else {
          newScrollTop = 0;
        }

      } else {

        if ( hrefId != '#' ) {
          newScrollTop = $(hrefId).offset().top;
        } else {
          newScrollTop = 0;
        }
      }

      scrollTo( newScrollTop, hrefId, $(this).attr('data-lightbox-id') );
    }
    
    return false;
  });
  
  function changeLocationHash( newLocationHash ) {
    window.location.hash = newLocationHash.replace( /^(#)?(.*)$/, '#$2' ) || '';
  }
  
  function scrollTo( scrollTop, newLocationHash, lightboxId ) {
    // functionLog('scrollTop');
    var currentScrollTop = $window.scrollTop();
    if ( scrollTop != currentScrollTop ) {
      
      CHANGING_SECTION = true;
      
      var direction = scrollTop - currentScrollTop > 0 ? 'down' : 'up';
      
      if ( (direction == 'up' && window.currentSection == window.previousSection) || (direction == 'down' && window.currentSection == window.nextSection) ) {
        
      } else {
        fadeOutAllBoxes();
      }
      
      var $elementToScroll = $.browser.webkit ? $('body') : $('html');

      $elementToScroll.clearQueue().animate({ 'scrollTop': scrollTop }, 'slow', function() {
        if ( newLocationHash && !($.browser.mobile || IPAD) ) {
          changeLocationHash( newLocationHash );
        }
        setTimeout(function() {
          CHANGING_SECTION = false;
        }, 400);
        
        if (lightboxId) {
          setTimeout(function() {
            $('a[href="' + lightboxId + '"]').trigger('click');
          }, 800);
        }
      });
      
    }
  }

/* }}} */

/* section 2 {{{
******************************************************************************/

  var $onTheList      = $('ul#on-the-list'),
    $onTheCouchLink = $('a[href="#On-the-Couch"]'),
    $onTheGoLink    = $('a[href="#On-the-Go"]'),
    $onTheNetLink   = $('a[href="#On-the-Net"]'),
    $initialOnTheP  = $('#initial-on-the'),
    $onTheCouchP    = $('#On-the-Couch'),
    $onTheGoP       = $('#On-the-Go'),
    $onTheNetP      = $('#On-the-Net');
  
  $onTheCouchLink.click(function() {
    return false;
  });
  $onTheGoLink.click(function() {
    return false;
  });
  $onTheNetLink.click(function() {
    return false;
  });

  $onTheCouchP.css( 'opacity', 0 );
  $onTheGoP.css( 'opacity', 0 );
  $onTheNetP.css( 'opacity', 0 );

  $onTheCouchLink.mouseenter(function() {
    $onTheCouchP.clearQueue().animate({ 'opacity': 1 }).siblings('p').clearQueue().animate({ 'opacity': 0 });
  });
  $onTheGoLink.mouseenter(function() {
    $onTheGoP.clearQueue().animate({ 'opacity': 1 }).siblings('p').clearQueue().animate({ 'opacity': 0 });
  });
  $onTheNetLink.mouseenter(function() {
    $onTheNetP.clearQueue().animate({ 'opacity': 1 }).siblings('p').clearQueue().animate({ 'opacity': 0 });
  });
  
  $onTheList.mouseleave(function() {
    $initialOnTheP.clearQueue().animate({ 'opacity': 1 }).siblings('p').clearQueue().animate({ 'opacity': 0 });
  });

/* }}} */

/* section 4 {{{ */

  $('a.full-news-link, a.full-news-link-title').fancybox({
    overlayColor: '#1a1a1a',
    autoDimensions: false,
    centerOnScroll: true,
    width: 450,
    height: 260,
    padding: 0
  });

/* }}} */

if ( $.browser.mobile || IPAD ) {

  $('#arrows').hide();

} else {

/* onscroll {{{
******************************************************************************/

  var scrollDelay,
    nombreDeScrollsAvantDeCacherLesBoites = 0,
    timerResetNombreDeScrollsAvantDeCacherLesBoites,
    $divContent = $('div.content'),
    $divsHeadingBackground = $('div.heading-background');

  $divContent.css({
    'margin-top': 0,
    'position': 'fixed',
    'bottom': '35%',
    'visibility': 'hidden'
  });

  function scrollAndResizeCallback() {
    // functionLog( 'scrollAndResizeCallback' );

    nombreDeScrollsAvantDeCacherLesBoites++;
    
    clearTimeout( timerResetNombreDeScrollsAvantDeCacherLesBoites );
    timerResetNombreDeScrollsAvantDeCacherLesBoites = setTimeout(function() {
      nombreDeScrollsAvantDeCacherLesBoites = 0;
    }, 250);
    
    clearTimeout( scrollDelay );
    scrollDelay = setTimeout( refreshStates, 100 );
  }
  
  window.currentWindowScrollTop = 0;
  window.newWindowScrollTop = 0;
  // var checkScrollSpeedInterval = setInterval(function() {
  //   // functionLog('checkScrollSpeedInterval');
  //   
  //   window.newWindowScrollTop = $window.scrollTop();
  //   
  //   // if ( Math.abs(window.currentWindowScrollTop - window.newWindowScrollTop) > 100 ) {
  //   //  fadeOutAllBoxes();
  //   // }
  //   window.currentWindowScrollTop = window.newWindowScrollTop;
  // 
  // }, 100);
  
  window.previousSection = 0;
  window.currentSection = 0;
  window.nextSection = 0;
  
  function changePreviousAndNextSections() {
    // functionLog('changePreviousAndNextSections')
    
    var windowScrollTop = $window.scrollTop();
    
    // changer la section courante en fonction du scroll actuel
    if ( windowScrollTop < 90 ) {            //-----------------//     0
                                           //                 //
      previousSection = 1;                 //    Section 1    //
      currentSection  = 1;                 //                 //
      nextSection     = 2;                 //-----------------//    90
                                           
    } else if ( windowScrollTop  < 792 ) {   //-----------------//   792
                                           //                 //
      previousSection = 1;                 //                 //
      currentSection  = 0;                 //                 //
      nextSection     = 2;                 //                 //
                                           //                 //
    } else if ( windowScrollTop  < 871 ) {   //                 //
                                           //                 //
      previousSection = 1;                 //                 //
      currentSection  = 2;                 //    Section 2    //   871
      nextSection     = 2;                 //                 //
                                           //                 //
    } else if ( windowScrollTop == 871 ) {   //                 //
                                           //                 //
      previousSection = 1;                 //                 //
      currentSection  = 2;                 //                 //
      nextSection     = 3;                 //                 //
                                           //                 //
    } else if ( windowScrollTop  < 956 ) {   //-----------------//   956
                                           
      previousSection = 2;                 
      currentSection  = 2;                 
      nextSection     = 3;                 
                                           
    } else if ( windowScrollTop  < 1620 ) {  //-----------------//  1620
                                           //                 //
      previousSection = 2;                 //                 //
      currentSection  = 0;                 //                 //
      nextSection     = 3;                 //                 //
                                           //                 //
    } else if ( windowScrollTop  < 1703 ) {  //                 //
                                           //                 //
      previousSection = 2;                 //                 //
      currentSection  = 3;                 //    Section 3    //  1703
      nextSection     = 3;                 //                 //
                                           //                 //
    } else if ( windowScrollTop == 1703 ) {  //                 //
                                           //                 //
      previousSection = 2;                 //                 //
      currentSection  = 3;                 //                 //
      nextSection     = 4;                 //                 //
                                           //                 //
    } else if ( windowScrollTop  < 1790 ) {  //-----------------//  1790
      
      previousSection = 3;
      currentSection  = 3;
      nextSection     = 4;
      
    } else if ( windowScrollTop  < 2440 ) {  //-----------------//  2440
                                           //                 //
      previousSection = 3;                 //                 //
      currentSection  = 0;                 //                 //
      nextSection     = 4;                 //                 //
                                           //                 //
    } else if ( windowScrollTop  < 2524 ) {  //                 //
                                           //                 //
      previousSection = 3;                 //                 //
      currentSection  = 4;                 //    Section 4    //  2524
      nextSection     = 4;                 //                 //
                                           //                 //
    } else if ( windowScrollTop == 2524 ) {  //                 //
                                           //                 //
      previousSection = 3;                 //                 //
      currentSection  = 4;                 //                 //
      nextSection     = 5;                 //                 //
                                           //                 //
    } else if ( windowScrollTop  < 2610 ) {  //-----------------//  2610
      
      previousSection = 4;
      currentSection  = 4;
      nextSection     = 5;
      
    } else if ( windowScrollTop  < 3310 ) {  //-----------------//  3310
                                           //                 //
      previousSection = 4;                 //                 //
      currentSection  = 0;                 //                 //
      nextSection     = 5;                 //                 //
                                           //    Section 5    //
    } else {                                 //                 //
                                           //                 //
      previousSection = 4;                 //                 //
      currentSection  = 5;                 //                 //
      nextSection     = 5;                 //-----------------//
    }
    
    if ( !($divContent.eq(1).is(':visible') || $divContent.eq(3).is(':visible')) ) {
      window.arrowKeys.$arrows4.clearQueue().animate({ 'opacity': 0 });
    }
  }
  
  var changePreviousAndNextSectionsInterval = setInterval(function() {
    
    if ( !changePreviousAndNextSectionsDelay ) {
      changePreviousAndNextSections();
    }
    
  }, 300);
  

  function fadeOutAllBoxes() {
    // // functionLog( 'fadeOutAllBoxes' );
    $divContent.filter(':visible').fadeOutContentBlock();
  }

  function refreshStates() {
    // functionLog( 'refreshStates' );

    $divsHeadingBackground.each(function() {
      var $this = $(this);

      var $contentDiv = $('#content-'+ $this.attr('data-order'));

      var elementPercentageTopOffset = $this.percentageWindowOffset();

      if ( elementPercentageTopOffset >= 7 && elementPercentageTopOffset <= 33 ) {
        
        $contentDiv.fadeInContentBlock();
        
      } else if ( $contentDiv.is(':visible') ) {
        
        $contentDiv.fadeOutContentBlock();
      }
    });
  }

  $divContent.css('visibility','hidden').hide(function() {
    $divContent.css('visibility','visible');
    refreshStates();
  });

  $window.scroll( scrollAndResizeCallback );

/* }}} */

/* arrow and number keys {{{
******************************************************************************/

  var changePreviousAndNextSectionsDelay,
    CHANGING_SECTION = false;
  $document.keydown(function(e) {
    switch ( e.which ) {
      
      case 49 : // 1
        if ( !CHANGING_SECTION ) {
          if ( !(window.currentSection == 1 && window.previousSection == 1) ) {
            window.arrowKeys.showArrow( 'up' );
          }
          scrollTo( headingScrollTops[0] );
        }
        return false;
      case 50 : // 2
        if ( !CHANGING_SECTION ) {
          scrollTo( headingScrollTops[1] );
        }
        if ( 2 > window.currentSection ) {
          window.arrowKeys.showArrow('down');
        } else if ( 2 < window.currentSection ) {
          window.arrowKeys.showArrow('up');
        }
        return false;
      case 51 : // 3
        if ( !CHANGING_SECTION ) {
          scrollTo( headingScrollTops[2] );
        }
        if ( 3 > window.currentSection ) {
          window.arrowKeys.showArrow('down');
        } else if ( 3 < window.currentSection ) {
          window.arrowKeys.showArrow('up');
        }
        return false;
      case 52 : // 4
        if ( !CHANGING_SECTION ) {
          scrollTo( headingScrollTops[3] );
        }
        if ( 4 > window.currentSection ) {
          window.arrowKeys.showArrow('down');
        } else if ( 4 < window.currentSection ) {
          window.arrowKeys.showArrow('up');
        }
        return false;
      case 53 : // 5
        if ( !CHANGING_SECTION ) {
          if ( !(window.currentSection == 5 && window.nextSection == 5) ) {
            window.arrowKeys.showArrow( 'down' );
          }
          scrollTo( headingScrollTops[4] );
        }
        return false;
        
      case 40 : // DOWN arrow
        
        if ( !CHANGING_SECTION ) {
          
          if ( !(window.currentSection == 5 && window.nextSection == 5) ) {
            window.arrowKeys.showArrow( 'down' );
          }
          scrollTo( headingScrollTops[nextSection-1] );

          if ( !(previousSection == 1 && nextSection == 2) ) {
            previousSection++;
          }
          if ( previousSection > 5 ) {
            previousSection = 5;
          }

          nextSection++;
          if ( nextSection > 5 ) {
            nextSection = 5;
          }
        }
        
        return false;
      
      case 38 : // UP arrow
        
        if ( !CHANGING_SECTION ) {
          
          if ( !(window.currentSection == 1 && window.previousSection == 1) ) {
            window.arrowKeys.showArrow( 'up' );
          }
          scrollTo( headingScrollTops[previousSection-1] );

          if ( !(nextSection == 5 && previousSection == 4) ) {
            nextSection--;
          }
          if ( nextSection < 1 ) {
            nextSection = 1;
          }

          previousSection--;
          if ( previousSection < 1 ) {
            previousSection = 1;
          }
        }
        
        return false;
      
      case 39 : // RIGHT arrow
        carousel1.slideNext();
        carousel2.slideNext();
        return false;
      
      case 37 : // LEFT arrow
        carousel1.slidePrev();
        carousel2.slidePrev();
        return false;
    }
    return true;
    
  });
  window.headingScrollTops = [
    0, 871, 1703, 2524, 3390
  ];

/* }}} */

/* arrow keys graphic thingy in bottom right corner {{{
******************************************************************************/
  
  function ArrowKeys() { this.initialize(); }
  ArrowKeys.prototype = {
    
    initialize: function() {
      var this_ = this;

      this_.$arrows2 = $('#arrows-2');
      this_.$arrows4 = $('#arrows-4');
      
      this_.arrows = {
        'up':    $('#arrow-up'),
        'right': $('#arrow-right'),
        'down':  $('#arrow-down'),
        'left':  $('#arrow-left')
      };
      this_.arrows['up'].css( 'opacity', 0 );
      this_.arrows['right'].css( 'opacity', 0 );
      this_.arrows['down'].css( 'opacity', 0 );
      this_.arrows['left'].css( 'opacity', 0 );
    },
    
    showArrow: function( arrowName ) {
      var this_ = this;
      
      this_.arrows[ arrowName ].css( 'opacity', 1 ).clearQueue().animate({ 'opacity': 0 });
    }
    
  };
  window.arrowKeys = new ArrowKeys;

/* }}} */

/* onresize {{{
******************************************************************************/

  function repositionnerBandesContenu() {
    
    var windowHeight = $window.height(),
      pourcentageEquivalentAuOffsetDuTitre = 150 * 100 / windowHeight,
      topOffsetDeLaBandeDeContenu;
    
    if ( pourcentageEquivalentAuOffsetDuTitre <= 11 ) {
      topOffsetDeLaBandeDeContenu = 45;
    } else if ( pourcentageEquivalentAuOffsetDuTitre >= 20.5 ) {
      topOffsetDeLaBandeDeContenu = 2;
    } else {
      topOffsetDeLaBandeDeContenu = 2 + ((20.5 - pourcentageEquivalentAuOffsetDuTitre) * 4);
    }
    
    if ( IE8 || ($.browser.mozilla && parseInt(GECKO_VERSION,10) < 191 && GECKO_VERSION.charAt(0) != '2') ) {
      $('#Get-In-Touch').css( 'padding-bottom', windowHeight - (150 + 143) );
    } else if ( $.browser.webkit && parseInt($.browser.version,10) >= 534 ) {
      $('#Get-In-Touch').css( 'padding-bottom', windowHeight - (150 + 143) );
    } else {
      $('#Get-In-Touch').css( 'margin-bottom', windowHeight - (150 + 143) );
    }
    
    $divContent.css( 'bottom', topOffsetDeLaBandeDeContenu.toString().substr(0,5) + '%' );
  }
  repositionnerBandesContenu();
  
  $window.resize( scrollAndResizeCallback ).resize( repositionnerBandesContenu );

/* }}} */

}

});
})(window.jQuery);
