var i = 0;
var gmarkers = [];
var mtypes = [];
var gmap;
var geocoder;

	
   function showAddress(address) {
	  geocoder.getLatLng(
		address,
		function(point) {
		  if (!point) {
			alert(address + " not found");
			showAddress('San Antonio, TX');
		  } else {
			gmap.setCenter(point, 11);
		  }
		}
	  );
	}

function load() {
  if (GBrowserIsCompatible()) {
	
	function createMarker(point, title, details, icontype, typeid) {
	  var marker = new GMarker(point, eval(icontype));
	  
	  GEvent.addListener(marker, "mouseover", function() {
		marker.openInfoWindowHtml('<div style="color: #000; width: 180px; overflow: auto;"><b>' + title + '</b><p style="color: #000; font-size:10px; margin-bottom:0px;">' + details + '</p></div>');
	  });
		
		gmarkers[i] = marker;
		mtypes[i] = typeid;
		i++;
		
		return marker;
	}
	
	
	
	gmap = new GMap2(document.getElementById("gmap"));
	gmap.addControl(new GLargeMapControl());
	gmap.addControl(new GScaleControl());
	gmap.addControl(new GOverviewMapControl()); 
	
	
	geocoder = new GClientGeocoder();
	
	if(zip != '') {
		showAddress(zip);
	} else if(city != '') {
		showAddress(city);
	} else {
		showAddress('San Antonio, TX');
	}
	
	var cicon = new GIcon();
	cicon.image = "themes/xarDWTX/images/gmChurch.png";
	cicon.shadow = "themes/xarDWTX/images/gmChurch_shadow.png";
	cicon.iconSize = new GSize(37, 41);
	cicon.shadowSize = new GSize(37, 41);
	cicon.iconAnchor = new GPoint(19, 36);
	cicon.infoWindowAnchor = new GPoint(31, 6);
	
	var sicon = new GIcon();
	sicon.image = "themes/xarDWTX/images/gmSchool.png";
	sicon.shadow = "themes/xarDWTX/images/gmSchool_shadow.png";
	sicon.iconSize = new GSize(37, 41);
	sicon.shadowSize = new GSize(37, 41);
	sicon.iconAnchor = new GPoint(19, 36);
	sicon.infoWindowAnchor = new GPoint(31, 6);
		  
	GDownloadUrl("index.php/base/mapdata?mtype="+mtype, function(data, responseCode) {
	  var xml = GXml.parse(data);
	  var markers = xml.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")));
		gmap.addOverlay(createMarker(point,
							markers[i].getAttribute("title"),
							markers[i].getAttribute("details"),
							markers[i].getAttribute("icontype"),
							markers[i].getAttribute("typeid")));
	  }
	});
  }
}

