function GMapTrack(name, points, levels, waypoints, map, bounds, opacity)
{
	this.name = name;
	this.points = points;
	this.levels = levels;
	this.waypoints = waypoints;
	this.map = map;
	this.trackcolour = "#ff0000"; // red
	this.trackwidth = 5;
	this.bounds = bounds;
	this.opacity = opacity||0.5;

	this.CreatePolyline();

	var span = this.GetBounds().toSpan();
	this.size = Math.sqrt(Math.pow(span.lat(), 2) + Math.pow(span.lng(), 2));
	
	this.isAddedToMap = false;
}

GMapTrack.prototype.GetName = function()
{
	return this.name;
}

GMapTrack.prototype.CreatePolyline = function()
{
	this.polyline = new GPolyline.fromEncoded({
		color: this.trackcolour,
		weight: this.trackwidth,
		points: this.points,
		levels: this.levels,
		opacity: this.opacity,
		zoomFactor: 2,
		numLevels: 18
	});
}

// Set the colour of the track line segements.
GMapTrack.prototype.SetTrackColour = function(colour, opacity)
{
	this.trackcolour = colour;
	this.opacity = opacity||0.5;
	
	var wasAddedToMap = this.isAddedToMap;
	
	if (wasAddedToMap)
		this.RemoveFromMap();

	this.CreatePolyline();
	
	if (wasAddedToMap)
		this.AddToMap();
}

// Set the width of the track line segements
GMapTrack.prototype.SetTrackWidth = function(width)
{
	this.trackwidth = width;
}

GMapTrack.prototype.CreateMarker = function(latitude, longitude, contents)
{
	var marker = new GMarker(new GLatLng(latitude,longitude)); //, icon);
	
	this.trackMarker = marker;
	
	return marker;
}

GMapTrack.prototype.Add = function()
{
	this.AddToMap();

	if (this.waypoints != null)
		this.AddWaypointsToMap(this.waypoints);
}


GMapTrack.prototype.AddToMap = function()
{
	if (!this.isAddedToMap)
	{
		this.map.addOverlay(this.polyline);
		if (this.trackMarker != null)
			this.map.addOverlay(this.trackMarker);

		this.isAddedToMap = true;
	}
}

GMapTrack.prototype.RemoveFromMap = function()
{
	if (this.isAddedToMap)
	{
		this.map.removeOverlay(this.polyline);
		if (this.trackMarker != null)
			this.map.removeOverlay(this.trackMarker);
		this.isAddedToMap = false;
	}
}

GMapTrack.prototype.Hide = function()
{
	if (this.polyline.supportsHide())
		this.polyline.hide();

	if (this.trackMarker != null)		
		this.trackMarker.hide();
}

GMapTrack.prototype.Show = function()
{
	if (this.polyline.supportsHide())
		this.polyline.show();

	if (this.trackMarker != null)		
		this.trackMarker.show();
}

GMapTrack.prototype.GetBounds = function()
{
	return this.bounds;
}

GMapTrack.prototype.GetSize = function()
{
	return this.size;
}

GMapTrack.prototype.SetTrackOnClick = function(delegate)
{
	GEvent.addListener(this.polyline, "click", delegate);
}

GMapTrack.CentreAndZoom = function (map, bounds, maptype)
{
	// Center around the middle of the points
	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds), maptype);
}

GMapTrack.prototype.AddWaypointsToMap = function (waypoints)
{
	for (var i=0; i < waypoints.length; i++)
	{
		this.AddWaypointMarker(new GLatLng(waypoints[i][0],waypoints[i][1]), waypoints[i][2]);
	}
}

GMapTrack.prototype.AddWaypointMarker = function(latlng, contents)
{
	var marker = new GMarker(latlng);

	GEvent.addListener(marker, "click",
		function()
		{
			marker.openInfoWindowHtml(contents);
		}
	);
	
	this.map.addOverlay(marker);
}
