  var infowindow;
  var map;
  var image = 'http://labs.google.com/ridefinder/images/mm_20_green.png';
  var ombre = '/images/mm_20_shadow.png';
  var sideBar = document.getElementById("sidebar");
  var geocoder;
  var message;
  var activityMessage = '<img src="/images/activity.gif" width="16" height="16" alt="" align="top" hspace="3" /> Veuillez patienter, recherche en cours...';
  

function getDistribByRadius(location) { 
	var coordonnees = location.toString();
	var coordonnees = Remplace(coordonnees,"(","");
	var coordonnees = Remplace(coordonnees,")","");
	var vecteur = coordonnees.split(",");
    var lat = vecteur[0];
	var lng = vecteur[1];
	var radius = 50;
	var distributeurs = new Array();
	
	//map.clearOverlays();
	document.getElementById("message").innerHTML = activityMessage;

	AjaxRequest.get(
		{
			'url':'distributeurs.ajax.php',
			'parameters': { 'action':'getDistribByRadius', 'lat':lat, 'lng':lng, 'radius': radius },
			'onSuccess':function(req){ 
				distributeurs = eval( '(' + req.responseText + ')' );
				//alert(req.responseText.length);
				var marker;
				//for (var i = 0;i<req.responseText.length;i++) {
				for(var i in distributeurs) { 
					var latitude = distributeurs[i].gmap_latitude;
					var longitude = distributeurs[i].gmap_longitude;
					var point = new google.maps.LatLng(parseFloat(latitude), parseFloat(longitude));
					var marker = new google.maps.Marker({position: point, map: map, icon: image, shadow: ombre, title: distributeurs[i].enseigne});
					infobulle(marker, distributeurs[i]);
				}
				map.setCenter(new google.maps.LatLng(lat, lng));
				map.setZoom(9);
				document.getElementById("message").innerHTML = 'Cliquez sur la carte pour trouver les distributeurs de la zone sélectionnée';
			},
			'onError':function(req){
				message.innerHTML = '';
				alert('Erreur :\nStatusText='+req.statusText+'\nContents='+req.responseText);
			}
		}
	);
}
  
  function infobulle(marker, distributeur) {
  					google.maps.event.addListener(marker, "click", function() {
					if (infowindow) infowindow.close();
					var contact = "<b>"+distributeur.enseigne+"</b><br/>";
					contact += distributeur.adresse_1 + '<br/>';
					if(distributeur.adresse_2) { contact += distributeur.adresse_2 + '<br/>'; }
					contact += distributeur.cp + ' ' + distributeur.ville + '<br/>';
					if(distributeur.nom) { contact += 'Contact : ' + distributeur.civilite + ' ' + distributeur.nom + '<br/>';}
					contact += 'T&eacute;lephone : ' + distributeur.telephone + '<br/>';
					if(distributeur.fax) { contact += 'Fax : ' + distributeur.fax + '<br/>';}
					if(distributeur.www) { contact += '<a target="blank" href="http://'+ distributeur.www + '">'+ distributeur.www +'</a>';}
		
					infowindow = new google.maps.InfoWindow({content: contact});
					infowindow.open(map, marker);
					});
  }
  
  function initialize() {
    var myLatlng = new google.maps.LatLng(46.769167,2.444167);
    var myOptions = {
      zoom: 6,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.TERRAIN,
	  navigationControl: true,
	  scrollwheel: false,
	  navigationControlOptions: {style: google.maps.NavigationControlStyle.ZOOM_PAN}
	  
    }
	
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	google.maps.event.addListener(map, 'click', function(event){ getDistribByRadius(event.latLng); });
	
	function createMarker(point, distributeur) {
	
				//var marker = new google.maps.Marker({position: latlng, map: map, icon: image, shadow: ombre, title: distributeur.enseigne});
				//var sideb = addToSideBar(marker, distributeur.enseigne);
				alert("ok");
				 var marker = new google.maps.Marker({position: point, map: map, icon: image, shadow: ombre, title: distributeur.enseigne});
				google.maps.event.addListener(
					marker,
					"click",
					function() {
						contact = '<div class="contact"><b>' + distributeur.enseigne + '</b><br/>';
						contact += distributeur.adresse_1 + '<br/>';
						if(distributeur.adresse_2) contact += distributeur.adresse_2 + '<br/>';
						contact += distributeur.cp + ' ' + distributeur.ville + '<br/>';
						if(distributeur.nom) contact += 'Contact : ' + distributeur.civilite + ' ' + distributeur.nom + '<br/>';
						contact += 'T&eacute;lephone : ' + distributeur.telephone + '<br/>';
						if(distributeur.fax) contact += 'Fax : ' + distributeur.fax;
						contact += '</div>';
						marker.openInfoWindowHtml(contact);
						
					}
				);
				return marker;
	}
	
  }
  
   function Remplace(expr,a,b) {
      var i=0
      while (i!=-1) {
         i=expr.indexOf(a,i);
         if (i>=0) {
            expr=expr.substring(0,i)+b+expr.substring(i+a.length);
            i+=b.length;
         }
      }
      return expr 
   }


 function createElem(opt_className, opt_html, opt_tagName) {
  var tag = opt_tagName||"div";
  var elem = document.createElement(tag);
  if (opt_html) elem.innerHTML = opt_html;
  if (opt_className) elem.className = opt_className;
  return elem;
}
  

function addToSideBar(point,titre){ 
	  //var titrec = titre.substr(14);
	  var label = createElem("sidebar-entry", titre, "a");
	  var sideBar = document.getElementById("sidebar");
	  label.href = "#";
	  label.style.display = "block";
	  label.onclick = function(){new google.maps.event.trigger(point,'click'); return false};//x-browser
	  label.onfocus = function(){new google.maps.event.trigger(point,'click'); return false};
	  sideBar.appendChild(label);
	  google.maps.event.addListener(point, 'click', function(){label.focus(); return false});
	
	  return point;
}

function clearSideBar(){
  var sideBar = document.getElementById("sidebar");
  while (sideBar.firstChild) {
    sideBar.removeChild(sideBar.firstChild);
  }
}

function recentre() {  
var centre = new google.maps.LatLng(46.769167,2.444167); 
map.setCenter(centre);
map.setZoom(6);
}
