var center_lat = 0;
var center_lng = 0;
var annonces = new Array();

//Construction de l'image des points
var baseIcon = new GIcon();
baseIcon.shadow = "http://www.allianceconstruction.fr/img/map/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"));
    
    // 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">' + txt + '</div>';
        var icon = new GIcon(baseIcon);

        // Cas spéciale pour l'annonce principale
        if(index == 0) {
            icon.image = "http://www.allianceconstruction.fr/img/map/markerA.png";
            map.setCenter(new GLatLng(center_lat, center_lng), 9);
        } else {
            icon.image = "http://www.allianceconstruction.fr/img/map/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;
    }


	// On a pas d'annonce alors on redirige vers la page du département;
	if(annonces.length <= 0) {
		/*window.location.replace(
		 "/pack-terrain-plus-maison/pack-terrain-plus-maison.html");*/
	}
	
    for (var i = 0; i < annonces.length; i++) {
        var point = new GLatLng(parseFloat(annonces[i][0]),
                                parseFloat(annonces[i][1]));

        map.addOverlay(createMarker(point, annonces[i][2], i));
    }
  }
}
