//<![CDATA[
    var map = null;
    
    function load() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        gdir = new GDirections(map, document.getElementById("direcciones"));
        map.setCenter(new GLatLng(41.76692, -2.466024), 8);

		 GEvent.addListener(gdir, "error", handleErrors);
        
         geocoder = new GClientGeocoder();
        
       			map.addMapType(G_PHYSICAL_MAP);
         		map.addControl(new GLargeMapControl());
		        map.addControl(new GMapTypeControl());
		        map.addControl(new GOverviewMapControl());
		        map.addControl(new GScaleControl());
		        
		        map.setMapType(G_PHYSICAL_MAP);
        

// Creamos el icono CASITA
	function consigueicono(imagen){
	var iconcasita = new GIcon();
	iconcasita.image = "../googlemaps/"+imagen+".png";
	iconcasita.shadow = "../googlemaps/"+imagen+"_sombra.png";
	iconcasita.iconSize = new GSize(32, 32);
	iconcasita.shadowSize = new GSize(60, 32);
	iconcasita.iconAnchor = new GPoint(32, 32);
	iconcasita.infoWindowAnchor = new GPoint(5, 1);
	 return iconcasita;
	}

		

	// FUNCION QUE CREA LOS BOCATAS
	function createMarker(point, titulo,subtitulo,foto,idficha,icono,lati,longi) {
	
	  var marker = new GMarker(point,consigueicono(icono));
	  GEvent.addListener(marker, "click", function() {
	  
	  if (foto) {
	  	archivo="<img src='../include/resizeimag.asp?imag="+foto+"&amp;ancho=50&amp;alto=50&formatooriginal=encaja' class='miniaturamapa' />"
	  }else {
	  	archivo=""
	  }
	  
	  if (idficha!=0) {
	  	enlaceficha=" - <a href='?idsec="+idficha+"'>Ver ficha</a>"
	  } else {
	  	enlaceficha=""
	  }
	  
	  
	  marker.openInfoWindowHtml(archivo+"<h1>" + titulo + "</h1><p>" + subtitulo + "</p><p><a href='#' onclick='muevemapa("+lati+","+longi+");return false;'>Zoom aqu&iacute;</a>"+enlaceficha+"</p>");

		  });
	  return marker;
	}  





	 function descargaxml(){       
        var waypoints = [];

        // Descargamos los waypoints desde el fichero data.asp (podr’a ser data.xml)
       
        GDownloadUrl("../googlemaps/data.asp?idfam="+idfamilia, function(data) {
          var xml = GXml.parse(data);
          var markers = xml.documentElement.getElementsByTagName("marker");
	          for (var i = 0; i < markers.length; i++) {
	          
	           
	            var lati=parseFloat(markers[i].getAttribute("lat"));
			    var longi=parseFloat(markers[i].getAttribute("lng"));
				var point = new GLatLng(lati,longi);
	           
	            var titulo = markers[i].getAttribute("titulo");
	             var subtitulo = markers[i].getAttribute("subtitulo");
	             var foto = markers[i].getAttribute("foto");
	            var idficha = markers[i].getAttribute("idf");
	            var icono = markers[i].getAttribute("ico");
	            //map.addOverlay(new GMarker(point));
	            
	            
	  			map.addOverlay(createMarker(point, titulo,subtitulo,foto,idficha,icono,lati,longi));
	  		   
	          }
        });
	}
	











	
    descargaxml();

         
      }
    }

		function muevemapa(lati,longi) {
            map.panTo(new GLatLng(lati,longi));
            map.setZoom(13);
        } 

function setDirections(fromAddress, toAddress, locale) {
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale });
    }


function marcaRuta(fromTo,locale) {
      gdir.load(fromTo);
    }



function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No se ha encontrado una de las ubicaciones geográficas especificadas. Quizá sea una dirección relativamente nueva, o está incorrecta.\nError code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}
	
	

	function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load()
      // results.

      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	}

    
    //]]>
