$(document).ready(function(){
	$('#add_comment_form').ajaxForm({
		dataType: 'json',
		beforeSubmit: function(){
			$('#add_comment_form input, #add_comment_form textarea').attr('disabled', 'disabled');
		},
		success: function(data){
			if(data.error) {
				if($('#add_comment_form .error').size() <= 0) {
					$('#add_comment_form').prepend('<p class="error"></p>');
				}
				$('#add_comment_form .error').text(data.error);
				$('#add_comment_form input, #add_comment_form textarea').attr('disabled', '');
			} else {
				document.location.href = '/';
			}
		}
	});
});

function comment_reply_to(id) {
	var comment_id = '#comment_'+id;
	if($('ul', comment_id).size() < 1) {
		$(comment_id).append('<ul class="comments"></ul>');
	}

	if($('#add_reply').size() > 0) {
		$('#add_reply').appendTo(comment_id+' > ul');
	} else {
		$('#add_comment_form').clone().appendTo(comment_id+' > ul').
			attr('id', '').
			wrap('<li id="add_reply"></li>');
	}
	$('#add_reply input[name=parent_id]').val(id);
	$('#add_reply textarea').val('');
	$('#add_reply form input, #add_reply form textarea').attr('disabled', '');

	$('#add_reply form').ajaxForm({
		dataType: 'json',
		beforeSubmit: function(){
			$('#add_reply form input, #add_reply form textarea').attr('disabled', 'disabled');
		},
		success: function(data) {
			if(data.error) {
				if($('#add_reply form .error').size() <= 0) {
					$('#add_reply form').prepend('<p class="error"></p>');
				}
				$('#add_reply form .error').text(data.error);
				$('#add_reply form input, #add_reply form textarea').attr('disabled', '');
			} else {
				$('#add_reply').replaceWith(data.new_comment);
			}
		}
	});

	return false;
}
