/**
 * Fonctions pour propulser des cartes Google Maps
 * @author cyril
 */

var map = null;
var geocoder = null;
var mapError = null;
var pointAdresse = null;
var lat = null;
var lng = null;
var number = null; 
var markers;
var homeIcon;
var garageIcon;

var cercleMapAuto = new google.maps.Circle();
var cercleMapMoto = new google.maps.Circle();
var rayonCercleAuto = 0;
var rayonCercleMoto = 0;


var mapOptions = {
	zoom: 10,
	navigationControl: true,
	mapTypeControl: true,
	scaleControl: false,
	navigationControlOptions: {style: google.maps.NavigationControlStyle.ZOOM_PAN},
	mapTypeId: google.maps.MapTypeId.ROADMAP
};

/**
 * Chargement d'une carte Google Map dans une div
 * @param {Object} idDivCarte
 * @param HTTP_HOST
 * @param zoom
 * @return
 */
function load_map(idDivCarte, HTTP_HOST, zoom) {
	map = new google.maps.Map(document.getElementById(idDivCarte), mapOptions);
	
	pointAdresse = new google.maps.LatLng(lat, lng);
	
	
	//créer un cercle
	var parametresCercle = {
		radius: 0,
		center: pointAdresse,
		fillColor: '#000000', 
		fillOpacity: 0.1, 
		strokeWeight: 1, 
		strokeOpacity:1, 
		strokeColor: '#000000'
	};
	cercleMapAuto.setOptions(parametresCercle);
	cercleMapMoto.setOptions(parametresCercle);
	cercleMapAuto.setRadius(rayonCercleAuto);
	cercleMapMoto.setRadius(rayonCercleMoto);
	
	if(zoom == null) {
		map.setCenter(pointAdresse);
		map.setZoom(10);
	} else {
		map.setCenter(pointAdresse);
		map.setZoom(zoom);
	}
	
	var extension = "png";
	var ie6 = false;
	if(navigator.appVersion.indexOf("MSIE 6") != -1) {
		extension = "gif";
		ie6 = true;
	}
	
	homeIcon = new google.maps.MarkerImage(
		"http://" + HTTP_HOST + "/layouts/Commun/scripts/google_map/icone_home." + extension,
		new google.maps.Size(20, 34),
		new google.maps.Point(0,0),
		new google.maps.Point(10, 34)
	);
	
	if (!ie6) {
		homeShadowIcon = new google.maps.MarkerImage(
			"http://" + HTTP_HOST + "/layouts/Commun/scripts/google_map/icone_home_omb.png",
			new google.maps.Size(38, 34)
		);
	}
	
	garageIcon = new google.maps.MarkerImage(
		"http://" + HTTP_HOST + "/layouts/Commun/scripts/google_map/icone_garage." + extension,
		new google.maps.Size(36, 43),
		new google.maps.Point(0,0),
		new google.maps.Point (19, 43)
	);
	
	if(!ie6) {
		garageShadowIcon = new google.maps.MarkerImage (
			"http://" + HTTP_HOST + "/layouts/Commun/scripts/google_map/icone_garage_omb.png",
			new google.maps.Size(56, 43)
		);
	} else {
		garageShadowIcon = new google.maps.MarkerImage (null, new google.maps.Size(56, 43));
	}
	
	google.maps.event.trigger(map, 'resize');
	if(lat != null && lng != null) {
		addMarker(lat, lng, homeIcon);
	}
	
	geocoder = new google.maps.Geocoder();
	google.maps.event.trigger(map, 'resize');
}
	
/**
 * Création d'un markeur
 * @param latlng
 * @param img
 * @return marker
 */
function createMarker(latlng, img) {
	number++;
	var marker = new google.maps.Marker({
		position: latlng,
		icon: img
	});
	return marker;
}

/**
 * Ajout d'un marqueur sur la carte
 * @param latGM : latitude
 * @param lngGM : longitude
 * @param img : 
 * @return marker
 */
function addMarker(latGM,lngGM,img) {
	var latlng = new google.maps.LatLng(latGM, lngGM);
	var marker =  createMarker(latlng,img);
	marker.setMap(map); 
	return marker;
}

/**
 * Afficher ou cacher un marqueur sur la carte
 * @param nomMarker
 * @param opt
 * @return void
 */
function toggleMarker(nomMarker,opt) {
	if (markers[nomMarker]){ //vérifier que le marker existe ! sinon erreur javascript
		if(opt == "hide")
			markers[nomMarker].setVisible(false);
		else if(opt == "show"){
			markers[nomMarker].setVisible(true);
		}
	}
}

