var mymap=null;
var mymapcenter=null;
var myom = null;
var stnicon = null;
var stationsxml = new Array();
var tMarker = null;
var selectedStation = null;
var stations = new Array();
var stationcount = 0;

function initMap() {
	if (GBrowserIsCompatible()) {
		myom = new OverlayMessage(document.getElementById('search_map'));
		myom.Set('<b>Loading...<\/b>');
		document.getElementById('search_map').innerHTML = '';
		mymap = new GMap2(document.getElementById('search_map'));
		mymapcenter = new GLatLng(10,-150);
		mymap.addControl(new GLargeMapControl());
		mymap.addControl(new GMapTypeControl());
		mymap.enableDoubleClickZoom();
		var mt = mymap.getMapTypes();
		for (var i=0; i<mt.length; i++) {
			mt[i].getMinimumResolution = function() {return 1;}
			mt[i].getMaximumResolution = function() {return 18;}
		}
		mymap.setCenter(mymapcenter, 1, G_HYBRID_MAP);
		stnicon = new GIcon();
		stnicon.image = "/images/maps/markers/tiny_yellow_marker.png";
		stnicon.shadow = null;
		stnicon.iconSize = new GSize(11, 11);
		stnicon.shadowSize = null;
		stnicon.iconAnchor = new GPoint(5, 5);
		stnicon.infoWindowAnchor = new GPoint(5, 5);
		if (document.getElementById && document.getElementById('station_id')) {
			document.getElementById('station_id').value = "";
			document.getElementById('station_id').focus();
		}
		GDownloadUrl('/historical_met_stations.xml',function(data,code) {
			if(data=="" || !(code==200||code==304) ) { 
				return;
			}
			var xml = GXml.parse(data);
			if(xml.documentElement) {
		  		stationsxml = xml.documentElement.getElementsByTagName("station");
			} else {
				xml = document.createElement("div");
				xml.innerHTML = data;
				stationsxml = xml.getElementsByTagName("station");
			}
			updateMarkers(0,50);
			return;
		});
	} else {
		if (document.getElementById && document.getElementById('search_map')) {
			document.getElementById('search_map').innerHTML = "<h3 style=\"text-align:center\">Sorry!  Your browser does not support Google Maps.</h3>";
		} else {
			alert("Sorry!  Your browser does not support Google Maps.");
		}
	}
}

function setStation(stn) {
	if (document.getElementById && document.getElementById('station_id') && document.getElementById('yearlist')) {
		document.getElementById('station_id').value=stn;
		var yearlist = "The station ID you entered has no historical data.";
		for (i=0;i<stationcount;i++) {
			if (stations[i].mystn == selectedStation) {
				stations[i].setImage('/images/maps/markers/tiny_yellow_marker.png');
			}
			if (stations[i].mystn == stn) {
				stations[i].setImage('/images/maps/markers/tiny_red_marker.png');
				if (stations[i].myyears == null) {
					yearlist = 'No historical meteorological data are available from '+stn.toUpperCase()+'.';
				} else {
					yearlist = 'Historical meteorological data from '+stn.toUpperCase()+' are available for '+stations[i].myyears+'.';
				}
			}
		}
		selectedStation = stn;
		document.getElementById('yearlist').innerHTML = yearlist;
	} else {
		alert("Sorry, your browser does not fully support this map.");
	}
}

function updateMarkers(i,inc) {

	function createMarker(stn,lat,lon,owner,name,years,icon) {
		var stnpos = new GLatLng(lat,lon);
		var marker = null;
		var tt = stn.toUpperCase();
		marker = new GMarker(stnpos,{title:tt,icon:icon});
		marker.mystn = stn;
		marker.myyears = years;
		GEvent.addListener(marker, "click", function() {
			setStation(stn);
		});
		return marker;
	}

	if(!i) {
		i = 0;
	}
	if(!inc) { 
		inc = 50;
	}

	var len = stationsxml.length;
	var stop = Math.min(len,i+inc);

	for (;i<stop;i++) {
		stations[i] = createMarker(stationsxml[i].getAttribute("id"),stationsxml[i].getAttribute("lat"),stationsxml[i].getAttribute("lon"),stationsxml[i].getAttribute("owner"),stationsxml[i].getAttribute("name"),stationsxml[i].getAttribute("years"),stnicon);
		mymap.addOverlay(stations[i]);
	}
	if (stop<len) {
		stationcount = stations.length;
		myom.Set('<b>Loaded '+stop+' of '+len+' stations...<\/b>');
		window.clearTimeout(tMarker);
		tMarker = window.setTimeout('updateMarkers('+stop+','+inc+')',50);
	} else {
		stationcount = stations.length;
		myom.Clear();
	}
}

window.onload=initMap;
window.onunload=GUnload;
