function scInit() {
	// Check URL for specific comment reference
	
	// TODO add init function to slick comments
	
	// Scroll to that comment, move the form to be a reply to it
	
}

function toggleComments(id) {
	if (!jQuery(id + ' ol').size()) {
		// Build out the list of comments + form
		// AJAX request to get the current comments + post form
		var ajax = '';
		jQuery.ajax({
			url: slickComments + 'post-comments.php?post=' + id.replace(/[^0-9]/g, '') + '&user=' + currentUserID,
			type: 'GET',
			error: function(xhttp, text, e) {
				alert('Please try again in a moment.');
			},
			success: function(data, status) {
				if ('success' == status) {
					ajax = data;
					jQuery(id).hide('fast', function() {
						jQuery(id).html(ajax);
						jQuery(id).slideDown('normal', function(){
							jQuery(id).scrollTo();
							
							// Focus so that we can start writing
							jQuery(id + ' .comment-txt').focus();
						});
					});
				}
			}
		});
	} else {
		// We know the comments HTML is there, toggle it
		jQuery(id).slideToggle('normal', function() {
			if (jQuery(id + ':visible').size()) {
				jQuery(id).scrollTo();
				
				// Focus so that we can start writing
				jQuery(id + ' .comment-txt').focus();
			}
		});
	}
}

function replyToComment(thread, comment) {
	form = jQuery(thread + ' li.form');
	if (form.size()) {
		form.slideUp('fast', function() {
			// See if there's an OL of replies already for this comment 
			if (!jQuery(comment + ' > ol').size()) {
				// Figure out depth, because we don't indent past 3
				if (2 > jQuery(comment).parents('ol.indent').size()) {
					c = 'indent';
				} else {
					c = 'no-indent';
				}
				// Adding a new OL, indented if required
				jQuery(comment + ' > .comment-block').after('<ol class="' + c + '"></ol>');
			}
			
			// Change the parent ID to match where the form is about to be placed
			form.find('input:hidden[name^=comment_parent_ID-]').val(comment.replace(/[^0-9]/g, ''));
			
			// Add the LI containing the form
			jQuery(comment + ' > ol').prepend('<li class="form" style="display:none;">' + form.html().replace('Comment on this post', 'Reply to this comment').replace('Submit Comment', 'Submit Reply') + '<li>');
			jQuery(comment + ' > ol li.form').slideDown('normal', function() {
				jQuery(comment + ' li.form textarea').focus();
			});
			form.remove();
			
			// If there's no "Post a Comment" link, add one
			if (!jQuery(thread + ' .post-comment').size()) {
				jQuery(thread).append('<div class="post-comment"><a href="javascript:void(0);" onclick="postComment(\'' + thread + '\');">Comment on this post</a></div>')
			}
		});
	}
}

function postComment(thread) {
	form = jQuery(thread + ' li.form');
	if (form.size()) {
		form.slideUp('fast', function() {
			form.find('input:hidden[name^=comment_parent_ID-]').val('0');
			jQuery(thread + ' > ol').append('<li class="form" style="display:none;">' + form.html().replace('Reply to this comment', 'Comment on this post').replace('Submit Reply', 'Submit Comment') + '</li>');
			jQuery(thread + ' li.form').slideDown('normal', function() {
				jQuery(thread + ' li.form').scrollTo();
				jQuery(thread + ' li.form textarea').focus();
			});
			form.remove();
		});
		
		jQuery(thread + ' .post-comment').slideUp('fast', function() {
			jQuery(thread + ' .post-comment').remove();
		});
	}
}

function submitComment(thread) {
	// Get the form, or die if we can't
	form = jQuery(thread + ' li.form');
	if (!form.size()) {
		return false;
	}
	
	// comment_post_ID
	comment_post_ID = form.find('input:hidden[name^=comment_post_ID-]').val();
	
	// comment_parent_ID
	comment_parent_ID = form.find('input:hidden[name^=comment_parent_ID-]').val();
	comment_parent_ID = comment_parent_ID ? comment_parent_ID : 0;
	
	// This is the comment textarea
	ta = 'textarea[name^=comment-]';
	
	// comment_content (comment)
	comment = form.find(ta).val();
	
	// Get dimensions of the textarea so we can overlay something
	w = form.find(ta).width();
	h = form.find(ta).height() - 20;
	
	// Disable the form and set "sending" status display
	form.find(ta).before('<div class="comment-posting" style="width:' + w + 'px; height:' + h + 'px;z-index:1000;"><img src="' + imageDir + 'comments-spinner.gif" width="16" height="16" align="absmiddle" alt="" border="0" /> Posting your comment...</div>');
	
	// AJAX it
	jQuery.ajax({
		url: bookHome + 'wp-comments-post.php',
		type: 'POST',
		data: {
			comment_post_ID:   comment_post_ID,
			comment_parent_ID: comment_parent_ID,
			comment:           comment
		},
		error: function(xhttp, text, e) {
			alert('Please try again in a moment, you may be posting too quickly or forgot to enter a comment.');
			form.find('.comment-posting').remove();
		},
		success: function(data, status) {
			if ('success' == status) {
				// Get the comment details as JSON and parse them
				comment = eval('(' + data + ')');
				
				// Hide and re-enable form, display comment inline
				form.find('.comment-posting').fadeOut();
				form.slideUp('normal', function() {
					// Be careful to use the same formatting (unless you want to mark it as new?) as in the
					// slick-comments.php file that creates the other comments.
					li =  '<li id="comment-' + comment.comment_ID + '" style="display:none;">';
					li += '	<div class="comment-block">';
					li += '		<div class="avatar">' + myAvatar + '</div>';
					li += '		<div class="comment-posted-by">Posted By: <a href="' + myProfile + '">' + comment.comment_author + '</a></div>';
					li += '		<div class="comment-date">' + comment.comment_date + ' <span class="comment-time">' + comment.comment_time + '</span></div>';
					li += '		<div class="comment-body">' + comment.comment_content + '</div>';
					li += '		<div class="comment-reply"><a href="javascript:void(0);" onclick="deleteComment(\'' + comment.comment_ID + '\')" class="delete">Delete</a> | <a href="javascript:void(0);" onclick="replyToComment(\'#comments-' + comment.comment_post_ID + '\', \'#comment-' + comment.comment_ID + '\');" class="reply">Reply to this comment</a></div>';
					li += '		<div class="clear"></div>';
					li += '	</div>';
					li += '</li>';
					form.parent('ol').append(li);
					jQuery('#comment-' + comment.comment_ID).slideDown('normal', function() {
						jQuery('#comment-' + comment.comment_ID).scrollTo();
					});
					
					// We also reset the value in the form to avoid it being copied around
					form.find(ta).val('');
				});
			} else {
				alert('Please try again in a moment, you may be posting too quickly.');
				form.find('.comment-posting').remove();
			}
		}
	});
	
	return false;
}

function deleteComment(comment) {
	yes = confirm("Are you sure?\n\nDeleting a comment removes it permanently, and if\nthere are any replies, they will also be deleted.\n\n");
	if (!yes) {
		return false;
	}
	// AJAX request to delete
	jQuery.ajax({
		url: slickComments + 'delete-comment.php',
		type: 'POST',
		data: { comment_ID: comment, nonce: delNonce, user: currentUserID },
		error: function(xhttp, text, e) {
			alert('Please try again in a moment.');
		},
		success: function(data, status) {
			if ('success' == status) {
				postComment('#' + jQuery('#comment-' + comment).parents('div[id^=comments-]').attr('id'));
				jQuery('#comment-' + comment).animate({opacity:0, height:0}, 'normal', null, function() { jQuery('#comment-' + comment).remove(); });
			} else {
				alert('Please try again in a moment.');
			}
		}
	});
}

function editComment(comment) {
	// TODO ability to edit a comment inline?
	alert('edit comment ' + comment);
}