var Comments = {
	objectId: null,
	orderASC: true,
	
	del: function(commentId, isOwner) {
		if(!confirm('Вы действительно хотите удалить этот комментарий?'))
		{
			return false;
		}
		
		$.ajax({
			url: Config.baseURL+'comments/json_delete'+(isOwner ? '_owner' : ''),
			data: 'comment='+commentId,
			type: 'post',
			dataType: 'json',
			error: function() { AJAXError(); },
			success: function(response) {
				if (response.success) {
					Comments.afterDelete(commentId);
				} else {
					AJAXError();
				}
			}
		});
	},
	
	add: function() {
		$.ajax({
			url: Config.baseURL+'comments/json_add',
			data: $('form#new_comment').serialize(),
			type: 'post',
			dataType: 'json',
			beforeSend: function() { Comments.addStart(); },
			complete: function() { Comments.addEnd(); },
			error: function() { AJAXError(); },
			success: function(response) {
				if(response.success) {
					Comments.reload();
					
					$('#new_comment_text').val('');
				} else {
					AJAXError();
				}
			}
		});
	},
	
	addStart: function()
	{
		$('form#new_comment')
			.unbind('submit')
			.submit(function() { return false; });
	
		$('#new_comment_submit_button').addClass('disabled');
		$('#new_comment_loader').addClass('visible');
	},
	
	addEnd: function()
	{
		$('#new_comment_submit_button').removeClass('disabled');
		$('#new_comment_loader').removeClass('visible');
		$('#new_comment_text').val('');
		$('#new_comment_text').blur();
		
		$('form#new_comment')
			.unbind('submit')
			.submit(function() {
				return Comments.formSubmit();
			});
	},
	
	afterDelete: function(commentId)
	{
		$('#comment_'+commentId).replaceWith(HTMLMessage('Комментарий удален'));
	},
	
	goPage: function(page) {
		this.reload(page);
		document.location.hash = '#comments';
	},
		
	reload: function(page) {
		$('#comments-list').load(Config.baseURL+'comments/comments?object_id='+this.objectId+(page === undefined ? '' : '&comments_page='+page)+(!this.orderASC ? '&order=desc' : '')+'&is_desk_owner='+(this.isDeskOwner ? this.isDeskOwner : 'false'));
	},
	
	vote: function(commentId) {
		if(!Config.userVerified)
		{
			voteUnverifiedError();
			return false;
		}
		
		$.ajax({
			url: Config.baseURL+'comments/json_vote',
			data: {comment_id: commentId},
			type: 'post',
			dataType: 'json',
			error: function() { AJAXError(); },
			success: function(response) {
				if (response.success) {
					$('#comment_vote_'+commentId).addClass('voted');
					
					$('#comment_vote_'+commentId)
						.click(function() {return false;})
						.attr('onclick', '');
					
					$('#comment_rating_value_'+commentId).text(response.rating);
				} else {
					AJAXError();
				}
			}
		});
	},
	
	formSubmit: function()
	{
		if ($('#new_comment_text').val() == '') {
			$('#new_comment_error').show();
			$('#new_comment_text').focus();
			return false;
		} else {
			$('#new_comment_error').hide();
		}
		
		Comments.add();
		
		return false;
	},
	
	initForm: function()
	{
		$('#new_comment_text')
			.focus(function() {
				if($(this).val() == '' || $(this).val() == 'Нажмите, чтобы написать комментарий...')
				{
					$(this).animate({height: '60px'}, 'fast');
					$('#new_comment_submit_button').show('slow');
				}
			})
			.blur(function() {
				if($(this).val() == '' || $(this).val() == 'Нажмите, чтобы написать комментарий...')
				{
					$('#new_comment_submit_button').hide('slow');
					$(this).animate({height: '20px'}, 'fast');
				}
			});
		
		$('form#new_comment').submit(function() {
			return Comments.formSubmit();
		});
	},
	
	subscribe: function(objectId)
	{
		$.ajax({
			url: Config.baseURL+'comments/json_subscribe',
			data: { object_id: objectId },
			type: 'post',
			dataType: 'json',
			error: function() { AJAXError(); },
			success: function(response) {
				if(!response.success)
				{
					AJAXError();
					return;
				}
				
				$('.comments-subscribe').toggle();
			}
		})
	}
};

$(document).ready(function() {
	Comments.initForm();
});
