//Robert Aspinall
//robert.aspinall@noaa.gov

var markers = null;
var popups;
var stationarray = new Array();

// Open the info box for the specified marker.
function popup( i ) {
    markers[i].openInfoWindowHtml(popups[i]);
}

// This returns a function closure that calls popup() with the specified arg.
// This is necessary because the API left out client-data.
function makePopupCaller( i )
    {
    return function() { popup( i ); };
    }

// Center the map on the West Coast region
var map = new GMap2(document.getElementById("map"));

map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.addControl(new GScaleControl());
//map.setMapType(G_SATELLITE_MAP);

//mapit("cb");
/*
GEvent.addListener(map, 'zoom', function (fromZoom, toZoom) {
alert('zoom');
   initPoints();
});
*/
GEvent.addListener(map, 'zoomend', function (fromZoom, toZoom) {
	if (markers != null) {
		for (var j=0; j < markers.length; j++) {
			markers[j].showTooltip();
		}
	}
});

function initPoints() {
   map.clearOverlays();
   for (var j = 0; j < markers.length; j++) {
      map.addOverlay(markers[j]);
   }
}


function mapit(region) {
    switch (region) {
	case "cb":
	map.setCenter(new GLatLng(38.251666, -76.150000), 7);
	break;
	case "lo":
	case "lowfo":
	map.setCenter(new GLatLng(43.700, -77.630), 7);
	break;
	case "lm":
        case "lmwfo":
	map.setCenter(new GLatLng(43.946, -86.441), 6);
	break;
	case "le":
	case "lewfo":
	map.setCenter(new GLatLng(42.5, -81.0), 7);
	break;
	case "lh":
	case "lhwfo":
	map.setCenter(new GLatLng(44.75, -82.42), 7);
	break;
	case "ls":
	case "lswfo":
	map.setCenter(new GLatLng(47.48, -88.0), 6);
	break;
	case "ny":
	map.setCenter(new GLatLng(40.696, -73.95), 10);
    break;
	case "gb":
	map.setCenter(new GLatLng(29.48,-94.80), 10);
    break;
    case "sj":
    map.setCenter(new GLatLng(30.03,-81.55), 10);
	}

    var stations = new Array();
    
    map.clearOverlays();
	
    var request = GXmlHttp.create();
    var regionname = "/ofsmap/" + region + ".xml";
    request.open("GET", regionname, true);
    request.onreadystatechange = function() {

    if (request.readyState == 4) {
        var xmlDoc = request.responseXML;
        var locations = xmlDoc.documentElement.getElementsByTagName("location");
        markers = new Array(locations.length);
        popups = new Array(locations.length);
        for (var i = 0; i < locations.length; i++) {
            var pointsarray = locations[i].getElementsByTagName("point");
            var lng, lat;
            lng = parseFloat(pointsarray[0].getAttribute("lng"));
            lat = parseFloat(pointsarray[0].getAttribute("lat"));
//            var point = new GPoint(lng, 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;
			var x;
			var y;
			if (locations[i].getElementsByTagName("stationid")[0].firstChild) { 
				stnid = locations[i].getElementsByTagName("stationid")[0].firstChild.nodeValue;
			} else { stnid = ""; }
            //var position = locations[i].getElementsByTagName("position")[0].firstChild.nodeValue;
			if (locations[i].getElementsByTagName("x")[0].firstChild) {
				x = locations[i].getElementsByTagName("x")[0].firstChild.nodeValue;
			} else { x = 0; }
			if (locations[i].getElementsByTagName("y")[0].firstChild) {
				y = locations[i].getElementsByTagName("y")[0].firstChild.nodeValue;
			} else { y = 0; }
			var WaterLevel = locations[i].getElementsByTagName("WaterLevel")[0].getAttribute("href");
			var Winds = locations[i].getElementsByTagName("Wind")[0].getAttribute("href");
			var WaterTemp = locations[i].getElementsByTagName("WaterTemp")[0].getAttribute("href");
			var Currents = locations[i].getElementsByTagName("Currents")[0].getAttribute("href");
			var crex;
			var shef;
			var buoy;
			if (locations[i].getElementsByTagName("crex")[0].firstChild) { 
				crex = locations[i].getElementsByTagName("crex")[0].firstChild.nodeValue;
			} else { crex = ""; }
			if (locations[i].getElementsByTagName("shef")[0].firstChild) { 
				shef = locations[i].getElementsByTagName("shef")[0].firstChild.nodeValue;
			} else { shef = ""; }
			if (locations[i].getElementsByTagName("buoy")[0].firstChild) { 
				buoy = locations[i].getElementsByTagName("buoy")[0].firstChild.nodeValue;
			} else { buoy = ""; }
			
            var html = "<div id='infowindowframe'>";
                html += "<div id='infowindowtitle'>" + commonname + " " + stnid + "</div>";
                html += "<div id='infowindowleftpane'>"
				
				
				if (WaterLevel) {
					html += "<a href=\"" + WaterLevel + "\"><img border=0 src='/ofsmap/tideicon.png'></a><br>";
					html += "<a href=\"" + WaterLevel + "\">Water Level</a><br>";
				}
				if (Winds) {
					html += "<a href=\"" + Winds + "\"><img border=0 src='/ofsmap/windicon.png'></a><br>";
					html += "<a href=\"" + Winds + "\">Winds</a><br>";
				}
				if (WaterTemp) {
					html += "<a href=\"" + WaterTemp + "\"><img border=0 src='/ofsmap/tempicon.png'></a><br>";
					html += "<a href=\"" + WaterTemp + "\">Water Temp</a><br>";
				}
				if (Currents) {
					html += "<a href=\"" + Currents + "\"><img border=0 src='/ofsmap/curricon.png'></a><br>";
					html += "<a href=\"" + Currents + "\">Currents</a><br>";
				}
				
                //html += "<a target='_blank' href=" + link + "><img border=0 src='tideicon.png'></a>";
                html += "</div>";
                html += "<div id='infowindowrightpane'>";
				if (crex != "") {
					html += "CREX ID: " + crex + "<br>";
				}
				if (shef != "") {
					html += "SHEF ID: " + shef + "<br>";
				}
				if (buoy != "") {
					html += "BUOY ID: " + buoy + "<br>";
				}
                //html += phone + "<br>" + platid + "<br>" + channel + "<br>" + xmittime + "<br>" + waterlevel;
				
                html += "</div>";
            html += "</div>";
            popups[i] = html;

      		
			if (typeof(x) != "undefined" && typeof(y) != "undefined") {
				markers[i] = new GxMarker(point, null, commonname, "static", x, y);
			} else {
            	markers[i] = new GxMarker(point, null, commonname, "static");
			}

			//markers[i] = new GxMarker(point, null, commonname);
			//markers[i] = new GMarker(point);
            
			GEvent.addListener(markers[i], "click", makePopupCaller(i));
            //map.addOverlay(markers[i]);
			//markers[i].showTooltip;
        }
	initPoints();
    }
}
request.send(null);
}








