﻿
// Function permettant de créer un marker et d'ajouter du HTML 
function createMarker(point,html,image) {
        var myicon = new GIcon(G_DEFAULT_ICON);
        if (image!=null && image!="") {
            myicon.image = inconPath + image;
        } else {
            myicon.image = inconPath + "marker.png";
        }
        // Set up our GMarkerOptions object
        markerOptions = { icon:myicon };

        var marker = new GMarker(point,markerOptions);
        if (html != null && html.toString() != "" && html.toString() != " ") {
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        }
        return marker;
}

// Affiche un plan google map
function Google_ShowMap(mapid, maplat, maplng, zoom) {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById(mapid));
        map.addControl(new GSmallMapControl());
        //Centrage de la carte
        var center = new GLatLng(maplat, maplng);
        map.setCenter(center, zoom);
        //Marqueur
        var point = new GLatLng(maplat, maplng);
        // Création du markeur
        var marker = createMarker(point, null, null);
        map.addOverlay(marker);
    }
}

// Ajoute une agence sur la carte la carte google Map
function AjouteAgence(map, agence) {
     var point = new GLatLng(agence.latitude, agence.longitude);
      // Création du markeur
      var marker = createMarker(point, agence.GetHtmlBulle(), null);
       map.addOverlay(marker);
}

// Affiche un plan google Map
function ShowGMap(mapid, maplat, maplng, zoom) 
{
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById(mapid));
        map.addControl(new GSmallMapControl());
        //Centrage de la carte
        var center = new GLatLng(maplat, maplng);
        map.setCenter(center, zoom);
        return map;
    }
    return null;
}

// Affiche un plan google map en utilisant un fichier xml
function Google_initialize(mapid) {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById(mapid));
        map.addControl(new GSmallMapControl());
       
        // Lecture du fichier Data.xml
        var request = GXmlHttp.create();
        request.open("GET", inconPath + dataFile, true);
        request.onreadystatechange = function() {
        if (request.readyState == 4) {
          var xmlDoc = GXml.parse(request.responseText);
          // Lecture du noeud map                  
          var maplat = parseFloat(xmlDoc.documentElement.getAttribute("lat"));
          var maplng = parseFloat(xmlDoc.documentElement.getAttribute("lng"));
          var zoom = parseInt(xmlDoc.documentElement.getAttribute("zoom"));
          var center = new GLatLng(maplat,maplng);           
          map.setCenter(center,zoom);
          
          // Lecture des markers
          var markers = xmlDoc.documentElement.getElementsByTagName("marker");                   
          for (var i = 0; i < markers.length; i++) {
            // lecture des attribut
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
            var image = markers[i].getAttribute("image");
            var point = new GLatLng(lat,lng);                       
            var html = GXml.value(markers[i].getElementsByTagName("html")[0]);
                                    
            // Création du markeur
            var marker = createMarker(point,html,image);
            map.addOverlay(marker);
          }          
        }
      }
      request.send(null);
        
      }
  }

  

// Class Agence
function Agence(nom,adresse,ville,codepostal,telephone,latitude ,longitude, url, image) {
     this.nom = nom;
     this.adresse = adresse;
     this.ville = ville;
     this.codepostal = codepostal;
     this.telephone = telephone;
     this.latitude = latitude;
     this.longitude = longitude;     
     this.url = url;
     this.image = image;        	
}

// Function GetHtml de la classe Agence
Agence.prototype.GetHtmlBulle = function()    // Define Method
{
    var html = "<div style=\"margin: 7px;\"><table><tr>";
    if (this.image != null) {
        html += "<td><div class=\"photoagence\"><img  src=\""+ this.image + "\"></div></td>";
    }    
    html += "<td><div class=\"adresse\"><h3>Complétude&nbsp;" + this.nom + "</h3>\n";
    html += this.adresse + "<br />\n";
    html += this.codepostal + "&nbsp;" + this.ville + "<br />\n";
    html += "<b>" + this.telephone + "</b>\n";
    html += "<a class=\"boutonlarge\" href=\"" + this.url + "\">&gt; Accéder à la fiche</a></div></td></tr></table></div>";
    return html;
}
                            
                                