// globally declare map for the benefit of IE
var map;

function get(el) {
	if(document.all) {
		return el.innerText;
	} else {
		return el.textContent;
	}
}

function clearMarkers()
{
	for(var i=0; i < markers.length; i++)
	{
		markers[i].remove();
		markers[i].closeInfoWindow();
	}
	
	markers = new Array();
}

function showMarkers(monthNumber) {
	var places = $$('.dataset .place');
	var baseIcon = new GIcon(G_DEFAULT_ICON);
	baseIcon.iconSize = new GSize(0,0);
	baseIcon.shadowSize = new GSize(0,0);
	baseIcon.iconAnchor = new GPoint(0,0);

	places.each(function(place){
		var lat = get(place.down('.lat'));
		var lng = get(place.down('.long'));
		var displayName = '';
		monthData = place.down('.month' + monthNumber);
		var temperature = get(monthData.down('.temperature'));
/*
		alert('Temp: ' + temperature);
		alert('Rainfall: ' + precipitation);
*/
		var precipitation = get(monthData.down('.precipitation'));
		
		// fix the temp & prec. if they are null or empty
		temperature = (temperature != null && temperature != '') ? temperature : '';
		precipitation = (precipitation != null && precipitation != '') ? precipitation : '';
		
		var point = new GLatLng(lat, lng);
		var options = {
			icon: baseIcon,
			"clickable": true,
			"labelClass": 'markerLabel temp' + temperature,
			"labelText": '<ul><li class="temp">' + temperature + '&deg;C</li><li class="precipitation">' + precipitation + 'mm</li></ul>',
			"labelOffset": new GSize(-23,-20)	
		};
		var marker = new LabeledMarker(point, options);
		markers.push(marker);
		map.addOverlay(marker);
	});
}

document.observe('dom:loaded', function(ev){
	
	if($$('.monthControl .month').length != 0) {
	
		allowMapControl = false;
	
		/* extendMapEast - if true, extends the bounding box 20% east to cover map category control */
		extendMapEast = true;
		extendDivisor = 2.5;
	
		markers = [];
	
		if (GBrowserIsCompatible()) {
		
			var bbNorth = parseFloat(get($$('.countryMapData .bbNorth')[0]));
			var bbWest = parseFloat(get($$('.countryMapData .bbWest')[0]));
			var bbSouth = parseFloat(get($$('.countryMapData .bbSouth')[0]));
			var bbEast = parseFloat(get($$('.countryMapData .bbEast')[0]));
		
			if(extendMapEast)
			{
				oldEast = bbEast;
				bbEast = bbEast + ((bbEast - bbWest)/extendDivisor);
	//			alert('East:\n\tFrom: ' + oldEast + '\n\t' + 'To: ' + bbEast);
			}
		
			bounds = new GLatLngBounds();
			bounds.extend(new GLatLng(bbNorth, bbWest));
			bounds.extend(new GLatLng(bbSouth, bbEast));
			map = new GMap2(document.getElementById("map"));
			map.setMapType(G_PHYSICAL_MAP);
			map.setCenter(bounds.getCenter());
			map.setZoom(map.getBoundsZoomLevel(bounds));
			if(allowMapControl) {
				map.setUIToDefault();
				map.enableContinuousZoom();
			} else {
				map.disableDragging();
				map.disableScrollWheelZoom();
				map.disableDoubleClickZoom();
			}
		}
	
		showMarkers(1);
	
		$$('.monthControl .month').each(function(month){
			month.observe('click', function(ev){
				Event.stop(ev);
				month = Event.element(ev);
				month = (month.tagName == 'DIV') ? month : month.up('div');
				monthNumber = month.previousSiblings().length;
			
				// deactivate all other months
				$$('.monthControl .month a.active').each(function(activeMonth){
					activeMonth.removeClassName('active');
				});
			
				month.down('a').addClassName('active');
			
				clearMarkers();
				showMarkers(monthNumber);
			})
		});
	}
});
