
function raterecipeVote(recipe_id, vote) {
	jQuery.ajax({
		type: 'POST',
		data: {'recipe_id': recipe_id, 'vote': vote},
		url: 'http://www.soupcookbook.com/index.php?ak_action=raterecipe_vote',
		timeout: 2000,
		error: function() {},
		success: function(r) { 
			raterecipeVoteAvg(recipe_id);
		}
	})
	return false;
}

function raterecipeVoteAvg(recipe_id) {
	jQuery.ajax({
		type: 'POST',
		data: {'recipe_id': recipe_id},
		url: 'http://www.soupcookbook.com/index.php?ak_action=raterecipe_vote_avg',
		timeout: 2000,
		error: function() {},
		success: function(r) { 
			jQuery('#raterecipe-'+recipe_id).find('div.rating-info').text('Rating ' + r + ' out of 5');
		}
	})
	return false;
}

jQuery(document).ready(function() {
	jQuery('div.raterecipe').not('.voted').find('span div').hover(
		function() {
			rating_text = {
				'1': 'Not so good',
				'2': 'Just OK',
				'3': 'Average',
				'4': 'Yummy',
				'5': 'Delicious!'};
			$current = jQuery(this);
			$raterecipe = $current.parent().parent();
			new_rating = $current.attr('rel');
			$raterecipe.find('span').removeClass().addClass('rating' + new_rating);
			$raterecipe.find('div.status-info').text(rating_text[new_rating]);
		},
		function (){
			$raterecipe = jQuery(this).parent().parent();
			$raterecipe.find('span').removeClass().addClass('rating' + $raterecipe.attr('rel'));
			$raterecipe.find('div.status-info').text('');
		});
	jQuery('div.raterecipe').not('.voted').find('span div').click(function() {
		$current = jQuery(this);
		$raterecipe = $current.parent().parent();
		vote = $current.attr('rel');
		recipe_id = $raterecipe.attr('id').split('-')[1];
		$raterecipe.find('span div').unbind();
		$raterecipe.attr('rel', vote);
		$raterecipe.find('span').removeClass().addClass('rating' + vote);
		$raterecipe.find('div.status-info').text('Thank you for voting!');
		raterecipeVote(recipe_id, vote);
	});
});
