$(document).ready(function() {
	$('#comment_preview_post').show().click(function() {
		$('#comment_post_form').submit();
		return false;
		});
	$('#comment_preview_edit').click(function() {
		$('html,body').animate({scrollTop: $('#comment_post_head').offset().top}, 1000, 'easeOutExpo');
		return false;
		});
	$('#comment_preview_button').show().click(function() {
		if(comment_validate_post()) {
			$('.comment_status').hide();
			$('#comment_preview .fillin').html('<p>Loading comment preview...</p>');
			$('#comment_preview').show();
			$('.comment_list').show();
			//$('.comment_list')[0].scrollIntoView();
			$('html,body').animate({scrollTop: $('#comment_block').offset().top}, 1000, 'easeOutExpo');
			$('#comment_preview .fillin').load($('link[@rel=home]').attr('href') + 'plugins/comments/preview_cb', { name: $('#comment_name').val(), title: $('#comment_title').val(), comment: $('#comment_comment').val() });
			}
		return false;
		});
	$('#comment_post_form').submit(function() {
		var v = $('#comment_remember_box').attr('checked')
			? [ escape($('#comment_name').attr('value')), escape($('#comment_title').attr('value')), 1 ]
			: [ '', '', '' ];
		$.cookie('comments_saved_info', v.join(' '), { expires: 365, path: '/' });
		return comment_validate_post();
		});
	$('#comment_comment').maxlength({maxCharacters: 800, showAlert: true, status: false});
	$('#comment_remember').show();
	if($.cookie('comments_saved_info')) {
		var v = $.cookie('comments_saved_info').split(' ');
		$('#comment_name').attr('value', unescape(v[0]));
		$('#comment_title').attr('value', unescape(v[1]));
		$('#comment_remember_box').attr('checked', v[2] ? 'checked' : '');
		}
	else {
		$('#comment_remember_box').attr('checked', 'checked');
		}
	});

function comment_validate_post() {
	var errors = '';
	if($('#comment_name').val().indexOf(' ') <= 0) errors += 'You must enter your full name. ';
	if(!$('#comment_title').val()) errors += 'You must enter your title. ';
	if(!$('#comment_comment').val()) errors += 'You must enter a comment. ';
	if(errors) {
		$('.comment_status').hide();
		$('#comment_validation_error').text(errors).show();
		return false;
		}
	else {
		return true;
		}
	}

