 var bounds=[];
 var tfo_markerCollection=[];


function tfo_getMarkerIndex(marker, markerCollection){
	for (var i=0; i<markerCollection.length; i++) {
		if(markerCollection[i]['GMARKER']==marker){
			return i;
		}
	}
}


function tfo_getMarkerIndexByIdTFO(idT, markerCollection){
	for (var i=0; i<markerCollection.length; i++) {
		if(markerCollection[i]['IDT']==idT){
			return i;
		}
	}
}


/**
 * remplir la carto avec des marqueurs
 * @todo recalculer le centrage et le niveau de zoom apres ajout des marqueurs
 * @todo exclure les marqueurs dont lat et long sont fantaisistes (ex. 0/0)
 */ 

function tfo_populate(map, jsonPoints, nomCarte){
	var markerCollection = new Array();
	map.clearOverlays();	
	bounds[nomCarte] = new GLatLngBounds();
	var nb = jsonPoints.length;
	
	var marqueursErreur = new Array();
	for (var i = 0; i < nb; i++) {
		if(jsonPoints[i]==null){
			//alert('ce marqueur est null' + i);
			marqueursErreur.push(i);
		}else{
			//alert('marqueur ' + jsonPoints[i].lat + ' ' + jsonPoints[i].lon);
			var latlng = new GLatLng(jsonPoints[i].lat, jsonPoints[i].lon);
			if(jsonPoints[i].lat==0 || jsonPoints[i].long==0){
				// on fait rien
				//alert(i);
				marqueursErreur.push(i);
			}else{
				var icon = new GIcon();
				  
				icon.shadowSize = new GSize(40, 33);
				icon.iconAnchor = new GPoint(6, 20);
				icon.infoWindowAnchor = new GPoint(5, 1);
				icon.shadow = tfo_const['cheminAssets']+'/gmapMarker/' + 'shadow.png';
				
				if(tfo_const['typeNumerotation']=='alphabetique'){
					var index = jsonPoints[i].index+'';
					index = index.toUpperCase();
					icon.iconSize = new GSize(20, 35);
					
				  	//icon.image = tfo_const['gmapImgMarker'] + tfo_const['gmapMarkerColor'] + '_Marker' + index + '.png';
					//alert(tfo_const['gmapMarkerColor']);
					icon.image = tfo_const['gmapImgMarker'](tfo_const['gmapMarkerColor'], index);
					var marker = new GMarker(latlng, {title:jsonPoints[i].tit, icon:icon});
				}else{
					icon.iconSize = new GSize(20, 36);
					var index = jsonPoints[i].index+'';
					icon.image = tfo_const['gmapImgMarkerNum'](index);
					var marker = new GMarker(latlng, {title:jsonPoints[i].tit, icon:icon});
					
				}
				map.addOverlay(marker);
				var tmp = new Array();
				tmp['IDT'] = jsonPoints[i].idT;
				tmp['GMARKER'] = marker;
				markerCollection.push(tmp);

				bounds[nomCarte].extend(latlng);
			}
		}
	}


	// gestion des erreurs sur les marqueurs
	var nbNull = marqueursErreur.length;
	if(nbNull>0){
		var str = '';		
		for (var i = 0; i < nbNull; i++) {
			str += marqueursErreur[i] + ', ';
		}
		//alert('les marqueurs suivants sont errones : ' + str);
	}
	//alert(bounds[nomCarte]);
	
	return markerCollection;
}

// demarrage de la carto, lorsque tout est charge
function tfoCarto_Initialize(nomCarte, tfo_jsonPoints) {
	//alert(nomCarte);
	//alert(tfo_jsonPoints);
	var divCarto = document.getElementById("tfoConteneurCartoGMap"+nomCarte);
	if( tfo_jsonPoints==null){
		//ne fait rien
	}
	else
	{
		if(divCarto==null){
			alert(tfo_trads['JsErreurCarto']);		
		}else{
			
			divCarto.style.zIndex = 1000;
			divCarto.style.whiteSpace = 'normal';
			divCarto.style.overflow = 'hidden';// pour eviter que les 'Terms of use' debordent
			var map = new google.maps.Map2(divCarto);
			
			map.setCenter(new google.maps.LatLng(tfo_const['gmapX'+nomCarte], tfo_const['gmapY'+nomCarte]), tfo_const['gmapZoom'+nomCarte]);
			map.addControl(new GSmallMapControl());
			map.addControl(new google.maps.MenuMapTypeControl({useShortNames:true}));
			divCarto.parentNode.style.display = 'none';
			
			if(tfo_jsonPoints!==null){
				tfo_markerCollection[nomCarte] = tfo_populate(map, tfo_jsonPoints, nomCarte);
				//alert(tfo_jsonPoints[0]['idT']);
				if(tfo_markerCollection[nomCarte].length>0){
					divCarto.parentNode.style.display = 'block';
				}else{
					divCarto.parentNode.style.display = 'none';
				}
			}else{
				// todo
			   // alert('Aucun marqueur n\'est defini. La cartographie ne peut etre chargee.');
			}
			
		if(tfo_const['gmapRepere'+nomCarte])
		   {
		  latlngLieuPrincipal = new GLatLng(tfo_const['gmapX'+nomCarte], tfo_const['gmapY'+nomCarte]); //sauveterre
		   bounds[nomCarte].extend(latlngLieuPrincipal); //permet rendre le lieu repère visible même si pas de picto
		   map.setCenter(bounds[nomCarte].getCenter(), map.getBoundsZoomLevel(bounds[nomCarte])-1); // centrage auto
	
		  }
		 else
			map.setCenter(bounds[nomCarte].getCenter(), tfo_const['gmapZoom'+nomCarte]); // centrage auto
		
			GEvent.addListener(map, 'click', function(overlay){
				if (overlay instanceof GMarker) {
					var i = tfo_getMarkerIndex(overlay, tfo_markerCollection[nomCarte]);
					tfo_const['gmapFuncClic'+nomCarte](overlay, tfo_jsonPoints[i]);
					return;
				}
					});	
			
		}
	}
}
var carteNormalFermee=false;
function ouvrirFermerGoogleMapListe(){
	var divCarto = document.getElementById('tfoConteneurCartoGMapListeZoom');
	var divGoogleMap = document.getElementById('googleMap');

	if(carteNormalFermee)
	{
		divCarto.style.position='relative';
		divCarto.style.left='0px';
		divGoogleMap.style.zIndex=10;
		divGoogleMap.style.height='300px';
		carteNormalFermee=false;
	}
	else
	{
		divCarto.style.position='absolute';
		divCarto.style.left='-2000px';
		divGoogleMap.style.zIndex=9;
		divGoogleMap.style.height='8px';
		carteNormalFermee=true;
	}
}



