var gMap = null;
var geoCoder = null;
var gIcon = null;
var gIconLocalize = null;

/**
 * la fonction qui permet d'afficher la carte google maps
 */
function loadMap( aAddress )
{
   	if (GBrowserIsCompatible())
	{
   		gMap = new GMap2( document.getElementById('gMapContainer') );
   		geoCoder = new GClientGeocoder();
   		gIcon  		= new GIcon();
   		gIconLocalize  	= new GIcon();
		
   		gMap.addControl(new GLargeMapControl());
    	gMap.addControl(new GMapTypeControl());
    	gMap.setCenter(new GLatLng(46.2, -1.42), 11);
    	gMap.enableScrollWheelZoom();
    	
		gIcon.iconSize 	= new GSize(16, 16);
		gIcon.image  	= "http://maps.google.com/mapfiles/kml/pal3/icon55.png";
		gIcon.iconAnchor = new GPoint(12, 12);
		gIcon.infoWindowAnchor = new GPoint(5, 1);
		
		gIconLocalize.iconSize 	= new GSize(28, 28);
		gIconLocalize.image  	= "http://maps.google.com/mapfiles/kml/pal3/icon56.png";
		gIconLocalize.iconAnchor = new GPoint(12, 12);
		gIconLocalize.infoWindowAnchor = new GPoint(5, 1);
   		
   		if(aAddress)
   		{ 		
   			if(aAddress.length > 0){  			
				addPointByAddress( aAddress );
   			}
   		}
	}
}
                
/**
 * afficher une Map google à partir d'une adresse
 */
function addPointByAddress( address, htmlCode, localize )
{
	if (geoCoder) 
	{
		geoCoder.getLocations( address,
							function(response) 
							{
								if ( !response || response.Status.code != 200) {
					            	alert(address + " not found");
					            } else {
					            	place = response.Placemark[0];
							        point = new GLatLng(place.Point.coordinates[1],
							                            place.Point.coordinates[0]);
									
									if( localize == true )
									{
										markerOptions = { icon:gIconLocalize, zIndexProcess:function(){return 2;} };	
										gMap.setCenter(point,12);
									}
									else
										markerOptions = { icon:gIcon, zIndexProcess:function(){return 1;} };	

					            	var marker = new GMarker(point, markerOptions);
					            	GEvent.addListener(marker, "click", function() {
							            marker.openInfoWindowHtml(htmlCode);
							        });
					            	gMap.addOverlay(marker);
									
									if( localize == true )
										marker.openInfoWindowHtml(htmlCode);
					            }

							}
						)
	}
}

function addPointByCoord( lat, lng, htmlCode, localize )
{
	if (geoCoder) 
	{
		point = new GLatLng(lat, lng);
									
		if( localize == true )
		{
			markerOptions = { icon:gIconLocalize, zIndexProcess:function(){return 2;} };	
			gMap.setCenter(point,12);
		}
		else
			markerOptions = { icon:gIcon, zIndexProcess:function(){return 1;} };	

		var marker = new GMarker(point, markerOptions);

		GEvent.addListener(	marker, "click", function() {
							marker.openInfoWindowHtml(htmlCode);
							});
		gMap.addOverlay(marker);
		
		if( localize == true )
			marker.openInfoWindowHtml(htmlCode);
	}
}