function inputCounter(txtarea_id) {
	var max = 499;
	var txtarea_obj = document.getElementById(txtarea_id);
	var shared_ancestor = $(txtarea_obj).parent();
	var span_obj = $('.remaining-chars span', shared_ancestor);
	
	if (txtarea_obj.value.length > max) 
		txtarea_obj.value = txtarea_obj.value.substring(0, max);
	else
		span_obj.html((max - txtarea_obj.value.length));
}

$(function() {

	// Show the comment's editor if the user decides to discard the reply
	$('.reply-button').each(function() {
		var button = $(this);
		
		button.bind('click', function(e) {
			e.preventDefault();
			
			var main_parent = button.parent().parent();
			$('.item-comment-reply-editor:first', main_parent).removeClass('hidden').addClass('visible');
			$('.comment-editor-wrapper:first', main_parent).removeClass('hidden').addClass('visible');
		
			return false;
		});
	});
			
	// Hide the comment's editor if the user decides to discard the reply
	$('.discard-comment').each(function() {
		var button = $(this);
		
		button.bind('click', function(e) {
			e.preventDefault();
			
			var main_parent = button.parent().parent();
			main_parent.removeClass('visible').addClass('hidden');
		
			return false;
		});
	});
	
	// Require confirmation from the user before allowing a comment to be deleted
	$('.comment-delete-btn').each(function() {
		var button = $(this);
		
		button.bind('click', function(e) {
			var confirmed = confirm("Are you sure that you want to delete this comment?\n\nThis cannot be undone.");
			
			if (!confirmed) {
				e.preventDefault();
				return false;
			}

		});
	});
	
	// Clear the value of each comment name input field the first time it is focused on
	$('.comment-editor-name').each(function() {
		var field = $(this);
		
		field.bind('focus', function() {
			var field_val = field.attr('value');
			
			if ('Please enter your name' == field_val)
				field.attr('value', '');
		});
	});
});
