// ---------------------------------------------------------
// FishPlaceAdd class
// GUI для добавления рыбного места.
// ---------------------------------------------------------

PhotoAdd.Form = 	'<form	name="photo_uploader"' +
			'		enctype="multipart/form-data"' +
			'		id="photo_uploader"' +
			'		action="./commit_photo.php"' +
			'		method="post"' +
			'		style="margin: 0px; padding: 0px;"' +
			'		onsubmit="return(postPhoto(this));">' +
			'	<div style="margin-bottom: 5px;">' +
			'Фото:<br/>' +
			'<input type="file" name="photo_filename" id="photo_filename" style="width: 300px;"><br/><br/>' +
			'<input type="hidden" name="photo_lng" id="photo_lng">' +
			'<input type="hidden" name="photo_lat" id="photo_lat">' +
			'Название:<br/>' +
			'<input type="text" name="photo_name" id="photo_name" style="width: 300px;"><br/>' +
			'Описание:<br/>' +
			'<textarea name="photo_description" id="photo_description" style="width: 300px; height: 150px; font: 11px Tahoma;">' +
			'</textarea><br/><br/>' +
			'<input type="submit" style="border:1px solid #FFFFFF; font: 11px Tahoma; width: 300px; height: 30px; text-align: center;" value="Добавить">' +
			'</div>' +
			'</form>';

PhotoAdd.prototype._enable;

PhotoAdd.prototype._map;
PhotoAdd.prototype._layer;
PhotoAdd.prototype._marker;
PhotoAdd.prototype._infoWindow;
PhotoAdd.prototype._dragging;

var _PhotoAdd_ = null;

/**
 *  
 *  @param {VMapEngine} map карта.
 *  @constructor
 */
function PhotoAdd(map) {
	this._map = map;
	this._enable = false;
	this._dragging = false;
	this._layer = new VLayer();
	this._map.addLayer(this._layer);
	
	var _this = this;
	VEvents.addListener("mouseclick",
		function(point) {
			if (!_this._enable) return;
			
			if (_this._marker == null)
				_this._createMarker(point);
			else {
				_this._marker.setPoint(point);
				_this._dragging = true;
				_this._marker.closeInfoWindow();
				_this._marker.openInfoWindow();
				_this._dragging = false;
				_this._map.repaintMap();
			}
		}
	);
	
	_PhotoAdd_ = this;
	
	return(this);
}

/**
 *  
 *  @param {VPoint} point карта.
 */
PhotoAdd.prototype._createMarker = function(point) {
	_FishPlaceAdd_._disable();
	
	var geo = point.convertToGeoPoint();
	this._marker = new VMarker(geo);
	this._marker.setHint(	"Широта: " + geo.getLatitude().toPrecision(6) + "<br/>" +
				"Долгота: " + geo.getLongitude().toPrecision(6));
	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));
		}
	);
	
	this._marker.setDraggable(true);
	//this._marker.setHint("");
	this._infoWindow = new VInfoWindow("Добавление фотографии", PhotoAdd.Form);
	this._infoWindow.setSize(360, 350);
	this._infoWindow.setAlwaysOpen(true);
	//this._infoWindow.setScrollOnOpen(false);
	this._marker.bindInfoWindow(this._infoWindow);
	VEvents.addListener(this._infoWindow, "close",
		function() {
			if (_this._dragging) return;
			// Закрытие окна -> Отмена
			_this._disable();
		}
	);
	
	var _this = this;
	VEvents.addListener(this._marker, "enddrag",
		function() {
			_this._dragging = false;
			_this._marker.openInfoWindow();
		}
	);
	VEvents.addListener(this._marker, "mousedown",
		function() {
			_this._dragging = true;
		}
	);
	
	this._layer.addMarker(this._marker);
	this._map.repaintMap();
	this._marker.openInfoWindow();
}

/**
 *  Включение.
 */
PhotoAdd.prototype.enable = function() {
	this._layer.show();
	
	if (this._marker == null)
		this._createMarker(this._map.getCenter());
	else {
		this._marker.setPoint(this._map.getCenter());
		this._dragging = true;
		this._marker.closeInfoWindow();
		this._marker.openInfoWindow();
		this._dragging = false;
	}
	
	this._enable = true;
}

/**
 *  Отключение утилиты.
 */
PhotoAdd.prototype._disable = function() {
	this._layer.removeMarker(this._marker);
	this._marker = null;
	
	this._layer.hide();
	
	this._enable = false;
}

/**
 *  Отправка сообщения.
 */
function postPhoto(form) {
	var geoPoint = _PhotoAdd_._marker.getPoint().convertToGeoPoint();
	document.getElementById("photo_lng").value = geoPoint.getLongitude();
	document.getElementById("photo_lat").value = geoPoint.getLatitude();
	
	return(AIM.submit(form));
}

/**
 *  Отмена.
 */
function cancelPhoto() {
	_PhotoAdd_._disable();
}

