var map;
var markers;
var popups;
var mm;
var xmlDoc = null;

function getDoc(year, month, pan) {
    if (xmlDoc == null) {
        var request = GXmlHttp.create();
        var url = "anomalymapmonths.xml";
        request.open("GET", url, true);
        request.onreadystatechange = function() {
	        if (request.readyState == 4) {
	            xmlDoc = request.responseXML;
                if (map == null) {
	                map = new GMap2(document.getElementById("map"));
	
	                map.addControl(new GLargeMapControl());
	                map.addControl(new GMapTypeControl());
	                //map.addControl(new GOverviewMapControl());
	                var mini=new GOverviewMapControl(new GSize(200, 100));

	                map.addControl(mini);
	                mini.hide();
	                //map.addMapType(G_SATELLITE_3D_MAP);
	                map.setMapType(G_HYBRID_MAP);
	                map.setCenter(new GLatLng(42, -112), 3);
                }
				mapit(year, month, pan);
				maploaded = 1;
	        }
	    }
        request.send(null);
	} else {
		mapit(year, month, pan);
	}
}

function mapit(year, month, pan) {
    
	var position = ((year - 1900) * 12) + (month - 1);
	//alert(position);	
	document.getElementById("yearcounter").innerHTML = month + "/" + year;
	map.clearOverlays();
    markers = new Array();
	     	
	var locations = xmlDoc.documentElement.getElementsByTagName("location");

    popups = new Array(locations.length);
    var j = 0;
	
	var avstring = "";
    for (var i = 0; i < locations.length; i++) {

    	//Create the GLatLng object for this marker
      	var pointsarray = locations[i].getElementsByTagName("point");
      	var lng = parseFloat(pointsarray[0].getAttribute("lng"));
      	var lat = parseFloat(pointsarray[0].getAttribute("lat"));
      	var point = new GLatLng(lat,lng);
    
      	//Grab the tag names from the XML
      	var commonname = locations[i].getElementsByTagName("commonname")[0].firstChild.nodeValue;
      	var stnid = locations[i].getElementsByTagName("stationid")[0].firstChild.nodeValue;
      	var state = "";
        if (locations[i].getElementsByTagName("state")[0].firstChild) {
      		state = locations[i].getElementsByTagName("state")[0].firstChild.nodeValue;
      	}
      	var anoms = locations[i].getElementsByTagName("anomalies")[0].firstChild.nodeValue;

      	var anomalies = anoms.split(",");
      	var av = anomalies[position];
      	avstring += av + ",";
		var html = "";
		if (state != "") {
	    	html += "<div id='infowindowtitle' class='dfont'><a style='text-decoration: underline; color: white;' href='/sltrends/sltrends_station.shtml?stnid=" + stnid + "'>" + commonname + ", " + state + "</a><br><a style='text-decoration: underline; color: white;' href='/sltrends/sltrends_station.shtml?stnid=" + stnid + "'>" + stnid + "</a></div>";
	   	} else {
	   		html += "<div id='infowindowtitle' class='dfont'>" + commonname + "<br>" + stnid + "</div>";	
		}  
      	//var html = "<a style='text-decoration: underline;' href='/sltrends/sltrends_station.shtml?stnid=" + stnid + "'>" + commonname + "</a>";
		html += "<br>";
		html += "<div style='float: left; margin: 5px; width: 200px; text-align: left; font-size: 0.8em;'>";
		html += "<span style='font-weight: bold;'>Choose plot:</span><br>";
		html += "<a href='/sltrends/residual.shtml?stnid=" + stnid + "&name=" + commonname + "&state=" + state + "'>Interannual Variation</a><br>";
		html += "<a href='/sltrends/residual1980.shtml?stnid=" + stnid + "&name=" + commonname + "&state=" + state + "'>Interannual Variation since 1980</a><br>";
		html += "</div>";
      	popups[j] = html;

      	//Add the marker to the markers array
      	var title = commonname + " (" + av + ")";
      	if (av == 1) { // plus
      	    markers[j] = new GMarker(point, { icon: plusicon, title: title});
      	} else if (av == -1) { // minus
      	    markers[j] = new GMarker(point, { icon: minusicon, title: title});
      	} else if (av == 0 && av != " ") { //white dot
			//document.getElementById("debug").innerHTML += commonname + ":" + av + "<br>";
      	    markers[j] = new GMarker(point, { icon: whiteicon, title: title});			
      	}
			
		if (markers[j] != null && av != "") {
	        //Add the event listener to popup the info window on click for this marker
		    GEvent.addListener(markers[j], "click", makePopupCaller(j, stnid));
		    map.addOverlay(markers[j]);
			j++;
		//document.getElementById("debug").innerHTML += "j: " + j + " " + commonname + ",";
		} else {
		    //document.getElementById("debug").innerHTML += "j: " + j + " skipping " + commonname + "<br>";
		}
	}
	//alert(avstring);
}

function makePopupCaller( i, stnid ) {
	currentmarker = stnid;
    return function() { popup( i, stnid ); };
}

function popup( i, stnid ) {                
    markers[i].openInfoWindowHtml(popups[i]);
}

function pan(target) {
   switch(target) {
   	case 'eastcoast': map.setCenter(new GLatLng(36, -76.7), 5);
   	break;
   	case 'westcoast': map.setCenter(new GLatLng(42, -124.7), 5);
   	break;
   	case 'gulfcoast': map.setCenter(new GLatLng(27, -88.75), 6);
   	break;
	case 'alaska': map.setCenter(new GLatLng(61.5, -149), 4);
	break;
	case 'pacific': map.setCenter(new GLatLng(21, -157), 8);
	break;
	case 'global': map.setCenter(new GLatLng(0, 0), 1);
	break;
   }
}

var plusicon = new GIcon();
plusicon.image = "images/reddot.png";
plusicon.shadow = "images/shadow-plus.png";
plusicon.iconSize=new GSize(16, 15);
plusicon.shadowSize=new GSize(24,15);
plusicon.iconAnchor = new GPoint(8, 15);
plusicon.infoWindowAnchor = new GPoint(8, 0);

var minusicon = new GIcon();
minusicon.image = "images/bluedot.png";
minusicon.shadow = "images/shadow-minus.png";
minusicon.iconSize=new GSize(16, 15);
minusicon.shadowSize=new GSize(24,15);
minusicon.iconAnchor = new GPoint(8, 15);
minusicon.infoWindowAnchor = new GPoint(8, 0);

var whiteicon = new GIcon();
whiteicon.image = "images/whitedot.png";
whiteicon.shadow = "images/shadow-whitedot.png";
whiteicon.iconSize=new GSize(16, 15);
whiteicon.shadowSize=new GSize(24,15);
whiteicon.iconAnchor = new GPoint(8, 15);
whiteicon.infoWindowAnchor = new GPoint(8, 0);

