var timeline_jumpSpeed=1000;
var timeline_scrollSpeed=10000;
var timeline_timer;
var timeline_totalWidth=20000;


$(window).load(function() {
  timeline_totalWidth = 0;
  $('#scrollerContents div').each( function() { timeline_totalWidth+=$(this).width(); } );
  timeline_totalWidth -= $('#scrollerContainer').width();
});

$(function() {
  $('#timeline ul li a').click( function(e) {
    e.preventDefault();
	$('#scrollerContents').stop();
    $('#scrollerContents').animate( { marginLeft: "-=" + ($( $(this).attr("href") ).offset().left - $("#timeline").offset().left) }, timeline_jumpSpeed );
  });

  var updateSelectedYear = function() {
    $('#scrollerContents div').each( function() {
      var tmpOffset = $(this).offset().left - $("#timeline").offset().left;
      if ( tmpOffset >= -($(this).width() * 0.7) && tmpOffset <= ($(this).width() * 0.7) ) {
        $('#timeline ul li.selected').removeClass('selected');
        $('#timeline ul li a[href=#' + $(this).attr('id') + ']').parent().addClass('selected');
        return false;
      }
    } );
  }
  window.setInterval( updateSelectedYear, 500 );

  var scroller = function( increment ) { 
    var currentPos = $('#scrollerContents').css("margin-left").replace('px','');
	currentPos = currentPos == "auto" ? 0 : parseInt(currentPos);
    if (  ( currentPos > 0 && increment>0 ) ||  ( (currentPos+timeline_totalWidth) < 0  && increment < 0 ) ) { 
      window.clearInterval(timeline_timer); return; 
    }

    $('#scrollerContents').css("margin-left", currentPos+increment ); 

  } 

  $('#timelineLeft').hover( function() {
    var currentPos = $('#scrollerContents').css("margin-left").replace('px','');
	currentPos = currentPos == "auto" ? 0 : parseInt(currentPos);
    $('#scrollerContents').stop();
    $('#scrollerContents').animate({ marginLeft: 0 }, (((timeline_totalWidth - (timeline_totalWidth + currentPos)) / timeline_totalWidth) * timeline_scrollSpeed) ); 

  }, function() {
    $('#scrollerContents').stop();
  });

  $('#timelineRight').hover( function() {
    var currentPos = $('#scrollerContents').css("margin-left").replace('px','');
	currentPos = currentPos == "auto" ? 0 : parseInt(currentPos);
    $('#scrollerContents').stop();
    $('#scrollerContents').animate({ marginLeft: "-" + timeline_totalWidth + "px" }, ( (timeline_totalWidth + currentPos) / timeline_totalWidth ) *  timeline_scrollSpeed); 

  }, function() {
      $('#scrollerContents').stop();
  });
  
  $('a.timeline_thumb_link').fancybox();

});
