// Initials:
var currentYear = 1;
var yearInterval = null;


function nextYear() {
	
	currentYear++;
	
	if(currentYear > ($("div#timeline .toggle_item").length)) { 
		currentYear = 1; }
		
	toggleYears(); }
		
		
function prevYear() {
	
	currentYear--;
	
	if(currentYear < 1) { 
		currentYear = $("div#timeline .toggle_item").length; }
		
	toggleYears(); }


// Toggle through items
function toggleYears() {
	
	$("div#timeline .selected").removeClass("selected");
	$("div#timeline .toggle .toggle_item").eq(currentYear - 1).addClass("selected");
	$("div#timeline table th").eq(currentYear - 1).addClass("selected");
	
	var newLocation = (currentYear - 1) / $("div#timeline .toggle_item").length * 750;
	
	$("li#current_year").animate({"left": 30+newLocation+"px"}, "fast", function(){
	 $("li#current_year").text($("table#timeline_data th.selected").text()); });
	
	if(currentYear < $("div#timeline .toggle_item").length / 2) {
  var posOffset = 1; }
	else {
		var posOffset = 0; }	
	
	var endPos = $("div#timeline table#timeline_data").width() - $("div#timeline div.clipping_region").width();
	var newLocation = (currentYear - posOffset) / $("div#timeline .toggle_item").length * Math.abs(endPos);
	
	$("div#timeline table#timeline_data").animate({"left": -newLocation+"px"}, "slow"); }
		



// Listener functions:
$(document).ready(function() {
		
	// Cycle through the timeline years:
	yearInterval = setInterval("nextYear()", 4000);
	
	// Stop the timeline scroll on mouseover:
	$("table#timeline_data td.toggle_item").hover(function(){
 
		clearInterval(yearInterval);
		$(this).addClass("hover");
	
	// Restart timeline scroll on mouesout:
 }, function() {
		
		$(this).removeClass("hover");
		yearInterval = setInterval("nextYear()", 4000); });  
	
	
	// Stop the timeline scroll on control mouseover:
	$("div#location li img").hover(function(){
 
		clearInterval(yearInterval);
	
	// Restart timeline scroll on mouesout:
 }, function() {
		
		yearInterval = setInterval("nextYear()", 4000); });  
	
	
	// Reduce the year if the prev year img is clicked:
	$("li#prev_year").click(function(){
		
		prevYear(); });
	
	$("li#next_year").click(function(){
		
		nextYear(); });
	
});
