$(function(){
  $('div#scroller_hor').css({'overflow' : 'hidden', 'position' : 'relative'}).wrapInner('<div style="width: 5000px; margin-left: 5000px;" />');
});
$(window).load(function () {
  scrollerWidth = $('div#scroller_hor').width();
  scrollDiv = $('div#scroller_hor div:first');
  scrollDiv.append('<span>&nbsp;</span>');
  lastItem = $('div#scroller_hor div:first :last-child');
  scrollWidth = lastItem.position().left - 5000;
  scrollDiv.css({'width' : scrollWidth, 'margin-left' : scrollWidth});
  lastItem.remove();
  scrollDuration = Math.max(scrollWidth, scrollerWidth) * 2 * 1000 / scrollerWidth * 5;
  function runScroller (noReset) {
    if (!noReset) {
      scrollDiv.css('margin-left', Math.max(scrollWidth, scrollerWidth));
      timeRemaining = scrollDuration;
    }
    else {
      timeRemaining = ((parseInt(scrollDiv.css('margin-left')) - scrollerWidth) / (scrollWidth * 2) + 1) * scrollDuration;
    }
    scrollDiv.animate({'margin-left' : scrollWidth * -1}, timeRemaining, 'linear', runScroller);
  }
  runScroller();
  $('div#scroller_hor').hover(
    function () {
      scrollDiv.stop();
    },
    function () {
      runScroller(true);
    }
  );
});
