$(function()
{
	function post_vote_click()
	{
		var post_id = /([0-9]+)$/.exec(this.id)[1];
		var post_mark = $(this).is('.plus') ? 1 : -1;
		var elem = $(this);
		$.post('/news/submit_vote', {'id': post_id, 'target': 'post', 'mark': post_mark}, function(response)
			{
				if (response.error != '') {
					$.jGrowl(response.error, {theme: 'error_message', 'header' : 'Ошибка', sticky: true});
				}
				else {
					$('#post'+post_id+' .voting form input:submit').attr('disabled', true);
					elem.parent().children('span').each(function()
					{
						$(this).addClass('selected').click(function() {});
					}).parent().find('span.' + (post_mark == 1 ? 'mins' : 'plus')).removeClass('active');
					$('#post_rating_'+post_id.toString()).text((response.rating > 0 ? "+" : "-")+response.rating.toString());
				}
			}, 'json');
	}
	$('.vbc_ul span.voting:not(.selected)').click(post_vote_click);
	
	var cur_form = null;
	$('.vb_left .voting form').ajaxForm({
		dataType : 'json',
		beforeSubmit : function(formData, form) {
			cur_form = form;
		},
		success: function(response) {
			if (response.error != '') {
					$.jGrowl(response.error, {theme: 'error_message', 'header' : 'Ошибка', sticky: true});
			}
			else {
				$(cur_form).find('input:submit').attr('disabled', true);
				var post_id = $(cur_form).children('input:hidden[name=id]').val();
				buttons = $(cur_form).parents('.vb_left').siblings('.vb_ccont').find('.vbc_ul > li:eq(0)');
				buttons.children('span').each(function()
				{
					$(this).addClass('selected').click(function() {});
				}).parent().find('span.mins').removeClass('active');
				$('#post_rating_'+post_id.toString()).text((response.rating > 0 ? "+" : "-")+response.rating.toString());
			}
		}
	});
});

