function TripItinerary()
{
	// object definition
	this.locations = new Array();
	this.markers = new Object();	// hash, actually.
	this.lines = new Object();	// hash, actually.
	this.lastLat = 0;
	this.lastLon = 0;
	this.lastCityID = 0;
	this.minLat = 180;
	this.minLon = 180;
	this.maxLat = -180;
	this.maxLon = -180;
	
	// instance methods
	this.parse = TripItinerary_parse;
	this.removeAt = TripItinerary_removeAt;
	this.swapNeighbors = TripItinerary_swapNeighbors;
	this.undraw = TripItinerary_undraw;
	this.renderSummaryTable = TripItinerary_renderSummaryTable;
	this.renderMap = TripItinerary_renderMap;
	this.renderFeature = TripItinerary_renderFeature;
	this.renderLineTo = TripItinerary_renderLineTo;
	this.hideOverlays = TripItinerary_hideOverlays;
	this.setCenter = TripItinerary_setCenter;
	this.toString = TripItinerary_toString;
	
	// constructor
}


function TripItinerary_parse(raw)
{
	var lines = raw.split("\n");
	for (var a=0; a<lines.length; a++)
	{
		if (lines[a] != "")
		{
			var location = new TripLocation();
			if (location.parse(lines[a]))
			{
				this.locations[this.locations.length] = location;
			}
		}
		
	}
}

function TripItinerary_removeAt(index)
{
	this.undraw(index);
	
	this.locations.splice(index, 1);
}

// move the selected location up one notch
function TripItinerary_swapNeighbors(index)
{
	this.undraw(index);
	this.undraw(index-1);

	var out = this.locations.splice(index, 1);
	this.locations.splice(index-1, 0, out[0]);
}

function TripItinerary_undraw(index)
{
	if (index > 0)
	{
		
		var key = this.locations[index-1].cityID + "_" + this.locations[index].cityID;
		var polyline = this.lines[key];
		if (polyline)
		{
			map.removeOverlay(polyline);
			this.lines[key] = null;
		}
	}
	if (index < this.locations.length-1)
	{
		var key = this.locations[index].cityID + "_" + this.locations[index+1].cityID;
		var polyline = this.lines[key];
		if (polyline)
		{
			map.removeOverlay(polyline);
			this.lines[key] = null;
		}
	}
	
	var marker = this.markers[this.locations[index].cityID];
	if (marker)
	{
		map.removeOverlay(marker);
		this.markers[this.locations[index].cityID] = null;
	}
}

function TripItinerary_renderSummaryTable(table)
{
	while( table.rows.length > 1 )
	{
		table.deleteRow( 1 );
	}
	
	var rowCount = this.locations.length;
	for (var a=0; a<rowCount; a++)
	{
		if (a > 0)
		{
			var tr = table.insertRow(-1);
			var td = document.createElement("TD");
			td.setAttribute("colspan",  4);
			td.colSpan = 4; // to keep IE happy
			td.appendChild(document.createElement("HR"));
			tr.appendChild(td);
		}

		this.locations[a].renderSummaryRow(table.insertRow(-1), a, rowCount);
	}
}

//
// requires global variable "map", and Google map script
//
function TripItinerary_renderMap()
{
	//map.clearOverlays();
	this.hideOverlays();
	
	for (var a=0; a<this.locations.length; a++)
	{
		var location = this.locations[a];
		
		if (a > 0)
		{
			this.renderLineTo(location.lon, location.lat, location.cityID);
		}

		this.renderFeature(location.lon, location.lat, location.cityID, location.locationName);
	}

}

function TripItinerary_renderFeature(lon, lat, id, name)
{
	var point = new GLatLng(lat, lon);
	var marker = this.markers[id];
	if (!marker)
	{
		// reuse markers when possible
		marker = new GMarker(point, starMarker);
		marker.placeName = name;
		map.addOverlay(marker);
		
		this.markers[id] = marker;
	}
	else
	{
		//marker.display(true);
	}
	
	this.lastLat = lat;
	this.lastLon = lon;
	this.lastCityID = id;
	
	if (lat < this.minLat)
		this.minLat = lat;
	if (lon < this.minLon)
		this.minLon = lon;
	if (lat > this.maxLat)
		this.maxLat = lat;
	if (lon > this.maxLon)
		this.maxLon = lon;

}

function TripItinerary_renderLineTo(lon, lat, cityID)
{
//alert("lineto: " + lon + ", " + lat + ", " + cityID);
	var key = this.lastCityID + "_" + cityID;
	var polyline = this.lines[key];
	if (!polyline)
	{

		polyline = new GPolyline([new GLatLng(this.lastLat, this.lastLon),
					new GLatLng(lat, lon)],
					"#ff8000", 3, 1);
		map.addOverlay(polyline);
		this.lines[key] = polyline;
	}
	else
	{
		//polyline.display(true);
	}
}

function TripItinerary_hideOverlays()
{
/*
	for (var key in this.markers)
	{
		this.markers[key].display(false);
	}
	
	for (var key in this.lines)
	{
		this.lines[key].display(false);
	}
*/	

/*
	for (var key in this.markers)
	{
		map.removeOverlay(this.markers[key]);
	}
	
	for (var key in this.lines)
	{
		map.removeOverlay(this.lines[key]);
	}
*/
}

function TripItinerary_setCenter()
{
	var center = new GLatLng( (this.maxLat+this.minLat)/2, (this.maxLon+this.minLon)/2 );
	var bounds = new GLatLngBounds(
						new GLatLng(this.maxLat, this.minLon)
						, new GLatLng(this.minLat, this.maxLon)
						);
	
	var minZoom = map.getBoundsZoomLevel(bounds);
//alert(minZoom);	
	if (minZoom > 8)
	{
		minZoom = 8;
	}
	
	map.setCenter(center, minZoom);
}


function TripItinerary_toString()
{
	var out = "";
	
	for (var a=0; a<this.locations.length; a++)
	{
		var location = this.locations[a];
		out += location.toString() + "\r\n";
	}
	
	return out;
}





