
// Variables
var site_url = "http://webcommunityforum.com";
var ratings_ajax_url = "/wp-content/plugins/postratings/postratings.php";
var ratings_text_wait = "Please rate only 1 post at a time.";
var ratings_image = "stars";
var ratings_max = "5";
var ratings_mouseover_image = new Image();
ratings_mouseover_image.src = site_url + "/wp-content/plugins/postratings/images/" + ratings_image + "/rating_over.gif";
var ratings = new sack(ratings_ajax_url);
var post_id = 0;
var post_rating = 0;
var is_ie = (document.all && document.getElementById);
var is_moz = (!document.all && document.getElementById);
var is_opera = (navigator.userAgent.indexOf("Opera") > -1);
var is_being_rated = false;

// after a user's post rating has been processed
function update_average() {
  // reset to non-rating status
  is_being_rated = false;

  // update the average vote
  ratings.reset();
  ratings.setVar("pid", post_id);
  ratings.setVar("update", "average");
  ratings.method = 'GET';
  ratings.element = 'post-avg-' + post_id;
  ratings.runAJAX();
}

// When User Mouse Over Ratings
function current_rating(id, rating, rating_text) {
	if(!is_being_rated) {
		post_id = id;
		post_rating = rating;
		for(i = 1; i <= rating; i++) {
			document.images['rating_' + post_id + '_' + i].src = eval("ratings_mouseover_image.src");
		}
		if(document.getElementById('ratings_' + post_id + '_text')) {
			document.getElementById('ratings_' + post_id + '_text').style.display = 'inline';
			document.getElementById('ratings_' + post_id + '_text').innerHTML = rating_text;
		}
	}
}

// When User Mouse Out Ratings
function ratings_off(rating_score, insert_half) {
	if(!is_being_rated) {
		for(i = 1; i <= ratings_max; i++) {
			if(i <= rating_score) {
				document.images['rating_' + post_id + '_' + i].src = site_url + '/wp-content/plugins/postratings/images/' + ratings_image + '/rating_on.gif';
			} else if(i == insert_half) {
				document.images['rating_' + post_id + '_' + i].src = site_url + '/wp-content/plugins/postratings/images/' + ratings_image + '/rating_half.gif';
			} else {
				document.images['rating_' + post_id + '_' + i].src = site_url + '/wp-content/plugins/postratings/images/' + ratings_image + '/rating_off.gif';
			}
		}
		if(document.getElementById('ratings_' + post_id + '_text')) {
			document.getElementById('ratings_' + post_id + '_text').style.display = 'none';
			document.getElementById('ratings_' + post_id + '_text').innerHTML = '';
		}
	}
}


// Process Post Ratings
function rate_post() {	
	if(!is_being_rated) {
		is_being_rated = true;
		rate_process();		
	} else {		
		alert(ratings_text_wait);
	}
}

// Process Post Ratings
function rate_process() {
  ratings.reset();
  ratings.setVar("pid", post_id);
  ratings.setVar("rate", post_rating);
  ratings.method = 'GET';
  ratings.element = 'post-vote-' + post_id;
  ratings.onCompletion = update_average;
  ratings.runAJAX();
}