var map = null;
var geocoder = null;
var isLoaded = false;


function load() {
  if (GBrowserIsCompatible() && map == null) {
    map = new GMap2(document.getElementById("map"));
  }
}

var nb_ad = 5;
var ad = ""; var titre = ""; 
var cpt = 0; var dpt = 0;
var boolC = false;
var ads = new Array; var titres = new Array;
var ics = new Array;
var baseIcon = new GIcon(); 
//baseIcon.image = "http://www.nicoweb.fr/imgs/gmaps-localizator/elysee.png";
baseIcon.iconSize = new GSize(16,16);
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 launchAddressGeocoding () {
	
	geocoder = new GClientGeocoder();
	cpt = 0;
	dpt = 0;
	boolC = false;
	$('debug').innerHTML = "";

	if (isLoaded == false) {
		map.setCenter(new GLatLng(0,0), 13);
		isLoaded = true;
	}
	
	for (i=0;i<=nb_ad;i++) {
		$('img_'+i).style.display = "inline";

		ads[cpt] = $F('ad_'+i);
		titres[cpt] = $F('titre_'+i);
		ics[cpt] = $F('icon_'+i);
		
  	if (geocoder) {
      geocoder.getLatLng(ads[cpt], function(point) {
          if (!point) {
            $('debug').innerHTML += ad + " n'a pu être localisée.<br/>";
            dpt++;
          } 
          else {
						if ($F('center_'+dpt) == "true" && boolC == false) {
							$('debug').innerHTML += ads[dpt] + " ("+titres[dpt]+") est défini comme centre.<br/>";
							map.setCenter(point, 13);
							boolC = true;
						}
            map.addOverlay(creerPoint(point,dpt));

						// debug 
						$('debug').innerHTML += ads[dpt] + " ("+titres[dpt]+") a bien été localisée.<br/>";
            $('img_'+dpt).style.display = "none";
            dpt++;
          }
        }
     );
   }
   cpt++;
	}
	return true;
}

/*
	* creerPoint ( GPoint point )
*/
function creerPoint (p,ref) {
	
	// gestion icone personnalisé
	baseIcon.image = ics[ref];
	var opts = { icon : baseIcon };
	var marker = new GMarker(p,opts);
 	GEvent.addListener(marker, "click", function() {
  marker.openInfoWindowHtml(titres[ref]+"<br/> <br/>"+ads[ref]);
  });
  
  
  return marker;
}

/*
	* updateIcon ( int id ) 
*/
function updateIcon (idT) {
	
	var imgSrc = $F('icon_'+idT);
	$('prevIcon_'+idT).src = imgSrc;
	return true;
}