var map = null;
var geocoder = null;
var baseIconG = null;
var baseIconH = null;

function initialize() {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map_canvas"));
	
    map.setCenter(new GLatLng(47.15984, 2.988281), 3);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GOverviewMapControl());
	
	map.enableScrollWheelZoom();
	
	geocoder = new GClientGeocoder();
	

	// Create a base icon for all of our markers that specifies the
	// shadow, icon dimensions, etc.
	baseIconG = new GIcon();
	baseIconG.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIconG.iconSize = new GSize(20, 34);
	baseIconG.shadowSize = new GSize(37, 34);
	baseIconG.iconAnchor = new GPoint(9, 34);
	baseIconG.infoWindowAnchor = new GPoint(9, 2);
	baseIconG.infoShadowAnchor = new GPoint(18, 25);
	
	baseIconH = new GIcon();
	baseIconH.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIconH.iconSize = new GSize(20, 34);
	baseIconH.shadowSize = new GSize(37, 34);
	baseIconH.iconAnchor = new GPoint(9, 34);
	baseIconH.infoWindowAnchor = new GPoint(9, 2);
	baseIconH.infoShadowAnchor = new GPoint(18, 25);
	
	
  }
}

function initializePetite() {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map_canvas"));
	
    map.setCenter(new GLatLng(47.15984, 2.988281), 1);
	//map.addControl(new GLargeMapControl());
	//map.addControl(new GMapTypeControl());
	//map.addControl(new GOverviewMapControl());
	
	//map.enableScrollWheelZoom();
	
	geocoder = new GClientGeocoder();
	

	// Create a base icon for all of our markers that specifies the
	// shadow, icon dimensions, etc.
	baseIconG = new GIcon();
	baseIconG.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIconG.iconSize = new GSize(20, 34);
	baseIconG.shadowSize = new GSize(37, 34);
	baseIconG.iconAnchor = new GPoint(9, 34);
	baseIconG.infoWindowAnchor = new GPoint(9, 2);
	baseIconG.infoShadowAnchor = new GPoint(18, 25);
	
	baseIconH = new GIcon();
	baseIconH.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIconH.iconSize = new GSize(20, 34);
	baseIconH.shadowSize = new GSize(37, 34);
	baseIconH.iconAnchor = new GPoint(9, 34);
	baseIconH.infoWindowAnchor = new GPoint(9, 2);
	baseIconH.infoShadowAnchor = new GPoint(18, 25);
	
	zoomPays();
  }
}


function zoomPays() {

	idCat=document.stages.idPays.selectedIndex;
	//alert(idCat);
	titreCat=document.stages.idPays.options[idCat].text;
	//alert(titreCat);
	trouvePoint(titreCat, 3);
}


function trouvePoint(address, zoom) {
	if (geocoder) {
	geocoder.getLatLng(
	  	address,
	  	function(point) {
			if (!point) {
				map.setCenter(new GLatLng(47.15984, 2.988281), 1);
			}else{
					//alert(point);
				map.setCenter(point, zoom);
			}
		}  
	);}
	
}

function showAddress(titre, address, type, first, zoom) {
//alert("bbb");
  if (geocoder) {
  //alert("aaa");
	geocoder.getLatLng(
	  address,
	  function(point) {
		if (!point) {
		 // alert(address + " not found");
		 	
		} else {
		  
		  if (first==true) {
			map.setCenter(point, 5);
		  }
		  
			switch (type) {
			case 'G':
				//index=6;	
				baseIcon=baseIconG;
				image=CHEMIN_SITEWEB+"images/markerG-trans.png"
				break;
			case 'H':
				//index=7;
				baseIcon=baseIconH;	
				image=CHEMIN_SITEWEB+"images/markerH-trans.png"
				break;
			}


		 // var letter = String.fromCharCode("A".charCodeAt(0) + index); //1=index
		  var letteredIcon = new GIcon(baseIcon);
		//  letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

		letteredIcon.image = image;
		  // Set up our GMarkerOptions object
		  markerOptions = { icon:letteredIcon };
		  //alert(point);
		  var marker = new GMarker(point, markerOptions);
		  
// var marker = new GMarker(point);
		  map.addOverlay(marker);
		 // marker.openInfoWindowHtml(address);
		  
		   GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(titre+"<br />"+address+"<br />texte + lien ici<br />encore<br />encore<br />encore<br />encore<br />...");
			  });
		}
	  }
	)
  }
}

function showAdressPoint(titre, address, type, first, point2) {
 	
	
	
	//alert(point2);
 	//var point = new Array();
 	tabPoint=point2.split(",");
	//for (u=0;u<tabPoint.length;u++) {
		//point.push(tabPoint[u]);
	//}
		 
	switch (type) {
	case 'G':
		//index=6;	
		baseIcon=baseIconG;
		image=CHEMIN_SITEWEB+"images/markerG-trans.png"
		break;
	case 'H':
		//index=7;
		baseIcon=baseIconH;	
		image=CHEMIN_SITEWEB+"images/markerH-trans.png"
		break;
	}

	

 // var letter = String.fromCharCode("A".charCodeAt(0) + index); //1=index
	var letteredIcon = new GIcon(baseIcon);
//  letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

	letteredIcon.image = image;
  // Set up our GMarkerOptions object
  	markerOptions = { icon:letteredIcon };
	//alert(point);
	//point="'"+point+"'";
	var point = new GLatLng(tabPoint[0],tabPoint[1]);
	//alert(point)
  	var marker = new GMarker(point, markerOptions);

  
// var marker = new GMarker(point);
  	map.addOverlay(marker);
 // marker.openInfoWindowHtml(address);
 
	 if (first==true) {
		map.setCenter(point, 12);
	  }
  
   GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(address);
	});
  //}
  
  
  
  
  //
 
  // Add 10 markers to the map at random locations
  //var bounds = map.getBounds();
  //var southWest = bounds.getSouthWest();
  //var northEast = bounds.getNorthEast();
 // var lngSpan = northEast.lng() - southWest.lng();
 // var latSpan = northEast.lat() - southWest.lat();
  //for (var i = 0; i < 1; i++) {
	//  alert(southWest.lat() + latSpan * Math.random());
	 // alert(southWest.lng() + lngSpan * Math.random());

	 // var point = new GLatLng(tabPoint[0],tabPoint[1]);
	 // map.addOverlay(new GMarker(point));

}
