document.observe('dom:loaded', function(ev){
	
	function getValue(el)
	{
		if(document.all)
		{
			return el.innerText;
		} else {
			return el.textContent;
		}
	}
	
	
	var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
	var longMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	
	// precipitation
	var pData = [];
	
	var pList = $('precipitationRow');
	
	if(pList) {
		var count = 0;
		$$('#precipitationRow td').each(function(el){
			if(count != 0) pData.push([count, parseInt(getValue(el))]);
			count++;
		});
	}

	// temperature
	var tData = [];
	tMin = 0;
	
	var tList = $('tempRow');
	
	if(tList) {
		var count = 0;
		$$('#tempRow td').each(function(el){
			var val = parseInt(getValue(el));
			if(count != 0) tData.push([count, val]);
			if(val < 0 && val < tMin) {
				tMin = val;
			}
			count++;
		});
	}
	
	if(tMin < 0) {
		tMin = Math.floor(tMin / 10) * 10
	}
	
	// sunlight
	var sData = [];
	
	var sList = $('sunlightRow');
	
	if(sList) {
		var count = 0;
		$$('#sunlightRow td').each(function(el){
			if(count != 0) sData.push([count, parseInt(getValue(el))]);
			count++;
		});
	}
		
	// daylight
	var dData = [];
	
	var dList = $('sunlightRow');
	
	if(dList) {
		var count = 0;
		$$('#daylightRow td').each(function(el){
			if(count != 0) dData.push([count, parseInt(getValue(el))]);
			count++;
		});
	}
	
	climate = Flotr.draw(
		$('climateChart'),
		[
		{
			data: pData,
			label:'Precipitation (mm)',
			color: '#37b9f0',
			bars: {
				show: true,
				centered: true
			},
			xaxis: 1,
			yaxis: 2
		},
		{
			data: tData,
			label:'Temperature (&deg;C)',
			color: '#990000',
			xaxis: 1,
			yaxis: 1
		}
		],
		{
			shadowSize: 0,
			grid: {
				verticalLines: false,
				backgroundColor: '#ffffff',
				labelMargin: 5
			},
			xaxis: {
				noTicks: 12,
				tickFormatter: function (n) { return months[n-1]; }
			},
			yaxis: {
				min: tMin,
				tickDecimals: 0,
				tickFormatter: function (n) { return n + '&deg;C'; },
				autoscaleMargin: 1
			},
			y2axis: {
				min: 0,
				tickDecimals: 0,
				tickFormatter: function (n) { return n + 'mm'; },
				autoscaleMargin: 1
			},
			legend: {
				position: 'ne',
				noColumns: 2
			}
		}
	);

	var day = Flotr.draw(
		$('dayChart'),
		[
		{
			data: dData,
			label:'Daylight Hrs',
			bars: {
				show: true,
				centered: true,
				lineWidth: 1,
				barWidth: 0.8,
				fillOpacity: 0.7
			},
			color: '#fff533'
		},
		{
			data: sData,
			label:'Sunshine Hrs',
			bars: {
				show: true,
				centered: true,
				lineWidth: 1,
				barWidth: 0.6,
				fillOpacity: 0.7
			},
			color: '#ff9d00'
		}
		],
		{
			shadowSize: 0,
			grid: {
				verticalLines: false,
				backgroundColor: '#ffffff',
				labelMargin: 5				
			},
			xaxis: {
				noTicks: 12,
				tickFormatter: function (n) { return months[n-1]; }
			},
			yaxis: {
				min: 0,
				tickDecimals: 0,
				autoscaleMargin: 1
			},
			legend: {
				position: 'ne',
				noColumns: 2
			}
		}
	);

});


