 //		Routen-Klasse
 //		 Autor: Hajo Schmitt
 //		 Letzte Änderung: 3.11.2010
 
 
 function VFRRoute(thisMap) {
  this.map = thisMap; // Referenz auf Google Map
  this.route = null; // die eigentliche Route
  this.PolylineOverlay = null; // zum merken des Overlays
  this.Marker  = []; // dito für die Marker zum Entfernen
 }
 
 VFRRoute.prototype.load = function(thisRoute, bZoom2Route, rteDiv){
 // initialisiert aus  JSON, es wird das Routen-Array ausgewertet (query.route)
  if (thisRoute) {
	this.Route = thisRoute;
    this.addToMap(); // ab in die Karte
	if (this.Route.length > 1) {
//		GLog.write("bZoom2Route " + bZoom2Route);
		if (bZoom2Route) {
			this.ZoomToRoute();
		} else {
			this.CenterOnLast();
		}
	}
	
	// die Route als Liste:
	if (rteDiv) this.fillRouteDiv (rteDiv);
	
	
	// else this.Clear(); // leere Route->Clear
  }
}
 
 // Private: fügt geladene Route als Overlay hinzu
 VFRRoute.prototype.addToMap = function() {
 
	try {
		if ( !this.Route ) {
			return;
		}
		
		// GLog.write("Add to Map  ..");
		
		iconStart=new GIcon(G_DEFAULT_ICON, "http://maps.google.com/mapfiles/dd-start.png");
		iconMid=new GIcon(G_DEFAULT_ICON, "http://maps.google.com/mapfiles/marker.png");
		iconDest=new GIcon(G_DEFAULT_ICON, "http://maps.google.com/mapfiles/dd-end.png");

		// alte Marker weg
		this.Clear();

		
		var line = []; // leeres Array
		this.Marker  = []; 
		myTotalDistance = 0;
		for (var i=0;i<this.Route.length;i++) {
			// alert ( this.Route[i].key );
			myCoord = new GLatLng(this.Route[i].lat,this.Route[i].lon);
			
			// der Marker des Wegpunkts
			if (i==0) {
				myMarker = new GMarker(myCoord,{draggable: false, icon:iconStart, title:this.Route[i].name});
			} else if (i==this.Route.length-1) {
				myMarker = new GMarker(myCoord,{draggable: false, icon:iconDest, title:this.Route[i].name});
			} else {
				myMarker = new GMarker(myCoord,{draggable: false, icon:iconMid, title:this.Route[i].name});
			}
			myMarker.Wpt = this.Route[i]; // den Wegpunkt anhängen for-further-study
			// Neu: noch die Gesamtentfernung mitziehen
			myTotalDistance = myTotalDistance + myMarker.Wpt.dist;
			myMarker.Wpt.TotalDist = myTotalDistance;
			// und das Click noch verdrahten		
			GEvent.addListener(myMarker, "click", function () {MarkerClicked(this);} );
			this.map.addOverlay(myMarker);
			this.Marker.push(myMarker);	// merken fürs entfernen
			// und in das Linien-Array
			line.push(myCoord);			
		}
		
		// neues Linienoverlay dazu
		if (line.length > 1) {
			this.PolylineOverlay = new GPolyline(line,"#ff0000", 3, 1,{geodesic:true})
			this.map.addOverlay(this.PolylineOverlay);
		}

	} catch (e) {
		alert ( 'addToMap: ' + e );
	}
  }
 
 VFRRoute.prototype.ZoomToRoute = function() {
   // BoundBox bestimmen und zoomen
//   GLog.write("ZoomToRoute");
	if(this.PolylineOverlay) {
		var bounds = new GLatLngBounds(); 
		bounds.extend(this.PolylineOverlay.getBounds().getSouthWest());
		bounds.extend(this.PolylineOverlay.getBounds().getNorthEast()); 
		this.map.setCenter( bounds.getCenter(), this.map.getBoundsZoomLevel(bounds)); 
   }
 }
 
  VFRRoute.prototype.CenterOnLast = function() {
	if (this.Marker.length > 0) {
		this.map.setCenter( this.Marker[this.Marker.length -1].getLatLng()); 
	}
 }
  
 VFRRoute.prototype.Clear = function() {
  // alte Marker weg
	// GLog.write("Clear ..");
	for (var i=0;i<this.Marker.length;i++) {
		this.map.removeOverlay(this.Marker[i]);
		// GLog.write("Marker " + i);
	}
 // alte Polyline weg
	if(this.PolylineOverlay) this.map.removeOverlay(this.PolylineOverlay);
	
	this.Marker = [];	
	this.PolylineOverlay = null;
 }

 VFRRoute.prototype.fillRouteDiv = function ( rteDiv ) 
 {
	var results = dojo.byId(rteDiv);
	while (results.firstChild)  {
		results.removeChild(results.firstChild);
	}
	
	if ( this.Route.length == 0 ) {
		document.getElementById(rteDiv).innerHTML = "Keine Route erkannt.";
	}
	
 	for (var i=0;i<this.Route.length;i++) {
		//myHtml += RenderWpt2HTML(this.Route[i]);	
		var newWpt = createElement('div', 'wptItem', 'onClick="javascript:centerWptOnMap(this);"');
		newWpt.setAttribute('onClick', 'javascript:centerWptOnMap(this);');

		newWpt.innerHTML = RenderWpt2Div(this.Route[i]);
		newWpt.Wpt = this.Route[i];
		document.getElementById(rteDiv).appendChild(newWpt);
	}
 }

function centerWptOnMap(thisWptDiv) {
		glbMap.setCenter(new GLatLng(thisWptDiv.Wpt.lat, thisWptDiv.Wpt.lon));
} 
function MarkerClicked(thisMarker) {
	
	// var myText = "<b>" + thisMarker.Wpt.name + "</b><br/>"
	
	// if  (thisMarker.Wpt.dist > 0 ) {
		
	//	myText += "True Course: <b>" + thisMarker.Wpt.TC.toFixed(0) + "°</b><br/>"
	//	myNM = thisMarker.Wpt.dist / 1.852;
	//	myText += "Leg: " + thisMarker.Wpt.dist.toFixed(1) + " km / <b>" + myNM.toFixed(1) + " nm</b><br/>"
	//	myNM = thisMarker.Wpt.TotalDist / 1.852;
	//	myText += "Total: " + thisMarker.Wpt.TotalDist.toFixed(1) + " km / <b>" + myNM.toFixed(1) + " nm</b><br/>"
	
	
	// }
	
	myText = RenderWpt2HTML(thisMarker.Wpt);

	thisMarker.openInfoWindowHtml(myText);	
}

function RenderWpt2HTML(thisWpt) {

	var myText = "<table>";
	myText += "<tr><td><b>" + thisWpt.name + "</b></td><td></td></tr>";
	
	if  (thisWpt.dist > 0 ) {
		myText += "<tr><td>True Course:</td><td><b>" + thisWpt.TC.toFixed(0) + "°</b></td></tr>";
		myNM = thisWpt.dist / 1.852;
		myText += "<tr><td>Leg:</td><td>" + thisWpt.dist.toFixed(1) + " km / <b>" + myNM.toFixed(1) + " nm</b></td></tr>";
		myNM = thisWpt.TotalDist / 1.852;
		myText += "<tr><td>Total:</td><td>" + thisWpt.TotalDist.toFixed(1) + " km / <b>" + myNM.toFixed(1) + " nm</b></td></tr>";
		
		if (thisWpt.tas > 0) {
			// die Zeiten
			myText += "<tr><td>Time:</td><td><b>" + thisWpt.timestr + "</b></td></tr>";
			myText += "<tr><td>Tot. Time:</td><td><b>" + thisWpt.TTimestr + "</b></td></tr>";
			// TAS
			myNM = thisWpt.tas / 1.852;
			myText += "<tr><td>TAS:</td><td>" + thisWpt.tas.toFixed(0) + " km/h / <b>" + myNM.toFixed(0) + " kt</b></td></tr>";

			// optional Winddreieck
			if (thisWpt.WS > 0) {
				myNM = thisWpt.GS / 1.852;
				myText += "<tr><td>GS:</td><td>" + thisWpt.GS.toFixed(0) + " km/h / <b>" + myNM.toFixed(0) + " kt</b></td></tr>";
				myNM = thisWpt.WS / 1.852;
				myText += "<tr><td>Wind:</td><td><b>" + thisWpt.WD.toFixed(0) + "° / " +  myNM.toFixed(0)  + "kt</b> / " + thisWpt.WS.toFixed(0) + "km/h</td></tr>";
				myText += "<tr><td>Hdg:</td><td><b>" + thisWpt.HD.toFixed(0) + "°</b></td></tr>";
			}
		}

	}
	myText += "</table>";
	return myText;
}	

function RenderWpt2Div(thisWpt) {
// Variante für Routen-Liste
	bHasWind = thisWpt.WS > 0;
    bHasTAS = thisWpt.tas > 0;
	bHasDist = thisWpt.dist > 0;

	var myText = '<div class="WptEntry">';
	myText += '<div class="WptName">' + thisWpt.name + '</div>';	
	if  (bHasDist) {
		myText += '<table>'
		myText += '<tr><td class="WptPrompt">TC:</td><td  class="WptValue">' + thisWpt.TC.toFixed(0) + '°</td>';
		
		if (bHasWind > 0) {
				myText += '<tr><td class="WptPrompt">TH:</td><td class="WptValue">' + thisWpt.HD.toFixed(0) + '°</td></tr>';
		} else {
				myText += '<td colspan="2"></td></tr>';
		}		
	
		
		
		myNM = thisWpt.dist / 1.852;
		//myText += '<tr><td  class="WptPrompt">Leg:</td><td class="WptValue">' + thisWpt.dist.toFixed(1) + ' km / ' + myNM.toFixed(1) + ' nm</td>';
		myText += '<tr><td  class="WptPrompt">Leg:</td><td class="WptValue" title="' + thisWpt.dist.toFixed(1) + ' km">' + myNM.toFixed(1) + ' nm</td>';
		myNM = thisWpt.TotalDist / 1.852;
		//myText += '<td  class="WptPrompt">Total:</td><td class="WptValue">' + thisWpt.TotalDist.toFixed(1) + ' km / ' + myNM.toFixed(1) + ' nm</td></tr>';
		myText += '<td  class="WptPrompt">Total:</td><td class="WptValue" title="'  + thisWpt.TotalDist.toFixed(1) + ' km">'  + myNM.toFixed(1) + ' nm</td></tr>';
		
		if (bHasTAS> 0) {
			// die Zeiten
			myText += '<tr><td  class="WptPrompt">Time:</td><td class="WptValue">' + thisWpt.timestr + '</td>';
			myText += '<td  class="WptPrompt">Tot. Time:</td><td class="WptValue">' + thisWpt.TTimestr + '</td></tr>';

			// TAS
			myNM = thisWpt.tas / 1.852;
			myText += '<tr><td  class="WptPrompt">TAS:</td><td class="WptValue" title="' + thisWpt.tas.toFixed(0) + ' km/h">' +  myNM.toFixed(0) + ' kt</td>';
			if (bHasWind > 0) {
				myNM = thisWpt.GS / 1.852;
				myText += '<td  class="WptPrompt">GS:</td><td class="WptValue" title="' + thisWpt.GS.toFixed(0) + ' km/h">' + myNM.toFixed(0) + ' kt</td></tr>';
			} else {
					myText += '<td colspan="2"></td></tr>';
			}		

			// optional Winddreieck
			if (bHasWind > 0) {
				myNM = thisWpt.WS / 1.852;
				// myText += '<tr><td  class="WptPrompt">Wind:</td><td class="WptValue">' + thisWpt.WD.toFixed(0) + '° / ' +  myNM.toFixed(0)  + 'kt / ' + thisWpt.WS.toFixed(0) + 'km/h</td><td colspan="2"></td></tr>';
				myText += '<tr><td  class="WptPrompt">Wind:</td><td class="WptValue">' + thisWpt.WD.toFixed(0) + '° / ' +  myNM.toFixed(0)  + 'kt</td><td colspan="2"></td></tr>';
			}
		}

		myText += '</table>'
	}
	myText += '</div>';
	return myText;
}	
    
  
