// initialise la map google pour le site
var map;
var baseIcon;
/**/
function load1WithCoord( x, y, zoom ){
	if (GBrowserIsCompatible()) {
		//alert( "load1WithCoord: " + x + ":" + y + ":" + zoom );
		
		map = new GMap2(document.getElementById( "map1" ));
		map.addControl(new GSmallZoomControl());
		//centre de l'image
		map.setCenter( new GLatLng( x, y ), zoom );
		//zoom flouté
		map.enableContinuousZoom();
		//objet google de recherche d'adresse
		geocoder = new GClientGeocoder();
		//alert( x + ":" + y + ":" + zoom );
		//sert dans notre cas de comptabilisation des points avec leur coordonnées (resize la carte pour voir tous les markers)
		bounds = new GLatLngBounds();
		
		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		baseIcon = new GIcon();
		baseIcon.shadow = "../images/n-shadow.png";
		baseIcon.iconSize = new GSize(20,19);
		baseIcon.shadowSize = new GSize(0,0);
		baseIcon.iconAnchor = new GPoint(20, 19);
		baseIcon.infoWindowAnchor = new GPoint(13, 5);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
	}
}
/**/
function load2WithCoord( x, y, zoom ){
	if (GBrowserIsCompatible()) {
		//alert( "load2WithCoord: " + x + ":" + y + ":" + zoom );
		
		map = new GMap2(document.getElementById( "map2" ));
		map.addControl(new GSmallZoomControl());
		//centre de l'image
		map.setCenter( new GLatLng( x, y ), zoom );
		//zoom flouté
		map.enableContinuousZoom();
		//objet google de recherche d'adresse
		geocoder = new GClientGeocoder();
		//alert( x + ":" + y + ":" + zoom );
		//sert dans notre cas de comptabilisation des points avec leur coordonnées (resize la carte pour voir tous les markers)
		bounds = new GLatLngBounds();
		
		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		baseIcon = new GIcon();
		baseIcon.shadow = "../images/n-shadow.png";
		baseIcon.iconSize = new GSize(20,19);
		baseIcon.shadowSize = new GSize(0,0);
		baseIcon.iconAnchor = new GPoint(20, 19);
		baseIcon.infoWindowAnchor = new GPoint(13, 5);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
	}
}
/**/
function loadWithCoord( x, y, zoom ){
	if (GBrowserIsCompatible()) {
		//alert( "loadWithCoord: " + x + ":" + y + ":" + zoom );
		
		map = new GMap2(document.getElementById( "map" ));
		map.addControl(new GSmallZoomControl());
		//centre de l'image
		map.setCenter( new GLatLng( x, y ), zoom );
		//zoom flouté
		map.enableContinuousZoom();
		//objet google de recherche d'adresse
		geocoder = new GClientGeocoder();
		//alert( x + ":" + y + ":" + zoom );
		//sert dans notre cas de comptabilisation des points avec leur coordonnées (resize la carte pour voir tous les markers)
		bounds = new GLatLngBounds();
		
		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		baseIcon = new GIcon();
		baseIcon.shadow = "../images/n-shadow.png";
		baseIcon.iconSize = new GSize(20,19);
		baseIcon.shadowSize = new GSize(0,0);
		baseIcon.iconAnchor = new GPoint(20, 19);
		baseIcon.infoWindowAnchor = new GPoint(13, 5);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
	}
}
/**/
function loadGMap(){
	if (GBrowserIsCompatible()) {
        //alert("load: " + map);
		map = new GMap2(document.getElementById( "map" ));
		map.addControl(new GSmallZoomControl());
		//centre de l'image
		map.setCenter(new GLatLng(47.154607, 1.740879), 5);
		//zoom flouté
		map.enableContinuousZoom();
		//objet google de recherche d'adresse
		geocoder = new GClientGeocoder();
		//sert dans notre cas de comptabilisation des points avec leur coordonnées (resize la carte pour voir tous les markers)
		bounds = new GLatLngBounds();
		
		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		baseIcon = new GIcon();
		baseIcon.shadow = "../images/n-shadow.png";
		baseIcon.iconSize = new GSize(20,19);
		baseIcon.shadowSize = new GSize(0,0);
		baseIcon.iconAnchor = new GPoint(20, 19);
		baseIcon.infoWindowAnchor = new GPoint(13, 5);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
	}
}
/***/
function loadGMapOwner( x, y, zoom )
{
	if( GBrowserIsCompatible() )
	{
		//alert( "loadGMapOwner: " + x + ":" + y + ":" + zoom );
		
		map = new GMap2(document.getElementById( "map_owner" ));
		map.addControl(new GSmallZoomControl());
		//centre de l'image
		map.setCenter( new GLatLng( x, y ), zoom );
		//zoom flouté
		map.enableContinuousZoom();
		//objet google de recherche d'adresse
		geocoder = new GClientGeocoder();
		//alert( x + ":" + y + ":" + zoom );
		//sert dans notre cas de comptabilisation des points avec leur coordonnées (resize la carte pour voir tous les markers)
		bounds = new GLatLngBounds();
		
		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		baseIcon = new GIcon();
		baseIcon.shadow = "../images/n-shadow.png";
		baseIcon.iconSize = new GSize(20,19);
		baseIcon.shadowSize = new GSize(0,0);
		baseIcon.iconAnchor = new GPoint(20, 19);
		baseIcon.infoWindowAnchor = new GPoint(13, 5);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
	}
}
//img_marker = le tableau de la fonction create marker
function addMarker(latitude,longitude,texte,img_maker){
	//alert( "addMarker: (" + map + ") " + latitude + ", " + longitude + ", " + texte + ", " + img_maker );
	
	// Creates a marker whose info window displays the letter corresponding
	// to the given index.
	var point = new GLatLng(latitude, longitude);
	//var marker['markerId_' + id]=createMarker(point,texte,img_maker);
	map.addOverlay(createMarker(point,texte,img_maker));
	//map.addOverlay(marker['markerId_' + id]);
	//ajoute la coordonnée du point dans la variable.
	bounds.extend(point);
	//zoomfit();
}
//####################### EFFACER TOUS LES MARKERS

function createMarker(point, info, index){
	// Create a lettered icon for this point using our icon class
    /**
     * Evaluated icon.
     */
    var rating;
    if (index == null)
        rating = 0;
    else
        rating = 1;
    var letter = index;
	var icon = new GIcon(baseIcon);
	var tab_img = new Array("bulleNoRated.png","bulleRated.png","bulleRated.png","bulleRated.png","bulleRated.png","bulleRated.png");

    var images = [
        // Unrated icons
        [
            '/images/bulleNoRated.png',
            '/images/bulleNoRated_s.png',
            '/images/bulleNoRated_s.png',
        ],
         // Rated icons
        [
            '/images/bulleRated.png',
            '/images/bulleRated_s.png',
            '/images/bulleRated_s.png'
        ],
    ];
    var iconSize = 3;
    var zoom = map.getZoom();
    var toolTip = '';
    if (zoom>14){
        icon.image = images[rating][0];
        toolTip = '<img src="' + images[rating][0] + '" /> ' + info;
    }
    else if (map.getZoom()>13){
        icon.image = images[rating][1];
        toolTip = '<img src="' + images[rating][0] + '" /> ' + info;
        iconSize = 7;
    }
    else {
        icon.image = images[rating][2];
        iconSize = 8;
    }
    icon.iconSize = new GSize(11,11);
    icon.iconAnchor = new GPoint(5.5, 5.5);

	var marker = new GMarker(point, {icon:icon, title:toolTip});
    
	//marker.title = 'test';
	//décommenter pour afficher une infobulle
	GEvent.addListener(marker, "click", function(){
		marker.openInfoWindowHtml(info, {maxWidth:50});
	});

    //décommenter pour afficher une infobulle
	//GEvent.addListener(marker, "mouseover", function(){
		//alert(info);
	//});
	
	//gmarkers['']=marker
	
	return marker;
}

//zoom sur une adresse donnée
function showAddress(address){
  if (geocoder) {
	geocoder.getLatLng(
	  address,
	  function(point) {
		if (!point) {
		  alert(address + " not found");
		} else {
		  map.setCenter(point, 13);
		  /*var marker = new GMarker(point);
		  map.addOverlay(marker);
		  marker.openInfoWindowHtml(address);*/
		}
	  }
	);
  }
}

//recadre la zone de la carte pour afficher tous les markers
function zoomfit()
{
	newzoom = map.getBoundsZoomLevel(bounds);
	newcenter = bounds.getCenter();
	map.setCenter (newcenter,newzoom);
}

//nettoye tous les markers
function clearMarkers(){
	map.clearOverlays();
	//solution intermédiaire car on ecrase la valeur au lieu de l'initialiser
	bounds = new GLatLngBounds();
}

// ...
function setGMapCenter( x, y, zoom )
{
	map.setCenter( new GLatLng( x, y ), zoom );
}
