
function ratepostVote(post_id, vote, ratepost_s, captcha_code) {
	jQuery.ajax({
		type: 'POST',
		data: {'post_id': post_id, 'vote': vote, 's': ratepost_s, 'captcha' : captcha_code},
		url: 'http://www.viewbookphotostory.com/index.php?ak_action=ratepost_vote',
		timeout: 2000,
		error: function() { 
			var sec_text = document.getElementById('sec_code_heading');
			sec_text.style.color = "red";
			sec_text.innerHTML = "Wrong code. Try again."
		},
		success: function(r) { 
			ratepostVoteAvg(post_id);
		}
	})
	return false;
}

function ratepostVoteAvg(post_id) {

	//Google Event Tracking
	pageTracker._trackEvent("Votes", "Click", "pid="+post_id);	

	//add count
	var vote_count = document.getElementById("vote_count");
	vote_count_n = vote_count.innerHTML;
	vote_count.innerHTML = parseInt(vote_count_n)+1;
	
	document.getElementById("captcha").style.display = "none";
	document.getElementById("sec_code_heading").style.display = "none";
    document.getElementById("captcha_code").style.display = "none";
	
	
	
	document.getElementById("vote_button").innerHTML = "Thanks!";

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

jQuery(document).ready(function() {
	jQuery('div.ratepost').not('.voted').find('span div').hover(
		function() {
			rating_text = {
				'1': 'Needs Work',
				'2': 'Below Average',
				'3': 'Average',
				'4': 'Very Good',
				'5': 'Excellent'};
			$current = jQuery(this);
			$ratepost = $current.parent().parent();
			new_rating = $current.attr('rel');
			$ratepost.find('span').removeClass().addClass('rating' + new_rating);
			$ratepost.find('div.status-info').text(rating_text[new_rating]);
		},
		function (){
			$ratepost = jQuery(this).parent().parent();
			$ratepost.find('span').removeClass().addClass('rating' + $ratepost.attr('rel'));
			$ratepost.find('div.status-info').text('');
		});
	jQuery('div.ratepost').not('.voted').find('span div').click(function() {
		$current = jQuery(this);
		$ratepost = $current.parent().parent();
		vote = $current.attr('rel');
		post_id = $ratepost.attr('id').split('-')[1];
		//$ratepost.find('span div').unbind();
		//$ratepost.attr('rel', vote);
		//$ratepost.find('span').removeClass().addClass('rating' + vote);
		//$ratepost.find('div.status-info').text('Thank you for voting!');
	
		var ratepost_s = document.getElementById('ratepost-s').innerHTML;
		var captcha_code = document.getElementById('captcha_code').value;

		if(captcha_code.length > 0) {
			ratepostVote(post_id, vote, ratepost_s, captcha_code);
		}
		
		
	});
});
