/*
+----------------------------------------------------------------+
|																							|
|	WordPress 2.5 Plugin: WP-PostRatings 1.31								|
|	Copyright (c) 2008 Lester "GaMerZ" Chan									|
|																							|
|	File Written By:																	|
|	- Lester "GaMerZ" Chan															|
|	- http://lesterchan.net															|
|																							|
|	File Information:																	|
|	- Post Ratings Javascript File													|
|	- wp-content/plugins/wp-postratings/postratings-js.php				|
|																							|
+----------------------------------------------------------------+
*/


// Variables
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;


// Post Ratings Fade In Text
function rade_fadein_text(vote) {
	if(rate_fadein_opacity < 100) {
		var note=document.getElementById('nbrePointFeedback').innerHTML;
		if(note==""){
			note=parseInt(vote);
		}
		else{
			note=parseInt(note)+parseInt(vote);
		}
		document.getElementById('nbrePointFeedback').innerHTML=note;
		$('noteFeedback').shake(
			{
			 distance :10
			}
		);
		$('notez').fade(
		{ 
			duration: 0.3,
			afterFinish:function(){
				document.getElementById('notez').innerHTML="Les r&eacute;sultats ";
			}
		});
		
		$('notez').appear({ delay:0.5,duration: 0.3 });

		$('post-ratings-' + post_id).appear({ delay:0.5,duration: 0.5 });
		rate_fadein_opacity = 100;
		setTimeout("rade_fadein_text()", 100); 
	} else {
		rate_fadein_opacity = 100;
		rate_unloading_text();
		is_being_rated = false;
	}
}



function current_rating_v2(id, rating, rating_text){
	if(!is_being_rated) {
		post_id = id;	
		post_rating = rating;
		document.images['rating_' + post_id + '_' + rating].src = eval("ratings_" + rating + "_mouseover_image.src");
	
		if(document.getElementById('voteInfo')) {
			afficher_bulle_vote(rating_text);
			//document.getElementById('ratings_' + post_id + '_text').style.display = 'inline';
			//document.getElementById('ratings_' + post_id + '_text').innerHTML = rating_text;
		}
	}
}

// When User Mouse Over Ratings
function current_rating(id, rating, rating_text) {
	if(!is_being_rated) {
		
		post_id = id;	
		post_rating = rating;
		if(ratings_custom && ratings_max == 2) {
			document.images['rating_' + post_id + '_' + rating].src = eval("ratings_" + rating + "_mouseover_image.src");
		} else {
		
			for(i = 1; i <= rating; i++) {
				if(ratings_custom) {
					document.images['rating_' + post_id + '_' + i].src = eval("ratings_" + i + "_mouseover_image.src");
				} else {
					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_v2(id, rating) {
	if(!is_being_rated) {
		post_id = id;	
		post_rating = rating;
		document.images['rating_' + post_id + '_' + rating].src =ratings_plugin_url + '/images/' + ratings_image + '/rating_' + rating + '_off.gif';
	
	//	document.images['rating_' + post_id + '_' + i].src = ratings_plugin_url + '/images/' + ratings_image + '/rating_' + rating + '_off.gif';
		
		if(document.getElementById('voteInfo')) {
			document.getElementById('voteInfo').style.display = 'none';
			document.getElementById('voteInfo').innerHTML = '';
		}
		
	}

}


function ratings_off(rating_score, insert_half) {
	if(!is_being_rated) {
		for(i = 1; i <= ratings_max; i++) {
			if(i <= rating_score) {
				if(ratings_custom) {
					document.images['rating_' + post_id + '_' + i].src = ratings_plugin_url + '/images/' + ratings_image + '/rating_' + i + '_on.gif';
				} else {
					document.images['rating_' + post_id + '_' + i].src = ratings_plugin_url + '/images/' + ratings_image + '/rating_on.gif';
				}
			} else if(i == insert_half) {
				if(ratings_custom) {
					document.images['rating_' + post_id + '_' + i].src = ratings_plugin_url + '/images/' + ratings_image + '/rating_' + i + '_half.gif';
				} else {
					document.images['rating_' + post_id + '_' + i].src = ratings_plugin_url + '/images/' + ratings_image + '/rating_half.gif';
				}
			} else {
				if(ratings_custom) {
					document.images['rating_' + post_id + '_' + i].src = ratings_plugin_url + '/images/' + ratings_image + '/rating_' + i + '_off.gif';
				} else {
					document.images['rating_' + post_id + '_' + i].src = ratings_plugin_url + '/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 = '';
		}
	}
}

// Post Ratings Loading Text
function rate_loading_text() {
	if(ratings_show_loading) {
		document.getElementById('post-ratings-' + post_id + '-loading').style.display = 'block';
	}
}


// Post Ratings Finish Loading Text
function rate_unloading_text() {
	if(ratings_show_loading) {
		document.getElementById('post-ratings-' + post_id + '-loading').style.display = 'none';
	}
}


// Process Post Ratings
function rate_post(vote) {	
	if(!is_being_rated) {
		is_being_rated = true;
		rate_loading_text();
		rate_process(vote);		
	} else {		
		alert(ratings_text_wait);
	}
}


// Process Post Ratings
function rate_process(vote) {
	if(rate_fadeout_opacity > 0) {
		document.getElementById('voteInfo').style.display = 'none';
		document.getElementById('voteInfo').innerHTML = '';
		//on fait rentrer le div post-rating-id
		$('post-ratings-' + post_id).fade({delay:0.03, duration: 0.4 });
		// on change le texte chargement
		$('notez').fade(
		{ 
			duration: 0.2,
			afterFinish:function(){
				document.getElementById('notez').innerHTML='<div class="loading"></div>';
			}
		});
		
		$('notez').appear({ delay:0.5,duration: 0.2 });
		rate_fadeout_opacity =0;
		setTimeout(function(){rate_process(vote)}, 100); 
	} else {
		rate_fadeout_opacity = 0;
		ratings.reset();
		ratings.setVar("pid", post_id);
		ratings.setVar("rate", post_rating);
		ratings.method = 'GET';
		ratings.element = 'post-ratings-' + post_id;
		ratings.onCompletion = function(){rade_fadein_text(vote)};
		ratings.runAJAX();
		rate_fadein_opacity = 0;
		rate_fadeout_opacity = 100;
	}
}