$(document).ready(function(){

	var current				 = '';
	var numberofslides = 1;
	var currentslide	 = 0;
	
	//$.post("/company/loadtimelineitems/",{year:'2001'}, setYear);
	$.ajax({
	 type: "POST",
	 url: "/company/loadtimelineitems/",
	 data: ({year:'2001'}),
	 success: setYear
	});
	
	function setYear(returnedString) {
		$("#timeline_preload").html(returnedString);
		
		// lets play with the JSON
		$('#months span').removeClass("selected");
		$('#months span').removeClass("available");
		currentslide = 0;
		
		months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
		first	 = '';
		
		for(i=0;i<=12;i++) {
			if(isdefined(json_timeline,months[i])) {
				$('#'+months[i]).addClass("available");
				if(!first) { first = months[i]; }
			}
		}
		
		// set the first item in the array to the selected one and let js know how many slides there are
		numberofslides = json_timeline[first].length;
		if(numberofslides > 1) {
			$('#nextarrow').show();
			$('#prevarrow').hide();
		} else {
			$('#prevarrow').hide();
			$('#nextarrow').hide();
		}
		setTimelineItem(json_timeline[first][0]);
	}
	
	function isdefined(object, variable)
	{
		return (typeof(eval(object)[variable]) != 'undefined');
	}
	
	$("#months span").click(function () {
		month = this.id;
		currentslide = 0;
	
		if(json_timeline[month] != undefined) {
			// find out how many items there are
			numberofslides = json_timeline[month].length;
			if(numberofslides > 1) {
				$('#nextarrow').show();
				$('#prevarrow').hide();
			} else {
				$('#prevarrow').hide();
				$('#nextarrow').hide();
			}
			
			setTimelineItem( json_timeline[month][0] );
		}
  });	
	
	function setTimelineItem(item) {
		$('#year').html(item.year);
		$('#timeline_text').html(item.description);
		$('#image img').attr('src','/images/timeline/'+item.picture);
		
		if(current != '') {
			$('#'+current).removeClass("selected");
			$('#'+current).addClass("available");
		}
		$('#'+item.month).removeClass("available");
		$('#'+item.month).addClass("selected");
		
		current = item.month;
	}
	
	$("#nextarrow").click(function () {
		nextslide		 = currentslide+1;
		currentslide = nextslide;
		
		setTimelineItem(json_timeline[current][nextslide]);
		$('#prevarrow').show();
		$('#nextarrow').hide();

  });	
	
	$("#prevarrow").click(function () {
		nextslide = currentslide-1;
		currentslide = nextslide;
		
		setTimelineItem(json_timeline[current][nextslide]);
		$('#prevarrow').hide();
		$('#nextarrow').show();

  });	
  
	$("#timeline_back img").click(function () {
		current = '';
		newyear = Number($('#year').html())-1;
		
		if(newyear >= 2001) {
			$.ajax({
			 type: "POST",
			 url: "/company/loadtimelineitems/",
			 data: ({year:newyear}),
			 success: setYear
			});
			
			$('#timeline_forward img').attr('src','/images/timeline/forward.jpg');
			$('#timeline_forward img').css('cursor','pointer');
		}
		
		if(newyear == 2001) {
			$('#timeline_back img').attr('src','/images/timeline/inactive_back.jpg');
			$('#timeline_back img').css('cursor','default');
		}
  });	
  
	$("#timeline_forward img").click(function () {
		current = '';
		newyear = Number($('#year').html())+1;
		
		if(newyear <= 2009) {
			$.ajax({
			 type: "POST",
			 url: "/company/loadtimelineitems/",
			 data: ({year:newyear}),
			 success: setYear
			});
			
			$('#timeline_back img').attr('src','/images/timeline/back.jpg');
			$('#timeline_back img').css('cursor','pointer');
		}
		
		if(newyear == 2009) {
			$('#timeline_forward img').attr('src','/images/timeline/inactive_forward.jpg');
			$('#timeline_forward img').css('cursor','default');
		}
  });	


});