// Looking for map xml looking like the following
//	<markers centery="30.405602" centerx="-97.715192"
//	  zoom="3" iconbase="http://techrageo.us/wp-content/"
//	  maptype="HYB">
//	  <marker lat="30.402602" lng="-97.715192"
//	    html="IBM Dev" icon="icon1.png"
//	    iconsh="icon1_shadow.png"/>
//	  <marker lat="30.401602" lng="-97.720192"
//	    html="IBM Mfg" icon="icon1.png"
//	    iconsh="icon1_shadow.png"/>
//	</markers>

// Modified from Google Maps API Reference
// Added support for specifying icons and html captions
function createMarker(point, icon, html) {
	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

// GooMapXml
// Reads an xml file, the name of which is specified
//  in an attribute of a span with the id mapinfo.
// Sample Call from HTML
//	<html xmlns="http://www.w3.org/1999/xhtml">
//	  <head>
//	    <title>Google Maps JavaScript API Example - simple</title>
//	    <script src="http://maps.google.com/maps?file=api&v=1&key=[**your api key**]"
//	      type="text/javascript"></script>
//	    <script src="http://techrageo.us/wp-content/GooMap.js"></script>
//	  </head>
//	<body>
//	<div id="map" style="width:300px; height:200px"></div>
//	<span id="mapinfo" name="http://techrageo.us/wp-content/goomap1.xml"></span>
//	<script type="text/javascript">GooMapXml();</script>
//	</body>
//	</html>

function GooMapXml(){
	// Find mapinfo span and get name attribute
	var mapinfo = document.getElementById('mapinfo').getAttribute('name');
	// Create GXmlHttp object to fetch XML document
	var request = GXmlHttp.create();
	// Open the connection to the XML file
	request.open("GET", mapinfo, true);
	// Describe a function to handle the completed request
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
	    	// Get XML file contents
			var xmlDoc = request.responseXML;
			// Get markers element
			var markers = xmlDoc.getElementsByTagName("markers");
      	// Get iconbase attribute
			iconbase = markers[0].getAttribute("iconbase");

      	// Get map div from HTML doc
			var map = new GMap(document.getElementById("map"));
      	// Add small google map controls
			map.addControl(new GSmallMapControl());
	map.setMapType(G_SATELLITE_MAP);
   	// Create point for center (get x,y from attributes of markers element)
			var cpt = new GPoint(parseFloat(markers[0].getAttribute("centerx")),
										parseFloat(markers[0].getAttribute("centery")));
   	// Center and zoom map (zoom specified in attribute of markers element)
			map.centerAndZoom(cpt, parseInt(markers[0].getAttribute("zoom")));
      	// Get marker element vector
			var markerlist = xmlDoc.documentElement.getElementsByTagName("marker");
      	// Loop through marker elements
			for (var i = 0; i < markerlist.length; i++) {
        		// Create points based on lat, lng attributes
				var point = new GPoint(parseFloat(markerlist[i].getAttribute("lng")),
												parseFloat(markerlist[i].getAttribute("lat")));
        		// Create icon
				var icon = new GIcon();
        		// Set image and shadow from iconbase and icon/iconsh attributes
			        icon.image = "http://www.made-in-nina.com/goomaps/mm_20_red.png";
			        icon.shadow = "http://www.made-in-nina.com/goomaps/mm_20_shadow.png";
        		// Set icon and shadow sizes from attributes
			        icon.iconSize = new GSize(12, 20);
			        icon.shadowSize = new GSize(22, 20);
			// Set icon anchor
			        icon.iconAnchor = new GPoint(6, 20);
	        // Set icon window anchor
				icon.infoWindowAnchor = new GPoint(5, 1);
        		// Call createMarker w/ point, icon and html from attribute
				var marker = createMarker( point, icon, markerlist[i].getAttribute("html") );
        		// Add marker overlay to map
				map.addOverlay(marker);
			}
		}
	}
	// Send request to get XML file
	request.send(null);
}
