
  function p_lock() {
        can_update = 0;
        document.getElementById("lock").innerHTML = i18n_lock;
        if (document.getElementById("address_lock"))
        document.getElementById("address_lock").innerHTML = "<a href='#' onclick='return p_unlock()'><img class='adopt_icon' src='/images/lock_locked.gif'/></a>";
  }
  
  function p_unlock() {
        can_update = 1;
        document.getElementById("lock").innerHTML = i18n_unlock;
        if (document.getElementById("address_lock"))
        document.getElementById("address_lock").innerHTML = "";
        addressChanged();
        return false;
  }
  
  var myindex = 0;
  var mymap;
  var theMarkers = {};      
  var can_update = 1;
  
  var i18n_msg_undo;
  var i18n_msg_redo;
  var i18n_lock;
  var i18n_unlock;  
  
  function setI18nMessagesLock(i_lock, i_unlock, i_undo, i_redo) {
    i18n_msg_undo = i_undo;
    i18n_msg_redo = i_redo;      
    i18n_lock = i_lock;
    i18n_unlock = i_unlock;
  }  
  
  
  function p_setUndo(redo) {
    if(!redo)
        str = i18n_msg_undo;
    else
        str = i18n_msg_redo;
    if (p_last_undo_marker)
      document.getElementById("undo").innerHTML = "<a href=# onclick='p_undo()'>"+str+"</a>";
  }
  
  function p_unsetUndo() {
    document.getElementById("undo").innerHTML = "";
    undo = 1;
  }
  
  var redo = 0;
  var p_last_undo_marker;
  function p_undo() {
    redo = (redo) ? 0 : 1;
    var last_marker = p_last_undo_marker;
    last_marker.place_creator.icon = "/images/markers/markerPI.gif";
    document.getElementById("preview_flag").style.visibility = "hidden";
    p_updateMarkerLatLon(last_marker, last_lat,  last_lng,  last_marker.place_creator.innerhtml, true);
    p_setUndo(redo);
  }
  
  function p_can_update() {
        return can_update;
  }
  
    function searchFromHere() {
      var bound = mapstraction.getBounds();
      var thelocation = document.location.href;
      thelocation = thelocation.replace(/\-.*$/, '');
      if (thelocation.match(/\/$/))
          thelocation = thelocation.substr(0, -1);
      thelocation = thelocation.replace(/([\?\&]?)from=[^\&]*/, '');
      thelocation = thelocation.replace(/([\?\&]?)map_zoom=[^\&]+/, '');
      thelocation = thelocation.replace(/([\?\&]?)zoom=[^\&]+/, '');
      thelocation = thelocation.replace(/([\?\&]?)r=\d+/, '');
      thelocation = thelocation.replace(/([\?\&]?)sort=\w+/, '');
      thelocation = thelocation.replace(/([\?\&]?)page=\d+/, '');
      if (!thelocation.match(/\?/)) {
            if (thelocation.match(/\&/))
                  thelocation = thelocation.replace(/\&/, '?');
            else
                  thelocation += "?";
      }
      if (!thelocation.match(/[\?\&]q=/))
            thelocation += "&q=";
      if (!thelocation.match(/search/)) {
            thelocation = thelocation.replace(/\?/, "/search?");
      }
      var from = bound.ne.lat+","+bound.ne.lng+";"+bound.sw.lat+","+bound.sw.lng;
      thelocation += "&from=@"+from+"&sort=p";
      thelocation = thelocation.replace(/\?\&+/g, '?');
      thelocation = thelocation.replace(/\&+/g, '&');
      document.location.href = thelocation;
      return ;
    }

    function zoomIn() {
      if (mapstraction.getZoom() == 17)
        return ;
      mapstraction.setZoom(mapstraction.getZoom()+1);  
    }
  
    function zoomStreet() {
      mapstraction.setZoom(17);
    }
    
    function zoomOut() {
      if (mapstraction.getZoom() == 1)
        return ;
      mapstraction.setZoom(mapstraction.getZoom()-1);  
    }
     
    // Ouvre la bulle passé en parametre par le stripped_name
    function openABubble(i) {
      theMarkers[i].openBubble();
    }
      
    var p_last_marker;
    function p_getLastMarker(){
        return p_last_marker;
    }

    function p_changeIconMarker(icon, open_bubble, marker) {
        if (!marker)
            marker = p_getLastMarker();
            
        marker.place_creator.icon = icon;
        marker.place_creator.lat = marker.location.lat;
        marker.place_creator.lng = marker.location.lng;
        
        marker.setIcon(icon, [25, 28], [4, 26]);
        p_updateMarkerLatLon(marker, marker.location.lat, marker.location.lng, marker.place_creator.innerhtml, open_bubble);
    }

    function p_addMarker(a_place, myindex, is_draggable, drag_callback) {
        
        if (! drag_callback) {
            if (a_place.drag_callback) {
                drag_callback = a_place.drag_callback;
            }else {
                drag_callback = function(a) {};
            }
        }
      //used by undo
      cur_lat = a_place.lat;
      cur_lng = a_place.lng; 
 
      var myPoint = new LatLonPoint(a_place.lat, a_place.lng);
      var myMarker = new Marker(myPoint);
      myMarker.place_creator = a_place;
      
      a_place.drag_callback = drag_callback;
      myMarker.setDraggable(is_draggable, drag_callback);
      myMarker.setInfoBubble(a_place.innerhtml);
    
      myMarker.addMyEventhandler(a_place.eventhandler);
    
      // Gestion des lettres associées au marker
      var letter = String.fromCharCode("A".charCodeAt(0) + myindex);
      // Icone, Taille de l'icone, Point d'attache de l'icone
      if (! a_place.icon) {
        a_place.icon =  "/images/markers/marker" + letter + ".gif";
      }
      
      // NEWLOGO : myMarker.setIcon( a_place.icon , [25, 36], [4, 36]);
      myMarker.setIcon( a_place.icon , [25, 28], [4, 26]);      
  
      // Cela sert à retrouver le bon marker
      theMarkers[a_place.name_stripped] = myMarker;
  
      mapstraction.addMarker(myMarker);
      
      p_last_marker = myMarker;
      
      return myMarker;
    }
    
    var last_lat = 0;
    var last_lng = 0;
    var cur_lat = 0;
    var cur_lng = 0;
    

    // Met à jour les markers sur la carte  
    function p_updateMarkerLatLon(marker, lat, lng, my_innerhtml, has_bubble) {
      // On enleve l'ancien
      
      //mapstraction.removeMarker(marker);
      mapstraction.removeAllMarkers();
      
      last_lat = cur_lat;
      last_lng = cur_lng;     
      if (last_lat || last_lng)
        p_setUndo();
      
      // On créé le nouveau
      marker.place_creator.lat = lat;
      marker.place_creator.lng = lng;
      
      marker =  p_addMarker(marker.place_creator, 0, true);
      if (has_bubble)
        marker.openBubble();
      
    }

    // Positionne les places sur la carte
    function p_setPlaces(the_places) {
      var id;
        for (id in the_places) if (id && (the_places[id].lng || the_places[id].lat)) {
        // Ajoute le marker à la carte
        if ( typeof p_draggable == "undefined")
          p_draggable = false;
        p_addMarker(the_places[id], myindex, p_draggable);
        // Permet de savoir où l'on se trouve dans la liste des lieux
        myindex = myindex + 1;
      }
    }
     
    // Ouvre l'info html sur le premier lieu
    function p_openBubble(a_place) {
        if (theMarkers[a_place])
            theMarkers[a_place].openBubble();
    }
      
    // On zoome ET centre sur tous les markers de la carte
    function p_autoCenterAndZoom() {
      mapstraction.autoCenterAndZoom();
    }

var mapstraction;
var p_map_raw_controls = 0;

      function p_map_load() {
            document.getElementById("mymap").onclick = null;
            document.getElementById('mymap').innerHTML = "";
            if (document.getElementById('map_search'))
            document.getElementById('map_search').style.visibility = "visible";
            if (document.getElementById('map_tools'))
            document.getElementById('map_tools').style.visibility = "visible";

         // Connect the 'simplemap' division to a Googlemap,using Mapstraction
         mapstraction = new Mapstraction('mymap','google');
      
        if (p_map_raw_controls)  
          mapstraction.addControls({"pan": false, "zoom": false, "overview": false, "scale": false, "map_type": false });       
        else
         mapstraction.addControls({"pan": true, "zoom": 'large', "overview": false, "scale": false, "map_type": true });
         mapstraction.addMapTypeControls();
//         mapstraction.addSmallControls();


         // Nécessaire car il n'est pas possible d'ajouter un lieu si un zoom / center n'est pas défini
         
         // A zoom constant, on centre sur la recherche si elle existe
         mapstraction.setCenterAndZoom(new LatLonPoint($geoloc_lat,$geoloc_lng), 10);
         
          // On positionne les lieux

          if (places && (typeof ( nb_places ) == "undefined" || nb_places)) {
            p_setPlaces(places);
    
            // On positionne met en valeur le premier lieu
            for (id in places) if (id && (places[id].lng || places[id].lat)) {
              myclick([places[id].name_stripped]);
              break;
            }
            // On ouvre la premiere bulle
            p_openBubble();
          
            p_autoCenterAndZoom();

          }
    
          load_bis();
      //    zoomOut();
            return false;
      }

  function p_clickListener(f) {
      mapstraction.addEventListener('click', f);
  }

  var currentSelected = "";

  function myclick(i) {
    // Ouvre la bubble sur la carte
    openABubble(i);

    // Retirer la mise en valeur celui qui était déjà selectionné
    if (currentSelected && document.getElementById(currentSelected))
    {
      document.getElementById(currentSelected).className = 'bloc_lieux';
    }
    // Met en valeur dans la liste des lieux, celui qui a été selectionné
    if (document.getElementById(i))
      document.getElementById(i).className ='bloc_lieux bloc_lieux_selected';
    currentSelected = i;

    if(slowmap)
      clearTimeout(slowmap);
    slowmap = 0;
  
    return false;
  }

  function p_map_load_static() {
      document.getElementById('mymap').innerHTML = "";
      document.getElementById("mymap").onclick = function() {alert(static_click_msg);};
      width = document.getElementById('mymap').style.width.replace(/px/, '');
      height = document.getElementById('mymap').style.height.replace(/px/, '');
      html = '<img width='+width+' id="static_map" height='+height+' src="http://maps.google.com/staticmap?markers=';
      cpt = 0;
      pointers = "";
      for (id in places) if (id && (places[id].lng || places[id].lat)) {
            pointers += places[id].lat + ',' + places[id].lng + ',red'+String.fromCharCode("a".charCodeAt(0) + cpt)+'%7C';
            cpt++;
      }
      if (!pointers)
            pointers = "&center="+$geoloc_lat+","+$geoloc_lng+"&zoom=9";
      html += pointers+'&size='+width+'x'+height+'&key='+map_key+'"/>';
      document.getElementById('mymap').innerHTML = html;
      if (document.getElementById('map_search'))
            document.getElementById('map_search').style.visibility = "hidden";
      if (document.getElementById('map_tools'))
            document.getElementById('map_tools').style.visibility = "hidden";
      tags = document.getElementsByTagName('li');
      for (id in tags) {
            li = tags[id];
            if (li.className == "map_link")
                  li.innerHTML = "";
      }
            return false;
  }
can_static = typeof(map_key) != "undefined";
var slowmap = 0;

if (can_static && staticMap == '1') {
 window.onload = p_map_load_static;
}else{
 if(typeof(stopDynMap) != "undefined")
      slowmap = setTimeout(stopDynMap, 10000);
 window.onload = p_map_load;
 window.onunload = GUnload;
}
