function create_map(lon,lat){
	
	var map = new GMap2(document.getElementById("map"));
	
	if( (lon != '') && (lat != '') ){
		var situation = new GLatLng(lon,lat);
	}else{
		var situation = new GLatLng(40.40,-3.70);
	}

	map.setCenter(situation, 14);
	var marca = new GMarker(situation,{draggable:true});	
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.addOverlay(marca);
	
	if(document.getElementById("to")){
		var to = document.getElementById("to");
		to.value=lon+","+lat;
	}

	if(document.getElementById('detalle_ruta')){
		document.getElementById('detalle_ruta').style.display = "none"; 	
	}
	
	if(document.getElementById("direcciones")){
		document.getElementById("direcciones").style.display = "none"; 	
	}	
	
	GEvent.addListener(marca, "dragend", function() {
	 	var point = String( marca.getLatLng() );
	 	point = point.replace("(","");
	 	point = point.replace(")","");
		var lonlat= point.split(",");
	
		if(document.getElementById("lon")){
			var lon = document.getElementById("lon");
			lon.value=lonlat[0];
		}
	
		if(document.getElementById("lat")){
			var lat = document.getElementById("lat");
			lat.value=lonlat[1];
		}

 	 });
}

function create_map_with_marks(){
	var map = new GMap2(document.getElementById("map"));
	var center = new GLatLng(40.396764, -3.713379);
	map.setCenter(center, 5);
	
	map.addControl(new GMapTypeControl());
	map.addControl(new GSmallMapControl());
	
	var iconoMarca = new GIcon(G_DEFAULT_ICON);
	iconoMarca.image = "/templates/default/imgs/gv.gif";
	var tamanoIcono = new GSize(23,20);
	iconoMarca.iconSize = tamanoIcono; 
	iconoMarca.iconAnchor = new GPoint(20, 15); 
	
	/*situation = new GLatLng(39.3669119,-3.3549911)
	marca = new GMarker(situation,{draggable:false});
	map.addOverlay(marca);
	GEvent.addListener(marca, "click", function() {
		document.location.href = "#company_18";
		for (i=0; capa = document.getElementsByTagName('a')[i]; i++){
	  		id_capa = capa.id;
	  		partes = id_capa.split("_");
	  		if (partes[0] == "hrefcompany"){
				capa.style.color = "#7FAA34";
			}
	  	}
	  	
		document.getElementById('hrefcompany_18').style.color = "red"; 
	});*/
	
	for (g=0; hidden = document.getElementsByTagName('input')[g]; g++){
		var id_hidden = hidden.id;
		var partes_hid = id_hidden.split("_");
		if (partes_hid[0] == "location"){
		 	var location_values = hidden.value.split("|");
			var situation = new GLatLng(location_values[0],location_values[1])
			var marca = new GMarker(situation,iconoMarca);
			map.addOverlay(marca);
			var idh = partes_hid[1];
			GEvent.addListener(marca, "click", function(loc) {
			 
			 	for (l=0; hidden = document.getElementsByTagName('input')[l]; l++){
					var id_hidden = hidden.id;
					var partes_hid = id_hidden.split("_");
					if (partes_hid[0] == "location"){
						var location_array = hidden.value.split("|");
						var value = "(" + location_array[0] + "," + location_array[1] + ")";
					 	if (value == loc){
							var idh = partes_hid[1];
						}
					} 		
			 	}
			
			 	var nameancla = "#company_" + idh;
			 	var namehref = "hrefcompany_" + idh;
			 	
			 	var detailcompany = "detailemp-" + idh;
			 	var logocompany = "logoemp-" + idh;
			 	var urlcompany = "urlemp-" + idh;
			 	
			 	nombreempresa = document.getElementById(namehref).innerHTML; 
			 	logoempresa = document.getElementById(logocompany).src; 
			 	detalleempresa = document.getElementById(detailcompany).innerHTML; 
			 	urlemp = document.getElementById(urlcompany).value; 
			 	
			 	this.openInfoWindowHtml("<b>"+nombreempresa+"</b><br><br><div style='width:250px;'><img src='"+logoempresa+"' style='margin:5px;'>"+detalleempresa+"</div><br><br><a href='"+urlemp+"'>Ver ficha de esta empresa</a>");
				//document.location.href = nameancla;
				for (i=0; capa = document.getElementsByTagName('a')[i]; i++){
			  		var id_capa = capa.id;
			  		var partes = id_capa.split("_");
			  		if (partes[0] == "hrefcompany"){
						capa.style.color = "#7FAA34";
					}
			  	}
				document.getElementById(namehref).style.color = "#F56413"; 
			});
		}
	}
}


var map;
var gdir;
var geocoder = null;
var addressMarker;

function setDirections() {
	document.getElementById("direcciones").innerHTML="";
	if(document.getElementById('detalle_ruta').style.display == "none"){
		document.getElementById('detalle_ruta').style.display = ""; 	
	}
	if(document.getElementById('direcciones').style.display == "none"){
		document.getElementById('direcciones').style.display = ""; 	
	}
	
	var map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	var gdir = new GDirections(map, document.getElementById("direcciones"));
	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	GEvent.addListener(gdir, "error", handleErrors);
 	var fromAddress = document.getElementById("from").value;
 	var toAddress = document.getElementById("to").value;
	var locale = "es";
	gdir.load("from: " + fromAddress + " to: " + toAddress,
{ "locale": locale });

}

function onGDirectionsLoad(){
}

function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("Dirección no disponible.\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_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 create_map_location(lon,lat,id,address,city,state,country,zip,phone,fax){

	if(id){
		document.getElementById('google_map').style.display="";	
	 	document.getElementById('id_update').value=id;
		document.getElementById('aux_address').value=address;
		document.getElementById('city').value=city;
		document.getElementById('select_state').value=state;
		document.getElementById('id_country').value=country;
		document.getElementById('zip').value=zip;
		document.getElementById('phone').value=phone;
		document.getElementById('fax').value=fax;
	}else{
		document.getElementById('google_map').style.display="";
		document.getElementById('id_update').value="";
		document.getElementById('aux_address').value="";
		document.getElementById('city').value="";
		document.getElementById('id_country').value=195;
		document.getElementById('select_state').value=0;			
	}
	
	var map = new GMap2(document.getElementById("map"));
	
	if( (lon != '') && (lat != '') ){
		var situation = new GLatLng(lon,lat);
	}else{
		var situation = new GLatLng(40.40,-3.70);
	}

	map.setCenter(situation, 14);
	marca = new GMarker(situation,{draggable:true});	
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.addOverlay(marca);

	GEvent.addListener(marca, "dragend", function() {
	 	var point = String( marca.getLatLng() );
	 	point = point.replace("(","");
	 	point = point.replace(")","");
		var lonlat= point.split(",");
	
		var lon = document.getElementById("lon");
		lon.value=lonlat[0];
	
		var lat = document.getElementById("lat");
		lat.value=lonlat[1];

 	 });
}

function getLocation(address){
 	var punto; 
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(address,function(point) {
    	if (!point) {
    		//
      	} else {
      	 	//Poner aquí las marcas en el mapa
      	}
    }
	);
	return punto;
}

function showAddress(address) {
 	var map = new GMap2(document.getElementById("map"));
	
	var geocoder = new GClientGeocoder();
	map.clearOverlays(); 
	geocoder.getLatLng(address,function(point) {
    	if (!point) {
    		alert(address + " not found");
      	} else {
        	map.setCenter(point, 16);
        	var marca = new GMarker(point,{draggable:true});
        	map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
        	map.addOverlay(marca);
        	
        	var point = String( marca.getLatLng() );
		 	point = point.replace("(","");
		 	point = point.replace(")","");
			var lonlat= point.split(",");

			if(document.getElementById("to")){
			 
				var to = document.getElementById("to");

				to.value=lonlat[0]+","+lonlat[1];	

			}

			if(document.getElementById('detalle_ruta')){
				document.getElementById('detalle_ruta').style.display = "none"; 	
			}
			
			if(document.getElementById("direcciones")){
				document.getElementById("direcciones").style.display = "none"; 	
			}
			
			if(document.getElementById("lon")){
				var lon = document.getElementById("lon");
				lon.value=lonlat[0];
			}
		
			if(document.getElementById("lat")){
				var lat = document.getElementById("lat");
				lat.value=lonlat[1];	
			}
			
			GEvent.addListener(marca, "dragend", function() {
			 	var point = String( marca.getLatLng() );
			 	point = point.replace("(","");
			 	point = point.replace(")","");
				var lonlat= point.split(",");
				
				if(document.getElementById("lon")){
					var lon = document.getElementById("lon");
					lon.value=lonlat[0];
				}
			
				if(document.getElementById("lat")){
					var lat = document.getElementById("lat");
					lat.value=lonlat[1];	
				}

		 	 });
      	}
    }
  );
}
