function GPolygonControl(myMap) {
	this.myMap = myMap;
	this.initVars();
	
	this.polyLines = new Array();
}

GPolygonControl.prototype = new GControl();

GPolygonControl.prototype.initVars = function() {
	this.points = new Array();
	this.markers = new Array();
	this.firstClick = true;
	this.monitored = true;
	this.clicked = false;
	this.clickHandler = null;
}

GPolygonControl.prototype.initialize = function(map) {
	this.button = document.createElement("div");
	this.setButtonStyle_(this.button, this.clicked);
	map.getContainer().appendChild(this.button);
	
	var callBack = GEvent.callback(this, this.doClick);
	GEvent.addDomListener(this.button, "click", callBack);
	
	return this.button;
}

GPolygonControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(111, 7));
}

GPolygonControl.prototype.setButtonStyle_ = function(button, clicked) {
	if (clicked)
		button.style.backgroundImage = "url(\"images/Bpd.png\")";
	else button.style.backgroundImage = "url(\"images/Bpu.png\")";
	button.style.width = "31px";
	button.style.height = "31px";
}

/*************
	Actions
**************/
GPolygonControl.prototype.doClick = function(overlay, point){

	this.clicked = !this.clicked;
	this.setButtonStyle_(this.button, this.clicked);
	
	if (this.clicked == false){
		GEvent.removeListener(this.clickHandler);
		this.initVars();
	}
	else {
		this.myMap.deactivateControls(this);
		this.clickHandler = GEvent.bind(this.myMap.map, "click", this, this.drawLine);
	}
	
}

GPolygonControl.prototype.drawLine = function(overlay, point){
	this.myMap.debug("drawLine<br/>", true);
	if (point != null){
		var marker = Line.prototype.createPoint(point, this.myMap);
		this.markers.push(marker);
		this.points.push(point);
		
		this.myMap.debug("this.firstClick = " + this.firstClick + "<br/>", true);
		this.myMap.debug("this.markers.length = " + this.markers.length + "<br/>", true);
		
		if (!this.firstClick){
			var line = new Line(this.markers[this.markers.length - 2], marker, this.myMap, "#FF0000");
			line.draw();
			this.polyLines.push(line.getPolyline());
		} else {
			GEvent.bind(marker, "click", this, this.drawPolygon);
			this.myMap.map.addOverlay(marker);
		}
		this.firstClick = false;
	}
}

GPolygonControl.prototype.drawPolygon = function(event){
	this.myMap.debug("drawPolygon", true);
	this.points.push(this.points[0]);
	for (var i=0; i<this.polyLines.length; i++){
		this.myMap.map.removeOverlay(this.polyLines[i]);
	}
	this.myMap.map.addOverlay(new GPolygon(this.points, "00FF00", true, 3, 0.5));
}
