/* timeline carrousel */

function start_carrousel (margin, step) {
var $t = $('#scroll-decades');
var length = 0;
  $t.children().each(function(){
    length += $(this).outerWidth();
  });
var unit = $t.parent().width();
var current = - parseInt ($t.css ('marginLeft'));

  length -= unit; // don't scroll into blank space
  length -= ( 51 + 12 ); // no need for a margin at the far end

  if (step) {
    current = current + ( margin ? unit : -unit );

    $t.stop().animate ({
      marginLeft: ( current <= 0 ) ? 0 : ( '-' + ( current >= length ? length : current ) + 'px' )
    }, {
      duration: 'slow'
    });
  }

  // if we've reached an edge, no need for auto-scrolling
  if ( ( margin == 0 && current <= 0 ) || ( margin == 1 && current >= length ) ) {
    return;
  }

  var speed = 10; /* rate of ms per px */;
  var delta = margin ? ( length - current ) : current;
  $t.animate ({
    marginLeft: margin ? '-' + length + 'px' : 0
  }, {
    duration: delta * speed,
    easing: 'linear'
  });
}

function move_carrousel_right() {
  start_carrousel (0, 0);
}
function move_carrousel_left() {
  start_carrousel (1, 0);
}

function stop_carrousel() {
  $('#scroll-decades').stop();
}

function step_carrousel_right() {
  start_carrousel (0, 1);
}
function step_carrousel_left() {
  start_carrousel (1, 1);
}

function show_caption(){
  $(this).children ('div.caption').show();
}
function hide_caption(){
  $(this).children ('div.caption').hide();
}

$(document).ready(function(){
  $('.carrousel-back').hover(
    move_carrousel_right,
    stop_carrousel
  ).click (step_carrousel_right);

  $('.carrousel-forward').hover(
    move_carrousel_left,
    stop_carrousel
  ).click (step_carrousel_left);

  $('div.illustration').hover(
    show_caption,
    hide_caption
  );
});

