// JavaScript librairie pour Google Maps API (gma)

//==============================================================================
//==============================================================================
// Variables globales
//==============================================================================
//==============================================================================

var polyShape;
var polygonMode=false;
var polygonDepth = "20";
var polyPoints = [];
var marker;
var geocoder = null;

var fillColor = "#0000FF"; // blue fill
var lineColor = "#000000"; // black line
var opacity = .5;
var lineWeight = 2;

//==============================================================================
//==============================================================================
// les fonctions de geocodage
//==============================================================================
//==============================================================================
function gma_geocode(addstr) {
	var geoc = new GClientGeocoder();
	geoc.setBaseCountryCode('fr');
	
	//
	// recuperation de l'adreese à géocoder
	// prendre la valeur des champs adresse et ville du formulaire
	//
	//var add = document.getElementById('input_address').value;
	
	//
	// lancer le geocodage
	//
	geoc.getLatLng(addstr, geocodeLatLng);
}

function geocodeLatLng(point) {
	//
	// mettre les valeurs de lat et lng dans 
	// les champs adéquats du forulaire de saisie
	//
	if (point) {
		// alert('latitude:'+point.lat()+', longitude: '+point.lng());
		document.getElementById('geocoder_output_lat').value = point.lat();
		document.getElementById('geocoder_output_lng').value = point.lng();
		map.addOverlay(new GMarker(point));
		map.setCenter(new GLatLng(point.lat(), point.lng()), 7);
	} else {
		// alert('sans');
		document.getElementById('geocoder_output_lat').value = 0;
		document.getElementById('geocoder_output_lng').value = 0;
	}
		
}


//==============================================================================
//==============================================================================
// la fonction de base appelée depuis le onload de la page html
//==============================================================================
//==============================================================================

function gma_geocoder_initialize (siteroot, lang, coords) {
	// doit etre appelee apres gma_initialize qui cree l'objet map
	// ajout du marker des infos courantes
	if (coords.length=2) {
		var Lat = coords[0];
		var Lng = coords[1];
		if (Lat!='' && Lng!='') {
			marker = new GMarker(new GLatLng(Lat,Lng));
			map.addOverlay(marker);
		}
	}
	// ajout du handler du onclick
	GEvent.addListener(map, 'click', mapClick);
}

// mapClick - Handles the event of a user clicking anywhere on the map
// Adds a new point to the map and draws either a new line from the last point
// or a new polygon section depending on the drawing mode.
function mapClick(marker, clickedPoint) {
	// Push onto polypoints of existing polygon
	polyPoints.push(clickedPoint);
	drawCoordinates();
}

// Clear current Map
function clearMap(){
	map.clearOverlays();
	polyPoints = [];
	document.getElementById("geocoder_output_lat").value = '';
	document.getElementById("geocoder_output_lng").value = '';
}

// Delete last Point
// This function removes the last point from the Polyline/Polygon and redraws
// map.
function deleteLastPoint(){
	map.removeOverlay(polyShape);
	
	// pop last element of polypoint array
	polyPoints.pop();
	drawCoordinates();
}

// drawCoordinates
function drawCoordinates(){
	//Re-create Polyline/Polygon
	if (polygonMode) {
		polyShape = new GPolygon(polyPoints,lineColor,lineWeight,opacity,fillColor,opacity);
	} else {
		polyShape = new GPolyline(polyPoints,lineColor,lineWeight,opacity);
	}
	map.clearOverlays();
	
	// Grab last point of polyPoints to add marker
	marker = new GMarker(polyPoints[polyPoints.length -1]);
	map.addOverlay(marker);
	//map.addOverlay(polyShape);
	logCoordinates();
}

//  for further encoding with poly-encode.php
function logCoordinates(){
	// check mode
	if (polygonMode){
	} else {
		// print coords header
		document.getElementById("geocoder_output_lat").value = polyPoints[polyPoints.length-1].lat();
		document.getElementById("geocoder_output_lng").value = polyPoints[polyPoints.length-1].lng();
	}
}

//	Fonction permettant de definir les coordonnées
function resetCoordonnee(Lat,Lng) {
	document.getElementById('geocoder_output_lat').value = Lat;
	document.getElementById('geocoder_output_lng').value = Lng;
	map.clearOverlays();
	var ll = new GLatLng(Lat,Lng);
	marker = new GMarker(ll);
	map.addOverlay(marker);
	map.panTo(ll);
}

function defineAdress() {
	var newAdress = '';
	newAdress = newAdress+document.getElementById('input_3').value+' ';
	newAdress = newAdress+document.getElementById('input_4').value+' ';
	newAdress = newAdress+document.getElementById('input_14').value+' ';
	newAdress = newAdress+document.getElementById('input_15').value+' ';
	document.getElementById('geocoder_input_address').value = newAdress;
}

function saveCoordonnee(ChampLat,ChampLng) {
	if (MapConfig.gmaMode=='shadowbox') {
		window.parent.document.getElementById(ChampLat).value = document.getElementById('geocoder_output_lat').value;
		window.parent.document.getElementById(ChampLng).value = document.getElementById('geocoder_output_lng').value;
		//window.parent.location.reload();
		window.parent.Shadowbox.close();
	} else {
		document.getElementById(ChampLat).value = document.getElementById('geocoder_output_lat').value;
		document.getElementById(ChampLng).value = document.getElementById('geocoder_output_lng').value;
	}
}
