
//<![CDATA[

function gMap_Polyline() {

	this.points = new Array();
	this.name = "";
	this.polyline = null;
	this.km = 0;
	this.miles = 0;
	this.onMap = false;
	this.color = null;
	this.startMarker = false;
	this.endMarker = false;

	this.addPoint = gMap_addPolylinePoint;
	this.removePoint = gMap_removePolylinePoint;
	this.removeLastPoint = gMap_removeLastPolylinePoint;
	this.clearPoints = gMap_clearPolylinePoints;
	this.draw = gMap_drawPolyline;
	this.addStartEndMarkers = gMap_addPolylineStartEndMarkers;
	this.serializePoints = gMap_serializePolylinePoints;
	this.unserializePoints = gMap_unserializePolylinePoints;

}

function gMap_PolylinePoint(latLng, lineColor, lineWidth) {

	this.lat = latLng.lat();
	this.lng = latLng.lng();
	this.color = lineColor;
	this.width = lineWidth;

	this.toGLatLng = gMap_PolylinePointToGLatLng;

}

function gMap_PolylinePointToGLatLng() {
	if (this.lat && this.lng) {
		obj = new GLatLng(this.lat, this.lng);
		return obj;
	} else {
		return false;
	}
}

function gMap_addPolylinePoint(latLng, lineWidth) {
	newPoint = new gMap_PolylinePoint(latLng, this.color, lineWidth);
	this.points[this.points.length] = newPoint;
}

function gMap_removePolylinePoint(index) {
	if (this.points[index]) {
		for (c = index + 1; c <= this.points.length; c++) {
			this.points[c - 1] = this.points[c];
		}
		this.points.pop();
		this.draw();
		this.addStartEndMarkers();
	}
}

function gMap_removeLastPolylinePoint() {
	if (this.points.length > 0) {
		this.points.pop();
		this.draw();
		this.addStartEndMarkers();
	}
}

function gMap_clearPolylinePoints() {
	this.points = new Array();
	this.draw();
}

function gMap_unserializePolylinePoints(serialized) {
	this.clearPoints();
	pointData = serialized.split("|");
	for (var i in pointData) {
		point = pointData[i].toString();
		if (point.length > 0) {
			pointLatLng = point.split(",");

			if (pointLatLng[0] && pointLatLng[1]) {
				latLng = new GLatLng(pointLatLng[0], pointLatLng[1]);
				this.addPoint(latLng);
			}
		}
	}
	this.draw();
}

function gMap_serializePolylinePoints() {
	serialized = "";
	for (var i in this.points) {
		point = this.points[i];
		if (point.lat && point.lng) {
			serialized += point.lat + "," + point.lng + "|";
		}
	}
	serialized = serialized.substring(0, serialized.length - 1);
	return serialized;
}

function gMap_drawPolyline(weight, opacity) {
	latLngs = new Array();

	for (var i in this.points) {
		point = this.points[i];
		if (point.lat && point.lng) {
			latLng = new GLatLng(point.lat, point.lng);
			latLngs[latLngs.length] = latLng;
		}
	}

	if (!this.color || typeof(this.color) == "undefined") {
		color = "#0000FF";
	} else {
		color = this.color;
	}

	if (!weight) weight = 5;
	if (!opacity) opacity = 0.75;

	if (this.onMap) {
		gMap.removeOverlay(this.polyline);
		if (this.startMarker) gMap.removeOverlay(this.startMarker);
		if (this.endMarker) gMap.removeOverlay(this.endMarker);
		this.onMap = false;
	}

	this.polyline = new GPolyline(latLngs, color, weight, opacity);

	this.km = (this.polyline.getLength() / 1000).toFixed(3);
	this.miles = (this.polyline.getLength() / 1609.344).toFixed(3);

	gMap.addOverlay(this.polyline);
	this.onMap = true;
}

function gMap_addPolylineStartEndMarkers() {
	if (this.polyline) {
		if (this.points.length >= 1) {
			firstPoint = this.points[0].toGLatLng();
			this.startMarker = gMap_addMarker(this.points[0].toGLatLng());
		}
		if (this.points.length > 1) {
			this.endMarker = gMap_addMarker(this.points[this.points.length - 1].toGLatLng());
		}
	}
}

//]]>

