var markers = [];
var map;


function getMarkers(mostra){
	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 baseIcon2 = new GIcon();
   	baseIcon2.iconSize=new GSize(18,18);
  	baseIcon2.shadowSize=new GSize(14,14);
   	baseIcon2.iconAnchor=new GPoint(14,14);
   	baseIcon2.infoWindowAnchor=new GPoint(10,0);
	var baseIcon3 = new GIcon();
   	baseIcon3.iconSize=new GSize(27,18);
  	baseIcon3.shadowSize=new GSize(14,14);
   	baseIcon3.iconAnchor=new GPoint(14,14);
   	baseIcon3.infoWindowAnchor=new GPoint(10,0);
	
	var baseIcon4 = new GIcon();
   	baseIcon4.iconSize=new GSize(18,27);
  	baseIcon4.shadowSize=new GSize(14,14);
   	baseIcon4.iconAnchor=new GPoint(14,14);
   	baseIcon4.infoWindowAnchor=new GPoint(10,0);
	
	var baseIcon5 = new GIcon();
   	baseIcon5.iconSize=new GSize(25,25);
  	baseIcon5.shadowSize=new GSize(52,25);
	baseIcon5.shadow = "/imagens/google/shadow.png";
   	baseIcon5.iconAnchor=new GPoint(12,24);
   	baseIcon5.infoWindowAnchor=new GPoint(10,0);
	
//	var pin = new GIcon(baseIcon5, "/p.googleicon.g/text/PRT",null,null);
	
	var lupa = new GIcon(baseIcon, "/imagens/google/lupa.png",null,null);
	var logo = new GIcon(baseIcon, "/imagens/google/fap.png",null,null);
	var info = new GIcon(baseIcon, "/imagens/google/i.png",null,null);
	var barraca = new GIcon(baseIcon2, "/imagens/google/barraca.png",null,null);
	var barraca2 = new GIcon(baseIcon3, "/imagens/google/barraca2.png",null,null);
	var barraca3 = new GIcon(baseIcon4, "/imagens/google/barraca3.png",null,null);
	
    //var urlstr="backend.php";
	if(mostra){ var urlstr="/p.googlemaps.g/mostra/" + mostra; }
	else 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();
					id = locations[i].getAttribute("id");
					if(locations[i].getAttribute("icontype"))
						icon = eval(locations[i].getAttribute("icontype"));
					else if(locations[i].getAttribute("icontext"))
					{
						icon = new GIcon(baseIcon5, "/p.googleicon.g/text/"+locations[i].getAttribute("icontext"),null,null);
					}
						
					markers[id] = new GMarker(new GLatLng(locations[i].getAttribute("lat"),locations[i].getAttribute("lng")),icon);
					// Add attributes to the marker so we can poll them later.
					// When clicked, an overlay will have these properties.
					markers[id].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[id].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[id].db_id = locations[i].getAttribute("location_id");
					
					map.addOverlay(markers[id]);
					
				}
			}
		}
	}
	  
	request.send(null);
}



function Map_onLoad(latitude, longitude, make, zoom, mostra){
//<![CDATA[
	if( !zoom )  zoom = 17;
	
	map = new GMap2(document.getElementById("div_map"));
	//map.addControl(new GSmallMapControl());
	 map.addControl(new GLargeMapControl())
	map.addControl(new GMapTypeControl(),
	new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 10)));
	map.setCenter(new GLatLng(latitude, longitude),zoom);
	map.addMapType(G_PHYSICAL_MAP);
	map.setMapType( G_PHYSICAL_MAP );
	getMarkers(mostra);

	if( make ){
	
		GEvent.addListener(map, "click", function(overlay, point) {
			if (overlay){	// marker clicked
				overlay.openInfoWindowHtml(overlay.infowindow);	// open InfoWindow
			} else if (point) {	// background clicked
				map.clearOverlays();
				getMarkers();
				map.addOverlay(new GMarker(point));
				map.panTo(point);
				msg = "Latitude: "+point.lat()+"<br />"+"Longitude: "+point.lng();
				msg = msg + "<br />" + "<a href='' onclick='document.getElementById(\"mapa_latitude\").value = "+point.lat()+"; document.getElementById(\"mapa_longitude\").value = "+point.lng()+" ; return false;' >Utilizar Coordenadas</a>";
				
				document.getElementById("mypoint").innerHTML = msg;
			}
		 
     	 });
	  } else {
	  	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);

}