
//
// Add things to YMap
//


YMap.prototype.addPolyLine = function(polyline) {
	if (!this.polylines) {
		this.polylines = [];
	}
	this.polylines.push(polyline);
	var themap = this;
	YEvent.Capture(this, EventsList.changeZoom, function() { polyline.redraw(themap); } ); 
	polyline.redraw(this);
}
YMap.prototype.removePolyLine = function(polyline) {
	// TODO: what's the opposite of YEvent.capture?
	// remove the custom overlay	
	if (polyline.overlay) {	
		this.removeOverlay(polyline.overlay);
		polyline.overlay = null;
	}
}
YMap.prototype.clearPolyLines = function() {
	for (var i = this.polylines.length-1; i >= 0; i--) {
		this.removePolyLine(this.polylines[i]);
	}
	this.polylines = [];
}

//
// Make a PolyLine object
//

function PolyLine(points) {
	this.points = points;
}

PolyLine.prototype.redraw = function(_map) {

	// no point drawing an un-line
	if (!this.points || !this.points.length || this.points.length < 2) {
		return;
	}

	// make a transformed polyline
	this.tpoly = [];

	// project into coord space (requires ymercator.js)
	for (var i = 0; i < this.points.length; i++) {
		this.tpoly[i] = project(this.points[i],_map);
	}

	// get extents of transformed coords
	this.minX = this.tpoly[0].x;
	this.minY = this.tpoly[0].y;
	this.maxX = this.tpoly[0].x;
	this.maxY = this.tpoly[0].y;
	for (var i = 0; i < this.tpoly.length; i++) {
		this.minX = Math.min(this.tpoly[i].x,this.minX);
		this.minY = Math.min(this.tpoly[i].y,this.minY);
		this.maxX = Math.max(this.tpoly[i].x,this.maxX);
		this.maxY = Math.max(this.tpoly[i].y,this.maxY);
	}

	// remove any existing overlay
	if (this.overlay) {	
		_map.removeOverlay(this.overlay);
		this.overlay = null;
	}

	// create a custom overlay at the lat lon of the top left point
	this.overlay = new YCustomOverlay(_map.convertXYLatLon(new YCoordPoint(this.minX, this.minY)));
	_map.addOverlay(this.overlay);

	
	if(this.graphic) {
		this.graphic = null;
	}

	// canvas version:
	if (window.CanvasRenderingContext2D) {
		// make a <canvas>
		this.graphic = YUtility.createNode('canvas','poly'); // TODO uid, not 'poly'?
		// kick start Explorer Canvas (NB: excanvas.js has been modified slightly for this demo!)
		if (window.G_vmlCanvasManager) {
			G_vmlCanvasManager.initElement(this.graphic);
		}
	}
	// image version:
	else { 
		this.graphic = YUtility.createNode('img','poly'); // TODO uid, not 'poly'?
	}

	// set size (belt and braces)
	this.graphic.width = Math.round(this.maxX-this.minX);
	this.graphic.height = Math.round(this.maxY-this.minY);
	this.graphic.style.width = this.graphic.width + 'px';
	this.graphic.style.height = this.graphic.height + 'px';
	// set position and zindex
	this.graphic.style.position = 'absolute';
	this.graphic.style.zIndex = '-1';

	// canvas version:
	if (window.CanvasRenderingContext2D) {
		this.ctx = this.graphic.getContext("2d");

		this.ctx.clearRect(0, 0, this.graphic.width, this.graphic.height);

		// set line style
		this.ctx.lineWidth = "3";
		this.ctx.strokeStyle = "rgb(0, 0, 255)";

		// draw poly
		this.ctx.translate(-this.minX, -this.minY);
		this.ctx.beginPath();
		this.ctx.moveTo(this.tpoly[0].x, this.tpoly[0].y);
		for(var i = 1; i < this.tpoly.length; i++) {
			this.ctx.lineTo(this.tpoly[i].x, this.tpoly[i].y);
		}
		this.ctx.stroke();
	}
	// image version:
	else {
		// draw poly
		this.pointString = (this.tpoly[0].x-this.minX) + ',' + (this.tpoly[0].y-this.minY); // no leading comma
		for(var i = 1; i < this.tpoly.length; i++) {
			this.pointString += ',' + (this.tpoly[i].x-this.minX) + ',' + (this.tpoly[i].y-this.minY);
		}
		this.graphic.src = 'polyline.php?width='+this.graphic.width+'&height='+this.graphic.height+'&points='+this.pointString;
	}

	YUtility.appendNode(this.overlay,this.graphic);
}

