var ratios = 
{
	tablesAttributes : {}
	,
	formatTable : function(table)
	{
		var columnHeaders = $("thead th", table);
		var columnsEmpty = columnHeaders.filter(function() { return $(this).text() == ""; });
		var totalColumns = columnHeaders.index(columnsEmpty[0]); //get the index of the first empty column, thats the total of visible columns
		if (columnsEmpty.length == 0)
		{
		    totalColumns = columnHeaders.length - 1;
		}
		
		//Hide the empty columns
		$("thead tr th:nth-child(1n+" + (totalColumns+1) + "), tbody tr td:nth-child(1n+" + (totalColumns+1) + ")", table).hide();
		
		var firstColumn = totalColumns-3;
		if (totalColumns > 3)
		{
			//Hide rest of the years
			for (var i=1;i<firstColumn;i++)
			{
				$("thead tr th:nth-child(" + (i+1) + "),tbody tr td:nth-child(" + (i+1) + ")", table).hide();
			}
		}
		else
		{
			//hide buttons
			$("div.buttons", table.parent()[0]).hide();
		}
		
		ratios.tablesAttributes[table.attr("id")] = 
		{
			totalColumns : totalColumns,
			firstColumn : firstColumn
		};
	}
	,
	_swapColumns : function(table, firstColumn, totalColumns)
	{
		for (var i=1;i<totalColumns;i++)
		{
			var cssSelector = "thead tr th:nth-child(" + (i+1) + "), tbody tr td:nth-child(" + (i+1) + ")"; 
			if (i>=firstColumn && i<firstColumn+3)
			{
				$(cssSelector, table).show();
			}
			else
			{
				$(cssSelector, table).hide();
			}
		}
	}
	,
	checkButtons : function(firstColumn, totalColumns, sender)
	{
		var parent = $(sender).parent("div.buttons");
		if (firstColumn>1)
		{
			$("a.previous", parent).show();
		}
		else
		{
			$("a.previous", parent).hide();
		}
		if (firstColumn+3<totalColumns)
		{
			$("a.next", parent).show();
		}
		else
		{
			$("a.next", parent).hide();
		}
	}
	,
	previous : function(tableSelector, sender)
	{
		var table = $(tableSelector);
		var totalColumns = ratios.tablesAttributes[table.attr("id")].totalColumns;
		var firstColumn = ratios.tablesAttributes[table.attr("id")].firstColumn;
		
		if (firstColumn>1)
		{
			firstColumn--;
		}
		
		ratios._swapColumns(table, firstColumn, totalColumns);
		
		ratios.tablesAttributes[table.attr("id")].firstColumn = firstColumn;
		
		ratios.checkButtons(firstColumn, totalColumns, sender);
	}
	,
	next : function(tableSelector, sender)
	{
		var table = $(tableSelector);
		var totalColumns = ratios.tablesAttributes[table.attr("id")].totalColumns;
		var firstColumn = ratios.tablesAttributes[table.attr("id")].firstColumn;
		
		if (firstColumn+3<totalColumns)
		{
			firstColumn++;
		}
		
		ratios._swapColumns(table, firstColumn, totalColumns);
		
		ratios.tablesAttributes[table.attr("id")].firstColumn = firstColumn;
		
		ratios.checkButtons(firstColumn, totalColumns, sender);
	}
	,
	toggleContent : function(contentSelector, sender)
	{
		var back = $(sender).css("background-image");
		if (back.indexOf("more") >= 0)
		{
			$(sender).css("background-image", back.replace("more", "less"));
		}
		else
		{
			$(sender).css("background-image", back.replace("less", "more"));
		}
		
		$(contentSelector).slideToggle();
	}
};
