var Karpela_GPSCoords = null;

GPSCoords.prototype._map;

GPSCoords.prototype._enable;
GPSCoords.prototype.enabled = function() { return(this._enable); };

GPSCoords.prototype._marker;

function GPSCoords(map) {
	this._map = map;
	this._enable = false;
	Karpela_GPSCoords = this;
	
	return(this);
}

GPSCoords.prototype.toggle = function() {
	this._enable = !this._enable;
	
	if (this._enable) {
		this._createMarker();
		document.getElementById("coords_button").className = "viewport_button_active";
	} else {
		this._map.removeMarker(this._marker);
		document.getElementById("link_this_place").value = "";
		document.getElementById("coords_button").className = "viewport_button";
	}
}

GPSCoords.prototype._createMarker = function() {
	var geo = this._map.getCenter().convertToGeoPoint();
	this._marker = new VMarker(geo);
	this._marker.setDraggable(true);
	this._marker.setHint(	"Широта: " + geo.getLatitude().toPrecision(6) + "<br/>" +
				"Долгота: " + geo.getLongitude().toPrecision(6));
	changeLink(geo);
	
	var _this = this;
	VEvents.addListener(this._marker, "enddrag",
		function() {
			var geo = _this._marker.getPoint().convertToGeoPoint();
			
			_this._marker.setHint(	"Широта: " + geo.getLatitude().toPrecision(6) + "<br/>" +
						"Долгота: " + geo.getLongitude().toPrecision(6));
			changeLink(geo);
		}
	);
	
	VEvents.addListener("zoomchange",
		function() {
			var geo = _this._marker.getPoint().convertToGeoPoint();
			
			_this._marker.setHint(	"Широта: " + geo.getLatitude().toPrecision(6) + "<br/>" +
						"Долгота: " + geo.getLongitude().toPrecision(6));
			changeLink(geo);
		}
	);
	
	this._map.addMarker(this._marker);
	this._map.repaintMap();
}

