//alert("enter tripMgr");
//---- constants ---------------------------------------------------------------
var kPathWidth = 4.0;
var kPathTransparency= 0.8;
var dimension = kDimensions[arg.dim ? arg.dim : 0];
var noiseThreshold = 500;
var longUpdateInterval = 1 * 60;	// 1 Minute

//--------------------------------------------------------------------------
// randomColor
//..........................................................................
function randomColor() {
	var colors = [	"#000","#008","#00f","#080","#088","#08f","#0f0","#0f8","#0ff",
					"#800","#808","#80f","#880","#888","#88f","#8f0","#8f8","#8ff",
					"#f00","#f08","#f0f","#f80","#f88","#f8f","#ff0","#ff8","#fff"];	
	return colors[ Math.floor ( Math.random() * 26.9999) ];
}
//---- _tripMgr -----------------------------------------------------------------
var theTripMgr = 0;
function _tripMgr ( data, map, infoBoxID ) {
	theTripMgr = this;
	var tripList = [];
	var selectedTrip = {leg:false,elem:false};
	var infoOwner = false;
	this.lastUpdate = new Date();
	this.nextLongUpdate = new Date();
	this.pathLen = -1;
	this.barograf = 0;
	
	this.mouseEnter = function ( evt ) {
		this.barograf.mouseEnter(evt);
	}
	
	this.mouseExit = function ( evt ) {
		this.barograf.mouseExit(evt);
	}
	
	this.mouseMove = function ( evt ) {
		this.barograf.mouseMove(evt);
	}
	
	this.mouseDown = function ( evt ) {
		this.barograf.mouseDown(evt);
	}
	
	this.mouseUp = function ( evt ) {
		this.barograf.mouseUp(evt);
	}
	//--------------------------------------------------------------------------
	// newData
	//..........................................................................
	//	new data have arrived
	this.newData = function ( data ) {
		for ( var i = 0; i < data.length; ++i ) {
			var leg = tripList.findLeg(data[i].id);
			// append any waypoints
			if ( leg ) {
				leg.addWaypoints ( data[i] );
			}
			// create a new leg
			else {
				leg = new _trip ( data[i], map );
				tripList.insertLeg(leg);
				if ( infoBoxID && !leg.isTaskElement() ) {
					leg.insertInfoBox ( infoBoxID );
				}
			}
		}
	}
	//--------------------------------------------------------------------------
	// makeMeTheInfoOwner
	//..........................................................................
	this.makeMeTheInfoOwner = function ( trip ) {
		if (infoOwner)
			infoOwner.myInfoWindow = false;
		infoOwner = trip;
		infoOwner.myInfoWindow = true;
	}
	//--------------------------------------------------------------------------
	// handleNameClick
	//..........................................................................
	//	select a trip and keep ist centered on the map
	this.handleNameClick = function ( legID, elem ) {
		if ( selectedTrip.elem !== false ) {
			var same = selectedTrip.elem === elem.parentNode;
			selectedTrip.leg.select(false,selectedTrip.elem);
			selectedTrip.elem = false;
			if ( same ) return;
		}
		selectedTrip.elem = elem.parentNode;
		selectedTrip.leg = tripList.findLeg(legID);
		selectedTrip.leg.select(true,elem.parentNode,map);
	}
	//--------------------------------------------------------------------------
	// handleInfoBoxClick
	//..........................................................................
	this.handleInfoBoxClick = function ( legID, elem ) {
		tripList.findLeg(legID).handleInfoBoxClick(elem);
	}
	//--------------------------------------------------------------------------
	// handleVisibleRadioClick
	//..........................................................................
	this.handleVisibleRadioClick = function ( legID ) {
		tripList.findLeg(legID).handleVisibleRadioClick();
	}
	//--------------------------------------------------------------------------
	// handleBaroButtonClick
	//..........................................................................
	this.handleBaroButtonClick = function ( legID ) {
		if ( _$('baro').getContext ) {
			var trip = tripList.findLeg(legID);
			if ( this.barograf ) {
				if ( this.barograf.tripID() == legID ) {
					delete this.barograf;
					this.barograf = 0;
					showHide ( 'baro' );
					resize();
				}
				else {
					delete this.barograf;
					this.barograf = new barograph ( trip, 'baro' );
					this.barograf.draw();
				}
			}
			else {
				showHide ( 'baro' );
				resize();
				this.barograf = new barograph ( trip, 'baro' );
				this.barograf.draw();
			}
		}
		else
			alert(_s("canvas browser"));
	}
	//--------------------------------------------------------------------------
	// hideTrip
	//..........................................................................
	// hide this trip. If legID == undefined hide all
	this.hideTrip = function ( legID ) {
		for ( var i = 0; i < tripList.length; ++i ) {
			if ( legID == undefined || tripList[i].id == legID ) {
				tripList[i].hide();
			}
		}
	}
	//--------------------------------------------------------------------------
	// showTrip
	//..........................................................................
	// show this trip. If legID == undefined show all
	this.showTrip = function ( legID ) {
		for ( var i = 0; i < tripList.length; ++i ) {
			if ( legID == undefined || tripList[i].id == legID ) {
				tripList[i].show();
			}
		}
	}
	//--------------------------------------------------------------------------
	// jumpForward
	//..........................................................................
	//	fast move toTime
	this.jumpForward = function ( toTime ) {
		for ( var i = 0; i < tripList.length; ++i )
			tripList[i].jumpForward( toTime );
	}
	//--------------------------------------------------------------------------
	// jumpBack
	//..........................................................................
	//	fast move toTime
	this.jumpBack = function ( toTime ) {
		for ( var i = 0; i < tripList.length; ++i )
			tripList[i].jumpBack( toTime );
	}
	//--------------------------------------------------------------------------
	// nextGapTime
	//..........................................................................
	//	returns the time of the last waypoint
	this.nextGapTime = function ( ) {
		var closest = new Date();
		for ( var i = 0; i < tripList.length; ++i ) {
			var gapTime = tripList[i].nextGapTime();
			if ( gapTime != REACHED_THE_END )
				if ( gapTime < closest )
					closest.setTime(gapTime.getTime());
		}
		return closest;
	}
	//--------------------------------------------------------------------------
	// lastGapTime
	//..........................................................................
	//	returns the time of the last waypoint
	this.lastGapTime = function ( ) {
	// delete new Date???
		var closest = new Date(0);
		for ( var i = 0; i < tripList.length; ++i ) {
			var gapTime = tripList[i].lastGapTime();
			if ( gapTime != REACHED_THE_BEGINNING )
				if ( gapTime > closest )
					closest.setTime(gapTime.getTime());
		}
		return closest;
	}
	this.centerTrip = function ( userID ) {
		if ( userID == undefined && tripList.length > 0 )
			tripList[0].centerOnMap();
		else {
			var leg = tripList.findLeg(userID);
			if ( leg ) 
				leg.centerOnMap();
		}
			
	}
	// delete new Date???
	this.startTime = function ( ) {
		var start = new Date();
		for ( var i = 0; i < tripList.length; ++i ) {
			var tripStart = tripList[i].startWaypoint();
			if ( tripStart.time < start )
				start = tripStart.time;
		}
		return start;
	}
	// delete new Date???
	this.endTime = function ( ) {
		var end = new Date(0);
		for ( var i = 0; i < tripList.length; ++i ) {
			var tripEnd = tripList[i].endWaypoint();
			if ( tripEnd.time > end )
				end = tripEnd.time;
		}
		return end;
	}
	//--------------------------------------------------------------------------
	// redrawPath	
	//..........................................................................
	//	select or deselect this leg
	this.redrawPath = function ( ) {
		for ( var i = 0; i < tripList.length; ++i )
			tripList[i].redrawPath();
	}
	//--------------------------------------------------------------------------
	// setPathLen
	//..........................................................................
	// 
	this.setPathLen = function ( seconds ) {
		if ( seconds )
			this.pathLen = seconds * 1000;
		else if ( this.pathLen == -1 )
			if ( _$('tripLength') )
				this.setPathLen(_$('tripLength').options[_$('tripLength').selectedIndex].value);
	}
	this.pathStartsAtTime = function ( ) {
		this.setPathLen();
		var milli = ( this.pathLen == -1 ? 24 * 60 * 60 * 1000 : this.pathLen );
		return new Date( this.lastUpdate.getTime() - milli );
	}
	this.initializeCurrentLocation = function(atTime) {
		for ( var i = 0; i < tripList.length; ++i )
			tripList[i].initializeCurrentLocation( atTime );
	}
	this.update = function ( atTime ) {
		this.lastUpdate.copy(atTime);
		var bigUpdateNow = atTime.getTime() > this.nextLongUpdate.getTime();
		for ( var i = 0; i < tripList.length; ++i ) {
			var leg = tripList[i];
			leg.setCurrentLocation( atTime );
			var overtime = leg.currentLocation.overTheEnd();
			leg.printAltitudeLabel();
			if ( leg.selected && !overtime ) {
				leg.centerOnMap();
			}
			if ( this.barograf && 
				 this.barograf.tripID() == leg.id && 
				 this.barograf.restore() ) {
					this.barograf.draw();
			}
			if ( infoBoxID && !leg.isTaskElement() ) {
				var wp;
				if ( overtime  ) {
					wp = leg.endWaypoint();
					var arrival = wp.time.toLocaleTimeString().substring(0,5);
					leg.printInfoBox ( sprintf(_s("um %s angekommen."), arrival ) );
				}
				else {
					wp = leg.currentLocation;
					leg.printDataBox();
				}
				if ( leg.infoBoxIsOpen() )
					leg.updateOpenInfoBox(wp,bigUpdateNow);
			}
		}
		if ( bigUpdateNow )
			this.nextLongUpdate.add(longUpdateInterval);
			
	}
	this.destruct = function() {
		for ( var i = 0; i < tripList.length; ++i ) {
			tripList[i].destruct(map);
			delete tripList[i];
			tripList[i] = 0;
			delete tripList;
			tripList = [];
			_$(infoBoxID).innerHTML = "";
		}
		selectedTrip = {leg:false,elem:false};
		theTripMgr = 0;
	}
	this.newData ( data );
	_$('baro').addEventListener('mouseover', this.mouseEnter.bind(this),false);
	_$('baro').addEventListener('mousemove', this.mouseMove.bind(this), false);
	_$('baro').addEventListener('mouseout',  this.mouseExit.bind(this), false);
	_$('baro').addEventListener('mousedown',  this.mouseDown.bind(this), false);
	_$('baro').addEventListener('mouseup',  this.mouseUp.bind(this), false);
}
//alert("exit tripMgr");
/*
http://www.onlinecontest.org/olc-2.0/gliding/dataprov.html
Request Headers
Accept:application/xml, text/xml, 
Content-Type:application/x-www-form-urlencoded
Origin:http://www.onlinecontest.org
Referer:http://www.onlinecontest.org/olc-2.0/gliding/flightinfo.html?dsId=735738
User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; de-de) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16
X-Requested-With:XMLHttpRequest

<root> 
	<scoring timespan="06:50:59" distance="499.54" points="413.89" speed="72.93" class="18m" index="114.0" club="SG Olten" claimeddate="23.03.2009 17:57:00" igcvalid="d" status="s"/> 
	<map href="dataprov.html?nature=map&amp;ref=880439208"/> 
	<mapovl href="dataprov.html?nature=mapovl&amp;id=735738"/> 
	<scorepath> 
		<wp name="Start" lat="46°14.342N" lon="009°08.937E" elv="1006m" date="09:39:49" /> 
		<wp name="WP1" lat="45°50.529N" lon="007°47.582E" elv="4840m" date="12:15:35" /> 
		<wp name="WP2" lat="46°23.005N" lon="009°16.921E" elv="3559m" date="13:24:08" /> 
		<wp name="WP3" lat="46°07.681N" lon="008°18.330E" elv="2937m" date="14:32:23" /> 
		<wp name="WP4" lat="46°30.004N" lon="009°12.291E" elv="3223m" date="15:37:48" /> 
		<wp name="WP5" lat="46°06.440N" lon="008°49.330E" elv="2339m" date="16:07:32" /> 
		<wp name="Finish" lat="46°21.430N" lon="009°14.870E" elv="2048m" date="16:30:48" /> 
	</scorepath> 
	<statistic> 
		<leg name="Leg1" distance="113.58" nk="44.12" nlift="41" dh="1.71" gama="35.22" vquer="43.75" /> 
		<leg name="Leg2" distance="129.60" nk="28.32" nlift="10" dh="1.79" gama="38.47" vquer="113.43" /> 
		<leg name="Leg3" distance="80.27" nk="31.65" nlift="10" dh="1.70" gama="28.44" vquer="70.57" /> 
		<leg name="Leg4" distance="80.51" nk="24.18" nlift="9" dh="2.04" gama="48.83" vquer="73.85" /> 
		<leg name="Leg5" distance="52.64" nk="15.19" nlift="3" dh="1.55" gama="40.34" vquer="106.23" /> 
		<leg name="Leg6" distance="42.94" nk="19.91" nlift="2" dh="2.33" gama="45.68" vquer="110.73" /> 
		<leg name="Total" distance="499.54" nk="32.88" nlift="74" dh="1.78" gama="37.40" vquer="72.93" /> 
	</statistic> 
</root>

http://www.onlinecontest.org/olc-2.0/gliding/dataprov.html
Request Headers
Accept:application/xml, text/xml, 
Content-Type:application/x-www-form-urlencoded
Origin:http://www.onlinecontest.org
Referer:http://www.onlinecontest.org/olc-2.0/gliding/flightinfo.html?dsId=735738
User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; de-de) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16
X-Requested-With:XMLHttpRequest
<root> 
	<scoring timespan="06:58:05" distance="277.26" points="243.21" speed="39.79" class="18m" index="114.0" club="SG Olten" claimeddate="23.03.2009 17:57:00" igcvalid="d" status="s"/> 
	<map href="dataprov.html?nature=map&amp;ref=880439208"/> 
	<mapovl href="dataprov.html?nature=mapovl&amp;id=735739"/> 
	<scorepath> 
		<wp name="Start" lat="46°13.846N" lon="009°08.265E" elv="817m" date="09:39:02" /> 
		<wp name="WP1" lat="46°29.100N" lon="008°18.050E" elv="4012m" date="11:26:35" /> 
		<wp name="WP2" lat="45°50.874N" lon="007°51.437E" elv="5343m" date="12:13:27" /> 
		<wp name="WP3" lat="46°18.390N" lon="009°16.772E" elv="2997m" date="15:21:45" /> 
		<wp name="Finish" lat="46°14.361N" lon="009°08.041E" elv="1543m" date="16:37:07" /> 
	</scorepath> 
	<statistic> 
		<leg name="Leg1" distance="70.17" nk="45.92" nlift="27" dh="1.80" gama="33.04" vquer="39.15" /> 
		<leg name="Leg2" distance="78.65" nk="42.11" nlift="15" dh="1.53" gama="163.17" vquer="100.69" /> 
		<leg name="Leg3" distance="120.97" nk="27.84" nlift="26" dh="1.85" gama="14.80" vquer="38.55" /> 
		<leg name="Leg4" distance="13.45" nk="18.55" nlift="7" dh="1.80" gama="4.53" vquer="10.71" /> 
		<leg name="Total" distance="283.24" nk="32.33" nlift="74" dh="1.78" gama="20.64" vquer="40.65" /> 
	</statistic> 
</root> 
*/



