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

function getDoc(year, pan) {
    if (!(year >= 1900 && year <= 2008)) { return; }
    if (xmlDoc == null) {
        var request = GXmlHttp.create();
        var url = "anomalymap.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, pan);
				maploaded = 1;
	        }
	    }
        request.send(null);
	} else {
		mapit(year, pan);
	}
}

function mapit(year, pan) {
//document.getElementById("debug").innerHTML += "Mapit called with " + year + " " + pan + "<br>";
    /*
	if (mm == null) {
       mm = new MarkerManager(map);
    } else {
       mm.clearMarkers();
    }
	*/
	
	document.getElementById("yearcounter").innerHTML = year;
	map.clearOverlays();
    markers = new Array();
	     	
	    var locations = xmlDoc.documentElement.getElementsByTagName("location");

            popups = new Array(locations.length);
            var j = 0;
      	    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[year - 1900];
//      document.getElementById("debug").innerHTML += "|AVBEGIN|" + anomalies + "|AVEND|";
	  			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 = "";	  
	  			if (state != "") {
      	        	html = "<a style='text-decoration: underline;' href='/sltrends/sltrends_station.shtml?stnid=" + stnid + "'>" + commonname + "</a><br>";
				} else {
					html = "<a style='text-decoration: underline;' href='/sltrends/sltrends_station.shtml?stnid=" + stnid + "'>" + commonname + "</a><br>";
				}
*/
				var term = "anomaly";
				if (av > 1 || av < -1) { term = "anomalies"; }
				if (av < 0) {
					html += (av * -1) + " negative " + term + " in " + year;
				} else if (av > 0) {
					html += av + " positive " + term + " in " + year;
				} else {
					html += "No anomalies in " + year;
				}
				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 + ")";
				markers[j] = null;
      	    	if (av >= 10 && av <= 12) {
      			    markers[j] = new GMarker(point, { icon: red4icon, title: title});
      		    } else if (av >= 7 && av <= 9) {
      			    markers[j] = new GMarker(point, { icon: red3icon, title: title});
      		    } else if (av >= 4 && av <= 6) {
      			    markers[j] = new GMarker(point, { icon: red2icon, title: title});			
      		    } else if (av >= 1 && av <= 3) {
      			    markers[j] = new GMarker(point, { icon: red1icon, title: title});		
      		    } else if (av == 0 && av != "") { //white
      		        markers[j] = new GMarker(point, { icon: whiteicon, title: title});
      		    } else if (av >= -3 && av <= -1) {
      			    markers[j] = new GMarker(point, { icon: blue1icon, title: title});						
      		    } else if (av >= -6 && av <= -4) {
      			    markers[j] = new GMarker(point, { icon: blue2icon, title: title});			
      		    } else if (av >= -9 && av <= -7) {
      			    markers[j] = new GMarker(point, { icon: blue3icon, title: title});			
      		    } else if (av >= -12 && av <= -10) {
      			    markers[j] = new GMarker(point, { icon: blue4icon, title: title});			
      		    }
			
		        if (markers[j] != null) {
	                //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 + "|" + markers[j] + "|" + ",";
		        } else {				    
		            //document.getElementById("debug").innerHTML += "j: " + j + " skipping " + commonname + " av: " + av + "<br>";
		        }
	        }
	    //Add stations to marker manager
        //mm.addMarkers(markers, 1, 17);
	    //mm.refresh();
}

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 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);

var red4icon = new GIcon();
red4icon.image = "images/red4.png";
red4icon.shadow = "images/shadow-redbar.png";
red4icon.iconSize=new GSize(16,43);
red4icon.shadowSize=new GSize(38,43);
red4icon.iconAnchor=new GPoint(8,43);
red4icon.infoWindowAnchor=new GPoint(8,0); 

var red3icon = new GIcon();
red3icon.image = "images/red3.png";
red3icon.shadow = "images/shadow-orangebar.png";
red3icon.iconSize=new GSize(16,37);
red3icon.shadowSize=new GSize(35,37);
red3icon.iconAnchor=new GPoint(8,37);
red3icon.infoWindowAnchor=new GPoint(8,0); 

var red2icon = new GIcon();
red2icon.image = "images/red2.png";
red2icon.shadow = "images/shadow-yellowbar.png";
red2icon.iconSize=new GSize(16,31);
red2icon.shadowSize=new GSize(32,31);
red2icon.iconAnchor=new GPoint(8,31);
red2icon.infoWindowAnchor=new GPoint(8,0); 

var red1icon = new GIcon();
red1icon.image = "images/red1.png";
red1icon.shadow = "images/shadow-greenbar.png";
red1icon.iconSize=new GSize(16,25);
red1icon.shadowSize=new GSize(29,25);
red1icon.iconAnchor=new GPoint(8,25);
red1icon.infoWindowAnchor=new GPoint(8,0); 


var blue1icon = new GIcon();
blue1icon.image = "images/blue1.png";
blue1icon.shadow = "images/shadow-bluebar.png";
blue1icon.iconSize=new GSize(16,25);
blue1icon.shadowSize=new GSize(29,25);
blue1icon.iconAnchor=new GPoint(8,25);
blue1icon.infoWindowAnchor=new GPoint(8,0); 


var blue2icon = new GIcon();
blue2icon.image = "images/blue2.png";
blue2icon.shadow = "images/shadow-lightbluebar.png";
blue2icon.iconSize=new GSize(16,31);
blue2icon.shadowSize=new GSize(31,31);
blue2icon.iconAnchor=new GPoint(8,31);
blue2icon.infoWindowAnchor=new GPoint(8,0); 

var blue3icon = new GIcon();
blue3icon.image = "images/blue3.png";
blue3icon.shadow = "images/shadow-lightpurplebar.png";
blue3icon.iconSize=new GSize(16,37);
blue3icon.shadowSize=new GSize(35,37);
blue3icon.iconAnchor=new GPoint(8,37);
blue3icon.infoWindowAnchor=new GPoint(8,0); 

var blue4icon = new GIcon();
blue4icon.image = "images/blue4.png";
blue4icon.shadow = "images/shadow-purplebar.png";
blue4icon.iconSize=new GSize(16,43);
blue4icon.shadowSize=new GSize(38,43);
blue4icon.iconAnchor=new GPoint(8,43);
blue4icon.infoWindowAnchor=new GPoint(8,0); 


