var CApplicationPage = CFormHandler.extend({
	initialize: function()
	{
		this.parent({
			assets: {
				FormSpinner: 'resources/images/form_spinner.gif',
				BarSpinner: 'resources/images/page_load_bar.gif',
				HoverStar: 'resources/images/app_star_hover.gif'
			}
		});
		

		multiColumnSettings=new MultiColumnSettings;
		multiColumnSettings.classNameScreen='FeatureColumns';
		multiColumnSettings.numberOfColumns=2;

		if ( $('FeatureList') && $ES('h3', $('FeatureList')).length > 1 )
		{
			new MultiColumn(document.getElementById('FeatureList'), multiColumnSettings);
		}
		
		this.initVoting();
		
		LoginForm.addEvent('onlogin', this.onLoginStateChanged.bind(this, true));
		LoginForm.addEvent('onlogout', this.onLoginStateChanged.bind(this, false));
	},

	onLoginStateChanged: function(LoggedIn)
	{
		if (!$('ReviewFormContent'))
			return false;
		
		this.ShowSpinner( 'CommentLoginSpinner', 'ReviewFormContent', 'FormSpinner', {});
		var CommentTextValue = $('Comment') ? $('Comment').getValue() : '';
		var CommentTitleValue = $('Title') ? $('Title').getValue() : '';
		
		new Phpr_Request({
			postBody: {IsLoggedIn: LoggedIn ? 1 : 0, CommentText: CommentTextValue, CommentTitle: CommentTitleValue},
			handler:'ev{App_OnUpdateCommentForm}',
			update: 'ReviewFormContent',
			onFailure: this.requestFailure,
			onBeforeComplete: function() {
				this.HideSpinner('CommentLoginSpinner');
			}.bind(this)
		});
	},
	
	initVoting: function()
	{
		new CVoting('RatingDESIGN', 'DESIGN', 'App_OnVote', {onVote: this.initVoting.bind(this)});
		new CVoting('RatingUSABILITY', 'USABILITY', 'App_OnVote', {onVote: this.initVoting.bind(this)});
		new CVoting('RatingFUNCTIONS', 'FUNCTIONS', 'App_OnVote', {onVote: this.initVoting.bind(this)});
	},
	
	previewComment: function()
	{
		this.ShowSpinner( 'PostCommentSpinner', 'ReviewFormContent', 'FormSpinner', {});
		
		new Phpr_Request({
			formId: 'CommentFormElement',
			handler:'ev{App_OnPreviewComment}',
			remote: 1,
			update: 'CommentPreview',
			errorBlockId: 'CommentError',
			onFailure: this.displayFormError.bind(this),
			onAfterError: function() {
				this.HideSpinner( 'PostCommentSpinner' );
			}.bind(this),
			onSuccess: function() {
				$('FormElements').hide();
				$('CommentPreview').show();
				$('CommentError').hide();
				this.HideSpinner( 'PostCommentSpinner' );
			}.bind(this)
		});
		
		return false;
	},
	
	editComment: function()
	{
		$('FormElements').show();
		$('CommentPreview').hide();
		$('Comment').focus();
		
		return false;
	},
	
	postComment: function(CommentFormMode)
	{
		this.ShowSpinner( 'PostCommentSpinner', 'ReviewFormContent', 'FormSpinner', {});

		var ErrorBlockId = CommentFormMode ? 'CommentPreviewError' : 'CommentError';

		new Phpr_Request({
			formId: 'CommentFormElement',
			handler:'ev{App_OnPostComment}',
			update: 'multi',
			errorBlockId: ErrorBlockId,
			onFailure: this.displayFormError.bind(this),
			onBeforeComplete: function() {
				this.HideSpinner( 'PostCommentSpinner' );
			}.bind(this)
		});

		return false;
	},
	
	postCommentVote: function(Comment, Positive)
	{
		new Phpr_Request({
			handler:'ev{App_OnCommentVote}',
			remote: 1,
			postBody: {CommentId: Comment, IsPositive: Positive},
			postBodyOnly: true,
			update: 'multi',
			onFailure: this.requestFailure
		});

		$('ReviewVote'+Comment).setHTML('<p class="Last">Thanks for your vote!</p>');
		
		return false;
	},

	addToFavorites: function()
	{
		this.ShowSpinner( 'FavoritesSpinner', 'AddToFavoritesBtn', 'BarSpinner', {Left: true, LeftOffset: -5});
		
		new Phpr_Request({
			handler:'ev{App_OnAddToFavorites}',
			remote: 1,
			update: 'multi',
			onFailure: this.requestFailure,
			onComplete: function() {
				this.HideSpinner( 'FavoritesSpinner' );
			}.bind(this),
			onSuccess: function() {
				$('AddToFavoritesBtn').setHTML('<p class="Last">The application has been added to your favorites.</p>');
			}
		});

		return false;
	}
});

var ApplicationPage = null;

window.addEvent('domready', function() {
	ApplicationPage = new CApplicationPage();
});