// File: GooMaps.js
// Auth// File: GooMaps.js
// Author: Bill Weaver, some code adapted from Google Maps sample code
// Website: http://techrageo.us
// 2005.08.15 - Initial code
//
// 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('title');
	// 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");
      	// Set default maptypes array
			maptypes = [G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP];
      	// If maptype attribute is present get it and set accordingly
			if( markers[0].getAttribute("maptype") != null ) {
				mt = markers[0].getAttribute("maptype").substring(0,3).toUpperCase();
				switch( mt ){
				    case "SAT": maptypes = [G_SATELLITE_MAP, G_NORMAL_MAP, G_HYBRID_MAP]; break;
				    case "HYB": maptypes = [G_HYBRID_MAP, G_NORMAL_MAP, G_SATELLITE_MAP]; break;
				}
			}
      	// Get map div from HTML doc
			var map = new GMap(document.getElementById("map"),maptypes);
      	// Add small google map controls
			map.addControl(new GSmallMapControl());
 	     // Add map type buttons
			map.addControl(new GMapTypeControl());
   	   // 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");
			var bounds = new GLatLngBounds();
      	// 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 = iconbase + markerlist[i].getAttribute("icon");
				//icon.shadow = iconbase + markerlist[i].getAttribute("iconsh")
        		// Set icon and shadow sizes from attributes
				//icon.iconSize = new GSize(16, 20);
				icon.iconSize = new GSize(parseInt(markerlist[i].getAttribute("iconszx")), parseInt(markerlist[i].getAttribute("iconszy")));
				icon.shadowSize = new GSize(parseInt(markerlist[i].getAttribute("iconshszx")), parseInt(markerlist[i].getAttribute("iconshszy")));
        		// Set icon anchor
				icon.iconAnchor = new GPoint(4, 4);
        		// Set icon window anchor
				icon.infoWindowAnchor = new GPoint(9, 2);
        		// 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);
}

