// JavaScript Document

  var geocoder;
  var map;
  var modo;

	//variable para controlar los errores
	//la primera vez, se calculará el mapa con la dirección postal completa
   modo = 'direccion';


     // On page load, call this function

   function load(address)
   {
   	  
 
      // Create new map object
      map = new GMap2($('#map').get(0),{size: new GSize (470,300)});

      // Create new geocoding object
      geocoder = new GClientGeocoder();

      // Retrieve location information, pass it to addToMap()
      geocoder.getLocations(address, comprobarError);
	  
	  //getLocations devuelve un objeto con la propiedad status que contiene un código de error
	  //si el código es G_GEO_SUCCESS, la dirección es correcta
	  //si no, se puede intentar volver a llamar a getLocations solo con el código y la provincia
	  //y si vuelve a ser erróneo, quitamos el mapa definitivamente
	  
	  }
	  
	  
	//funcion para comprobar errores
	function comprobarError(response) 
	{
	 
	switch(modo) 
	
	{
		
	case "direccion":
	 
		  if (response.Status.code == G_GEO_SUCCESS) {
			  //addToMap(response);
			  addToMap(response);
		  }
		  
		  else 
		  
		  {
			//cambiamos modo a ciudad
			modo = 'ciudad';
			var address = $('#cp_txt').text()+' '+$('#provincia_txt').text();
			geocoder.getLocations(address, comprobarError);
		
		  }
    break;
	
	case "ciudad":
	
			
		  if (response.Status.code == G_GEO_SUCCESS) {
			  //addToMap(response);
			  addToMap(response);
		  }
		  
		  else 
		  
		  {
			//cambiamos modo a provincia
			modo = 'pais';
			var address = $('#provincia_txt').text();
			geocoder.getLocations(address, comprobarError);
		
		  }
	
	break;
	
	case "pais":
	
		  if (response.Status.code == G_GEO_SUCCESS) {
			  //addToMap(response);
			  addToMap(response);
		  }
		  
		  else {
			  
			var address = 'España';
			geocoder.getLocations(address);
			  
		  }
		  
	break;
	
	
	}
	
	}

   // This function adds the point to the map

   function addToMap(response)
   {
   
   try {
      // Retrieve the object
      place = response.Placemark[0];
	 
      // Retrieve the latitude and longitude
      point = new GLatLng(place.Point.coordinates[1],
                          place.Point.coordinates[0]);

      // Center the map on this point
      map.setCenter(point, 15);
	  map.addControl(new GMapTypeControl());
      map.addControl(new GSmallZoomControl());
      map.addControl(new GOverviewMapControl());
 
      // Create a marker
      marker = new GMarker(point);

      // Add the marker to map
      map.addOverlay(marker);

      //Add address information to marker
	  var nombre = $('#nempresa').text();
	  //var address = $('#direccion_txt').text()+'<br>'+$('#cp_txt').text()+'<br>'+$('#provincia_txt').text();
	  marker.openInfoWindowHtml('<div style="width:200px; overflow:hidden"><b>'+nombre+'</b></div>');
	 
	 
	  }
	  
	  
       catch(err) {
	   
	   //para que la página llegue hasta la parte inferior de la ventana
       $('#google_map_cont').css('display','none');
 	   
     }
	  
	  
   }


   if ($('h1#cont_tit_empresa').text() != '') {

   var address = $('#direccion_txt').text()+' '+$('#cp_txt').text()+' '+$('#provincia_txt').text();
  
	load(address);
	
   } 
 
