// ---------------------------------------------------------
// FishPlaceAdd class
// GUI для добавления рыбного места.
// ---------------------------------------------------------

FishPlaceAdd._form;

FishPlaceAdd.prototype._enable;

FishPlaceAdd.prototype._map;
FishPlaceAdd.prototype._layer;
FishPlaceAdd.prototype._marker;
FishPlaceAdd.prototype._infoWindow;
FishPlaceAdd.prototype._dragging;

/**
 *  
 *  @param {VMapEngine} map карта.
 *  @constructor
 */
function FishPlaceAdd(map) {
	var element = document.getElementById("add_place_form");
	if (element == null) return;
	
	this._form = "<div id='form_container'>" + element.innerHTML + "</div>";
	document.body.removeChild(element);
	
	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();
			}
		}
	);
	
	_FishPlaceAdd_ = this;
	
	return(this);
}

/**
 *  
 *  @param {VPoint} point карта.
 */
FishPlaceAdd.prototype._createMarker = function(point) {
	_PhotoAdd_._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("Добавление рыбного места", this._form);
	this._infoWindow.setSize(400, 440);
	this._infoWindow.setAlwaysOpen(true);
	//this._infoWindow.setScrollOnOpen(false);
	this._marker.bindInfoWindow(this._infoWindow);
	VEvents.addListener(this._infoWindow, "close",
		function() {
			if (_this._dragging) return;
			if (_this._changing) 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._marker.openInfoWindow();
	this._map.repaintMap();
}

/**
 *  Включение.
 */
FishPlaceAdd.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;
       //params_ready = false;
}

/**
 *  Отключение утилиты.
 */
FishPlaceAdd.prototype._disable = function() {
	this._layer.removeMarker(this._marker);
	this._marker = null;
	
	this._layer.hide();
	
	this._enable = false;
}

/**
 *  Отправка сообщения.
 */
function postPlace(form) {
	var geoPoint = _FishPlaceAdd_._marker.getPoint().convertToGeoPoint();
	document.getElementById("fishplace_lng").value = geoPoint.getLongitude();
	document.getElementById("fishplace_lat").value = geoPoint.getLatitude();
	
	return(AIM.submit(form));
}

/**
 *  Отмена.
 */
function cancelPlace() {
    _FishPlaceAdd_._disable();
}

function showCloseFishList() {
	var el = document.getElementById("fish_form");
	if (el.style.display == "block")
		el.style.display = "none";
	else
		el.style.display = "block";
	
	var fishes_list = document.getElementById("fishes_list");
	
	var list = new Array();
	if (document.getElementById("karp").checked) list.push('карп');
	if (document.getElementById("karas").checked) list.push('карась');
	if (document.getElementById("som").checked) list.push('сом');
	if (document.getElementById("tolstolob").checked) list.push('толстолоб');
	if (document.getElementById("amur").checked) list.push('амур');
	if (document.getElementById("shuka").checked) list.push('щука');
	if (document.getElementById("sudak").checked) list.push('судак');
	if (document.getElementById("shereh").checked) list.push('жерех');
	if (document.getElementById("okun").checked) list.push('окунь');
	if (document.getElementById("plotva").checked) list.push('плотва');
	if (document.getElementById("lesh").checked) list.push('лещ');
	if (document.getElementById("raki").checked) list.push('раки');
	if (document.getElementById("lin").checked) list.push('линь');
	
	var s = '';
	for (var i =0; i < list.length; i++) {
		if (i > 0) s += ', ';
		s += list[i];
	}
	if (list.length == 0) s = '[кликните для выбора]';
	
	fishes_list.innerHTML = s;
}

function enableDisablePrice(el) {
	document.getElementById("fishplace_price").disabled=el.checked;
}

var params_ready = false;
function checkParams() {
	if (document.getElementById("fishplace_price").value == '' && !document.getElementById("is_free").checked) {
		alert("Введите стоимость.");
		return(false);
	}
	if (document.getElementById("fishplace_depth").value == '') {
		alert("Введите максимальную глубину водоема.");
		return(false);
	}
	if (document.getElementById("fishplace_square").value == '') {
		alert("Введите площадь водоема.");
		return(false);
	}
	if (	document.getElementById("karp").checked == false &&
		document.getElementById("karas").checked == false &&
		document.getElementById("som").checked == false &&
		document.getElementById("tolstolob").checked == false &&
		document.getElementById("amur").checked == false &&
		document.getElementById("shuka").checked == false &&
		document.getElementById("sudak").checked == false &&
		document.getElementById("shereh").checked == false &&
		document.getElementById("okun").checked == false &&
		document.getElementById("plotva").checked == false &&
		document.getElementById("lesh").checked == false &&
		document.getElementById("raki").checked == false &&
		document.getElementById("lin").checked == false) {
		alert("Укажите виды рыб.");
		return(false);
	}
	if (document.getElementById("fishplace_contacts").value == '') {
		alert("Введите контакты владельца водоема.");
		return(false);
	}
	if (document.getElementById("fishplace_description").value == '') {
		alert("Введите описание.");
		return(false);
	}
	
	
	return(true);
}
