$(document).ready(function() {
// Textareas Limit
	$('textarea#comment').keyup(function() {
		var charLength = $(this).val().length;
		$('.counter').html(charLength);
		if( this.value.length >= 10000 ){$('.counter').parent().addClass('textHighlight');}
		else {$('.counter').parent().removeClass('textHighlight');}
	});
// Textareas Resize
	$('textarea.resizable:not(.processed)').TextAreaResizer();
// Comment Preview
	$("#cmntPreviewBtn").click(function(){
		var $comment = '';
		$comment = $('#comment').val();
		//line below detects urls and turns them into general hyperlinks
		//$comment = $comment.replace(/(\s|^)((https?:\/\/[^\/]+)[^\s]*)/g, '$1<a href="$2">$3/...</a>');
		//$comment = $comment.replace(/\n/g, "<br>").replace(/\n\n+/g, '<br><br>').replace(/(<\/?)script/g,"$1noscript");
		$comment = $comment.replace(/\n/g, "<br>").replace(/\n\n+/g, '<br><br>');
		$('#livePreview').html($comment);
		$('#cmntEdit').hide();
		$('#cmntPreviewBtn').hide();
		$('#cmntPreview').show();
		$('#cmntEditBtn').show();
		return false;
	});
// Comment Edit
	$("#cmntEditBtn").click(function(){
		$('#cmntEdit').show();
		$('#cmntPreview').hide();
		$('#cmntPreviewBtn').show();
		$('#cmntEditBtn').hide();
		return false;
	});
// Comment Cancel
	$("#cmntCancelBtn").click(function(){
		$('#livePreview').html('');
		$('#cmntForm').insertAfter('#comments');
		$('#cmntEdit').show();
		$('#cmntPreviewBtn').show();
		$('#cmntEditBtn').hide();
		$('#cmntPreview').hide();
		$('.cmntFooter a').removeClass('selected');
		// Reset textarea and counter
		$('#comment_form')[0].reset();
		$('.counter').html('0');
		$('.counter').parent().removeClass('textHighlight');
	});
// Comment Reply
	$('#cmntForm').show();
	$('#cmntPreviewBtn').show();
	$('#cmntEditBtn').hide();
	$('#cmntPreview').hide();
	// Find all the "reply to" links and bind to the click event
	$('a[href^=/posts/]').click(function() {
		var url_array = $(this)		// The anchor object
			.attr('href')						// Fetch the value of the href attribute
			.split("/");						// Divide into chunks, using / as the divider
		var id = url_array[3];		// We want the fourth chunk
		$('#weever-parent-id').val(id);	// Change the value of weever-parent-id
		$('#cmntForm').insertAfter(
			$(this).parent().parent()
			);
		$(this).addClass('selected');
		return false;
	});
});
