//<![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(42.003317, -4.527897), 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_NORMAL_MAP);
        

// Creamos el icono CASITA
	function consigueicono(imagen){
	var iconcasita = new GIcon();
	if (imagen.indexOf("casa")==-1) {
		iconcasita.image = "../../imagftp/"+imagen+"";
	} else {
		iconcasita.image = "../googlemaps/"+imagen+".png";
	}
	
	iconcasita.shadow = "../googlemaps/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 (idficha<0) {
	  	idficha=idficha*-1
	  	enlace="idsec="+idficha
	  } else {
	  	enlace="iddoc="+idficha
	  }
	  
	  
	  if (!foto) {
		  		
		  	    marker.openInfoWindowHtml("<h1>" + titulo + "</h1><p>" + subtitulo + "</p><p><a href='#' onclick='muevemapa("+lati+","+longi+");return false;'>Zoom aqu&iacute;</a> - <a href='?"+enlace+"'>Ver ficha</a></p>");
				
	  } else {
	  		    marker.openInfoWindowHtml("<img src='../include/resizeimag.asp?imag="+foto+"&amp;ancho=50&amp;alto=50&formatooriginal=encaja' class='miniaturamapa' /><h1>" + titulo + "</h1><p>" + subtitulo + "</p><p><a href='#' onclick='muevemapa("+lati+","+longi+");return false;'>Zoom aqu&iacute;</a> - <a href='?"+enlace+"'>Ver ficha</a></p>");

	  }
		  });
	  return marker;
	}  





	 function descargaxml(){       
        var waypoints = [];
		var hayfichas=0;
        // Descargamos los waypoints desde el fichero data.asp (podr’a ser data.xml)
       
        GDownloadUrl("../googlemaps/data.asp?idfam="+idfamilia+"&iddoc="+iddoc, function(data) {
          var xml = GXml.parse(data);
          var markers = xml.documentElement.getElementsByTagName("marker");
	      var bounds = new GLatLngBounds();
	      
	          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));
	            
	            if (icono.indexOf("casa")==-1) {
	            	hayfichas=1;
	            }
	            
	  			map.addOverlay(createMarker(point, titulo,subtitulo,foto,idficha,icono,lati,longi));
	  		    bounds.extend(point);
	          }
	          
	          
	          
	          if (hayfichas==1) {
	          map.setMapType(G_NORMAL_MAP);
	          }
	          
	          if (i>1) {
	         	//map.setCenter(new GLatLng(42.342401, -3.701703), 10);
	         	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
	         } else {
	         	map.setCenter(point, 15);
	         	//map.setMapType(G_HYBRID_MAP);
	         }
	          
        });
	}
	











	
    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 handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\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...
	}

    
    //]]>
