/****
 *
 * Contrôles possibles :
 *
 * - GLargeMapControl()				=> Contrôles étendus avec glissière pour le zoom
 * - GSmallMapControl() 			=> Contrôles simplifié avec boutons + / - et les fleches
 * - GSmallZoomControl()			=> Contrôles simplifié avec boutons + / -
 * - GScaleControl()				=> Affichage de l'échelle
 * - GMapTypeControl()				=> Choix du type de carte (normale / sat. / hybride)
 * - GHierarchicalMapTypeControl()	=> Choix du type de carte avec des options avancées
 * - GOverviewMapControl()			=> Affiche la mini carte
 */

/**
 * Créé un marqueur standard
 *
 * @var GLatLng coordinates		Les coordonnées lat / lng du marqueur
 * @var string 	bubbleMessage	Le texte a afficher quand on clique sur le marqueur
 * @return GMarker
 **/
function createMarker ( coordinates, bubbleMessage ) {
	var marker = new GMarker ( coordinates, {draggable: false} );
	if ( bubbleMessage != undefined && bubbleMessage != "" ) {
		GEvent.addListener(marker,"click", function() {
			marker.openInfoWindowHtml(bubbleMessage);
		});
	}
	return marker;
}

/**
 * Créé un marqueur standard avec une couleur précise
 *
 * @var GLatLng coordinates		Les coordonnées lat / lng du marqueur
 * @var GLatLng color			La couleur du marqueur en hexa sans le # : ed1a22
 * @var string 	bubbleMessage	Le texte a afficher quand on clique sur le marqueur
 * @return GMarker
 **/
function createColoredMarker ( coordinates, color, bubbleMessage ) {
	var iconMarker	= MapIconMaker.createMarkerIcon({width: 36, height: 32, primaryColor: color});
	var marker 		= new GMarker ( coordinates, {draggable: false, icon: iconMarker} );
	if ( bubbleMessage != undefined && bubbleMessage != "" ) {
		GEvent.addListener(marker,"click", function() {
			marker.openInfoWindowHtml(bubbleMessage);
		});
	}
	return marker;
}

/**
 * Trouve les coordonnées Lat / Lng d'une adresse postale
 *
 * @var string address		L'adresse postale
 * @var function callback	La fonction a appeler une fois l'adresse trouvé. Un objet geoResponse lui sera transmit
 * @return void
 **/
function findCoordinatesFromAddress (address, callback) {
	if ( address != "" ) {
		geoCoder.getLocations(address, callback );
	}
}

/**
 * Ajouter une marqueur sur une carte
 *
 * @var objet reponseGeoCoder	La réponse du geocoder
 * @return void
 **/
function addMarkerFromGeoCoder( reponseGeoCoder ) {
	if ( !reponseGeoCoder || reponseGeoCoder.Status.code != 200 ) {
		$("infosAdresse").update("L'adresse saisie n'est pas connue");
	} else {
		var Position 			= reponseGeoCoder.Placemark[0];
		var Lat					= Position.Point.coordinates[1];
		var Lng					= Position.Point.coordinates[0];
		var markerCoordinates	= new GLatLng(Position.Point.coordinates[1], Position.Point.coordinates[0]);

		Map.addOverlay( createMarker ( markerCoordinates ) );
	}
}


function afficherAdresse (reponseGeoCoder) {
	if ( !reponseGeoCoder || reponseGeoCoder.Status.code != 200 ) {
		$("infosAdresse").update("L'adresse saisie n'est pas connue");
	}
	else {
		var Position 		= reponseGeoCoder.Placemark[0];
		var Lat				= Position.Point.coordinates[1];
		var Lng				= Position.Point.coordinates[0];
		var coordsPoint		= new GLatLng(Position.Point.coordinates[1], Position.Point.coordinates[0]);

		var adresseAffiche	= $("saisieAdresse").value + "<br />Lat : " + Lat + "<br />Lng : " + Lng;
		$("infosAdresse").update(Position.AddressDetails.Accuracy + "<hr />" + adresseAffiche);

		if ( $("ajouterMarqueur").checked == true ) {
			var couleurMarqueur = $("couleurMarqueur").value;
			var rayonRecherche	= $("rayonRecherche").value;
			if (couleurMarqueur == "") {
				laCarte.addOverlay( creerMarqueur ( coordsPoint, adresseAffiche ) );
			}
			else {
				laCarte.addOverlay( creerMarqueurCouleur ( coordsPoint, couleurMarqueur, adresseAffiche ) );
			}

			if (rayonRecherche > "0") {
				dessineUnCercle(coordsPoint, rayonRecherche, "#0000ff", "2", "0.5", "#FF00F0", "0.2", 60);
			}
		}

		//--> MAINTENANT ON CALCUL LA DISTANCE ENTRE CHAQUE REVENDEUR ET L'ADRESSE DU CLIENT
		var i = 0;
		for ( i=0; i < listeAdressses.length; i++ ) {
			$("infosAdresse").innerHTML += "<hr />Distance avec : " + listeAdressses[i].Nom + "<br />" + calculerDistance (Lat, Lng, listeAdressses[i].Lat, listeAdressses[i].Lng) + " km";
		}
	}
}

function dessineUnCercle(centre, rayon, cercleCouleur, cercleEpaisseur, cercleOpacite, disqueCouleur, disqueOpacite, segment) {
	var cercle;
	var latConv = centre.distanceFrom(new GLatLng(centre.lat()+0.1, centre.lng()))/100;
	var lngConv = centre.distanceFrom(new GLatLng(centre.lat(), centre.lng()+0.1))/100;
	var points = [];
	var step = parseInt(360/segment)||10;
	for(var i=0; i<=360; i+=step){
		var pint = new GLatLng(centre.lat() + (rayon/latConv * Math.cos(i * Math.PI/180)), centre.lng() + (rayon/lngConv * Math.sin(i * Math.PI/180)));
		points.push(pint);
	}
	cercle = new GPolygon(points, cercleCouleur, cercleEpaisseur, cercleOpacite, disqueCouleur, disqueOpacite);
	laCarte.addOverlay(cercle);
}

function Arrondir(Valeur,X) {
	X = (!X ? 3 : X);
	return Math.round( Valeur * Math.pow( 10 , X ) ) / Math.pow( 10 , X );
}

function calculerDistance (departLat, departLng, arriveLat, arriveLng) {
	a = departLat;
	b = departLng;
	c = arriveLat;
	d = arriveLng;

	e = ( 3.1415926538 * a / 180 );
	f = ( 3.1415926538 * b / 180 );

	g = ( 3.1415926538 * c / 180 );
	h = ( 3.1415926538 * d / 180 );

	i = ( Math.cos(e) * Math.cos(g) * Math.cos(f) * Math.cos(h) + Math.cos(e) * Math.sin(f) * Math.cos(g) * Math.sin(h) + Math.sin(e) * Math.sin(g) );
	j = ( Math.acos(i) );

	return Arrondir(6371 * j);
}