
var geocoder = null;
var marker = null;
var cLat = null;
var cLng = null;
var Direccio = [];

//---------------------------------------------
//funcions

function showAddress(response) {
      
         if (!response || response.Status.code != 200) {
//         alert("Status Code:" + response.Status.code);
      } else {
        var place = response.Placemark[0];
        var point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]); 
		Direccio[0]= response.Status.code;
		Direccio[1]= response.Status.request;
		Direccio[2]= place.address;
		Direccio[3]= place.AddressDetails.Accurancy;
		Direccio[4]= place.AddressDetails.Country.CountryNameCode;
       
       		//var myHtml = '<br />' + Direccio[2] + ' ' + Direccio[3] + ' ' + Direccio[4] + ' ';
       		if (marker == null)
       		{
       			alert('null');
       		}else{
			marker.openInfoWindowHtml('<br /><b>' + Direccio[2] + '</b>');
		}
       
//          var marker = markers[row_no -1];
//          marker.openInfoWindowHtml(
//           '<b>orig latlng:</b>' + response.name + '<br/>' + 
//           '<b>Reverse Geocoded latlng:</b>' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + '<br>' +
//           '<b>Status Code:</b>' + response.Status.code + '<br>' +
//           '<b>Status Request:</b>' + response.Status.request + '<br>' +
//           '<b>Address:</b>' + place.address + '<br>' +
//           '<b>Accuracy:</b>' + place.AddressDetails.Accuracy + '<br>' +
//           '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
        }
}

//--------------------------------------------


$(document).ready(
		function()
		{
			//----------------------------------------------------------------
			//MAPA !!!!
			$(".mapcontainer").each(
				function()
				{
					//alert("Mapa !!!");
					function initMapa(iodm,ilat,ilon)
					{
						if (GBrowserIsCompatible()) {
							
							//inicialitzo les classes
							var map = new GMap2(document.getElementById(iodm));
							geocoder = new GClientGeocoder();
							
							
											
							var myGeographicCoordinates = new GLatLng(ilat, ilon);
							zoom = 13;
							map.setCenter(myGeographicCoordinates, zoom);
							map.setCenter(myGeographicCoordinates, zoom);
							var point = new GLatLng(ilat, ilon); 
							marker = new GMarker(point);
							map.addOverlay(marker);
		// 					//mostro l'adreça
		// 					geocoder.getLocations(myGeographicCoordinates, showAddress);
							
							
						//###########################################################################
							//CONTROLS					
							//enable google bar ?
							//map.enableGoogleBar();	//no cal m'agrada més la barra de busqueda
							//posa les fletxetes
// 							map.addControl(new GSmallMapControl());
							//posa lo de satelit, hybrid, etc
// 							map.addControl(new GMapTypeControl());
							// posa el buscador
						
						
						}
					}
					var oid = $(this).attr("id");
					var ilat = $("#lat_" + oid).attr('value');
					var ilon = $("#lon_" + oid).attr('value');
					
					if(ilat != '' && ilon != '' )
					{
						initMapa(oid,ilat,ilon);
					}
					//GSearch.setOnLoadCallback(initMapa);
			});
		}
	);