var ratings_ajax_url = 'process/ratings.php';
var ratings = new sack(ratings_ajax_url);
var post_id = 0;
var post_rating = 0;
var rate_fadein_opacity = 0;
var rate_fadeout_opacity = 100;
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;
var column = "";

function rate_plan( a_planid, a_rating, a_column ) {	
	ratings_ajax_url = 'process/planratings.php';
	ratings = new sack(ratings_ajax_url);
	post_id = a_planid;
	post_rating= a_rating;
	column = a_column;
	if( !is_being_rated ) {
		is_being_rated = true;	
		rate_loading_text1();
		rate_plan_process();		
	} else {		
		alert(ratings_text_wait);
	}
}
// Process Post Ratings
function rate_plan_process() {
	if(rate_fadeout_opacity > 0) {
		rate_fadeout_opacity -= 10;
		if(is_opera) {
			rate_fadein_opacity = 0;
		} else if(is_ie) {
			
		} else if(is_moz) {
			if( column == "rating_up" ) {
				document.getElementById('post-ratingup-' + post_id).style.MozOpacity = (rate_fadeout_opacity/100);
			} else {
				document.getElementById('post-ratingdown-' + post_id).style.MozOpacity = (rate_fadeout_opacity/100);
			}
		}
		setTimeout("rate_plan_process()", 100); 
	} else {
		rate_fadeout_opacity = 0;
		ratings.setVar( "plan_id", post_id );
		ratings.setVar( "rating", post_rating );
		ratings.setVar( "column", column );
		ratings.method = 'GET';
		if( column == "rating_up" ) {
			ratings.element = 'post-ratingup-' + post_id;
		} else {
			ratings.element = 'post-ratingdown-' + post_id;
		}
		ratings.onCompletion = rateplan_fadein_text;
		ratings.runAJAX();
		rateplan_fadein_opacity = 0;
		rate_fadeout_opacity = 100;
	}
}

// Post Ratings Fade In Text
function rateplan_fadein_text() {
	var l_element;
	var l_response;
	if( column == "rating_up" ) {
	    l_element = document.getElementById('post-ratingup-' + post_id);
	    var l_suffix = " points";

		l_response = "+" + ratings.response + l_suffix;
		l_element.innerHTML = l_response;
	} else {
	    l_element = document.getElementById('post-ratingdown-' + post_id);	
	    var l_suffix = " points";
	    l_response = "-" + ratings.response + l_suffix;
	    l_element.innerHTML = l_response;
	}
	if(rate_fadein_opacity < 100) {
		rate_fadein_opacity += 10;
		if(is_opera)  {
			rate_fadein_opacity = 100;
		} else	 if(is_ie) {
			
		} else	 if(is_moz) {
			if( column == "rating_up" ) {
			    document.getElementById('post-ratingup-' + post_id).style.MozOpacity = (rate_fadein_opacity/100);
			} else {
			    document.getElementById('post-ratingdown-' + post_id).style.MozOpacity = (rate_fadein_opacity/100);
			}
		}
		setTimeout("rateplan_fadein_text()", 100); 
	} else {
		if(is_moz || is_ie ) {
		    rate_fadein_opacity = 0;
		} else {
		    rate_fadein_opacity = 100;
		}
		rate_unloading_text1();
		is_being_rated = false;
	}
}

function rate_post( a_postid, a_rating ) {	
	post_id = a_postid;
	post_rating= a_rating;
	if(!is_being_rated) {
		is_being_rated = true;
		rate_loading_text();		
		rate_process();		
	} else {		
		alert(ratings_text_wait);
	}
}

// Process Post Ratings
function rate_process() {
	if(rate_fadeout_opacity > 0) {
		rate_fadeout_opacity -= 10;
		if(is_opera) {
			rate_fadein_opacity = 0;
		} else if(is_ie) {
			
		} else if(is_moz) {
			document.getElementById('post-ratings-' + post_id).style.MozOpacity = (rate_fadeout_opacity/100);
		}
		setTimeout("rate_process()", 100); 
	} else {
		rate_fadeout_opacity = 0;
		ratings.setVar("pid", post_id);
		ratings.setVar("rate", post_rating);
		ratings.method = 'GET';
		ratings.element = 'post-ratings-' + post_id;
		ratings.onCompletion = rate_fadein_text;
		ratings.runAJAX();
		rate_fadein_opacity = 0;
		rate_fadeout_opacity = 100;
	}
}

// Post Ratings Fade In Text
function rate_fadein_text() {
	l_element = document.getElementById('post-ratings-' + post_id);	
	l_response = "+" + ratings.response + " Good fun!";
	document.getElementById('post-ratings-' + post_id).innerHTML = l_response;
	if(rate_fadein_opacity < 100) {
		rate_fadein_opacity += 10;
		if(is_opera)  {
			rate_fadein_opacity = 100;
		} else	 if(is_ie) {
			
		} else	 if(is_moz) {
			document.getElementById('post-ratings-' + post_id).style.MozOpacity = (rate_fadein_opacity/100);
		}
		setTimeout("rate_fadein_text()", 100); 
	} else {
		rate_fadein_opacity = 100;
		rate_unloading_text();
		is_being_rated = false;
	}
}

// Post Ratings Loading Text
function rate_loading_text() {
	document.getElementById('post-ratings-' + post_id + '-loading').style.display = 'inline';	
}


// Post Ratings Finish Loading Text
function rate_unloading_text() {
	document.getElementById('post-ratings-' + post_id + '-loading').style.display = 'none';
}

function rate_loading_text1() {
	document.getElementById('post-ratings-' + post_id + '-loading').style.display = 'inline';	
}
function rate_unloading_text1() {
	document.getElementById('post-ratings-' + post_id + '-loading').style.display = 'none';
}


