//<![CDATA[

    if (GBrowserIsCompatible()) {
      // Variable global pour l'objet GClientGeocoder qui traduit une adresse en longitude,latitude
      var geocoder;
      var side_bar_html = "";
      var gmarkers = new Array();
      var htmls = new Array();
      // arrays to hold variants of the info window html with get direction forms open
      var to_htmls = new Array();
      var from_htmls = new Array();


      // A function to create the marker and set up the event window
      function createMarker(point,name,html) {
        var marker = new GMarker(point);

        var i = gmarkers.length;

        // The info window version with the "to here" form open
        to_htmls[i] = html + '<br \/><br \/>Itinéraire: <b>Vers ici<\/b> - <a href="javascript:fromhere(' + i + ')">A partir d\'ici<\/a>' +
           '<br \/>Adresse de départ:<form action="javascript:getDirections()" class="formIti">' +
           '<input type="text" SIZE=24 MAXLENGTH=60 name="saddr" id="saddr" value="" /><br \/>' +
           '<INPUT value="Recherche&nbsp;d\'itinéraire" TYPE="SUBMIT" class="boutonIti"><br \/>' +
           '<span class="floatl">A pieds &nbsp;</span><input type="checkbox" name="walk" id="walk" class="floatl" /><span class="floatl">Eviter les autoroutes &nbsp;</span><input type="checkbox" name="highways" id="highways" class="floatl" />' +
           '<input type="hidden" id="daddr" value="'+name+"@"+ point.lat() + ',' + point.lng() + 
           '"/><\/p>';
        // The info window version with the "from here" form open
        from_htmls[i] = html + '<br \/><br \/>Itinéraire: <a href="javascript:tohere(' + i + ')">Vers ici<\/a> - <b>A partir d\'ici<\/b>' +
           '<br \/>Adresse d\'arrivée:<form action="javascript:getDirections()" class="formIti">' +
           '<input type="text" SIZE=24 MAXLENGTH=60 name="daddr" id="daddr" value="" /><br \/>' +
           '<INPUT value="Recherche&nbsp;d\'itinéraire" TYPE="SUBMIT" class="boutonIti"><br \/>' +
           '<span class="floatl">A pieds &nbsp;</span><input type="checkbox" name="walk" id="walk" class="floatl" /><span class="floatl">Eviter les autoroutes &nbsp;</span><input type="checkbox" name="highways" id="highways" class="floatl" />' +
           '<input type="hidden" id="saddr" value="'+name+"@"+ point.lat() + ',' + point.lng() +
           '"/><\/p>';
        // The inactive version of the direction info
        html = html + '<br \/><br \/>Itinéraire: <a href="javascript:tohere('+i+')">Vers ici<\/a> - <a href="javascript:fromhere('+i+')">A partir d\'ici<\/a><\/p>';

        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
		  $('.gmnoprint .gmnoprint img[src$="iw_close.gif"]').attr({ src: "includes/images/googlemap/closegmap.png"}).height(13);
        });
        // save the info we need to use later for the side_bar
        gmarkers.push(marker);
        htmls[i] = html;
        // add a line to the side_bar html
        if (navigator.appName != 'Microsoft Internet Explorer')
		{
        	side_bar_html += '<a href="javascript:myclick(' + i + ')"><img src="http://maps.gstatic.com/intl/fr_ALL/mapfiles/marker.png" alt="Magasin" width="20" height="34" />' + name + '<\/a> ';
		} else {
			side_bar_html += '<a href="javascript:myclick(' + i + ')"><img src="http://maps.gstatic.com/intl/fr_ALL/mapfiles/markerie.gif" alt="Magasin" width="20" height="34" />' + name + '<\/a> ';
		}
        return marker;
      };

      // ===== request the directions =====
      function getDirections() {
        // ==== Set up the walk and avoid highways options ====
        var opts = {};
        if (document.getElementById("walk").checked) {
           opts.travelMode = G_TRAVEL_MODE_WALKING;
        };
        if (document.getElementById("highways").checked) {
           opts.avoidHighways = true;
        };
        // ==== set the start and end locations ====
        var saddr = document.getElementById("saddr").value
        var daddr = document.getElementById("daddr").value
        gdir.load("from: "+saddr+" to: "+daddr, opts);
      };


      // This function picks up the click and opens the corresponding info window
      function myclick(i) {
        gmarkers[i].openInfoWindowHtml(htmls[i]);
		$('.gmnoprint .gmnoprint img[src$="iw_close.gif"]').attr({ src: "includes/images/googlemap/closegmap.png"}).height(13);
      };

      // functions that open the directions forms
      function tohere(i) {
        gmarkers[i].openInfoWindowHtml(to_htmls[i]);
		$('.formIti').jqTransform({imgPath:'includes/js/jqtransformplugin/img/'});
      };
      function fromhere(i) {
        gmarkers[i].openInfoWindowHtml(from_htmls[i]);
		$('.formIti').jqTransform({imgPath:'includes/js/jqtransformplugin/img/'});
      };


      // create the map
      var divMap = document.getElementById("map_google");
      var map = new GMap2(divMap);
      geocoder = new GClientGeocoder();
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng(43.58716,0.41337),10);

      // === create a GDirections Object ===
      var gdir=new GDirections(map, document.getElementById("directions"));

      // === Array for decoding the failure codes ===
      var reasons=new Array();
			reasons[G_GEO_SUCCESS]            = "OK";
			reasons[G_GEO_MISSING_ADDRESS]    = "Adresse manquante.";
			reasons[G_GEO_UNKNOWN_ADDRESS]    = "Adresse inconnue.";
			reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Adresse non disponible.";
			reasons[G_GEO_BAD_KEY]            = "Clé API invalide.";
			reasons[G_GEO_TOO_MANY_QUERIES]   = "Trop de demandes, le quota du site a été dépassé.";
			reasons[G_GEO_SERVER_ERROR]       = "Erreur du serveur.";
			reasons[G_GEO_BAD_REQUEST]        = "La requête d'itinéraire ne peut pas être parsée.";
			reasons[G_GEO_MISSING_QUERY]      = "Le champ est vide.";
			reasons[G_GEO_UNKNOWN_DIRECTIONS] = "L'itinéraire ne peut pas être tracé entre ces deux points.";

      // === catch Directions errors ===
      GEvent.addListener(gdir, "error", function() {
        var code = gdir.getStatus().code;
        var reason="Code "+code;
        if (reasons[code]) {
          reason = reasons[code]
        } 

        alert("Echec de l'itinéraire, "+reason);
      });



      // Read the data from marqueurs.xml
      var request = GXmlHttp.create();
      request.open("GET", "includes/xml/marqueurs.xml", true);
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          var xmlDoc = GXml.parse(request.responseText);
          // obtain the array of markers and loop through it
          var markers = xmlDoc.documentElement.getElementsByTagName("marker");
          
          for (var i = 0; i < markers.length; i++) {
            // obtain the attribues of each marker
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
            var point = new GLatLng(lat,lng);
            var html = "<p class='texteGoogle'><img src='includes/images/googlemap/LogoGoogleMap.jpg' alt='Henri Primeurs' />" + markers[i].getAttribute("html");
            var label = markers[i].getAttribute("label");
            // create the marker
            var marker = createMarker(point,label,html);
            map.addOverlay(marker);
          }
          // put the assembled side_bar_html contents into the side_bar div
          document.getElementById("side_bar").innerHTML = side_bar_html;
        }
      }
      request.send(null);
    }

    else {
      alert("Votre navigateur ne permet pas l\'affichage de carte Google Maps.");
    }

// A la fermeture de la page on libère la mémoire allouée à la carte
window.onunload=GUnload;
//]]>
