$(function() {
  
  // Contenu de la toolbar récuperé en AJAX
  $('#toolbar .ajax-content').one('mouseover', function(e) {
    var $this = $(this),
        id = $this.siblings('ul').attr('id');
    $this.siblings('ul').load(this.href + ' .toolbar-content-' + id, function() {
      id === 'video' && videoLoaded();
    });
  })
  
  // No border-bottom on last li
  $('#toolbar ul li ul li:last-child a').css({ borderBottom: 'none' });
  
  // Enter/Leave behaviors
  jQuery("#toolbar > ul > li:not('.first'):not('.last')").hover(function() {
    $(this).addClass('open');
  }, function () {
    $(this).removeClass('open');
  });
  
  
  var videoLoaded = function() {
    var $video = $('#video video'),
        video = $video[0],
        $controls = $('#controls');
    // Si le navigateur support la balise video
    if (video && 'play' in video) {
      var $window = $(window),
          $globalVideo = $('#global_video'),
          $bgdSound = $('#bgd_sound'),
          $currentTime = $('#current_time'),
          $bufferedTime = $('#buffered_time'),
          $cursor = $('#cursor'),
          leftFullScreen = 0;
      
      // Quand curseur et barre de progression de la vidéo
      video.addEventListener('timeupdate', function(e) {
        $currentTime.width(video.currentTime * 460 / video.duration);
        $cursor.css('left', (video.currentTime * 460 / video.duration - 6));
      }, false);
      
      // Barre de buffering
      video.addEventListener('progress', function(e) {
        // On s'occupe pas du `start()`
        $bufferedTime.width(video.buffered.end(0) * 460 / video.duration);
      }, false);
      
      // Play/pause la video, au click sur la vidéo et sur le bouton
      function playPause() {
        if (video.getAttribute('data-play') === 'true') {
          video.pause();
          video.setAttribute('data-play', 'false');
        } else {
          video.play();
          video.setAttribute('data-play', 'true');
        }
      }
      
      $video.click(playPause);
      // Click sur les bouttons
      $('#controls button').click(function(e) {
        var $this = $(this);
        switch ($this.attr('data-type')) {
          case 'play':
            playPause();
            break;
          case 'sound':
            video.volume = e.offsetX / 34;
            $bgdSound.width(e.offsetX);
            break;
          case 'chapter':
            video.currentTime = $this.attr('data-time');
            break;
          case 'screen':
            // jQuery < 1.4.3, pas $.fn.data()
            if ($globalVideo.attr('data-fullscreen') === 'false') {
              $globalVideo.attr('data-fullscreen', 'true');
              $controls.css('left', ($window.width() - 556) / 2);
            } else {
              $globalVideo.attr('data-fullscreen', 'false')
            }
            break;
        }
      });
      
      // Click sur la timeline
      $('#cursor_timeline').click(function(e) {
        var percent = e.offsetX / $(this).width() * 100;
        video.currentTime = percent * video.duration / 100;
      });
    } else {
      if (!video) $video = $controls.prev();
      $controls.hide();
      $video.hide();
      var playerURL = '/images/atlantic_player.swf',
          flashvars = {
            urlXml: "/images/footer_video.xml"
          },
          params = {
            wmode: "transparent",
            allowScriptAccess: 'always'
          },
          attributes = {};
      $video.attr('id', 'flv-player-toolbar');
      swfobject.embedSWF(playerURL, 'flv-player-toolbar', 556, 342, "9.0.0", "", flashvars, params, attributes);
    }
  }
});
