/*
*
L'objet GMapManager permet de créer un google map, de l'affecter à un conteneur.
Des méthodes permettent d'affecter des points géolocalisés, de les afficher, de leur affecter
des infobulles spécifiques....
*
*/


/*
* GMapManager : Constructeur de l'objet
* @param	pathIcon	String	chemin de l'image illustrant le marker
* @param	pathIcon	String	chemin de l'image illustrant le marker au rollOver
* @param	idContainer	String	id du conteneur de la map
*/
function GMapManager(pathIcon, pathIconOver, idContainer)
{	
	this.idContainer = idContainer;
	if (GBrowserIsCompatible())
	{
		this.bounds = new GLatLngBounds();
		this.coder = new GClientGeocoder();
		
        //Création du layer des reliefs		
		var layerRelief = [ G_PHYSICAL_MAP.getTileLayers()[0] ]; 
        var mtRelief = new GMapType(layerRelief, G_PHYSICAL_MAP.getProjection(), "Relief");
		
		//initialisation de la carte dans le conteneur affecté
		var map = new GMap2(document.getElementById(idContainer),{mapTypes:[G_PHYSICAL_MAP, G_NORMAL_MAP, G_SATELLITE_MAP]});
		
		//Ajout du type relief sur la carte
		//map.addMapType(mtRelief);
		
		
		map.addControl(new GLargeMapControl());	
		map.addControl(new GOverviewMapControl()); //Le carré en bas à droite pour visualisation
		map.disableDoubleClickZoom();
		
		var icon = new GIcon();
		icon.image = pathIcon;
		icon.iconSize = new GSize(23, 25);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);		
		
		var iconOver = new GIcon();
		iconOver.image = pathIconOver;
		iconOver.iconSize = new GSize(23, 25);
		iconOver.iconAnchor = new GPoint(6, 20);
		iconOver.infoWindowAnchor = new GPoint(5, 1);		
		
		this.map = map;
		this.icon = icon;
		this.iconOver = iconOver;
		this.pathIconOver = pathIconOver;
		this.pathIcon = pathIcon;
		this.bInfoWindow = true;
		
		this.map.setCenter(new GLatLng(0,0),0);
	}
	else
		alert("Votre navigateur ne permet pas d'afficher la carte Google");
	
}

/*
* addLayersControl : ajout du control des différents types de vue (plan, sat et relief)
* @param
*/
GMapManager.prototype.addLayersControl = function()
{
	var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(20, 30));
	this.map.addControl(new GMapTypeControl('false'), pos);
}


/*
* enableInfoWindow : ouverture d'un infoWindow au clic sur un marker. (true => ajout sur la carte de pdMarker, false => ajout sur la carte de markers simple (natifs GM))
* @param	bValue	Boolean	valeur de la variable bInfoWindow
*/
GMapManager.prototype.enableInfoWindow = function(bValue)
{
	this.bInfoWindow = bValue;
}


/*
* setDefaultZoom : 
* @param	zoomValue	Number	valeur du zoom	
*/
GMapManager.prototype.setDefaultZoom = function(zoomValue)
{
	this.defaultZoom = zoomValue;
}
	

/*
* showMap : est ce nécessaire ? Franchement ?
* @param lat
* @param lng
*/
GMapManager.prototype.showMap = function (lat, lng)
{
	//this.map.setCenter(new GLatLng(lat,lng), this.defaultZoom);	
	var thisObj = this;
	var customZoom = thisObj.map.getBoundsZoomLevel(thisObj.bounds);
	this.map.setZoom(customZoom);
	this.map.setCenter(thisObj.bounds.getCenter());
}


/*
* removeMarkers : retire tous les markers de la carte
* @param lat
* @param lng
*/
GMapManager.prototype.removeMarkers = function (lat, lng)
{
	this.map.clearOverlays();
}


/*
* setMarkers : contruction des markers et ajout sur la carte
* @param	aPoint	Array	tableau de points à placer
* aPoint[i][0] = [lat, lng] || = "adresse..."
* aPoint[i][1] = objet HTML correspondant
*/
GMapManager.prototype.setMarkers = function (aPoint)
{
	var thisObj = this;
	//thisObj.bounds = new GLatLngBounds();
	for(var i in aPoint)
	{
		if(typeof(aPoint[i][0]) != 'string')
		{
			if(aPoint[i][0])
			{
				thisObj.adddMarker(aPoint[i][1], i, aPoint[i][0][0], aPoint[i][0][1]);
				thisObj.bounds.extend(new GLatLng(aPoint[i][0][0], aPoint[i][0][1]));				
			}
		}
		else{			
			
			this.getCoordByAddress(aPoint[i][0], aPoint[i][1], i);
		}
	}
}


/*
* adddMarker : ajout d'un marker sur la carte
* @param	htmlContent	html	objet html à afficher dans l'infowindow correspondant au marker
* @param	i	int	indice du marker
* @param	lat	int
* @param	lng	int
*/
GMapManager.prototype.adddMarker = function (htmlContent, i, lat, lng)
{
	var thisObj = this;
	
	//On autorise les infoWindow
	//-----------------------------------------
	if(this.bInfoWindow)
	{
		var pMarker = new PdMarker(htmlContent, this.map, i, new GLatLng(lat, lng), {icon: this.icon });
		pMarker.onMouseOver = function()
		{
			this.showTooltip();
			this.setImage(thisObj.pathIconOver);
		}
				
		pMarker.onMouseOut = function()
		{
			this.hideTooltip();
			this.setImage(thisObj.pathIcon);
		}
		
		//pMarker.setTooltip("3 résultats");
		
		this.map.addOverlay(pMarker);
	}
	
	//Markers simples sans les infoWindow
	//-----------------------------------------
	else
	{
		var marker = new GMarker(new GLatLng(lat,lng), this.icon);
        this.map.addOverlay(marker);
	}
}


/*
* getCoordByAddress : placement d'un point sur la carte à partir d'une adresse postale
* @param	address	String	adresse postale
* @param	htmlContent	html	objet html à afficher dans l'infowindow correspondant au marker
* @param	i	int	indice du marker
*/
GMapManager.prototype.getCoordByAddress = function (address, htmlContent, i) 
{
	var thisObj = this;
	this.coder.getLatLng
    (address,
			function(point)
			{
			  point = point.toString();
			  var strLatLongSimple = point.substr(1, point.length - 2);
			  var aLatLngSimple = strLatLongSimple.split(",");
			  thisObj.adddMarker(htmlContent, i, aLatLngSimple[0], aLatLngSimple[1]);
			  thisObj.bounds.extend(new GLatLng(aLatLngSimple[0], aLatLngSimple[1]));
			 }
	);
}




