/*
requires loaded
http://www.google.com/jsapi?key=
*/

var map; //LOADED AT THE END
var marker = null;

function crearMarker (point, address)
{
	if (marker == null)
  {
  	marker = new GMarker(point);
  	map.setCenter(point,12);
  }
  else
  {
  	map.setCenter(point);
  	marker.setLatLng (point);
  }
	
	var html = address + "<BR><span style='cursor: pointer' onclick='borrarMarker()'><strong>Eliminar</strong></span>";
  
  	
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });
  map.addOverlay(marker);
}

function borrarMarker ()
{
	map.removeOverlay(marker);
	marker = null; //Crear el proxim cop
	document.preferencias.latitude.value = "";
	document.preferencias.longitude.value = "";
	document.preferencias.ciudad.val_anterior = null;

}


/*ACTUALITZA EL MAPA DE DADES_PERSONALS*/
function actualitzarMapa (localizar)
{
		/*Usa variables de t en dades_personals*/
		if (GBrowserIsCompatible()) 
		{ 
			var address = ""; 
			if (document.preferencias.ciudad.value != "")
			{
					address =   document.preferencias.pais.options[document.preferencias.pais.selectedIndex].text + "," + document.preferencias.ciudad.value
					if (document.preferencias.direccion_postal.value != "")
						address =  address + "," + document.preferencias.direccion_postal.value
			}
			
			 
			if ( (document.preferencias.latitude.value || document.preferencias.longitude.value) && marker == null)
			{
				 //SI l'usuari te posició la mostrem
				 crearMarker (new GLatLng(document.preferencias.latitude.value,document.preferencias.longitude.value),address);
			 }
		
			if (localizar)
			{
				if (address.length > 0)
				{
					document.preferencias.ciudad.val_anterior = document.preferencias.ciudad.value;
					geocoder = new GClientGeocoder();
					geocoder.getLatLng(address,function(point) {
				  if (!point) {
				    alert(address + " not found");
				  } else {
				    crearMarker (point,address);
				    document.preferencias.latitude.value = point.lat ();
				    document.preferencias.longitude.value = point.lng ();
				    window.onbeforeunload=  function () { return t._('aviso_actualizacion'); };
				  }
				  });
				}
				else
				{
					alert (t._('introduce_ciudad'));
				}
			}
    }
    else 
    {
      // display a warning if the browser was not compatible
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }

   return 0; //Avoid to submit
}

/*INICIA EL MAPA DELS VEINS*/
function iniciarMapaVeins ()
{
	if (GBrowserIsCompatible()) 
	{
		cercle_veins (5);
	}
}


//DIBUIXA EL CERCLE EN EL MAPA
function cercle_veins (radi)
{
	if (!map) return; 
	
	var c1,c2,z;
	switch (radi*1)
	{
		case 5: c1=0;c2=7;z=10; break;
		case 10: c1=7;c2=17;z=9; break;
		case 25: c1=17;c2=38;z=8; break;
		case 50: c1=38;c2=75;z=7; break;
		case 100: c1=75;c2=100;z=6; break;
	}
	
	if (GBrowserIsCompatible()) 
	{
		map.clearOverlays ();
		point = new GLatLng(document.posicio.latitude.value,document.posicio.longitude.value);
		map.setCenter(point,z);
		drawCircle (map.getCenter(),c2,30);
	}
}


/*
ORIGINAL CODE: http://esa.ilmari.googlepages.com/circle.htm
*/
function drawCircle(center, radius, nodes)
{
// Esa 2006
	//calculating km/degree
	var liColor, liWidth, liOpa, fillColor, fillOpa;
	
	
	var latConv = center.distanceFrom(new GLatLng(center.lat()+0.1, center.lng()))/100;
	var lngConv = center.distanceFrom(new GLatLng(center.lat(), center.lng()+0.1))/100;

	//Loop 
	var points = [];
	var c1 = [];
	var step = parseInt(360/nodes)||10;
	for(var i=0; i<=360; i+=step)
	{
	var pint = new GLatLng(center.lat() + (radius/latConv * Math.cos(i * Math.PI/180)), center.lng() + 	(radius/lngConv * Math.sin(i * Math.PI/180)));
	points.push(pint);
	c1.push (pint);
	}
		
	points.push(points[0]); // Closes the circle, thanks Martin
	c1.push(c1[0]);
	
	fillColor = fillColor||liColor||"#0055ff";
	liWidth = liWidth||2;
	var poly = new GPolygon(points,liColor,2,liOpa,fillColor,fillOpa);
	map.addOverlay(poly);
	
	//Draw Circles
	//map.addOverlay(new GPolygon(c1,liColor,2,liOpa,0,0));
	//map.addOverlay(new GPolygon(c2,liColor,2,liOpa,0,0));
	
}



