var markers = [];

function getMarkers(){
	var baseIcon = new GIcon();
   	baseIcon.iconSize=new GSize(28,28);
  	baseIcon.shadowSize=new GSize(56,32);
   	baseIcon.iconAnchor=new GPoint(10,27);
   	baseIcon.infoWindowAnchor=new GPoint(10,0);	

	

	var logo = new GIcon(baseIcon, "/imagens/google/logo.png",null,null);

	
    //var urlstr="backend.php";
	var urlstr="/p.googlemaps";
    var request = GXmlHttp.create();
    request.open('GET', urlstr , true);	// request XML from PHP with AJAX call
    request.onreadystatechange = function () {
		if (request.readyState == 4) {
			var xmlDoc = request.responseXML;
			locations = xmlDoc.documentElement.getElementsByTagName("location");
			//markers = [];
			if (locations.length){
				for (var i = 0; i < locations.length; i++) { // cycle thru locations
					//alert();
					markers[i] = new GMarker(new GLatLng(locations[i].getAttribute("lat"),locations[i].getAttribute("lng")),eval(locations[i].getAttribute("icontype")));
					// Add attributes to the marker so we can poll them later.
					// When clicked, an overlay will have these properties.
					markers[i].infowindow = locations[i].getAttribute("name");
					
					// Useful things to store on a marker (Not needed for this example, could be removed)
					// Tells you what index in the markers[] array an overlay is
					markers[i].markerindex = i;
					// Store the location_id of the location the marker represents.
					// Very useful to know the true id of a marker, you could then make
					// AJAX calls to the database to update the information if you had it's location_id
					markers[i].db_id = locations[i].getAttribute("location_id");
					
					map.addOverlay(markers[i]);
					
				}
			}
		}
	}
	request.send(null);
}



function Map_onLoad(){
//<![CDATA[
	map = new GMap2(document.getElementById("div_map"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl(),
	new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 10)));
	map.setCenter(new GLatLng(40.62035770227307, -8.50242555141449),14);
	getMarkers();
	GEvent.addListener(map, "click", function(overlay, point) {
	   if (overlay){	// marker clicked
	     overlay.openInfoWindowHtml(overlay.infowindow);	// open InfoWindow
	   }
	});
//]]>
}

function myclick(i) {
    markers[i].openInfoWindowHtml(markers[i].infowindow);
}