function showCommentsList() {
	
	if (!$('comments_main_container').visible()) {
		
		new Effect.Appear('comments_main_container', {duration:.25, queue: 'end', limit:1});
		$('comment_toggle_button').update('Hide Comments');
		
	} else {
		
		$('comments_main_container').hide();
		$('comment_toggle_button').update('Show Comments');
				
	}
	
}

function regenerateCommentsList(s, d) {
	
	new Ajax.Updater('comments_container', 'ajaxShowComments.php', {
		
		parameters: {id: id, type: 'document', s: s, d: d},
		evalScripts: true
		
	});
	
}

function initEditComment(commentId) {
	
	commentContainer = 'comment_container_' + commentId;
	commentBody = 'comment_' + commentId;
	
	//handle existing editor (if necessary)
	if ($('edit_in_place')) {
		
		//remove all observers
		$('editor_ok').stopObserving();
		$('editor_cancel').stopObserving();

		//remove the edit-in-place dom object
		$('edit_in_place').remove();
		
		if ($(lastComment)) {
			
			$(lastComment).show();
			
		}
		
	}
	
	//hide the current static field
	$(commentContainer).hide();
	
	//create the editable text and ok & cancel buttons
	var showEditor ='<div id="edit_in_place" class="comment_container"><textarea id="comment_edit" rows="8" style="width:776px">' + $(commentBody).innerHTML.replace(/"/g,'&quot;').replace(/\'/g,'&#039;').replace(/<br>/g,'\n') + '</textarea><br><input type="button" id="editor_ok" value="ok"> <input type="button" id="editor_cancel" value="cancel"></div>';
	$(commentContainer).insert({after: showEditor});
	
	//assign the category being edited to a global variable
	lastComment = commentContainer;
	
	//create observers for ok and cancel buttons
	$('editor_ok').observe('click', function() {editComment(commentId);});
	$('editor_cancel').observe('click', function() {cancelEditComment();});	
	
}

function editComment(commentId) {
	
	new Ajax.Request('ajaxUpdateComment.php', {
		
		parameters: {id: commentId, value: $F('comment_edit'), type: 'document'},
		onComplete: function() {
			
			if ($('edit_in_place')) {

				//remove all observers
				$('editor_ok').stopObserving();
				$('editor_cancel').stopObserving();

				//remove the edit-in-place dom object
				$('edit_in_place').remove();
				
				//regenerate the comment list
				$(lastComment).show();

			}
			
		}
		
	});
	
}

function cancelEditComment() {
	
	//remove all observers
	$('editor_ok').stopObserving();
	$('editor_cancel').stopObserving();
	
	//remove the edit-in-place dom object
	$('edit_in_place').remove();
	
	//show the original static category name
	$(lastComment).show();
	
}

function deleteComment(commentId) {
	
	new Ajax.Request('ajaxDeleteComment.php', {
		
		parameters: {id: commentId, type: 'document'},
		onComplete: function() {
			
			regenerateCommentsList(last_s, '');
			
		}
		
	});
	
}

function showAddComment() {
	
	if (!$('add_comment_container').visible()) {
		
		new Effect.Appear('add_comment_container', {duration:.25, queue: 'end', limit:1});
		
		
	} else {
		
		$('add_comment_container').hide();
				
	}
	
}

function addComment() {
	
	if ($('add_comment') != null) {
		
		//wathes the submit button
		$('add_comment').observe('submit', function(e) {

			//e.stop prevents ("stops") the submit button from executing 
			e.stop();
			new Ajax.Request('ajaxAddComment.php', {

				parameters: $('add_comment').serialize(),
				onComplete: function() {

					regenerateCommentsList();
					$('add_comment').reset();

				}

			});

		});
		
	}
	
}

//show the spinner whenever anything ajax is happening 
Ajax.Responders.register({
	onCreate: function() { $('spinner').show(); },
	onComplete: function() {
			
			if (0 == Ajax.activeRequestCount) {
				
				$('spinner').hide();
								
			}
			
		}
});

document.observe('dom:loaded', function() {
	
	if ($('comments_container') != null) {
		
		regenerateCommentsList();
		
	}
	
	if ($('add_comment') != null) {
		
		addComment();
		
	}
	
});