jQuery(function($){
  var speeds = { 0: -16, 6: -8, 12: -4, 18: -2, 24: -1, 30: 0, 70: 1, 76: 2, 82: 4, 88: 8, 94: 16 }; 
  var container = $('#thumbnails');
  var list = $('#motiongallery');
  var containerWidth = container.width();
  var offset = $('#gallery').offset();
  var prev_speed = 0;

  container.mousemove(function(e){
    var listWidth = list.width();
    
    if(listWidth < containerWidth)
      return;
    
    var rightPos = listWidth - containerWidth;
    var position = e.pageX - offset.left;
    var perc = position/containerWidth * 100;
    var last_speed = 0;

    for(var pos in speeds) {
      if(perc < pos)
        break;
      
      last_speed = speeds[pos];
    }

    if(prev_speed == last_speed)
      return; // do nothing
    
    prev_speed = last_speed;
       
    if(last_speed == 0)
    {
      list.stop(true, false);
      return;
    }
    
    var to;
    
    if(last_speed < 0) {
      to = 0;
    } else {
      to = -rightPos;
    }

    var current = parseInt(list.css('left'));
    var duration = Math.abs(((to - current)*1000) / (60 *last_speed * 5));
    
    list.stop(true, false).animate({left: to + 'px'}, {duration: duration * 10});
  });
});