var CPostPage = CFormHandler.extend({
	initialize: function()
	{
		this.parent({
			assets: {
				FormSpinner: 'resources/images/form_spinner.gif'
			}
		});
		
		LoginForm.addEvent('onlogin', this.onLoginStateChanged.bind(this, true));
		LoginForm.addEvent('onlogout', this.onLoginStateChanged.bind(this, false));
	},

	onLoginStateChanged: function(LoggedIn)
	{
		if (!$('CommentFormContent'))
			return false;
		
		this.ShowSpinner( 'CommentLoginSpinner', 'CommentFormContent', 'FormSpinner', {});
		var CommentTextValue = $('Comment') ? $('Comment').getValue() : '';
		
		new Phpr_Request({
			postBody: {IsLoggedIn: LoggedIn ? 1 : 0, CommentText: CommentTextValue},
			handler:'ev{Post_OnUpdateCommentForm}',
			update: 'CommentFormContent',
			onFailure: this.requestFailure,
			onBeforeComplete: function() {
				this.HideSpinner('CommentLoginSpinner');
			}.bind(this)
		});
	},

	postComment: function(PreviewMode)
	{
		var ContentElement = PreviewMode ? 'CommentPreviewFormContent' : 'FormContent';
		
		this.ShowSpinner( 'PostSpinner', ContentElement, 'FormSpinner', {});

		new Phpr_Request({
			formId: 'CommentFormElement',
			handler:'ev{Post_OnSendComment}',
			update: 'multi',
			errorBlockId: 'CommentError',
			onFailure: this.displayFormError.bind(this),
			onBeforeComplete: function() {
				this.HideSpinner('PostSpinner');
			}.bind(this),
			onSuccess: function() {
				$('Text').value = '';
			}.bind(this)
		});
		
		return false;
	},
	
	previewComment: function()
	{
		this.ShowSpinner( 'PostSpinner', 'FormContent', 'FormSpinner', {});

		new Phpr_Request({
			formId: 'CommentFormElement',
			handler:'ev{Post_OnPreviewComment}',
			update: 'CommentPreview',
			errorBlockId: 'CommentError',
			onFailure: this.displayFormError.bind(this),
			onBeforeComplete: function() {
				this.HideSpinner('PostSpinner');
			}.bind(this),
			onSuccess: function() {
				$('FormContent').hide();
				$('CommentPreview').show();
			}.bind(this)
		});
		
		return false;
	},
	
	editComment: function()
	{
		$('FormContent').show();
		$('CommentPreview').hide();
		$('Comment').focus();

		return false;
	}
});

var PostPage = null;

window.addEvent('domready', function() {
	PostPage = new CPostPage();
});