var center_lat = 0;
var center_lng = 0;

//Construction de l'image des points
var baseIcon = new GIcon();
baseIcon.shadow = "http://www.allianceconstruction.fr/img/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);

function load() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    
    // Creates a marker at the given point with the given number label
    function createMarker(point, txt, index) {
        // Construction du texte
        var texte = '<div class="txt_map"><img src="/art/rond-maison.gif" alt="Alliance Construction Bois" /> ' + txt + '</div>';
        var icon = new GIcon(baseIcon);

        // Cas spéciale pour l'annonce principale
        if(index == 0) {
            icon.image = "http://www.allianceconstruction.fr/img/markerA.png";
            map.setCenter(new GLatLng(center_lat, center_lng), 10);
        } else {
            icon.image = "http://www.allianceconstruction.fr/img/marker.png";
        }
        
        // Création du marker avec le point et l'icone
        var marker = new GMarker(point, icon);
        
        // Fonction de gestion du click sur le point
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(texte);
        });
        // Cas spéciale pour l'annonce principale, on l'affiche par défaut
        if(index == 0) {
            map.addOverlay(marker);
            marker.openInfoWindowHtml(texte);
        }
        return marker;
    }
    // Parse du fichiers xml pour les coordonnées
    GDownloadUrl("/xml/googlemap.php?lat="+center_lat+"&lng="+center_lng, function(data) {
        
        var xml = GXml.parse(data);
        markers = xml.documentElement.getElementsByTagName("marker");
        //center_lat = parseFloat(markers[0].getAttribute("lat"));
        //center_lng = parseFloat(markers[0].getAttribute("lng"));
        
        for (var i = 0; i < markers.length; i++) {
            var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                    parseFloat(markers[i].getAttribute("lng")));
            if (markers[i].childNodes[0].textContent) {
                txt = markers[i].childNodes[0].textContent;
            } else {
                txt = markers[i].childNodes[0].text;
            }
            /*
            txt = markers[i].getAttribute('txt');*/
            map.addOverlay(createMarker(point, txt, i));
        }
    });
  }
}
