Index: src/webapp/webclient/js/GnuBoat.js
===================================================================
--- src/webapp/webclient/js/GnuBoat.js	(revision 008bce99c15e3dad6fcf395f8c3a3f69b23b4b28)
+++ src/webapp/webclient/js/GnuBoat.js	(revision f04997d4738d8161eb8785b3791aee6c56618cba)
@@ -216,22 +216,4 @@
 	GnuBoat.private.renderControls = new THREE.OrbitControls( GnuBoat.private.renderCamera, GnuBoat.private.renderRenderer.domElement );
 	GnuBoat.private.renderControls.zoomSpeed = 2;
-	
-	
-	
-	var deep = new THREE.Color(0x347C7C);
-	var shallow = new THREE.Color(0xBCD9D9);
-	var size = 5;
-	GnuBoat.private.renderDepthSteps = 50;
-
-	var deltaR = (Math.max(deep.r, shallow.r) - Math.min(deep.r, shallow.r))/GnuBoat.private.renderDepthSteps;
-	var deltaG = (Math.max(deep.g, shallow.g) - Math.min(deep.g, shallow.g))/GnuBoat.private.renderDepthSteps;
-	var deltaB = (Math.max(deep.b, shallow.b) - Math.min(deep.b, shallow.b))/GnuBoat.private.renderDepthSteps;
-	for(var i = 0; i < GnuBoat.private.renderDepthSteps; i++){
-		var stepColor = new THREE.Color(Math.max(deep.r, shallow.r) - i * deltaR,
-										Math.max(deep.g, shallow.g) - i * deltaG,
-										Math.max(deep.b, shallow.b) - i * deltaB);
-		GnuBoat.private.renderDepthMaterials.push(stepColor);
-	}
-	GnuBoat.private.renderDepthMaterials.push(shallow);
 }
 
@@ -279,8 +261,15 @@
 	mapBounds: null,
 	mapModusLive: true,
+	mapHotlineGradientDefault: { absolute: false, palette: { 0.0: 'green', 0.5: 'yellow', 1.0: 'red' } },
 	dataMappings: {
 		speedthroughwater: {display: true, map: true, mapActive: false, titleLong: "Speed Through Water", titleShort: "STW", unit: "kts"},
 		speedoverground:  {display: true, map: true, mapActive: false, titleLong: "Speed Over Ground", titleShort: "SOG", unit: "kts"},
-		waterdepth: {display: true, map: true, mapActive: true, titleLong: "Waterdepth", titleShort: "Depth", unit: "m"},
+		waterdepth: {
+			display: true, map: true, mapActive: true, titleLong: "Waterdepth", titleShort: "Depth", unit: "m",
+			gradient: {
+				absolute: true,
+				palette: { 0: 'DarkBlue', 2: 'DodgerBlue', 6: 'LightBlue', 10: 'AliceBlue'},
+			}
+		},
 		watertemperature: {display: true, map: true, mapActive: false, titleLong: "Watertemperature", titleShort: "Water", unit: "°C"},
 		gpsfix: {display: true, map: false, mapActive: false, titleLong: "GPS Fix", titleShort: "Fix", unit: ""},
@@ -295,6 +284,4 @@
 	renderControls: null,
 	renderCloud: null,
-	renderDepthMaterials: [],
-	renderDepthSteps: null,
 	
 	openLink: function(target)
@@ -385,4 +372,6 @@
 			// only initialize the map (viewport) once
 			GnuBoat.private.map = L.map('mapViewport');
+			GnuBoat.private.map.on('overlayadd', function(layerObject){ GnuBoat.private.mapOverlayLegendSwitch(layerObject.layer); });
+			GnuBoat.private.map.on('overlayremove', function(layerObject){ GnuBoat.private.mapOverlayLegendSwitch(layerObject.layer); });
 		}
 		GnuBoat.private.map.setView([latitudeCenter, longitudeCenter], mapZoomStart);
@@ -480,6 +469,17 @@
 	},
 	
-	drawDensityEstimation: function(densityPoints, maximum, minimum)
-	{
+	drawDensityEstimation: function(densityPoints, gradient, maximum, minimum)
+	{
+		var colors = GnuBoat.private.gradientToRenderColors(gradient, maximum, minimum);
+		// converting all indizes into numbers to sort them correctly
+		// in descending order e.g. 10, 2, 1, 0 and not 2, 10, 1, 0
+		var colorsIndizes = Object.getOwnPropertyNames(colors);
+		colorsIndizes.sort(function(a, b){
+			a = +a;
+			b = +b;
+			return a-b;
+		});
+		var colorsCount = colorsIndizes.length;
+		
 		var material = new THREE.PointsMaterial({vertexColors: THREE.VertexColors, size: 5});
 		var geometry = new THREE.Geometry();
@@ -491,9 +491,13 @@
 			var mercator = GnuBoat.private.mercator(densityPoints[i].latitude, densityPoints[i].longitude);
 			var vertex = new THREE.Vector3(mercator.x - mercatorSouthWest.x, mercator.y - mercatorSouthWest.y, (densityPoints[i].depth * -1) + maximum);
+			var step = (densityPoints[i].depth - minimum)/(maximum-minimum);
 			var color = new THREE.Color(0x000000);
-			if(GnuBoat.private.renderDepthSteps != null){
-				var step = Math.floor((densityPoints[i].depth - minimum)/((maximum-minimum)/GnuBoat.private.renderDepthSteps));
-				if(step >= 0 && step < GnuBoat.private.renderDepthMaterials.length)
-					color = GnuBoat.private.renderDepthMaterials[step];
+			for(var j = 0; j < colorsCount; j++){
+				if(j < colorsCount - 1)
+					color = colors[colorsIndizes[j]];
+				if(step <= colorsIndizes[j+1]){
+					color = colors[colorsIndizes[j]];
+					break;
+				}
 			}
 			geometry.vertices.push(vertex);
@@ -612,4 +616,5 @@
 		}
 		
+		$("#mapLegend").empty();
 		var maximas = [];
 		var minimas = [];
@@ -630,16 +635,18 @@
 			for(var i = 0; i < keysMap.length; i++){
 				var key = keysMap[i];
-				if(key == "gpsfix")
+				var value = track[time][key];
+				if(!$.isNumeric(value))
 					continue;
 				if(!(key in maximas))
-					maximas[key] = track[time][key]
-				else if(maximas[key] < track[time][key])
-					maximas[key] = track[time][key];
+					maximas[key] = value
+				else if(maximas[key] < value)
+					maximas[key] = value;
 				if(!(key in minimas))
-					minimas[key] = track[time][key]
-				else if(minimas[key] > track[time][key])
-					minimas[key] = track[time][key];
-				points[key].push(new L.latLng(track[time]["latitude"], track[time]["longitude"], track[time][key]));
-				densityPoints.push({latitude: track[time]["latitude"], longitude: track[time]["longitude"], depth: track[time]["waterdepth"]});
+					minimas[key] = value
+				else if(minimas[key] > value)
+					minimas[key] = value;
+				points[key].push(new L.latLng(track[time]["latitude"], track[time]["longitude"], value));
+				if(key == "waterdepth")
+					densityPoints.push({latitude: track[time]["latitude"], longitude: track[time]["longitude"], depth: value});
 			}
 		}
@@ -647,8 +654,48 @@
 			var key = keysMap[i];
 			if(points[key].length > 0){
-				if(key in GnuBoat.private.mapPolylines)
+				var palette = GnuBoat.private.mapHotlineGradientDefault.palette;
+				var paletteLegend = GnuBoat.private.mapHotlineGradientDefault.palette;
+				if("gradient" in GnuBoat.private.dataMappings[key]){
+					palette = GnuBoat.private.gradientToPalette(GnuBoat.private.dataMappings[key]["gradient"], maximas[key], minimas[key]);
+					// use original palette, to use absolute values, if possible
+					paletteLegend = GnuBoat.private.dataMappings[key]["gradient"]["palette"];
+				}
+				
+				var legendTable = $('<table id="'+ key +'" class="hidden"></table>');
+				$("<tr><th colspan=2>"+ GnuBoat.private.dataMappings[key]["titleShort"] +" in "+ GnuBoat.private.dataMappings[key]["unit"] +"</th></tr>").appendTo(legendTable);
+				$("<tr><th>Level</th><th>Color</th></tr>").appendTo(legendTable);
+				// converting all indizes into numbers to sort them correctly
+				// e.g. 0, 1, 2, 10 and not 0, 1, 10, 2
+				var indizes = Object.getOwnPropertyNames(paletteLegend);
+				indizes.sort(function(a, b){
+					a = +a;
+					b = +b;
+					return a-b;
+				});
+				for(var j=0; j < indizes.length; j++){
+					var index = indizes[j];
+					$('<tr><td class="level">'+ index.toString() +'</td><td style="background-color: '+ paletteLegend[index] +'"></td></tr>').appendTo(legendTable);
+				}
+				$('<tr><td colspan="2">Extrema: '+ maximas[key].toString() +' / '+ minimas[key].toString() +'</td></tr>').appendTo(legendTable);
+				
+				var hotlineOptions = {
+					weight: 3,
+					outlineWidth: 0,
+					min: minimas[key],
+					max: maximas[key],
+					palette: palette,
+				};
+				
+				legendTable.appendTo("#mapLegend");
+			
+				if(key in GnuBoat.private.mapPolylines){
+					GnuBoat.private.mapPolylines[key].setStyle(hotlineOptions);
 					GnuBoat.private.mapPolylines[key].setLatLngs(points[key]);
+					if(GnuBoat.private.map.hasLayer(GnuBoat.private.mapPolylines[key]))
+						GnuBoat.private.mapOverlayLegendSwitch(GnuBoat.private.mapPolylines[key]);
+					GnuBoat.private.mapPolylines[key].unbindPopup();
+				}
 				else{
-					GnuBoat.private.mapPolylines[key] = L.hotline(points[key], {weight: 3, outlineWidth: 0, min: minimas[key], max: maximas[key]});
+					GnuBoat.private.mapPolylines[key] = L.hotline(points[key], hotlineOptions);
 					if(GnuBoat.private.mapControl){
 						var name = GnuBoat.private.dataMappings[key]["titleLong"] +" in "+ GnuBoat.private.dataMappings[key]["unit"] +" ("+ GnuBoat.private.dataMappings[key]["titleShort"] +")";
@@ -658,7 +705,83 @@
 						GnuBoat.private.mapPolylines[key].addTo(GnuBoat.private.map);
 				}
-			}
-		}
-		GnuBoat.private.drawDensityEstimation(densityPoints, maximas["waterdepth"], minimas["waterdepth"]);
+				GnuBoat.private.mapPolylines[key].bindPopup(legendTable[0].outerHTML);
+			}
+		}
+		var gradient = GnuBoat.private.mapHotlineGradientDefault.palette;
+		if("gradient" in GnuBoat.private.dataMappings["waterdepth"])
+			gradient = GnuBoat.private.dataMappings["waterdepth"]["gradient"];
+		GnuBoat.private.drawDensityEstimation(densityPoints, gradient, maximas["waterdepth"], minimas["waterdepth"]);
+	},
+	
+	gradientToPalette: function(gradient, maximum, minimum)
+	{
+		if(!("palette" in gradient)){
+			return GnuBoat.private.mapHotlineGradientDefault.palette;
+		}
+		if(!("absolute" in gradient) || !gradient.absolute){
+			// palette is already relative
+			return gradient.palette;
+		}
+		var range = maximum - minimum;
+		if(range == 0){
+			console.log("Range is zero --> Could not make gradient relative");
+			return  GnuBoat.private.mapHotlineGradientDefault.palette;
+		}
+		var palette = {};
+		var addedStop = false;
+		for(var indexAbs in gradient.palette){
+			var indexRel = (indexAbs - minimum)/range;
+			if(indexRel < 0){
+				// add the nearest minimum value (which might be configured smaller than minimum)
+				indexRel = 0;
+				palette[indexRel] = gradient.palette[indexAbs];
+				addedStop = true;
+			}
+			else if(indexRel < 1){
+				palette[indexRel] = gradient.palette[indexAbs];
+				addedStop = true;
+			}
+			else{
+				// add first index > 1 as the maximum value at 1
+				indexRel = 1;
+				palette[indexRel] = gradient.palette[indexAbs];
+				addedStop = true;
+				break;
+			}
+		}
+		if(!addedStop)
+			return GnuBoat.private.mapHotlineGradientDefault.palette;
+		return palette;
+	},
+	
+	gradientToRenderColors: function(gradient, maximum, minimum)
+	{
+		var paletteRelative = GnuBoat.private.gradientToPalette(gradient, maximum, minimum);
+
+		var colors = {};
+		for(var index in paletteRelative)
+			colors[index] = new THREE.Color(paletteRelative[index].toLowerCase());
+
+		return colors;
+	},
+
+	mapOverlayLegendSwitch: function(layer)
+	{
+		var overlayKey = null;
+		for(var key in GnuBoat.private.mapPolylines){
+			if(GnuBoat.private.mapPolylines.hasOwnProperty(key)){
+				if(GnuBoat.private.mapPolylines[key] === layer){
+					overlayKey = key;
+					break;
+				}
+			}
+		}
+		// ECMAScript 6
+// 		var overlayKey = Object.keys(GnuBoat.private.mapPolylines).find(key => GnuBoat.private.mapPolylines[key] === overlay);
+		if(overlayKey == null)
+			return;
+		if(overlayKey){
+			$("#mapLegend #"+ overlayKey).toggleClass("hidden");
+		}
 	},
 };
