// Marker Edit: This file deals with all of the functions concerning editing and removing markers..
// By: Max Sklar.

//This variable tells us that we are in edit mode.
var inEditMode;

//This is the new edited marker;
var editedMarker;

//This is the object for the form that we're working with.
var ourForm;
	
function loadEditForm() {
	inEditMode = true;
	dumpMarkerInfo();
	editedMarker = new SampleMarker(currentMarker, currentMarkerPoint, iconDatabase[currentIcon][1], map);
	
	if (inCreationMode) exitCreationMode();
	
	//Complete the edit form with the proper information.
	document.getElementById("editedTitle").value = currentTitle;

	setTextEditorHtml("editedDescription", currentDescription);

	ourForm = document.forms["editForm"];

	function maxZoomToCatId(maxzoom) {
		if (maxzoom > 15) return 1;
		if (maxzoom > 10) return 2;
		if (maxzoom > 5) return 3;
		return 4;
	}
	
	// use currentIcon to get the icon of the marker being edited.  Maybe later we'll want this to affect the icon controls.
	makeIconControl(maxZoomToCatId(currentMaxZoom), "editIconControls", "editForm", "changedIconWhileEditing");
	
	//document.getElementById("editLowestZoomAvailability").innerHTML = currentMaxZoom;
	//changeMarkerZoomOut(0); // Set the max zoom level displayed on the page to what it currently is
	ourForm.elements["edit_importance"].value = currentMaxZoom - currentMinZoom;
	
	document.getElementById("map_table").style.display = "none";

	//Show Edit Form
	document.getElementById("editFormContainer").style.display = "block";
}

function changeMarkerZoomOut(amount) {
	currentMinZoom = parseInt(currentMinZoom) + parseInt(amount);
	
	var s = "";
	if (currentMinZoom > currentMaxZoom - 4)
		s += "<a href=\"javascript:changeMarkerZoomOut(-1)\">&lt;&lt;</a> ";
	s += currentMinZoom;
	if (currentMinZoom < currentMaxZoom)
		s += " <a href=\"javascript:changeMarkerZoomOut(1)\">&gt;&gt;</a>";
	document.getElementById("editMaxZoomAvailability").innerHTML = s;
}

function submitEditedMarker() {
	var file = 'editMarker.php';
	
	var icon = getSelectedIcon("editForm");
	
	//Post the values to a string:
	var str = "";
	str += createPostStatement("editedTitle", ourForm.elements["editedTitle"].value);
	str += createPostStatement("editedText", getTextEditorHtml("editedDescription"));
	str += createPostStatement("latitude", currentMarkerPoint.y);
	str += createPostStatement("longitude", currentMarkerPoint.x);
	if (icon != -1) str += createPostStatement("icon", iconDatabase[getIconDbNumber(icon)][2]);
	str += createPostStatement("user", userInfo.id);
	str += createPostStatement("markerID", currentID);
	str += createPostStatement("minZoom", currentMaxZoom - ourForm.elements["edit_importance"].value);
	str += createPostStatement("maxZoom", currentMaxZoom);
	str += createPostStatement("submit", 1);
	
	if (isLoggedIn) {
		str += createPostStatement("userid", userInfo.id);
		str += createPostStatement("encpassword", coded_password);
	}
	
	str = str.substr(0,(str.length - 1));  //Get rid of last ampersand
	
	postWithFile(file, false, str, function(response) {
		alert(response);
		switchTab(EXPLORER_TAB);
		exitEditMode();
		loadMarkers();
	});
}

function changedIconWhileEditing(icon) {
	editedMarker.changeIcon(iconDatabase[getIconDbNumber(icon)][1]);
}

function exitEditMode() {
	inEditMode = false;
	if (editedMarker)
		editedMarker.destroy();
	makeInvisible("editFormContainer");
	document.getElementById("map_table").style.display = "block";
}