/******************************************************************************
* gmapsUtils.js
*******************************************************************************
fonctions de creation de cartes
*******************************************************************************
*                                                                             *
* Copyright 2006									                          *
*                                                                             *
******************************************************************************/

function gMapsLoadMap(myMapId, useLatLong, latitude, longitude, address, showSmallMap, overviewMapId)
{
	var myMap = null;
	var myMapOptions = {
		zoom: 17,
		mapTypeId: google.maps.MapTypeId.ROADMAP 
	};
	myMap = new google.maps.Map (document.getElementById (myMapId), myMapOptions);
	if (showSmallMap == "true") showOverviewMap(myMap, overviewMapId);

	var mapCenter = null;
	if (useLatLong == "false")
	{
		var theGeocoder = new google.maps.Geocoder();
		theGeocoder.geocode( {'address': address[0]},	function(point, status) {
			if (status!=google.maps.GeocoderStatus.OK) {
				alert("Impossible de trouver\n" + address[0] + "\nRaison : " + status);
			}
			else {
				mapCenter = point[0].geometry.location;
				positionMap(myMap, mapCenter, address);
			}
		});
	}
	else
	{
		mapCenter = new google.maps.LatLng(latitude, longitude);
		positionMap(myMap, mapCenter, address);
	}
}

function positionMap (myMap, mapCenter, address)
{
	myMap.setCenter(mapCenter);
	var tooltip = address[1] + "\n" + address[2] +"\n"+ address[3];
	var marker = new google.maps.Marker({
		map: myMap,
		position: mapCenter,
		draggable: false, 
		title: tooltip 
	});
	var infowindow = new google.maps.InfoWindow({
		content: '<span style="font-size: 9px; font-family: Verdana, Arial, Helvetica, sans-serif">'+address[1]+'<br>'+address[2]+'<br>'+ address[3]+'</span>', 
		position: mapCenter 
	});
	google.maps.event.addListener(marker, 'click', function() { 
		infowindow.open(myMap, marker);
	}); 
}

function gMapsDirectionObject (useToLatLong, useFromLatLong)
{
	this._dirService = new google.maps.DirectionsService();
	this._dirRenderer = new google.maps.DirectionsRenderer();
	this._useToLatLong = useToLatLong;
	this._useFromLatLong = useFromLatLong;
	this._initDone = false;
}

gMapsDirectionObject.prototype.setMap = function(myMap)
{
	this._dirRenderer.setMap(myMap);
}
gMapsDirectionObject.prototype.setPanel = function(directionsDiv)
{
	this._dirRenderer.setPanel(directionsDiv);
}

function gMapsLoadDirections(myMapId, directionsId, useToLatLong, useFromLatLong, toAddress, toLatitude, toLongitude)
{
	var destinationAddress = document.getElementById(toAddress).value;
	var myGmapsDirectionObject = new gMapsDirectionObject(useToLatLong, useFromLatLong);
	var myOptions = {
		zoom: 17,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	var myMap = new google.maps.Map(document.getElementById(myMapId), myOptions);
	if(useToLatLong == "true") {
		myMap.setCenter(new google.maps.LatLng(toLatitude, toLongitude));
	}
	else {
		var myGeocoder = new google.maps.Geocoder();
		myGeocoder.geocode( {'address': destinationAddress}, function(point, status) {
			if (status!=google.maps.GeocoderStatus.OK) {
				alert("Impossible de trouver\n" + destinationAddress + "\nRaison : " + status);
			}
			else {
				myMap.setCenter(point[0].geometry.location);
			}
		});
	}
	myGmapsDirectionObject.setMap(myMap);
	myGmapsDirectionObject.setPanel(document.getElementById(directionsId));
	
	return myGmapsDirectionObject;
}

function setDirections (myGmapsDirectionObject, fromAddress, toAddress, fromLatitude, fromLongitude, toLatitude, toLongitude, formType, startType)
{
	var fromAddressValue = '';
	var toAddressValue = '';
	var fromLatitudeValue = '';
	var fromLongitudeValue = '';
	var toLatitudeValue = '';
	var toLongitudeValue = '';
	if(fromAddress != null) fromAddressValue = fromAddress.value;
	if(toAddress != null) toAddressValue = toAddress.value;
	if(fromLatitude != null) fromLatitudeValue = fromLatitude.value;
	if(fromLongitude != null) fromLongitudeValue = fromLongitude.value;
	if(toLatitude != null) toLatitudeValue = toLatitude.value;
	if(toLongitude != null) toLongitudeValue = toLongitude.value;
	var useToLatLong = myGmapsDirectionObject._useToLatLong;
	var useFromLatLong = myGmapsDirectionObject._useFromLatLong;
	var originAddress = "";
	var destinationAddress = "";
	fromAddressValue = fromAddressValue.replace(/\n/g, " ");
	fromAddressValue = fromAddressValue.replace(/\r/g, " ");
	// originAddress : affected by init
	if(!myGmapsDirectionObject._initDone)
	{
		if(useFromLatLong == "true") {
			originAddress = new google.maps.LatLng(fromLatitudeValue, fromLongitudeValue);
		}
		else {
			originAddress = fromAddressValue;
		}
	}
	else
	{
		switch (formType) {
			case "coords":
				originAddress = new google.maps.LatLng(fromLatitudeValue, fromLongitudeValue);
				break;
			case "address":
				originAddress = fromAddressValue;
				break;
			case "both":
				if (startType.checked) {
					originAddress = fromAddressValue;
				}
				else {
					originAddress = new google.maps.LatLng(fromLatitudeValue, fromLongitudeValue);
				}
				break;
		}
	}
	// destination address : not affected by init
	if(useToLatLong == "true") {
		destinationAddress = new google.maps.LatLng(toLatitudeValue, toLongitudeValue);
	}
	else {
		destinationAddress = toAddressValue;
	}
	var request = { 
		origin:originAddress,  
		destination: destinationAddress, 
		travelMode: google.maps.DirectionsTravelMode.DRIVING
	};
	if (originAddress != "" && destinationAddress != "")
	{
		myGmapsDirectionObject._dirService.route(request, function(response, status) {
			if (status == google.maps.DirectionsStatus.NOT_FOUND) {
				alert("At least one of the origin, destination, or waypoints could not be geocoded");
			}
			if (status == google.maps.DirectionsStatus.UNKNOWN_ERROR) {
				alert("A directions request could not be processed due to a server error. The request may succeed if you try again.");
			}
			if (status == google.maps.DirectionsStatus.INVALID_REQUEST) {
				alert("The DirectionsRequest provided was invalid.");
			}
			if (status == google.maps.DirectionsStatus.OVER_QUERY_LIMIT) {
				alert("The webpage has gone over the requests limit in too short a period of time.");
			}
			if (status == google.maps.DirectionsStatus.REQUEST_DENIED) {
				alert("The webpage is not allowed to use the directions service.");
			}
			if (status == google.maps.DirectionsStatus.ZERO_RESULTS) {
				alert("No route could be found between the origin and destination.");
			}
			if (status == google.maps.DirectionsStatus.OK) { 
				myGmapsDirectionObject._dirRenderer.setDirections(response); 
			} 
		});
	}
	myGmapsDirectionObject._initDone = true;
}

function showOverviewMap (theMap, overviewMapId)
{
	// Initialize overview map

	var overlayMap = new google.maps.Map(
		document.getElementById(overviewMapId), {
		mapTypeId: google.maps.MapTypeId.ROADMAP, 
		disableDefaultUI: true
	});
	 
	// Set up zoom_changed listeners so that overlayMap's zoom changes to be 4
	// less than map's and map's 4 greater than overlayMap's.
	 
	google.maps.event.addListener(theMap, 'zoom_changed', function() {
		var newZoom = Math.max(theMap.getZoom() - 4, 0);
		if (overlayMap.getZoom() != newZoom) overlayMap.setZoom(newZoom);
	});
	google.maps.event.addListener(overlayMap, 'zoom_changed', function() {
		var newZoom = overlayMap.getZoom() + 4;
		if (theMap.getZoom() != newZoom) theMap.setZoom(newZoom);
	});
	 
	// overlayMap's center stays in sync with map's center 
	overlayMap.bindTo('center', theMap, 'center');
	
	theMap.setZoom(17); // This will trigger a zoom_changed on the map
	
	/**
	 * attach the overview map to the main map and set positioning
	 */
	var overDiv = overlayMap.getDiv();
	theMap.getDiv().appendChild(overDiv);
	overDiv.style.position = "absolute";
	overDiv.style.right = "0px";
	overDiv.style.bottom = "14px";
	overDiv.style.zIndex = 10;
	overDiv.style.display = "block";
	 
	google.maps.event.addListener(overlayMap, 'idle', function() {
		overlayMap.getDiv().style.zIndex = 10;
	});
}

