var listen = function() {
	$('comment-add').addEvent('click', function(e) {
		if($type(e) == 'event')
			Event(e).stop();
		if($('post-comment-form').retrieve('loadCount') > 0)
			return;
		$('post-comment-form').loading();
		new Request({
			url: '_req/post.php',
			method: 'post',
			data: {
				action: 'comment',
				name: $('comment-inp-name').value,
				email: $('comment-inp-email').value,
				homepage: $('comment-inp-homepage').value,
				comment: $('comment-inp-comment').value,
				phony: $('comment-inp-phony').value,
				post: $('comment-inp-post').value
			},
			onComplete: function(r) {
				try {
					r = JSON.decode(r);
					if(r.error) {
						$('post-comment-form').loaded();
						new error(r.error);
						return;
					}
					[
						$('comment-inp-name'),
						$('comment-inp-email'),
						$('comment-inp-homepage'),
						$('comment-inp-comment'),
						$('comment-inp-phony')
					].each(function(o) {
						o.set('value', '');
					});
					window.location = './?' + $('comment-inp-alias').value + '#comment-' + r.comment;
					window.location.reload();
					return;
				}
				catch(err) {
					$('post-comment-form').loaded();
					new error('There was an error while leaving your comment');
				}
			},
			onFailure: function() {
				$('post-comment-form').loaded();
				new error('There was an error while leaving your comment');
			}
		}).send();
	});
	
	$('comment-inp-homepage').addEvent('keyup', function(e) {
		if(Event(e).key == 'enter')
			$('comment-add').fireEvent('click');
	});
};

window.addEvent('domready', function() {
	listen();
});
