// JS for the leaflet page
(function($) {
	
	var updateTextarea = function(elem) {
		var container = $(elem).closest('.sbzLeafletElementContainer');
		var text = $('.comment .content p',container).html();
		$('textarea',container).html(text);
	};
	
	var showCommentForm = function(elem) {
		var container = $(elem).closest('.sbzLeafletElementContainer');
		$('.commentContainer',container).addClass('hasForm');
		$('.commentForm',container).show();
	};
	
	var hideCommentForm = function(elem) {
		var container = $(elem).closest('.sbzLeafletElementContainer');
		$('.commentContainer',container).removeClass('hasForm');
		$('.commentForm',container).hide();
	};	
	
	var showComment = function(elem) {
		var container = $(elem).closest('.sbzLeafletElementContainer');
		$('.comment',container).show();
	};
	
	var hideComment = function(elem) {
		var container = $(elem).closest('.sbzLeafletElementContainer');
		$('.comment',container).hide();
	};		
	
	var isFormVisible = function(elem) {
		var container = $(elem).closest('.sbzLeafletElementContainer');
		return $('.commentForm:visible',container).size() == 1;
	};
	
	$(document).ready(function() {
		$('.sbzLeafletElementContainer .actions .edit').click(function() {
			showCommentForm(this);
			hideComment(this);
			updateTextarea(this);
			return false;
		});
		$('.sbzLeafletElementContainer .addComment').click(function() {
			if (!isFormVisible(this)) {
				showCommentForm(this);
			} else {
				hideCommentForm(this);
			}
			return false;
		});
	});
	
})(jQuery);	
