$(document).ready(function() {
	$('.user_group_tip').hover(
		function() {$(this).parent().find('.user_group_tooltip').show();},
		function() {$(this).parent().find('.user_group_tooltip').hide();}
	);

	$('.post_hidden_message a').click(function () {
		var postFrame = $(this).parent().parent();
		postFrame.removeClass('post_hidden');

		// Hack to force IE to refresh the post contents
		postFrame.find('.post_userinfo').width(postFrame.find('.post_userinfo').width());
		return false;
	});

	$('.vote_up_button,.vote_down_button').click(function() {
		var postid = $(this).attr('href').match(/postid=(\d+)/)[1];
		var vote = $(this).attr('href').match(/vote=(-?1)/)[1];
		$.ajax({
			type: 'POST',
			url: $(this).attr('href'),
			dataType: 'json',
			data: {'postid': postid, 'vote': vote},
			success: function(data) {
				if (data.error != null) {
					console.log(data.error);
				} else if (data.postid != null) {
					if (data.should_close) {
						$('#post' + data.postid).addClass('post_hidden');
					} else {
						$('#post' + data.postid).removeClass('post_hidden');
					}

					var rating_html = 'Rating (';

					if (data.rating >= 0) {
						rating_html += '<span class="rating_positive">+' + data.rating + '</span>';
					} else {
						rating_html += '<span class="rating_negative">' + data.rating + '</span>';
					}

					rating_html += ')';

					$('#post' + data.postid + ' .post_rating').html(rating_html);
					$('#post' + data.postid + ' .vote_up_button, #post' + data.postid + ' .vote_down_button').hide();
				}
			}
		});

		return false;
	});
});
