// Create a basic icon (using a little one I made myself)
// All markers created with createMarker will use this
var baseIcon = new GIcon();
baseIcon.image = "http://www.chears.org/confluence/images/marker.png";
baseIcon.iconSize = new GSize(11,11);
baseIcon.iconAnchor = new GPoint(5,11);
baseIcon.infoWindowAnchor = new GPoint(5,11);
baseIcon.infoShadowAnchor  = new GPoint(7,13);

var yellowIcon = new GIcon();
yellowIcon.image = "http://www.chears.org/confluence/images/marker_yellow.png";
yellowIcon.iconSize = new GSize(11,11);
yellowIcon.iconAnchor = new GPoint(5,11);
yellowIcon.infoWindowAnchor = new GPoint(5,11);
yellowIcon.infoShadowAnchor  = new GPoint(7,13);

/*
baseIcon.iconSize = new GSize(20, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);
*/

var d_d_d_d_debug = document.getElementById('debug');

// Rounds a number to a specific number of decimal places
// num: the number to round
// places: the number of places to round num to
function Round(num, places)
{
	return Math.round(num*Math.pow(10,places))/Math.pow(10,places);
}

// Creates a new marker
// point: GPoint object
// message: Any sort of HTML
function createMarker(point, message, color, id)
{
	if(color == "yellow") {
		var mark = new GMarker(point,yellowIcon);
		message += "<br />Lat/Lon: " + point.lat() + ", " + point.lng();
	}
	else {
		var mark = new GMarker(point,baseIcon);
		message += "<br />Lat/Lon: " + point.lat() + ", " + point.lng();
	message += "<br />View this site's <a href=\"http://waterdata.usgs.gov/nwis/dv/?site_no=" + id + "\" target=\"_blank\">USGS Water Data</a>.";
	}
	GEvent.addListener(mark, 'click', function() {
		//map.recenterOrPanToLatLng(point);
		mark.openInfoWindowHtml("<div style='white-space:nowrap;'>"+message+"</div>");
	});

	return mark;
}

// If the user has a compatible browser, show the map
if (GBrowserIsCompatible())
{
	// Create the center point (somewhere in the middle of the bay) and the map
	var centerpoint = new GLatLng(39.554883059924016,-76.00341796875);
	var map = new GMap2(document.getElementById("map"));
	map.addMapType(G_PHYSICAL_MAP);
	map.addMapType(G_SATELLITE_3D_MAP);
					
// Zoom, and set some default map properties
	map.setCenter(centerpoint, 6);
	map.enableDoubleClickZoom();
	map.enableContinuousZoom();
	
	map.addControl(new GHierarchicalMapTypeControl());
	map.addControl(new GLargeMapControl());
	map.addControl(new GScaleControl(125));
	map.addControl(new GOverviewMapControl());
	map.setMapType(G_PHYSICAL_MAP);
	
	// Open and overlay the Farm2IslandKayak.kmz
	geoXml = new GGeoXml("http://chears.org/confluence/Farm2IslandKayak.kmz");
	map.addOverlay(geoXml);

	// Load the XML file with a big list of points
	var request = GXmlHttp.create();
	request.open('GET', 'data2.xml', true);
	request.onreadystatechange = function()
	{
	  if (request.readyState == 4)
	  {
		var xmlDoc = request.responseXML;
		var markers = xmlDoc.documentElement.getElementsByTagName("marker");
		
		for (var i = 0; i < markers.length; i++)
		{
			var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
								   parseFloat(markers[i].getAttribute("lng")));
			var message = markers[i].getAttribute("message");
			var color = markers[i].getAttribute("color");
			var id = markers[i].getAttribute("id");
			
			message.replace("&lt;", "<").replace("&rt;", ">");
			
			var marker = createMarker(point, message, color, id);
			map.addOverlay(marker);
		}
	  }
	}
	request.send(null);
}
