var monthscroller;

function innitEvents()
{
	monthscroller = $('#monthscroller').get();
	monthscroller.arrmonths = new Array();
	monthscroller.currentmonth = 0;
	monthscroller.selectedmonth = 0;
	
	$('#monthscroller li').each(function(i)
	{
		if (i>0) monthscroller.arrmonths[i] = $(this).innerWidth() + monthscroller.arrmonths[i-1] + 1;
		else monthscroller.arrmonths[i] = $(this).innerWidth() + 1;
		
		if ($(this).hasClass('selected'))
		{
			monthscroller.selectedmonth = i;
		}
	});

	//alert(monthscroller.arrmonths[0]);
	
	$('<a href="#" id="movescrollerright">Move right</a>').prependTo('#months').click(function()
	{
		//alert(monthscroller.arrmonths[monthscroller.currentmonth]);
		$('#monthscroller').animate( { left : -(monthscroller.arrmonths[monthscroller.currentmonth]) } );
		monthscroller.currentmonth++;
		$('#movescrollerleft').show();
		if (monthscroller.arrmonths[monthscroller.arrmonths.length -1] - monthscroller.arrmonths[monthscroller.currentmonth] + 10 < 507) $('#movescrollerright').hide();
		return false;
	});

	$('<a href="#" id="movescrollerleft">Move left</a>').prependTo('#months').hide().click(function()
	{
		//alert(monthscroller.arrmonths[monthscroller.currentmonth]);
		if (monthscroller.currentmonth == 1)
		{
			$('#monthscroller').animate( { left : 0 } );
			$(this).hide();
		}
		else
		{
			$('#monthscroller').animate( { left : -(monthscroller.arrmonths[monthscroller.currentmonth - 2]) } );
		}
		monthscroller.currentmonth--;
		if (monthscroller.arrmonths[monthscroller.arrmonths.length -1] - monthscroller.arrmonths[monthscroller.currentmonth] + 10 > 507) $('#movescrollerright').show();
		return false;
	});
	
	// ensure that the selected month is visible
	if (monthscroller.selectedmonth > 0)
	{
		if (monthscroller.arrmonths[monthscroller.selectedmonth] > 497)
		{
			var selectedDist = monthscroller.arrmonths[monthscroller.selectedmonth];
			var movetomonth = 0;
			//alert(selectedDist);
			$.each(monthscroller.arrmonths, function(i)
			{
				if (movetomonth == 0 && selectedDist - monthscroller.arrmonths[i] < 497)
				{
					movetomonth = i;
				}
			});
			$('#monthscroller').css( 'left', -(monthscroller.arrmonths[movetomonth]) );
			monthscroller.currentmonth = movetomonth+1;
			if (movetomonth > 0) $('#movescrollerleft').show();
			if (monthscroller.arrmonths[monthscroller.arrmonths.length -1] - monthscroller.arrmonths[monthscroller.currentmonth] + 10 < 507) $('#movescrollerright').hide();
		}
	}
}