//var mapCircle = Class.create(); 

//mapCircle.emptyVertexIconUrl = "polyvertex-empty.png"; 
//
//mapCircle.vertexIconUrl = "polyvertex.png"; 
//
//mapCircle.crosshairsCursor = "crosshairs.cur"; 

//mapCircle.prototype.initialize = function( map /*arg*/ ) 

mapCircle = function( map /*arg*/ ) 

{ 

//	if( arg["map"] == null ) { 
//
//                alert( "Must pass map to area in constructor" ); 
//
//                return; 
//
//        } 

//        this.emptyVertexIconUrl = arg["emptyVertexIconUrl"] == null ? mapCircle.emptyVertexIconUrl : arg["emptyVertexIconUrl"]; 
		this.emptyVertexIconUrl = mapCircle.emptyVertexIconUrl;

//        this.vertexIconUrl = arg["vertexIconUrl"] == null ? mapCircle.vertexIconUrl : arg["vertexIconUrl"]; 
		this.vertexIconUrl = mapCircle.vertexIconUrl;

//        this.crosshairsCursor = arg["crosshairsCursor"] == null ? mapCircle.crosshairsCursor : arg["crosshairsCursor"]; 
		this.crosshairsCursor = mapCircle.crosshairsCursor;

        this.closed = false; 

        this.editing = false; 

//        this.map = arg["map"]; 
		this.map = map; 

        this.normalProj = G_NORMAL_MAP.getProjection(); 

        this.vertices = new Array(); 

        this.points = new Array(); 

//        this.editable = arg["editable"] == null ? false : 
//arg["editable"]; 
//
//        this.strokeColor = arg["strokeColor"] == null ? "#C602C8" : 
//arg["strokeColor"]; 
//
//        this.fillColor = arg["fillColor"] == null ? "#C602C8" : 
//arg["fillColor"]; 
//
//        this.strokeWidth = arg["strokeWidth"] == null ? 5 : 
//arg["strokeWidth"]; 
//
//        this.strokeOpacity = arg["strokeOpacity"] == null ? 0.8 : 
//arg["strokeOpacity"]; 
//
//        this.fillOpacity = arg["fillOpacity"] == null ? 0.3 : 
//arg["fillOpacity"]; 
//
//        this.centerLatLng = arg["centerLatLng"] == null ? null : 
//arg["centerLatLng"]; 
//
//        this.radiusLatLng = arg["radiusLatLng"] == null ? null : 
//arg["radiusLatLng"]; 
//
//        this.clickable = arg["clickable"] == null ? false : 
//arg["clickable"]; 

        this.editable = true;

        this.strokeColor = "#C602C8"; 

        this.fillColor = "#C602C8"; 

        this.strokeWidth = 2; 

        this.strokeOpacity = 0.8; 

        this.fillOpacity = 0.3; 

        this.centerLatLng = null; 

        this.radiusLatLng = null; 

        this.clickable = false; 

//        if( arg["spec"] ) { 
//
//                var pts = arg["spec"].split( "," ); 
//
//                for( var i=0; i<pts.length; ++i ) { 
//
//                        pts[i] = parseFloat( pts[i] ); 
//
//                } 
//
//                this.centerLatLng = new GLatLng( pts[0], pts[1] ); 
//
//                this.radiusLatLng = new GLatLng( pts[2], pts[3] ); 
//
//        } 

//        this.tooltipsOn = arg["tooltipsOn"] == null ? false : 
//arg["tooltipsOn"]; 
        this.tooltipsOn = false; 

        this.tooltip = null; 

        this.feedbackLineWidth = 2; 

        this.overlay = null; 

        this.verticesOn = true; 

        this.feedbackLine = null; 

        var self = this; 

        if( this.centerLatLng && this.radiusLatLng ) { 

                this.closed = true; 

                this.render(); 

                this.showVertices( true ); 

        } 

        else if( this.editable ) { 

                this.startEditing(); 

        } 

        mrpGSnapHelper.addSnapArea(this); 

}; 

mapCircle.emptyVertexIconUrl = "polyvertex-empty.png"; 

mapCircle.vertexIconUrl = "polyvertex.png"; 

mapCircle.crosshairsCursor = "crosshairs.cur"; 


mapCircle.prototype.showTooltip = function( latlng ) 

{ 

//        if( !this.tooltipsOn && this.tooltip ) { 
//
//                this.tooltip.remove(); 
//
//                this.tooltip = null; 
//
//        } 
//
//        if( !this.tooltipsOn ) { 
//
//                return; 
//
//        } 
//
//        if( !this.centerLatLng && !this.radiusLatLng ) { 
//
//                this.setTooltipText( "- Click on the map to create the 
//<span class='keyword'>CENTER</span> of the circle" ); 
//
//        } 
//
//        else if( this.centerLatLng && !this.radiusLatLng ){ 
//
//                this.setTooltipText( "- Move the mouse and click to 
//create the <span class='keyword'>EDGE</span> of the circle" ); 
//
//        } 
//
//        this.tooltip.setPosition( latlng ); 

} 

mapCircle.prototype.setTooltipText = function( text ) 

{ 

//        if( this.tooltip == null ) { 
//
//                this.tooltip = new mrpGMouseTooltip( { 
//
//                        "text" : text, 
//
//                        "map" : this.map 
//
//                } ); 
//
//        } 
//
//        else { 
//
//                this.tooltip.setText( text ); 
//
//        } 

} 

mapCircle.prototype.getSnappablePoints = function() 

{ 

        if( this.centerLatLng && this.radiusLatLng ) { 

                return [ this.centerLatLng, this.radiusLatLng ]; 

        } 

        else { 

                return []; 

        } 

} 

mapCircle.prototype.getCurrentPolygon = function() 

{ 

        if( this.closed ) { 

                return this.overlay; 

        } 

        else { 

                return null; 

        } 

}; 

mapCircle.prototype.setOnPolygonCloseCallback = function( callback ) 

{ 

        this.polygonCloseCallback = callback; 

}; 

mapCircle.prototype.setOnPolygonEditedCallback = function( callback ) 

{ 

        this.polygonEditedCallback = callback; 

}; 

mapCircle.prototype.startEditing = function() 

{ 

        if( !this.editable ) { 

                return; 

        } 

        this.stopEditing(); 

        this.editing = true; 

        var self = this; 

        this.clickListener = GEvent.addListener( this.map, "click", 

                function( marker, point ) { 

                        self.processClick( marker, point ); 

                } ); 

        if( !this.closed ) { 

                this.mouseListener = new GEvent.addListener( this.map, "mousemove", 

                        function( latlng ) { 

//                                self.showTooltip( latlng ); 

                                self.renderFeedbackLine( latlng ); 

                        } ); 

        } 

        this.map.disableDoubleClickZoom(); 

        if( !this.closed ) { 

                this.map.getDragObject().setDraggableCursor( 'url(' + this.crosshairsCursor + '),crosshair' ); 

        } 

        if( this.closed && this.radiusLatLng ) { 

                this.renderFeedbackLine( this.radiusLatLng ); 

        } 

}; 

mapCircle.prototype.stopEditing = function() 

{ 

        if( !this.editable ) { 

                return; 

        } 

        if( this.clickListener ) { 

                GEvent.removeListener( this.clickListener ); 

                this.clickListener = null; 

        } 

        if( this.mouseListener ) { 

                GEvent.removeListener( this.mouseListener ); 

                this.mouseListener = null; 

        } 

        if( this.feedbackLine ) { 

                this.map.removeOverlay( this.feedbackLine ); 

                this.feedbackLine = null; 

        } 

        if( this.tooltip ) { 

                this.tooltip.remove(); 

                this.tooltip = null; 

        } 

        this.editing = false; 

        this.map.getDragObject().setDraggableCursor( 'default' ); 

}; 

mapCircle.prototype.renderFeedbackLine = function( mousePoint ) 

{ 

        if( this.centerLatLng == null ) { 

                return; 

        } 

        if( this.feedbackLine != null ) { 

                this.map.removeOverlay( this.feedbackLine ); 

                this.feedbackLine = null; 

        } 

        this.feedbackLine = new GPolyline( [ this.centerLatLng, mousePoint ], "#000000", this.feedbackLineWidth, 1.0, 
                { "clickable" : false } ); 

        this.map.addOverlay( this.feedbackLine ); 

}; 

mapCircle.prototype.processClick = function( marker, point ) 

{ 

        if( !this.editable ) { 

                return; 

        } 

        var wasClosed = this.closed; 

        if( !this.closed ) { 

                if( marker ) { 

                        point = marker.getLatLng(); 

                } 

                if( this.centerLatLng == null ) { 

                        this.centerLatLng = point; 

                        this.createVertex( this.centerLatLng, true ); 

                } 

                else { 

                        this.radiusLatLng = point; 

                        this.createVertex( this.radiusLatLng, false ); 

                        this.closed = true; 

                        if( this.mouseListener ) { 

                                GEvent.removeListener(this.mouseListener ); 

                                this.mouseListener = null; 

                        } 

                        if( this.feedbackLine && !this.verticesOn ) { 

                                this.map.removeOverlay(this.feedbackLine ); 

                                this.feedbackLine = null; 

                        } 

                        if( this.tooltip ) { 

                                this.tooltip.remove(); 

                                this.tooltip = null; 

                        } 

                        this.createCircle(); 

                        this.map.getDragObject().setDraggableCursor('default' ); 

                } 

                this.render(); 

        } 

        if( wasClosed != this.closed && this.polygonCloseCallback ) { 

                this.polygonCloseCallback( this ); 

        } 

}; 

mapCircle.prototype.createCircle = function() 

{ 

        var zoom = this.map.getZoom(); 

        var centerPt = 
this.normalProj.fromLatLngToPixel(this.centerLatLng, zoom); 

        var radiusPt = 
this.normalProj.fromLatLngToPixel(this.radiusLatLng, zoom); 

        var circlePoints = new Array(); 

        var radius = 
Math.floor(Math.sqrt(Math.pow((centerPt.x-radiusPt.x),2) + 

                Math.pow((centerPt.y-radiusPt.y),2))); 

        for (var a = 0 ; a < 361 ; a+=10 ) { 

                var aRad = a*(Math.PI/180); 

                var y = centerPt.y + radius * Math.sin(aRad); 

                var x = centerPt.x + radius * Math.cos(aRad); 

                var p = new GPoint(x,y); 

                circlePoints.push( this.normalProj.fromPixelToLatLng(p, 
zoom) ); 

        } 

        this.points = circlePoints; 

} 

mapCircle.prototype.getEndVertexIcon = function() 

{ 

        var icon = new GIcon(); 

        icon.image = this.vertexIconUrl; 

        icon.iconSize = new GSize(10, 10); 

        icon.iconAnchor = new GPoint(5, 5); 

        return icon; 

}; 

mapCircle.prototype.getVertexIcon = function() 

{ 

        var icon = new GIcon(); 

        icon.image = this.emptyVertexIconUrl; 

        icon.iconSize = new GSize(10, 10); 

        icon.iconAnchor = new GPoint(5, 5); 

        return icon; 

}; 

mapCircle.prototype.createVertex = function( point, isOpen ) 

{ 

        if( !this.verticesOn ) { 

                return null; 

        } 

        var vertex = null; 

        if( isOpen ) { 

                vertex = new GMarker( point, { 

                        "icon" : this.getEndVertexIcon(), 

                        "draggable" : this.editable, 

                        "bounceGravity" : 0, 

                        "dragCrossMove" : 1, 

                        "bouncy" : false 

                        } ); 

        } 

        else { 

                vertex = new GMarker( point, { 

                        "icon" : this.getVertexIcon(), 

                        "draggable" : this.editable, 

                        "dragCrossMove" : 1, 

                        "bounceGravity" : 0, 

                        "bouncy" : false 

                        } ) 

        } 

        var self = this; 

        if( this.editable ) { 

                GEvent.addListener( vertex, "dragstart", function() { 

                        self.onVertexDragStart( vertex ); 

                }); 

                GEvent.addListener( vertex, "drag", function() { 

                        self.onVertexDrag( vertex ); 

                }); 

                GEvent.addListener( vertex, "dragend", function() { 

                        self.onVertexDragEnd( vertex ); 

                }); 

        } 

        this.map.addOverlay( vertex ); 

        this.vertices.push( vertex );; 
        return vertex; 

}; 

mapCircle.prototype.showVertices = function( flag ) 

{ 

        this.verticesOn = flag; 

        for( var i=0; i<this.vertices.length; ++i ) { 

                this.map.removeOverlay( this.vertices[i] ); 

        } 

        this.vertices = new Array(); 

        if( this.feedbackLine ) { 

                this.map.removeOverlay( this.feedbackLine ); 

                this.feedbackLine = null; 

        } 

        if( flag ) { 

                this.createVertex( this.centerLatLng, true ); 

                this.createVertex( this.radiusLatLng, false ); 

                this.renderFeedbackLine( this.radiusLatLng ); 

        } 

}; 

mapCircle.prototype.render = function() 

{ 

        this.destroyOverlay(); 

        if( this.radiusLatLng && this.centerLatLng ) { 

                this.points = new Array(); 

                this.createCircle(); 

        } 

        if( this.points.length > 2 ) { 

                this.overlay = new GPolygon( this.points, this.strokeColor, this.strokeWidth, this.strokeOpacity, 
this.fillColor, this.fillOpacity, 

                                        { "clickable" : this.clickable } 
); 

                this.map.addOverlay( this.overlay ); 

        } 

}; 

mapCircle.prototype.destroyOverlay = function() 

{ 

        if( this.overlay != null ) { 

                this.map.removeOverlay( this.overlay ); 

                this.overlay = null; 

        } 

}; 

mapCircle.prototype.onVertexDragStart = function( marker ) 

{ 

}; 

mapCircle.prototype.onVertexDrag = function( marker ) 

{ 

        if( this.feedbackLine != null ) { 

                this.map.removeOverlay( this.feedbackLine ); 

                this.feedbackLine = null; 

        } 

        if( !this.closed ) { 

                return; 

        } 

        if( marker == this.vertices[0] ) { 

                this.feedbackLine = new GPolyline( [ marker.getLatLng(), this.radiusLatLng ], "#000000", this.feedbackLineWidth, 1.0 ); 

        } 

        else { 

                this.feedbackLine = new GPolyline( [ this.centerLatLng, marker.getLatLng() ], "#000000", this.feedbackLineWidth, 1.0 ); 

        } 

        this.map.addOverlay( this.feedbackLine ); 

}; 

mapCircle.prototype.onVertexDragEnd = function( marker ) 

{ 

        this.destroyOverlay(); 

        var thePoint = marker.getLatLng(); 

        var closestPoint = mrpGSnapHelper.findSnapPoint( this, this.map, thePoint, 5.0 ); 

        if( closestPoint ) { 

                thePoint = closestPoint; 

                marker.setLatLng( thePoint ); 

        } 

        if( marker == this.vertices[0] ) { 

                this.centerLatLng = thePoint; 

        } 

        else { 

                this.radiusLatLng = thePoint; 

        } 

        this.render(); 

        if( this.feedbackLine ) { 

                this.map.removeOverlay( this.feedbackLine ); 

                this.feedbackLine = null; 

        } 

        this.feedbackLine = new GPolyline( [ this.centerLatLng, this.radiusLatLng ], "#000000", this.feedbackLineWidth, 1.0 ); 

        this.map.addOverlay( this.feedbackLine ); 

        if( this.polygonEditedCallback ) { 

                this.polygonEditedCallback( this ); 

        } 

}; 

mapCircle.prototype.destroy = function() 

{ 

        //GEvent.removeListener( mrpGArea.clickListener ); 

        this.destroyOverlay(); 

        for( var i=0; this.vertices && i<this.vertices.length; ++i ) { 

                this.map.removeOverlay( this.vertices[i] ); 

                this.vertices[i] = null; 

        } 

        this.vertices = new Array(); 

        this.overlay = null; 

        this.stopEditing(); 

        if( this.feedbackLine ) { 

                this.map.removeOverlay( this.feedbackLine ); 

                this.feedbackLine = null; 

        } 

        if( this.tooltip ) { 

                this.tooltip.remove(); 

                this.tooltip = null; 

        } 

        mrpGSnapHelper.removeSnapArea(this); 

        this.map.getDragObject().setDraggableCursor( 'default' ); 

};

// NEW - return the polygon points of the circle
mapCircle.prototype.returnCenter = function()
{
	return this.centerLatLng;
}

mapCircle.prototype.returnRadius = function()
{
	return this.radiusLatLng;
}

//var mrpGRect = Class.create();

//mrpGRect.prototype.initialize = function( arg )
mrpGRect = function( map /*arg*/ ) 

{
//	if( arg["map"] == null ) {
//		alert( "Must pass map to area in constructor" );
//		return;
//	}
//	this.emptyVertexIconUrl = arg["emptyVertexIconUrl"] == null ? mrpGRect.emptyVertexIconUrl :
//		arg["emptyVertexIconUrl"];
//	this.vertexIconUrl = arg["vertexIconUrl"] == null ? mrpGRect.vertexIconUrl :
//		arg["vertexIconUrl"];
//	this.crosshairsCursor = arg["crosshairsCursor"] == null ? mrpGRect.crosshairsCursor :
//		arg["crosshairsCursor"];

	this.emptyVertexIconUrl = mrpGRect.emptyVertexIconUrl;
	this.vertexIconUrl = mrpGRect.vertexIconUrl;
	this.crosshairsCursor = mrpGRect.crosshairsCursor;

	this.closed = false;
	this.editing = false;
//	this.map = arg["map"];
	this.map = map; 

	this.normalProj = G_NORMAL_MAP.getProjection();
	this.vertices = new Array();
//	this.points = arg["points"] == null ? new Array() : Array.from( arg["points"] );
//	this.editable = arg["editable"] == null ? false : arg["editable"];
//	this.strokeColor = arg["strokeColor"] == null ? "#C602C8" : arg["strokeColor"];
//	this.fillColor = arg["fillColor"] == null ? "#C602C8" : arg["fillColor"];
//	this.strokeWidth = arg["strokeWidth"] == null ? 5 : arg["strokeWidth"];
//	this.strokeOpacity = arg["strokeOpacity"] == null ? 0.8 : arg["strokeOpacity"];
//	this.fillOpacity = arg["fillOpacity"] == null ? 0.3 : arg["fillOpacity"];
//	this.tooltipsOn = arg["tooltipsOn"] == null ? false : arg["tooltipsOn"];
//	this.clickable = arg["clickable"] == null ? false : arg["clickable"];
//	if( arg["spec"] ) {
//		var pts = arg["spec"].split( "," );
//		for( var i=0; i<pts.length; ++i ) {
//			pts[i] = parseFloat( pts[i] );
//		}
//		this.points.push( new GLatLng( pts[0], pts[1] ) );
//		this.points.push( new GLatLng( pts[2], pts[3] ) );
//		this.createRect();
//	}
	this.points = new Array();
	this.editable = true;
	this.strokeColor = "#C602C8";
	this.fillColor = "#C602C8";
	this.strokeWidth = 2;
	this.strokeOpacity = 0.8;
	this.fillOpacity = 0.3;
	this.tooltipsOn = false;
	this.clickable = false;

	this.tooltip = null;
	this.feedbackLineWidth = 2;
	this.overlay = null;
	this.verticesOn = true;
	this.feedbackLine = null;
	var self = this;
	
	if( this.points.length > 0 ) {
		for( var i=0; i<this.points.length; ++i ) {
			this.createVertex( this.points[i] );
		}
		this.closed = true;
		this.render();
		this.showVertices( true );
	}
	else if( this.editable ) {
		this.startEditing();
	}
	mrpGSnapHelper.addSnapArea(this);
};

mrpGRect.emptyVertexIconUrl = "polyvertex-empty.png";
mrpGRect.vertexIconUrl = "polyvertex.png";
mrpGRect.crosshairsCursor = "crosshairs.cur";


mrpGRect.prototype.showTooltip = function( latlng )
{
//	if( !this.tooltipsOn && this.tooltip ) {
//		this.tooltip.remove();
//		this.tooltip = null;
//	}
//	if( !this.tooltipsOn ) {
//		return;
//	}
//	if( this.points.length == 0 ) {
//		this.setTooltipText( "- Click on the map to <span class='keyword'>START</span> drawing the rectangle" );
//	}
//	else if( this.points.length == 1 ) {
//		this.setTooltipText( "- Move the mouse and click to <span class='keyword'>COMPLETE</span> the rectangle" );
//	}
//	
//	this.tooltip.setPosition( latlng );
}

mrpGRect.prototype.setTooltipText = function( text )
{
//	if( this.tooltip == null ) {
//		this.tooltip = new mrpGMouseTooltip( { 
//			"text" : text, 
//			"map" : this.map 
//		} );
//	}
//	else {
//		this.tooltip.setText( text );
//	}
}

mrpGRect.prototype.getSnappablePoints = function()
{
	return this.points;
}

mrpGRect.prototype.getCurrentPolygon = function()
{
	if( this.closed ) {
		return this.overlay;
	}
	else {
		return null;
	}
};

mrpGRect.prototype.setOnPolygonCloseCallback = function( callback )
{
	this.polygonCloseCallback = callback;
};

mrpGRect.prototype.setOnPolygonEditedCallback = function( callback )
{
	this.polygonEditedCallback = callback;
};

mrpGRect.prototype.startEditing = function()
{
	if( !this.editable ) {
		return;
	}
	this.stopEditing();
	this.editing = true;
	var self = this;
	this.clickListener = GEvent.addListener( this.map, "click", 
		function( marker, point ) {
			self.processClick( marker, point );
		} );
	if( !this.closed ) {
		this.mouseListener = new GEvent.addListener( this.map, "mousemove",
			function( latlng ) {
				self.showTooltip( latlng );
				self.renderFeedbackLine( latlng );
			} );
	}
	this.map.disableDoubleClickZoom();
	if( !this.closed ) {
		this.map.getDragObject().setDraggableCursor( 'url(' +
			this.crosshairsCursor + '),crosshair' );
	}
};

mrpGRect.prototype.stopEditing = function()
{
	if( !this.editable ) {
		return;
	}
	if( this.clickListener ) {
		GEvent.removeListener( this.clickListener );
		this.clickListener = null;
	}	
	if( this.mouseListener ) {
		GEvent.removeListener( this.mouseListener );
		this.mouseListener = null;
	}
	if( this.feedbackLine ) {
		this.map.removeOverlay( this.feedbackLine );
		this.feedbackLine = null;
	}
	if( this.tooltip ) {
		this.tooltip.remove();
		this.tooltip = null;
	}
	this.editing = false;
	this.map.getDragObject().setDraggableCursor( 'default' );
};

mrpGRect.prototype.renderFeedbackLine = function( mousePoint, oppositePoint )
{
	if( this.points.length == 0 ) {
		return;
	}
	if( this.feedbackLine != null ) {
		this.map.removeOverlay( this.feedbackLine );
		this.feedbackLine = null;
	}
	if( !oppositePoint ) {
		oppositePoint = this.points[0];
	}
	
	var p1 = mousePoint;
	var p3 = oppositePoint;
	var p2 = new GLatLng( p3.lat(), p1.lng() );
	var p4 = new GLatLng( p1.lat(), p3.lng() );
	var p5 = new GLatLng( p1.lat(), p1.lng() );
	
	this.feedbackLine = new GPolyline( [ p1, p2, p3, p4, p5 ], "#000000", this.feedbackLineWidth, 1.0, 
		{ "clickable" : false } );
	this.map.addOverlay( this.feedbackLine );
};

mrpGRect.prototype.processClick = function( marker, point )
{
	if( !this.editable ) {
		return;
	}
	var wasClosed = this.closed;
	if( !this.closed ) {
	
		if( marker ) {
			point = marker.getLatLng();
		}
		if( this.points.length == 0 ) {
			this.points.push( point );
			this.createVertex( point );
		}
		else if( this.points.length == 1 ){
			this.points.push( point );
			this.createVertex( point, false );
			this.closed = true;
			if( this.mouseListener ) {
				GEvent.removeListener( this.mouseListener );
				this.mouseListener = null;
			}
			if( this.feedbackLine && !this.verticesOn ) {
				this.map.removeOverlay( this.feedbackLine );
				this.feedbackLine = null;
			}
			if( this.tooltip ) {
				this.tooltip.remove();
				this.tooltip = null;
			}
			this.map.getDragObject().setDraggableCursor( 'default' );
		}
		this.render();
	}
	if( wasClosed != this.closed && this.polygonCloseCallback ) {
		this.polygonCloseCallback( this );
	}
};

mrpGRect.prototype.createRect = function()
{
	var p1 = this.points[0];
	var p3 = this.points[1];
	
	var p2 = new GLatLng( p3.lat(), p1.lng() );
	var p4 = new GLatLng( p1.lat(), p3.lng() );
	var p5 = new GLatLng( p1.lat(), p1.lng() );
	
	this.points = new Array();
	this.points.push( p1 );
	this.points.push( p2 );
	this.points.push( p3 );
	this.points.push( p4 );
	this.points.push( p5 );
}

mrpGRect.prototype.getEndVertexIcon = function()
{
	var icon = new GIcon();
	icon.image = this.vertexIconUrl;
	icon.iconSize = new GSize(10, 10);
	icon.iconAnchor = new GPoint(5, 5);
	return icon;
};

mrpGRect.prototype.getVertexIcon = function()
{
	var icon = new GIcon();
	icon.image = this.emptyVertexIconUrl;
	icon.iconSize = new GSize(10, 10);
	icon.iconAnchor = new GPoint(5, 5);
	return icon;
};

mrpGRect.prototype.createVertex = function( point, isOpen )
{
	if( !this.verticesOn ) {
		return null;
	}
	var vertex = null;
	if( isOpen ) {
		vertex = new GMarker( point, { 
			"icon" : this.getEndVertexIcon(),
			"draggable" : this.editable,
			"bounceGravity" : 0,
			"dragCrossMove" : 1,
			"bouncy" : false
			} );
	}
	else {
		vertex = new GMarker( point, { 
			"icon" : this.getVertexIcon(),
			"draggable" : this.editable,
			"dragCrossMove" : 1,
			"bounceGravity" : 0,
			"bouncy" : false
			} )
	}
	var self = this;
	if( this.editable ) {
		GEvent.addListener( vertex, "dragstart", function() {
			self.onVertexDragStart( vertex );
		});
		GEvent.addListener( vertex, "drag", function() {
			self.onVertexDrag( vertex );
		});
		GEvent.addListener( vertex, "dragend", function() {
			self.onVertexDragEnd( vertex );
		});
	}
	this.map.addOverlay( vertex );
	this.vertices.push( vertex );;
	return vertex;
};

mrpGRect.prototype.showVertices = function( flag )
{
	this.verticesOn = flag;
	for( var i=0; i<this.vertices.length; ++i ) {
		this.map.removeOverlay( this.vertices[i] );
	}
	this.vertices = new Array();
	if( this.feedbackLine ) {
		this.map.removeOverlay( this.feedbackLine );
		this.feedbackLine = null;
	}
	if( flag ) {
		// 1 fewer
		for( var i=0; i<this.points.length-1; ++i ) {
			this.createVertex( this.points[i] );	
		}
	}
};

mrpGRect.prototype.render = function()
{
	this.destroyOverlay();
	if( this.points.length == 2 ) {
		this.createRect();
		if( this.verticesOn ) {
			this.showVertices( true );
		}
	}
	if( this.points.length == 5 ) {
		this.overlay = new GPolygon( this.points, this.strokeColor, this.strokeWidth, 
					this.strokeOpacity, this.fillColor, this.fillOpacity,
					{ "clickable" : this.clickable } );
		this.map.addOverlay( this.overlay );
	}
};

mrpGRect.prototype.destroyOverlay = function()
{
	if( this.overlay != null ) {
		this.map.removeOverlay( this.overlay );
		this.overlay = null;
	}
};

mrpGRect.prototype.getOppositePoint = function( point )
{
	for( var i=0; i<this.points.length; ++i ) {
		if( this.points[i].lat() != point.lat() &&
			this.points[i].lng() != point.lng() ) {
			return this.points[i];
		}
	}
	return null;
}

mrpGRect.prototype.getMarkerIndex = function( marker )
{
	var idx = -1;
	for( var i=0; i<this.vertices.length; ++i ) {
		if( this.vertices[i] == marker ) {
			idx = i;
			break;
		}
	}
	return idx;
}


mrpGRect.prototype.onVertexDragStart = function( marker )
{
};

mrpGRect.prototype.onVertexDrag = function( marker )
{
	if( this.feedbackLine != null ) {
		this.map.removeOverlay( this.feedbackLine );
		this.feedbackLine = null;
	}
	
	if( !this.closed ) {
		return;
	}
	
	var idx = this.getMarkerIndex( marker );
	if( idx == -1 ) {
		return;
	}
	var oppositePoint = this.getOppositePoint( this.points[idx] );
	if( !oppositePoint ) {
		return;
	}
	
	this.renderFeedbackLine( marker.getLatLng(), oppositePoint );
	this.map.addOverlay( this.feedbackLine );
};

mrpGRect.prototype.onVertexDragEnd = function( marker )
{
	if( this.feedbackLine ) {
		this.map.removeOverlay( this.feedbackLine );
		this.feedbackLine = null;
	}
	this.destroyOverlay();
	
	var idx = this.getMarkerIndex( marker );
	if( idx == -1 ) {
		return;
	}
	var thePoint = this.points[idx];
	var oppositePoint = this.getOppositePoint( this.points[idx] );
	if( !oppositePoint ) {
		return;
	}
	thePoint = marker.getLatLng();
	
	var closestLatLng = mrpGSnapHelper.findSnapPoint( this, this.map, thePoint, 5.0 );
	if( closestLatLng ) {
		thePoint = closestLatLng;
	}
	
	this.points = new Array();
	this.points.push( thePoint );
	this.points.push( oppositePoint );
	this.render();
	this.showVertices( true );
	
	if( this.polygonEditedCallback ) {
		this.polygonEditedCallback( this );
	}
};

mrpGRect.prototype.destroy = function()
{
	//GEvent.removeListener( mrpGArea.clickListener );
	this.destroyOverlay();
	for( var i=0; this.vertices && i<this.vertices.length; ++i ) {
		this.map.removeOverlay( this.vertices[i] );
		this.vertices[i] = null;
	}
	this.vertices = new Array();
	this.overlay = null;
	this.stopEditing();
	if( this.feedbackLine ) {
		this.map.removeOverlay( this.feedbackLine );
		this.feedbackLine = null;
	}
	if( this.tooltip ) {
		this.tooltip.remove();
		this.tooltip = null;
	}
	mrpGSnapHelper.removeSnapArea(this);
	this.map.getDragObject().setDraggableCursor( 'default' );
};

// NEW - return the polygon points of the rectangle
mrpGRect.prototype.returnRect = function()
{
	return this.points;
}

var mrpGSnapHelper = {}; // namespace

mrpGSnapHelper.addSnapArea = function( area )
{
	if( mrpGSnapHelper.areas == null ) {
		mrpGSnapHelper.areas = new Array();
	}
	mrpGSnapHelper.areas.push( area );
};

mrpGSnapHelper.removeSnapArea = function( area )
{
	if( mrpGSnapHelper.areas == null ) {
		mrpGSnapHelper.areas = new Array();
	}
	mrpGSnapHelper.areas = mrpGSnapHelper.areas.without( area );
};

mrpGSnapHelper.clearAllSnapAreas = function()
{
	mrpGSnapHelper.areas = new Array();	
}

mrpGSnapHelper.pixelDistance = function( px1, px2 )
{
	var dx = px1.x - px2.x;
	dx *=dx;
	var dy = px1.y - px2.y;
	dy *= dy;
	return Math.sqrt( dx + dy );
};

mrpGSnapHelper.findSnapPoint = function( caller, map, point, proximityTolerance )
{
	var areas = mrpGSnapHelper.areas;
	if( areas == null ) {
		return null;
	}
	var proj = G_NORMAL_MAP.getProjection();
	var px = proj.fromLatLngToPixel( point, map.getZoom() );
	var closestLatLng = null;
	var closest = -1;
	for( var i=0; i<areas.length; ++i ) {
		if( areas[i] != caller ) {
			if( typeof areas[i].getSnappablePoints == 'undefined' ) {
				continue;
			}
			var points = areas[i].getSnappablePoints();
			for( var j=0; j<points.length; ++j ) {
				var d = mrpGSnapHelper.pixelDistance( 
					proj.fromLatLngToPixel( points[j], 
						map.getZoom()), px );
				if( isNaN( d ) ) {
					continue;
				}
				if( closest == -1 || d < closest) {
					closest = d;
					closestLatLng = points[j];
				}
			}
		}
	}
	if( closest <= 5.0 ) {
		return closestLatLng;
	}
	else {
		return null;
	}
};


//var mrpGArea = Class.create();

//mrpGArea.prototype.initialize = function( arg )
mrpGArea = function( map /*arg*/ ) 

{
//	if( arg["map"] == null ) {
//		alert( "Must pass map to area in constructor" );
//		return;
//	}
//	this.emptyVertexIconUrl = arg["emptyVertexIconUrl"] == null ? mrpGArea.emptyVertexIconUrl :
//		arg["emptyVertexIconUrl"];
//	this.vertexIconUrl = arg["vertexIconUrl"] == null ? mrpGArea.vertexIconUrl :
//		arg["vertexIconUrl"];
//	this.crosshairsCursor = arg["crosshairsCursor"] == null ? mrpGArea.crosshairsCursor :
//		arg["crosshairsCursor"];

	this.emptyVertexIconUrl = mapCircle.emptyVertexIconUrl;
	this.vertexIconUrl = mapCircle.vertexIconUrl;
	this.crosshairsCursor = mapCircle.crosshairsCursor;

	this.editing = false;
	//this.map = arg["map"];
	this.map = map; 
	
	this.normalProj = G_NORMAL_MAP.getProjection();
	this.points = new Array();
	this.pixels = new Array();
//	this.points = Array.from( arg["points"] ? arg["points"] : [] );
	this.points = [];
	this.vertices = new Array();
//	this.closed = arg["closed"] == null ? false : arg["closed"];
//	this.strokeColor = arg["strokeColor"] == null ? "#C602C8" : arg["strokeColor"];
//	this.fillColor = arg["fillColor"] == null ? "#C602C8" : arg["fillColor"];
//	this.editable = arg["editable"] == null ? true : arg["editable"];
//	this.strokeWidth = arg["strokeWidth"] == null ? 5 : arg["strokeWidth"];
//	this.strokeOpacity = arg["strokeOpacity"] == null ? 0.8 : arg["strokeOpacity"];
//	this.fillOpacity = arg["fillOpacity"] == null ? 0.3 : arg["fillOpacity"];
//	this.tooltipsOn = arg["tooltipsOn"] == null ? false : arg["tooltipsOn"];
//	this.clickable = arg["clickable"] == null ? false : arg["clickable"];
//	if( arg["spec"] ) {
//		var pts = arg["spec"].split( "," );
//		for( var i=0; i<pts.length; ++i ) {
//			pts[i] = parseFloat( pts[i] );
//			if( i > 0 && ( i % 2 ) != 0 ) {
//				this.points.push( new GLatLng( pts[i-1], pts[i] ) );
//			}
//		}
//	}

	this.closed = false;
	this.strokeColor = "#C602C8";
	this.fillColor = "#C602C8";
	this.editable = true;
	this.strokeWidth = 2;
	this.strokeOpacity = 0.8;
	this.fillOpacity = 0.3;
	this.tooltipsOn = false;
	this.clickable = false;

	this.overlay = null;
	this.feedbackLineWidth = 2;
	this.verticesOn = true;
	this.feedbackLine = null;
	this.tooltip = null;
	var self = this;
	
	var len = this.points.length;
	if( this.closed ) {
		--len;
	}	
	for( var i=0; i<len; ++i ) {
		var px = this.normalProj.fromLatLngToPixel( this.points[i], this.map.getZoom() );
		this.pixels.push( px );
		this.createVertex( this.points[i], !this.closed && i==0 );
	}
	
	if( this.points.length > 0 ) {
		this.render();
	}
	mrpGSnapHelper.addSnapArea(this);
};

mrpGArea.emptyVertexIconUrl = "polyvertex-empty.png";
mrpGArea.vertexIconUrl = "polyvertex.png";
mrpGArea.crosshairsCursor = "crosshairs.cur";
mrpGArea.proximityThreshold = 7;

mrpGArea.prototype.getCurrentPolygon = function()
{
	if( this.closed ) {
		return this.overlay;
	}
	else {
		return null;
	}
};

mrpGArea.prototype.setOnPolygonCloseCallback = function( callback )
{
	this.polygonCloseCallback = callback;
};

mrpGArea.prototype.setOnPolygonEditedCallback = function( callback )
{
	this.polygonEditedCallback = callback;
};

mrpGArea.prototype.startEditing = function()
{
	if( !this.editable ) {
		return;
	}
	this.stopEditing();
	this.editing = true;
	var self = this;
	this.clickListener = GEvent.addListener( this.map, "click", 
		function( marker, point ) {
			self.processClick( marker, point );
		} );
	if( !this.closed ) {
		this.mouseListener = new GEvent.addListener( this.map, "mousemove",
			function( latlng ) {
				self.showTooltip( latlng );
				self.renderFeedbackLine( latlng );
			} );
	}
	this.map.disableDoubleClickZoom();
	if( !this.closed ) {
		this.map.getDragObject().setDraggableCursor( 'url(' +
			this.crosshairsCursor + '),crosshair' );
	}
};

mrpGArea.prototype.showTooltip = function( latlng )
{
//	if( !this.tooltipsOn && this.tooltip ) {
//		this.tooltip.remove();
//		this.tooltip = null;
//	}
//	if( !this.tooltipsOn ) {
//		return;
//	}
//	if( this.points.length == 0 ) {
//		this.setTooltipText( "- Click on the map to <span class='keyword'>START</span> drawing the shape" );
//	}
//	else if( this.points.length == 1 || this.points.length == 2 ) {
//		this.setTooltipText( "- Move the mouse and click to <span class='keyword'>CONTINUE</span> drawing the shape" );
//	}
//	else if( this.points.length >= 3 ) {
//		this.setTooltipText( "- Move the mouse and click to <span class='keyword'>CONTINUE</span> drawing the shape<BR>" +
//		"- Or click on the first point to CLOSE the shape" );
//	}
//	
//	this.tooltip.setPosition( latlng );
}

mrpGArea.prototype.setTooltipText = function( text )
{
//	if( this.tooltip == null ) {
//		this.tooltip = new mrpGMouseTooltip( { 
//			"text" : text, 
//			"map" : this.map 
//		} );
//	}
//	else {
//		this.tooltip.setText( text );
//	}
}

mrpGArea.prototype.stopEditing = function()
{
	if( !this.editable ) {
		return;
	}
	if( this.clickListener ) {
		GEvent.removeListener( this.clickListener );
		this.clickListener = null;
	}	
	if( this.mouseListener ) {
		GEvent.removeListener( this.mouseListener );
		this.mouseListener = null;
	}
	if( this.feedbackLine ) {
		this.map.removeOverlay( this.feedbackLine );
		this.feedbackLine = null;
	}
	if( this.tooltip ) {
		this.tooltip.remove();
		this.tooltip = null;
	}
	this.editing = false;
	this.map.getDragObject().setDraggableCursor( 'default' );
};

mrpGArea.prototype.renderFeedbackLine = function( mousePoint )
{
	if( this.points.length == 0 ) {
		return;	
	}
	var p = this.points[this.points.length-1];
	if( this.feedbackLine != null ) {
		this.map.removeOverlay( this.feedbackLine );
		this.feedbackLine = null;
	}
	this.feedbackLine = new GPolyline( [ p, mousePoint ], "#000000", this.feedbackLineWidth, 1.0 );
	this.map.addOverlay( this.feedbackLine );
};

mrpGArea.prototype.processClick = function( marker, point )
{
	if( !this.editable ) {
		return;
	}
	var wasClosed = this.closed;
	if( !this.closed ) {
		this.processCreateClick( marker, point );
	}
	else {
		this.processEditClick( marker, point );
	}
	
	this.render();
	
	if( wasClosed != this.closed && this.polygonCloseCallback ) {
		this.polygonCloseCallback( this );
	}
};

mrpGArea.prototype.processCreateClick = function( marker, point )
{
	var markerFound = false;
	if( marker ) {
		for( var i=0; i<this.vertices.length; ++i ) {
			if( this.vertices[i] == marker ) {
				markerFound = true;
			}
		}
	}
	
	if( !markerFound && marker && marker == this.feedbackLine ) {
		point = this.feedbackLine.getVertex( 1 );
	}
	else if( !markerFound && marker ) {
		point = marker.getLatLng();
	}
	
	if( !markerFound ) {
		//mrplog( this.points.length );
		var vertex = this.createVertex( point, this.points.length == 0 );
		var px = this.normalProj.fromLatLngToPixel( point, this.map.getZoom() );
		
		this.points.push( point );
		this.pixels.push( px );
	}
	else {
		var latLng = marker.getLatLng();
		if( this.points.length > 2 && 
			latLng.lat() == this.points[0].lat() && 
			latLng.lng() == this.points[0].lng() ) {
			
			this.closed = true;
			if( this.mouseListener ) {
				GEvent.removeListener( this.mouseListener );
				this.mouseListener = null;
			}
			if( this.feedbackLine ) {
				this.map.removeOverlay( this.feedbackLine );
				this.feedbackLine = null;
			}
			if( this.tooltip ) {
				this.tooltip.remove();
				this.tooltip = null;
			}
			marker.setImage( mrpGArea.vertexIconUrl );
			this.points.push( this.points[0] );
			this.pixels.push( this.pixels[0] );
		}
	}
};

mrpGArea.prototype.onVertexDragStart = function( marker )
{
	var pt = marker.getLatLng();
	marker.origPoint = new GLatLng( pt.lat(), pt.lng() );
	marker.origIndex = this.findPointIndex( marker.origPoint );
};

mrpGArea.prototype.onVertexDrag = function( marker )
{
	if( this.feedbackLine != null ) {
		this.map.removeOverlay( this.feedbackLine );
		this.feedbackLine = null;
	}
	
	if( marker.origIndex == -1 ) {
		return;
	}
	
	if( !this.closed ) {
		return;
	}
	
	var left = null;
	var right = null;
	if( marker.origIndex == 0 ) {
		left = this.points[this.points.length-2];
		right = this.points[marker.origIndex+1];
	}
	else if( marker.origIndex == this.points.length - 1 ) {
		left = this.points[marker.origIndex-1];
		right = this.points[1];
	}
	else {
		left = this.points[marker.origIndex-1];
		right = this.points[marker.origIndex+1];
	}
	this.feedbackLine = new GPolyline( [ left, marker.getLatLng(), right ], "#000000", this.feedbackLineWidth, 1.0 );
	this.map.addOverlay( this.feedbackLine );
	
	if( this.map.gArea_mouse_over_vertex ) {
		marker.setImage( "circle.png" );
	}
};

mrpGArea.prototype.getSnappablePoints = function()
{
	return this.points;
}

mrpGArea.prototype.onVertexDragEnd = function( marker )
{
	var n = this.findPointIndex( marker.origPoint );
	if( n == -1 ) {
		return;
	}
	this.points[n] = null;
	var pt = marker.getLatLng();
	this.points[n] = pt;
	var px = this.normalProj.fromLatLngToPixel( marker.getLatLng(), this.map.getZoom() );
	this.pixels[n] = px;
	
	if( n == 0 && this.closed ) {
		this.points[this.points.length-1] = new GLatLng( pt.lat(), pt.lng() );
		this.pixels[this.pixels.length-1] = px;
	}
	
	if( this.feedbackLine != null ) {
		this.map.removeOverlay( this.feedbackLine );
		this.feedbackLine = null;
	}
	
	if( this.closed ) {
		var closestLatLng = mrpGSnapHelper.findSnapPoint( this, this.map, pt, 5.0 );
		/*
		var closestPoint = null;
		var closest = -1;
		var all = this.map.getContainer().gAreas;
		for( var i=0; i<all.length; ++i ) {
			if( all[i] != this ) {
				for( var j=0; j<all[i].pixels.length; ++j ) {
					var d = this.pixelDistance( 
						this.normalProj.fromLatLngToPixel(all[i].points[j], 
							this.map.getZoom()), px );
					if( isNaN( d ) ) {
						continue;
					}
					if( closest == -1 || d < closest) {
						closest = d;
						closestLatLng = all[i].points[j];
						closestPoint = all[i].pixels[j];
					}
				}
			}
		}
		*/
		
		if( /* closest <= 5.0 && */ closestLatLng != null ) {
			this.points[n] = new GLatLng( closestLatLng.lat(), closestLatLng.lng() );
			this.pixels[n] = this.normalProj.fromLatLngToPixel( closestLatLng, this.map.getZoom());
			marker.setLatLng( this.points[n] );
			
			if( n == this.points.length-1 ) {
				// update first too
				this.pixels[0] = new GPoint( this.pixels[n].x, this.pixels[n].y );
				this.points[0] = new GLatLng( this.points[n].lat(), this.points[n].lng() );
				this.vertices[0].setLatLng( this.points[0] );
			}
			else if( n == 0 ) {
				// update last too
				var last = this.points.length-1;
				this.points[last] = new GPoint( this.pixels[n].x, this.pixels[n].y );
				this.points[last] = new GLatLng( this.points[n].lat(), this.points[n].lng() );
			}
		}
		
		if( this.polygonEditedCallback ) {
			this.polygonEditedCallback( this );
		}
	}
	
	this.render();
};

mrpGArea.prototype.pixelDistance = function( px1, px2 )
{
	var dx = px1.x - px2.x;
	dx *=dx;
	var dy = px1.y - px2.y;
	dy *= dy;
	return Math.sqrt( dx + dy );
};

mrpGArea.prototype.onVertexDoubleClick = function( marker )
{
	var n = this.findPointIndex( marker.getLatLng() );
	if( n == -1 ) {
		return;
	}
	if( !confirm( "Are you sure you want to remove this vertex?" ) ) {
		return;
	}
	this.map.removeOverlay( marker );
	this.points.splice( n, 1 );
	this.pixels.splice( n, 1 );
	this.render();
};

mrpGArea.prototype.processEditClick = function( marker, point )
{
	if( !this.editable ) {
		return;
	}
	if( !this.verticesOn ) {
		return;
	}
	if( marker ) {
	}
	else {
		var res = this.getProximity( point );
		if( res["line"] != null ) {
			this.addVertex( point, res["line"] );
		}
		else {
		}
	}
};

mrpGArea.prototype.showVertices = function( flag )
{
	this.verticesOn = flag;
	if( !flag ) {
		for( var i=0; i<this.vertices.length; ++i ) {
			this.map.removeOverlay( this.vertices[i] );
		}
		this.vertices = new Array();
	}
	else {
		for( var i=0; i<this.points.length; ++i ) {
			this.createVertex( this.points[i], i == 0 );
		}
	}
};

mrpGArea.prototype.addVertex = function( point, lineNum )
{
	this.points.splice( lineNum, 0, point );
	var px = this.normalProj.fromLatLngToPixel( point, this.map.getZoom() );
	this.pixels.splice( lineNum, 0, px );
	this.createVertex( point, false );
};

mrpGArea.prototype.findPointIndex = function( latLng )
{
	for( var i=0; i<this.points.length; ++i ) {
		if( this.points[i].lat() == latLng.lat() && this.points[i].lng() == latLng.lng() ) {
			return i;
		}
	}
	return -1;
};

mrpGArea.prototype.render = function()
{
	this.destroyOverlay();
	if( this.points.length > 1 ) {
		if( !this.closed ) {
			this.overlay = new GPolyline( this.points, this.strokeColor, this.strokeWidth, 
				this.strokeOpacity, { "clickable": "false" } );
		}
		else {
			this.overlay = new GPolygon( this.points, this.strokeColor, this.strokeWidth, 
				this.strokeOpacity, this.fillColor, this.fillOpacity,
				{ "clickable" : this.clickable } );
		}
	}
	if( this.overlay != null ) {
		this.map.addOverlay( this.overlay );
	}
};

mrpGArea.prototype.destroyOverlay = function()
{
	if( this.overlay != null ) {
		this.map.removeOverlay( this.overlay );
		this.overlay = null;
	}
};

mrpGArea.prototype.getEndVertexIcon = function()
{
	var icon = new GIcon();
	icon.image = this.vertexIconUrl;
	icon.iconSize = new GSize(10, 10);
	icon.iconAnchor = new GPoint(5, 5);
	return icon;
};

mrpGArea.prototype.getVertexIcon = function()
{
	var icon = new GIcon();
	icon.image = this.emptyVertexIconUrl;
	icon.iconSize = new GSize(10, 10);
	icon.iconAnchor = new GPoint(5, 5);
	return icon;
};

mrpGArea.prototype.createVertex = function( point, isOpen )
{
	if( !this.verticesOn ) {
		return null;
	}
	var vertex = null;
	if( isOpen ) {
		vertex = new GMarker( point, { 
			"icon" : this.getEndVertexIcon(),
			"draggable" : this.editable,
			"bounceGravity" : 0,
			"dragCrossMove" : 1,
			"bouncy" : false
			} );
	}
	else {
		vertex = new GMarker( point, { 
			"icon" : this.getVertexIcon(),
			"draggable" : this.editable,
			"dragCrossMove" : 1,
			"bounceGravity" : 0,
			"bouncy" : false
			} )
	}
	var self = this;
	if( this.editable ) {
		GEvent.addListener( vertex, "dragstart", function() {
			self.onVertexDragStart( vertex );
		});
		GEvent.addListener( vertex, "drag", function() {
			self.onVertexDrag( vertex );
		});
		GEvent.addListener( vertex, "dragend", function() {
			self.onVertexDragEnd( vertex );
		});
		GEvent.addListener( vertex, "dblclick", function() {
			self.onVertexDoubleClick( vertex );
		});
	}
	else {
	/*
		GEvent.addListener( vertex, "mouseover", function() {
			mrplog( "mouseover" );
		});
		GEvent.addListener( vertex, "mouseout", function() {
			mrplog( "mouseout" );
		});
		GEvent.addListener( vertex, "mouseup", function() {
			mrplog( "mouseup" );
		});
		*/
	}
	this.map.addOverlay( vertex );
	this.vertices.push( vertex );
	return vertex;
};

mrpGArea.prototype.getProximity = function(mouseLatLng) 
{
	var zoom = this.map.getZoom();
	var mousePx = this.normalProj.fromLatLngToPixel(mouseLatLng, zoom);

	var minDist = mrpGArea.proximityThreshold;
	var minDistLine = -1;
	if (this.pixels.length > 1 ) {

		// second check for line proximity
		
		for (var n = 1 ; n < this.pixels.length ; n++ ) {
			if (this.pixels[n].x != this.pixels[n-1].x) {
				var a = (this.pixels[n].y - this.pixels[n-1].y) / (this.pixels[n].x - this.pixels[n-1].x);
				var b = this.pixels[n].y - a * this.pixels[n].x;
				var dist = Math.abs(a*mousePx.x + b - mousePx.y) / Math.sqrt(a*a+1);
	
			}
			else {
				var dist = Math.abs(mousePx.x - this.pixels[n].x)
			}
	
			// length^2 of line segment 
			var rl2 = Math.pow(this.pixels[n].y - this.pixels[n-1].y,2) + 
				Math.pow(this.pixels[n].x - this.pixels[n-1].x,2);
			
			// distance^2 of pt to end line segment
			var ln2 = Math.pow(this.pixels[n].y - mousePx.y,2) + 
				Math.pow(this.pixels[n].x - mousePx.x,2);
	
			// distance^2 of pt to begin line segment
			var lnm12 = Math.pow(this.pixels[n-1].y - mousePx.y,2) + 
				Math.pow(this.pixels[n-1].x - mousePx.x,2);
	
			// minimum distance^2 of pt to infinite line
			var dist2 = Math.pow(dist,2);
	
			// calculated length^2 of line segment
			var calcrl2 = ln2 - dist2 + lnm12 - dist2;
	
			// redefine minimum distance to line segment (not infinite line) if necessary
			if (calcrl2 > rl2) {
				dist = Math.sqrt( Math.min(ln2,lnm12) ); 
			};  
	
			if( minDist > dist ) {
				minDist = dist;
				minDistLine = n;
			}
		}
	
		if (minDist < mrpGArea.proximityThreshold) {
			return { "line" : minDistLine };
		}
	}
	return {};
};

mrpGArea.prototype.destroy = function()
{
	//GEvent.removeListener( mrpGArea.clickListener );
	this.destroyOverlay();
	for( var i=0; this.vertices && i<this.vertices.length; ++i ) {
		this.map.removeOverlay( this.vertices[i] );
		this.vertices[i] = null;
	}
	if( this.tooltip ) {
		this.tooltip.remove();
		this.tooltip = null;
	}
	this.vertices = new Array();
	this.overlay = null;
	this.stopEditing();
	mrpGSnapHelper.removeSnapArea(this);
	this.map.getDragObject().setDraggableCursor( 'default' );
};

// NEW - return the polygon points of the rectangle
mrpGArea.prototype.returnRect = function()
{
	return this.points;
}
