  var geocoder;
  var map;
  var idsArr= Array();
  var locations = Array();
  var infoWindows = Array();
  var geoCodes = Array();
  var  markers= Array();
  var places = Array();
  var URL = "";
  var json;
  var gZoom = 5;
  
  function initialize(zoom) {
    geocoder = new google.maps.Geocoder();
	//URL = url;
    var latlng = new google.maps.LatLng(40.463667,-3.7492);
    var myOptions = {
      zoom: zoom,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
		  places[0] = "41.350558,1.721764";
		  places[1] = " 40.589694,-3.582101";
		   places[2] = "43.29042,-8.254917";
		  places[10] = "<b>BARCELONA, M3 CARAVANING:</b>Ctra. N-340 Vilafranca-Barcelona, km. 1214,1 Vilafranca del Pnedés 08720";
		  places[11] = " Ctra. de Burgos, Km. 24 28700 San Sebastián de los Reyes, Madrid, Spain";
		   places[12] = " Ctra. N-VI, km 578, Raxas n°171, 15165 Bergondo, Tel. 981780770";

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	
	codeAddressAll();
	gZoom = zoom;
//geoCodes = new Array();
	//	markers = new Array();
  }

  function codeAddress(id) 
  {	
		
		geoCodes = new Array();
		markers = new Array();
		
		for(var i=0; i < places.length && i<10; i++)
		{
			address = places[i];
			ind =i;
			idsArr[i] = 0;
			locations[i] = places[i];
		  		geocoder.geocode( { 'address': address}, function(results, status) {
				geoCodes[geoCodes.length] = results[0].geometry.location;
					
				attachSecretMessage();
		  });
		    
		 }
	}
	
	var lll=0;

function codeAddressAll()
{	
			address = places[lll];
			ind =lll;
			idsArr[lll] = 0;
			locations[lll] = places[lll];
		  		geocoder.geocode( { 'address': address}, function(results, status) {
				if(status=="OK")geoCodes[lll] = results[0].geometry.location;
				if(lll<places.length)
				{
					lll++;
					setTimeout( "codeAddressAll()", 100);
				}else
				{
					for(var m=0; m < geoCodes.length; m++)
					{
						attachSecretMessageAll(m);
					}
					geoCodes = new Array();
						markers = new Array();
						
						lll=0;
				}
		  });
}

 function attachSecretMessageAll(l) 
  {	
  		var j = l;
  		var image = "web_images/map_icon.png";
		
		map.setCenter(geoCodes[l]);
		//if(json[j] == null){ return; }
		var marker= new google.maps.Marker(
		 {
			map: map, 
			position: geoCodes[j],
			icon: image
		});
		var latlng = new google.maps.LatLng(40.463667,-3.7492);
		map.setCenter(latlng,gZoom);
		  map.setZoom(gZoom);
		
		
		
		
		//var link = document.getElementById("empresa");
		//marker.setTitle(json[j]["nombre"]);
		var infowindow = new google.maps.InfoWindow({
			content: "<span style='color:#000'>"+places[l+10]+"</span>",
			size: new google.maps.Size(150, 50)
		});
		
		
		infoWindows[j] = infowindow;
		
		google.maps.event.addListener(marker, 'click', function(){
			
			for(var i=0; i < infoWindows.length; i++)
			{
				infoWindows[i].close(map,marker );
			}
			
			//setInfo(j);
			infowindow.open(map,marker );
		});
		
}

function setInfo(index)
{
	$('info_distribuidor').innerHTML = "<b>"+json[index]["nombre"]+"</b><br>"+json[index]["direccion"]+"<br>"+json[index]["cp"]+" "+json[index]["poblacion"]+"<br>" +json[index]["mon_provincia"]+"<br>";
	$('info_distribuidor').innerHTML += json[index]["telefono"]+"<br><a href='"+json[index]["web"]+"'>"+json[index]["email"]+"</a><a href='mailto:"+json[index]["email"]+"'>"+json[index]["web"]+"</a><br>";
}

function getDistribuidores(selectDOM)
{
	var idProvincia = selectDOM.options[selectDOM.selectedIndex].value;
	places = new Array();
	$('distribuidores_provincia').innerHTML = "";
	new Ajax.Request('get_distribuidores.php?id='+idProvincia, 
	{
		onComplete : function(response)
		{
			json =  response.responseText.evalJSON();
			for(var i=0; i < json.length; i++)
			{
				places[i] = json[i]["direccion"]+", "+json[i]["poblacion"]+","+json[i]["mon_provincia"]+", Spain";
				$('distribuidores_provincia').innerHTML += "<span id='empresa"+json[i]["id"]+"'>"+json[i]["nombre"]+"</span><br>";
			};
			
			places[places.length] = json[0]["nom_pais"];
			
			if(places.length)
			{
				initialize(6);
				codeAddressAll(0);
			}
		}
	});
}


