 var bounds;
 var tfo_markerCollectionListeNormale;
 var tfo_markerCollectionListeZoom;
 var tfo_markerCollectionDetailNormale;

function tfo_ouvreItineraireGMap(){
	var frm = document.forms['gmapFormItineraire'];
	
	if(frm.elements['daddr'].value!=''){
		// pour la liste des parametres, voir http://mapki.com/wiki/Google_Map_Parameters
		var url = "http://maps.google.com/maps?";
		url += "f=d"; //f=d Displays the "directions" form (two input boxes: from, to). 
		url +="&saddr=" + frm.elements['saddr'].value; // Source address
		url +="&daddr=" + frm.elements['daddr'].value; // Destination address(es)
		url +="&sll=47.15984,2.988281"; // Latitude,longitude of the point from which the business search should be performed
		url +="&sspn=10.610077,20.566406"; // Stands for "Screen span". Map bounds dimensions in Degrees,
		url +="&ie=UTF8"; // Can be used to specify the input character encoding set. e.g. ie=UTF8. 
		url +="&z=8"; // Sets the zoom level. (Range from 1-20) 
		//url +="&pw=2"; // Activates print mode and initiates printing. There seems to be a problem at the moment with pw=1, but using settings like pw=2 is OK.
		url +="&om=1"; // The presence of this parameter with a value other than 1 causes the overview map to be closed. If the parameter is omitted, or present with the value 1, then the overview map is open. 
	
		//alert(url);
		window.open (url, 'caravelis-itineraire', config='height=600, width=800, toolbar=no, menubar=yes, scrollbars=auto, resizable=yes, location=no, directories=no, status=no')
	}
	return false;	
}

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


function tfo_getMarkerIndexByIdTFO(idTfo, markerCollection){
	for (var i=0; i<markerCollection.length; i++) {
		if(markerCollection[i]['IDTFO']==idTfo){
			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){
	var markerCollection = new Array();
	map.clearOverlays();	
	bounds = 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].latitude + ' ' + jsonPoints[i].longitude);
			var latlng = new GLatLng(jsonPoints[i].latitude, jsonPoints[i].longitude);
			if(jsonPoints[i].latitude==0 || jsonPoints[i].longitude==0){
				// on fait rien
				//alert(i);
				marqueursErreur.push(i);
			}else{
				var icon = new GIcon();
				
				if(TYPE_NUMEROTATION=='alphabetique'){
					var index = jsonPoints[i].index.toUpperCase();
					icon.shadow = CHEMIN_IMAGE_MARQUEUR + 'shadow.png';
					
				  	icon.image = CHEMIN_IMAGE_MARQUEUR + TFO_GMAP_MARKER_COLOR + '_Marker' + index + '.png';
				  	icon.iconSize = new GSize(20, 35);
				  	icon.shadowSize = new GSize(30, 40);
					icon.iconAnchor = new GPoint(6, 20);
					icon.infoWindowAnchor = new GPoint(5, 1);
					var marker = new GMarker(latlng, {title:jsonPoints[i].title, icon:icon});
				}else{
					var marker = new GMarker(latlng, {title:jsonPoints[i].title});
				}
				map.addOverlay(marker);
				var tmp = new Array();
				tmp['IDTFO'] = jsonPoints[i].idTfo;
				tmp['GMARKER'] = marker;
				markerCollection.push(tmp);
				
				bounds.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);
	return markerCollection;
}



// demarrage de la carto, lorsque tout est charge
function tfoCarto_ListeNormaleInitialize() {
	//alert(tfo_jsonPoints);
	var divCarto = document.getElementById("tfoConteneurCartoGMap");
	if(divCarto==null){
		alert('pas de conteneur pour la carto !');		
	}else{
		
		divCarto.style.zIndex = 1000;
		divCarto.style.whiteSpace = 'normal';
		divCarto.style.overflow = 'hidden';// pour eviter que les 'Terms of use' debordent
		divCarto.style.width = '235px';// pour eviter que les 'Terms of use' debordent
		divCarto.style.height = '308px';// pour eviter que les 'Terms of use' debordent
	    var map = new google.maps.Map2(divCarto);
	    
	    map.setCenter(new google.maps.LatLng(44.59, 2.405), 11); // conques ;-)
	    map.addControl(new GSmallMapControl());
	    map.addControl(new google.maps.MenuMapTypeControl({useShortNames:true}));
	    divCarto.parentNode.style.display = 'none';
	    
	    if(tfo_jsonPoints!==null){
	    	tfo_markerCollectionListeNormale = tfo_populate(map, tfo_jsonPoints);
	    	if(tfo_markerCollectionListeNormale.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.');
	    }
	    
    
	    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds, new GSize(235,308))); // centrage auto
	      
	    GEvent.addListener(map, 'click', function(overlay){
	    	if (overlay instanceof GMarker) {
				var i = tfo_getMarkerIndex(overlay, tfo_markerCollectionListeNormale);
				tfo_goListeFromCarteListeNormale(tfo_jsonPoints[i]);//vueOi.js
				return;
			}
				});	
		
	}
}

function tfoCarto_DetailNormalInitialize() {
	
	var divCarto = document.getElementById("tfoConteneurCartoGMap");
	if(divCarto==null){
		alert('pas de conteneur pour la carto !');		
	}else{
		
		divCarto.style.zIndex = 1000;
		divCarto.style.whiteSpace = 'normal';
		divCarto.style.overflow = 'hidden';// pour eviter que les 'Terms of use' debordent
		divCarto.style.width = '235px';// pour eviter que les 'Terms of use' debordent
		divCarto.style.height = '308px';// pour eviter que les 'Terms of use' debordent
	    var map = new google.maps.Map2(divCarto);
	    map.setCenter(new google.maps.LatLng(44.5996, 2.3976), 11); // conques ;-)
	    map.addControl(new GSmallMapControl());
	    //	    map.addControl(new GLargeMapControl3D());
	    map.addControl(new google.maps.MenuMapTypeControl({useShortNames:true}));
	    divCarto.parentNode.style.display = 'none';
	    
	    if(tfo_jsonPoints!==null){
	    	tfo_markerCollectionDetailNormale = tfo_populate(map, tfo_jsonPoints);
	    	if(tfo_markerCollectionDetailNormale.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.');
	    }
	    
	    latlngLieuPrincipal = new GLatLng(44.59, 2.405); //conques
	    bounds.extend(latlngLieuPrincipal); //permet rendre le lieu repère visible même si pas de picto
	    tempZoomLevel=map.getBoundsZoomLevel(bounds, new GSize(313,254));
	    tempZoomLevel=(tempZoomLevel>11)?11:tempZoomLevel;
	    
	    map.setCenter(bounds.getCenter(), tempZoomLevel); // centrage auto
	    
	    GEvent.addListener(map, 'click', function(overlay){
	    	if (overlay instanceof GMarker) {
				var i = tfo_getMarkerIndex(overlay, tfo_markerCollectionDetailNormale);
				tfo_goZoomItineraireCarteListeDetail(tfo_jsonPoints[i]);//vueOi.js
				//overlay.openInfoWindowHtml(tfo_ecritPopCartoItineraire(tfo_jsonPoints[i])); 
				return;
			}
				});	
		
		// gestion du defilement de la carto
	    //tfo_console('googleMap', 0, 0);
	}
}
function tfoCarto_ListeZoomInitialize() {
	var divCarto = document.getElementById("tfoConteneurCartoGMapZoom");
	if(divCarto==null){
		alert('pas de conteneur pour la carto !');		
	}else{
		divCarto.style.zIndex = 1000;
		
		divCarto.style.whiteSpace = 'normal';
		divCarto.style.position = 'absolute';
		divCarto.style.overflow = 'hidden';// pour eviter que les 'Terms of use' debordent
	    var map = new google.maps.Map2(divCarto);
	    //map.setZoom(11);
	    map.setCenter(new google.maps.LatLng(44.602727, 2.397223), 9); // conques ;-)
	    map.addControl(new GSmallMapControl());
	    //	    map.addControl(new GLargeMapControl3D());
	    map.addControl(new GMapTypeControl());

	    divCarto.parentNode.style.display = 'none';
	    
	    if(tfo_jsonPoints!==null){
	    	tfo_markerCollectionListeZoom = tfo_populate(map, tfo_jsonPoints);
	    	if(tfo_markerCollectionListeZoom.length>0){
	    		divCarto.parentNode.style.display = 'block';
	    	}else{
	    		divCarto.parentNode.style.display = 'none';
	    	}
	    }else{
		    // todo
		   // alert('Aucun marqueur n\'est defini. La carto ne peut etre chargee.');
	    }	
	    
	    GEvent.addListener(map, 'click', function(overlay){
	    	if (overlay instanceof GMarker) {
				var i = tfo_getMarkerIndex(overlay, tfo_markerCollectionListeZoom);
				overlay.openInfoWindowHtml(tfo_ecritPopCartoItineraireCarteListeZoom(tfo_jsonPoints[i])); //vueOi.js
				return;
			}
				});		
	    
	}
}

var positionGoogle;
function ouvrirGoogleMap (){
	//clearInterval(tempsConsole);
	$('googleMap').setStyle({width: '660px'});
	$('googleMap').setStyle({height: '350px'});
	positionGoogle = $('googleMap').style.top;
	$('googleMap').setStyle({top: '0px'});
	$('tfoConteneurCartoGMap').setStyle({display: 'none'});
	$('ouvrirGoogleZoom').setStyle({display: 'none'});
	window.location.href="#tfo_liste"; 
	$('tfoConteneurCartoGMapZoom').setStyle({visibility: 'visible'});
	new Effect.Opacity('tfoConteneurCartoGMapZoom', { from: 0.0, to: 1.0,duration: 1.0});
	$('fermerGoogle').setStyle({visibility: 'visible'});
	new Effect.Opacity('fermerGoogle', { from: 0.0, to: 1.0,duration: 1.0});
	document.getElementById('googleMap').style.zIndex=1010;
}

function fermerGoogleMap (){
	$('tfoConteneurCartoGMapZoom').setStyle({visibility: 'hidden'});
	$('fermerGoogle').setStyle({visibility: 'hidden'});
	document.getElementById('googleMap').style.zIndex=1000;
	$('googleMap').setStyle({width: '235px'});
	$('googleMap').setStyle({height: '235px'});
	//$('googleMap').setStyle({top: positionGoogle});
	$('tfoConteneurCartoGMap').setStyle({display: 'block'});
	$('ouvrirGoogleZoom').setStyle({display: 'block'});
	$('listePrestataires').setStyle({display: 'block'});
	tempsConsole = setTimeout("stayTopLeft()", 70);
}