//------------------------------------------------------------------------------
// class _trip	
//..............................................................................
//	info box elements:
//	data box id (fieldset)		user
//	altitude box id				alt
//	vertical speed box id		vs
//	speed box id				speed
//	speed value id				spd

var MINIMUM_TIME_FOR_GAP = 300;
var MINIMUM_ELEVATION = -300;

function _trip ( data, map ) {
	this.id		= data.id;
	this.name	= (data.name ? data.name : "deleted");	
	this.map	= map;	
	this.box	= false;	// user box in the side bar
	this.selected= false;	// hilits the info box and centers the map
	this.path = 0;			// google polyline
	this.marker = false;	// google marker
	this.tempMarker = false;	// google marker
	this.color;				// color of the path and the box
	this.myInfoWindow = false;
	this.elevation = MINIMUM_ELEVATION;	// elevation of current location if infoWindow
	this.place = "-";		// name of current place if infoWindow
	this.pathWidth;			// width of the path
	this.pathTransparency = kPathTransparency;
	this.wpPointer;			// start waypoint
	this.currentLocation;	// interpolated waypoint for the actual display location
	this.lastInsertedWP;	// final waypoint
	this.pathEndWP;			// path ends at this wp
	this.visible;			// marker and path 
	//--------------------------------------------------------------------------
	// addWaypoints	
	//..........................................................................
	//	gets all data available at the database
	this.isTaskElement = function () {
		return this.isTurningPoint() || this.isSector();
	}
	this.isTurningPoint = function () {
		return (this.name == 'Ziel' || 
				this.name == ' Start' || 
				this.name == '1. WP' || 
				this.name == '2. WP' || 
				this.name == '3. WP' || 
				this.name == '4. WP' || 
				this.name == '5. WP' || 
				this.name == '6. WP');
	}
	this.isSector = function () {
		return (this.name == '#');
	}
	//--------------------------------------------------------------------------
	// addWaypoints	
	//..........................................................................
	//	gets all data available at the database
	this.addWaypoints = function ( data ) {
		for ( var i = 0; i < data.loc.length; ++i ) {
			this.lastInsertedWP = 
				new _waypoint ( data.loc[i], this.lastInsertedWP );
		}
	}
	//--------------------------------------------------------------------------
	// initializeCurrentLocation	
	//..........................................................................
	//	finds the interpolated waypoint at a given time
	this.initializeCurrentLocation = function ( atTime ) {
		var portion = this.wpPointer.portion ( atTime );
		while ( portion > 1.0 ) {
			this.wpPointer = this.wpPointer.leading;
			portion = this.wpPointer.portion ( atTime );
		}
		this.currentLocation = this.wpPointer.tempWaypoint ( portion, atTime );
		if (this.marker)
			this.marker.moveTo ( this.currentLocation.gLatLon );
	}
	//--------------------------------------------------------------------------
	// setCurrentLocation	
	//..........................................................................
	//	finds the interpolated waypoint at a given time
	this.setCurrentLocation = function ( atTime ) {
		var portion = this.wpPointer.portion ( atTime );
		var collectNewInfo = false;
		while ( portion > 1.0 ) {
			this.wpPointer = this.wpPointer.leading;
			this.wpPointer.insertIntoPath ( this.path );
			portion = this.wpPointer.portion ( atTime );
			collectNewInfo = true;
		}
		while ( ( portion < 0.0 ) && ( portion > BEGIN_OF_TRIP ) ) {
			this.redrawPath();
			this.wpPointer = this.wpPointer.preceding;
			portion = this.wpPointer.portion ( atTime );
		}
		delete this.currentLocation;
		this.currentLocation = this.wpPointer.tempWaypoint ( portion, atTime );
		if (this.marker) {
			this.marker.moveTo ( this.currentLocation.gLatLon );
			if ( this.infoWindow() ) {
				if ( collectNewInfo ) {
					this.getPlaceAndElevation();
					this.showInfoBubble();
				}
				else
					this.updateInfoBubble();
			}
		}
	}
	//--------------------------------------------------------------------------
	// redrawPath	
	//..........................................................................
	this.redrawPath = function ( ) {
		// remove the old path
		if ( this.path ) {
			map.removeOverlay(this.path);
			delete this.path;
		}
		var newPath = [];
		var start = (this.isTaskElement() ? 0 : theTripMgr.pathStartsAtTime().getTime());
		for ( var wp = this.wpPointer; wp != false; wp = wp.preceding ) {
			if ( start > wp.time.getTime() ) break;
			newPath.push( wp.gLatLon );
		}
		// create the path line
		this.path = new GPolyline ( newPath, this.color, this.pathWidth,this.pathTransparency);
		this.map.addOverlay ( this.path );
	}
	//--------------------------------------------------------------------------
	// redrawMarker	
	//..........................................................................
	this.redrawMarker = function ( ) {
		var that = this;
		if (!this.isSector()) {
			this.marker = new MarkerPack(this.currentLocation.gLatLon, radarIcon, this.map );
			this.marker.addClickHandler( function ( overlay, point ) {
				that.handleMarkerClick ( overlay, point );
			});
			this.marker.setLabels ( [this.name, '', this.wpPointer.altitude, '', '']);
		}
	}
	//--------------------------------------------------------------------------
	// tempMarker	
	//..........................................................................
	this.setTempMarker = function ( wp ) {
		this.tempMarker = new GMarker(wp.gLatLon, {draggable: false});
		this.map.addOverlay(this.tempMarker);
	}
	this.centerTempMarker = function ( wp ) {
		this.map.setMapCenterCoordinates(wp.gLatLon);
	}
	this.moveTempMarker = function ( wp ) {
		this.tempMarker.setPoint(wp.gLatLon);
	}
	this.removeTempMarker = function ( ) {
		this.map.removeOverlay(this.tempMarker);
		this.tempMarker = 0;
	}
	//--------------------------------------------------------------------------
	// hide
	//..........................................................................
	this.hidePath = function ( ) {
		if (this.path) {
			map.removeOverlay(this.path);
			delete this.path;
			this.path = 0;
		}
	}
	this.hideMarker = function ( ) {
		if (this.marker) {
			this.marker.destruct(map);
			this.marker = 0;
		}
	}
	this.hide = function ( ) {
		this.hidePath();
		this.hideMarker();
		if ( this.visible ) {
			var radio = _$(this.valueID("visibility"));
			radio.checked = this.visible = false;
			var box = _$(this.valueID("user"));
			box.className = "user invisible";
		}
	}		

	//--------------------------------------------------------------------------
	// show
	//..........................................................................
	this.show = function ( ) {
		if ( this.path == 0 )
			this.redrawPath();
		if ( this.marker == 0 )
			this.redrawMarker();
		if ( this.visible === false ) {
			var radio = _$(this.valueID("visibility"));
			radio.checked = this.visible = true;
			var box = _$(this.valueID("user"));
			box.className = (this.selected ? "user selected" : "user" );
		}
	}
	//--------------------------------------------------------------------------
	// select	
	//..........................................................................
	//	select or deselect this leg
	this.select = function ( on, elem, map ) {
		if ( this.selected = on ) {
			if ( elem ) elem.className = "user selected";
			map.setMapCenterCoordinates(this.currentLocation.gLatLon);
		}
		else if ( this.visible ) {
			elem.className = "user";
		}
		else if ( this.visible ) {
			elem.className = "user invisible";
		}
	}
	//--------------------------------------------------------------------------
	// trimPath	
	//..........................................................................
	//	returns true if the private part is visible
	this.trimPath = function ( fromTime ) {
		this.hidePath();
		this.redrawPath();
	}
	//--------------------------------------------------------------------------
	// infoBoxIsOpen	
	//..........................................................................
	//	returns true if the private part is visible
	this.infoBoxIsOpen = function ( ) {
		var div = _$(this.valueID("private"));
		return div.className != "hidden";
	}
	//--------------------------------------------------------------------------
	// jumpForward
	//..........................................................................
	//	fast move toTime
	this.jumpForward = function ( toTime ) {
		var start = this.wpPointer;
		while ( this.wpPointer.leading && this.wpPointer.leading.time < toTime )
			this.wpPointer = this.wpPointer.leading
		if ( start != this.wpPointer ) {
			this.redrawPath();
			this.setCurrentLocation(toTime);
		}
	}
	//--------------------------------------------------------------------------
	// nextGapTime
	//..........................................................................
	//	returns the time of the last waypoint
	this.nextGapTime = function ( ) {
		if ( this.wpPointer.leading == false )
			return REACHED_THE_END;
		var wp = this.wpPointer.leading;
		while ( wp.leading )
			if ( wp.leading.timeElapsed > MINIMUM_TIME_FOR_GAP )
				break;
			else
				wp = wp.leading;
		return wp.time;
	}
	//--------------------------------------------------------------------------
	// lastGapTime
	//..........................................................................
	//	returns the time of the last waypoint
	this.lastGapTime = function ( ) {
		if ( this.wpPointer.preceding == false )
			return REACHED_THE_BEGINNING;
		var wp = this.wpPointer.preceding;
		while ( wp.preceding )
			if ( wp.timeElapsed > MINIMUM_TIME_FOR_GAP )
				break;
			else
				wp = wp.preceding;
		return wp.time;
	}
	//--------------------------------------------------------------------------
	// centerOnMap
	//..........................................................................
	this.centerOnMap = function() {
		this.map.setMapCenterCoordinates(this.currentLocation.gLatLon);
	}
	//--------------------------------------------------------------------------
	// endWaypoint	
	//..........................................................................
	//	returns the time of the last waypoint
	this.endWaypoint = function ( ) {
		return this.lastInsertedWP;
	}
	//--------------------------------------------------------------------------
	// printAltitudeLabel	
	//..........................................................................
	//	prints the infos into the marker label
	this.printAltitudeLabel = function ( ) {
		var alt = this.altitudeString();
		if (this.currentLocation.noise > noiseThreshold)
			alt = "<span style=\"color:red;\">" + alt + "</span>";
		if (this.marker)
			this.marker.setHTML ( 2, alt );
	}
	//--------------------------------------------------------------------------
	// printDataBox	
	//..........................................................................
	//	prints altitude, speed and vario into the sidebar infobox
	this.altitudeString = function ( ) {
		var alt = this.currentLocation.altitude * dimension.altitude.factor;
		alt = alt.toFixed(0);
		if (alt > 0)
			return alt + dimension.altitude.dim;
		return "-";
	}
	this.elevationString = function ( ) {
		if ( this.elevation <= MINIMUM_ELEVATION )
			return "-";
		var h = this.elevation * dimension.altitude.factor;
		h = h.toFixed(0);
		return h + dimension.altitude.dim;
	}
	this.heightString = function ( trailer ) {
		if ( this.elevation <= MINIMUM_ELEVATION )
			return "";
		var h = Math.max(this.currentLocation.altitude - this.elevation, 0) * dimension.altitude.factor;
		h = h.toFixed(0);
		trailer = ( trailer == undefined ? "" : trailer );
		return trailer + h + dimension.altitude.dim;
	}
	this.distanceString = function ( dist ) {
		dist = dist * dimension.distance.factor;
		dist = dist.toFixed(dist < 9.95 ? 3 : dist < 99.95 ? 2 : dist < 999.95 ? 1 : 0);
		return dist + dimension.distance.dim;
	}
	this.speedString = function ( speed ) {
		var spd = (speed == undefined ? this.currentLocation.hSpeed : speed ) * dimension.hSpeed.factor;
		spd = spd.toFixed(spd < 9.95 ? 1 : 0);
		return spd + dimension.hSpeed.dim;
	}
	this.varioString = function ( vs ) {
		var vario = (vs == undefined ? this.currentLocation.vSpeed : vs ) * dimension.vSpeed.factor;
		vario = vario.toFixed(vario < 9.95 ? 1 : 0);
		vario = ( ( vario > 0 ? "+" : "" ) + vario );
		return vario + dimension.vSpeed.dim;
	}
	this.printDataBox = function ( ) {
		var alt = this.currentLocation.altitude * dimension.altitude.factor;
		var spd = this.currentLocation.hSpeed * dimension.hSpeed.factor;
		var vario = this.currentLocation.vSpeed * dimension.vSpeed.factor;
		var vs = vario.toFixed( ( vario > 10 || vario < -10 ? 0 : 1 ) );
		vs = ( ( vario > 0 ? "+" : "" ) + vs )
		var _alt = _$(this.valueID("alt"));
		var _spd = _$(this.valueID("speed"));
		var _vs  = _$(this.valueID("vs"));
		var spd_ = _$(this.valueID("spd")).value;
		// print the speed only if they differ more tha 4%
		if ((spd_ != spd) && (Math.min(spd_,spd) / Math.max(spd_,spd)<0.97)) {
			delete ( _spd.innerHTML );
			_spd.innerHTML = spd.toFixed(0) + dimension.hSpeed.dim;
			_$(this.valueID("spd")).value = spd;
		}
		delete ( _alt.innerHTML );
		delete ( _vs.innerHTML );
		_alt.innerHTML = alt.toFixed(0) + dimension.altitude.dim;
		_vs.innerHTML = vs + dimension.vSpeed.dim;
		_vs.className = ( vario > 0.5 ? 		"vs green"
							: ( vario < -0.5 ?	"vs red" 
							: 					"vs" ) );
		if (this.currentLocation.noise > noiseThreshold)
			_$(this.valueID("user")).style.backgroundColor = "#f66";
		else
			_$(this.valueID("user")).style.backgroundColor = "#ddf";
	}
	//--------------------------------------------------------------------------
	// printInfoBox	
	//..........................................................................
	//	prints message into the sidebar infobox
	this.printInfoBox = function ( msg ) {
		if ( _$(this.valueID("alt")).innerHTML ) {
			delete ( _$(this.valueID("alt")).innerHTML );
			_$(this.valueID("alt")).innerHTML = "";
			delete ( _$(this.valueID("vs")).innerHTML );
			_$(this.valueID("vs")).innerHTML = "";
			delete ( _$(this.valueID("speed")).innerHTML );
		}
		_$(this.valueID("speed")).innerHTML = msg;
	}
	//--------------------------------------------------------------------------
	// updateOpenInfoBox	
	//..........................................................................
	//	
	this.updateOpenInfoBox = function ( wp, deep ) {
		var distance = 0, average = 0, seconds, average;
		var limit = theTripMgr.pathStartsAtTime();
		var limitWP = wp;
		while ( limitWP.preceding != 0 ) {
			if ( limitWP.preceding.time.getTime() < limit.getTime() )
				break;
			distance += limitWP.distance;
			limitWP = limitWP.preceding;
		}
		if ( seconds = wp.time.difference(limitWP.time) )
			average = distance / seconds;
		// time
		var timeBox = _$(this.valueID("time"));
		if ( timeBox.innerHTML )
			delete timeBox.innerHTML;
		timeBox.innerHTML = formatTimeString(seconds);
		// distance
		var distBox = _$(this.valueID("distance"));
		if ( distBox.innerHTML )
			delete distBox.innerHTML;
		distBox.innerHTML = this.distanceString(distance);
		// speed
		var averageBox = _$(this.valueID("average"));
		if ( averageBox.innerHTML )
			delete averageBox.innerHTML;
		averageBox.innerHTML = this.speedString(average);
		// course
		var courseBox = _$(this.valueID("course"));
		if ( courseBox.innerHTML )
			delete courseBox.innerHTML;
		courseBox.innerHTML = wp.course.toFixed(0) + "˚";
		//
		if ( deep )
			this.getPlaceAndElevation();
		var heightBox = _$(this.valueID("height"));
		if ( heightBox.innerHTML )
			delete heightBox.innerHTML;
		heightBox.innerHTML = this.heightString();
		//
		var placeBox = _$(this.valueID("place"));
		if ( placeBox.innerHTML )
			delete placeBox.innerHTML;
		placeBox.innerHTML = this.place;
		//
		var elevBox = _$(this.valueID("elev"));
		if ( elevBox.innerHTML )
			delete elevBox.innerHTML;
		elevBox.innerHTML = this.elevationString();
	}
	//--------------------------------------------------------------------------
	// insertInfoBox	
	//..........................................................................
	//	creates the info box in the sidebar
	this.insertInfoBox = function ( containerID, mgr ) {
		var container = _$ ( containerID );
		container.innerHTML = container.innerHTML + this.createInfoBox ( );
	}
	//--------------------------------------------------------------------------
	// createInfoBox	
	//..........................................................................
	//	creates the info box in the sidebar
	this.createInfoBox = function ( mgr ) {
		var name = this.name;
		var nameBoxTitle = sprintf(_s("Klicke um den Benutzer %s auf der Karte zu zentrieren"),name);
		var fieldTitle = sprintf(_s("Klicke um die Einstellungen fuer den Benutzer %s zu sehen"),name);
		var visibleTip = _s("Pfad auf der Karte ein- oder ausblenden");
		var timeTip = _s("Laenge des Pfades in hh:mm:ss");
		var crsTip = _s("Bewegungsrichtung");
		var distanceTip = _s("Laenge des Pfades");
		var averageTip = _s("Durchschnittliche Geschwindigkeit ueber die ganze Pfadlaenge");
		var elevationTip = _s("Gelaendehoehe ueber Meer");
		var heightTip = _s("Hoehe ueber Grund oder GPS Ungenauigkeit");
		var placeTip = _s("Name der Ortschaft");
		if ( name.length > 18 )
			name = name.substring(0,16) + '...';
		return	'<fieldset class="user" id="' + this.valueID("user") + '" ' +
					'style="border-color: ' + this.color + ';">' +
						'<legend onclick="theTripMgr.handleNameClick(' + this.id + ', this)" ' +
							'title="' + nameBoxTitle + '" >' + 
							name + 
						'</legend>' +
						'<div ' +
							'onclick="theTripMgr.handleInfoBoxClick(' + this.id + ', this)" ' +
							'title="' + fieldTitle + '" >' +
							'<div class="alt"   id="' + this.valueID("alt")	+ '"></div>' +
							'<div class="vs"    id="' + this.valueID("vs")	+ '"></div>' +
							'<div class="speed" id="' + this.valueID("speed") + '"></div>' +
							'<input type="hidden" value="0" id="' + this.valueID("spd") + '"/>' +
						'</div>' +
						'<div class="hidden" id="' + this.valueID("private") + '" style="clear:both;">' +
							'<hr style="clear:both;" />' +
							'<input type="checkbox" checked="checked" ' +
								'id="' + this.valueID("visibility") + '" ' +
								'title="' + visibleTip + '" ' +
								'onchange="theTripMgr.handleVisibleRadioClick(' + this.id + ')" >' +
							'<label for="' + this.valueID("visibility") + '" ' + 
								'title="' + visibleTip + '" >' +
								_s("sichtbar") + '</label>' + 
								'<input type="button" value="' + _s("Barogramm") + '" ' + 
								'onclick="theTripMgr.handleBaroButtonClick(' + this.id + ')" ' + 
								'style="margin-left:10px;" /><br />' +
							'<p class="data">' +
							'<table cellspacing="0">' +
								'<body>' +
								'<tr title="' + timeTip + '" class="even" >' +
									'<td>' + _s("Time") + '</td>' +
									'<td id="' + this.valueID("time") + '">' + '</td>' +
								'</tr>' +
								'<tr title="' + crsTip + '" class="odd" >' +
									'<td>' + _s("course") + '</td>' +
									'<td id="' + this.valueID("course") + '">' + '</td>' +
								'</tr>' +
								'<tr title="' + distanceTip + '" class="even" >' +
									'<td>' + _s("distance") + '</td>' +
									'<td id="' + this.valueID("distance") + '">' + '</td>' +
								'</tr>' +
								'<tr title="' + averageTip + '" class="odd" >' +
									'<td>' + _s("average") + '</td>' +
									'<td id="' + this.valueID("average") + '">' + '</td>' +
								'</tr>' +
								'<tr title="' + elevationTip + '" class="even" >' +
									'<td>' + _s("elevation") + '</td>' +
									'<td id="' + this.valueID("elev") + '">' + '</td>' +
								'</tr>' +
								'<tr title="' + heightTip + '" class="odd" >' +
									'<td>' + _s("height") + '</td>' +
									'<td id="' + this.valueID("height") + '">' + '</td>' +
								'</tr>' +
								'<tr title="' + placeTip + '" class="even" >' +
									'<td>' + _s("place") + '</td>' +
									'<td id="' + this.valueID("place") + '">' + '</td>' +
								'</tr>' +
								'</body>' +
							'</table>' +
							'</p>' +
						'</div>' + 
				'</fieldset>';
	}
	//--------------------------------------------------------------------------
	// startWaypoint	
	//..........................................................................
	//	find the first waypoint
	this.startWaypoint = function ( ) {
		var wp = this.lastInsertedWP;
		while ( wp.preceding ) wp = wp.preceding;
		return wp;
	}
	//--------------------------------------------------------------------------
	// lastWaypoint	
	//..........................................................................
	//	find the last waypoint
	this.lastWaypoint = function ( ) {
		var arrived = this.lastInsertedWP.time.getTime() < this.currentLocation.time.getTime();
		return ( arrived ? this.lastInsertedWP : this.currentLocation );
	}
	//--------------------------------------------------------------------------this.lastInsertedWP
	// getPlaceAndElevation	
	//..........................................................................
	this.getPlaceAndElevation = function ( ) {
		var wp = this.lastWaypoint();
		var that = this;
		nearbyPlace ( wp.lat, wp.lon, function(obj) {
			if (obj.status)
				return;
			that.place = obj.geonames[0].name;
			if (obj.geonames[0].adminCode1)
				that.place += ' ' + obj.geonames[0].adminCode1;
		} );
		terrainElevation ( wp.lat, wp.lon, function(obj) {
			if (obj.status)
				return;
			if (obj.srtm3 > MINIMUM_ELEVATION)
				that.elevation = obj.srtm3;
			else
				that.elevation = MINIMUM_ELEVATION;
		} );
	}
	//--------------------------------------------------------------------------
	// InfoHTML	
	//..........................................................................
	//	
	this.infoWindow = function() {
		return (_$("infoWindow") && this.myInfoWindow ? true : false );
	}
	this.showInfoBubble = function ( ) {
		var content = '<div id="infoWindow">' + this.InfoHTML() + '</div>';
		this.map.openInfoWindow(this.currentLocation.gLatLon, content );
	}
	this.updateInfoBubble = function ( ) {
		_$("infoWindow").innerHTML = this.InfoHTML();
	}
	this.InfoHTML = function ( ) {
		var wp = this.lastWaypoint();
		var location = wp.gLatLon.formatDegMinSec();
		var course = (wp.course < 0 ? "-" : wp.course.toFixed(0) + "˚" );
		var html = 
		'<table class="infoTable" cellspacing="0">' +
		'<caption>' + this.name + '</caption>' + 
			'<tbody>' +
				'<tr class="even">' +
					'<td class="col1">' + _s("Date") + '</td>' +
					'<td class="col2">' + wp.time.localDate() + '</td>' +
				'</tr>' + 
				'<tr class="odd">' +
					'<td class="col1">' + _s("Time") + '</td>' +
					'<td class="col2">' + wp.time.unixTime() + '</td>' +
				'</tr>' + 
				'<tr class="even">' +
					'<td class="col1">' + _s("latitude") + '</td>' +
					'<td class="col2">' + location.lat + '</td>' +
				'</tr>' + 
				'<tr class="odd">' +
					'<td class="col1">' + _s("longitude") + '</td>' +
					'<td class="col2">' + location.lon + '</td>' +
				'</tr>' + 
				'<tr class="even">' +
					'<td class="col1">' + _s("altitude") + '</td>' +
					'<td class="col2" id="altitude">' + this.altitudeString() + this.heightString(" / ") + '</td>' +
				'</tr>' + 
				'<tr class="odd">' +
					'<td class="col1">' + _s("speed") + '</td>' +
					'<td class="col2">' + this.speedString() + '</td>' +
				'</tr>' + 
				'<tr class="even">' +
					'<td class="col1">' + _s("vario") + '</td>' +
					'<td class="col2">' + this.varioString() + '</td>' +
				'</tr>' + 
				'<tr class="odd">' +
					'<td class="col1">' + _s("course") + '</td>' +
					'<td class="col2">' + course + '</td>' +
				'</tr>' + 
				'<tr class="even">' +
					'<td class="col1">' + _s("elevation") + '</td>' +
					'<td class="col2" id="elevation">' + this.elevationString() + '</td>' +
				'</tr>' + 
				'<tr class="odd">' +
					'<td class="col1">' + _s("place") + '</td>' +
					'<td class="col2" id="ort">' + this.place + '</td>' +
				'</tr>' + 
			'</tbody>' + 
		'</table>';
		return html;
	}
	//--------------------------------------------------------------------------
	// destruct	
	//..........................................................................
	//	deletes a trip
	this.destruct = function ( map ) {
		map.removeOverlay(this.path);
		delete this.path;
		if (this.marker)
			this.marker.destruct(map);
		var wp;
		while ( wp = this.lastInsertedWP ) {
			this.lastInsertedWP = wp.preceding;
			wp.destruct();
			delete wp;
		}
	}	
/*-------- private stuff from here --------------------------------------------------------*/
	//--------------------------------------------------------------------------
	// valueID	
	//..........................................................................
	//	gets all data available at the database
	this.valueID = function ( value ) {
		return value + this.id;
	}
	//--------------------------------------------------------------------------
	// handleMarkerClick	
	//..........................................................................
	//
	this.handleMarkerClick = function ( overlay, point ) {
		theTripMgr.makeMeTheInfoOwner(this);
		this.getPlaceAndElevation();
		this.showInfoBubble();
	}
	//--------------------------------------------------------------------------
	// handleInfoBoxClick
	//..........................................................................
	this.handleInfoBoxClick = function ( elem ) {
		var div = _$(this.valueID("private"));
		if ( div.className == "hidden" ) {
			div.className = "visible";
			this.getPlaceAndElevation();
		}
		else {
			div.className = "hidden";
		}
	}
	//--------------------------------------------------------------------------
	// handleVisibleRadioClick
	//..........................................................................
	this.handleVisibleRadioClick = function ( ) {
		var radio = _$(this.valueID("visibility"));
		if ( radio.checked ) {
			this.show();
		}
		else {
			this.hide();
		}
	}
	this.visible = true;
	if (this.isTurningPoint()) {
		this.color = '#00f';
		this.pathTransparency = 0.45;
		this.pathWidth = 16;
	}
	else if (this.isSector()) {
		this.color = '#f00';
		this.pathWidth = 2;
	}
	else {
		this.color = randomColor();
		this.pathWidth = kPathWidth
	}
	// insert all initial waypoints
	this.addWaypoints ( data );
	// find the start waypoint by looping backwards
	this.wpPointer = this.startWaypoint();
	// get the initial display location
	this.currentLocation = this.wpPointer.tempWaypoint(0);
	// create the path line
	this.redrawPath();
	// create the marker with its labels
	this.redrawMarker();
}
