var map;
var gmarkers = [];
var htmls = [];
//var side_bar_html = "";
var i = 0;

//function myzoom(a) {
//  map.setZoom(map.getZoom() + a);
//}

// === Custiom Icons: Create an associative array of GIcons() ===
var gicons = [];
gicons["red"] = new GIcon(G_DEFAULT_ICON, "/images/pointer.png");
gicons["red"].iconSize = new GSize(10, 17);
gicons["red"].shadowSize = new GSize(0, 0);

function loadmaps(querystring) {
scroll(0,0);
  if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById("map"));
 	map.addControl(new GLargeMapControl());
	//map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
	
    
	// set the starting location and zoom
	map.setCenter(new GLatLng(37.969144, -122.510502), 13);

	// A function to create the marker and set up the event window
	function createMarker(point,name,html,icontype) {
     
	// === create a marker with the requested icon ===
    var marker = new GMarker(point, gicons[icontype]);
	//var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml('<span class="text1">' + html + '</span>');
     });

     // save the info we need to use later for the side_bar
     gmarkers[i] = marker;
     htmls[i] = html;
	//thelink[i] = thelink;
	
	// add a line to the side_bar html
	//side_bar_html += '';
	    
  	i++;
    return marker;
  	}
  	
	var bounds = new GLatLngBounds();
   // GDownloadUrl("/ajax/map_xml.asp?" + querystring, function(data) {
   GDownloadUrl("googlemaps.aspx?" + querystring, function(data) {
   
    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")));
        
		var html = markers[i].getAttribute("html");
        var label = markers[i].getAttribute("label");
		var icontype = markers[i].getAttribute("icontype");
		var marker = createMarker(point,label,html,icontype);
        map.addOverlay(marker);
		bounds.extend(point);		  
	 	var ii = i + 1
	}

	// put the assembled side_bar_html contents into the side_bar div
	//if (side_bar_html == '') {
	//side_bar_html = 'No records found.'} //DEALS WITH EOF
	
	//var fianl_side_bar_html = '<table cellpadding="2" cellspacing="0">'+side_bar_html+'</table>'
	//document.getElementById("side_bar").innerHTML = fianl_side_bar_html;
	
	// ===== determine the zoom level from the bounds =====
	if (ii > 1) { map.setZoom(map.getBoundsZoomLevel(bounds))};

    // ===== determine the centre from the bounds ======
    var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
    var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
     
	if (clat != 0) { map.setCenter(new GLatLng(clat,clng));
	}
	else //if no records then show specific map (rather than world map)
	{ map.setCenter(new GLatLng(31.95216223802497, -98.61328125), 3);}
	    });
	  }
}

//This function picks up the click and opens the corresponding info window
function myclick(i) {
	gmarkers[i].openInfoWindowHtml(htmls[i]);
 }