/*
+----------------------------------------------------------------+
|																							|
|	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;
var photo_voted = 0;
var totalpages = 0; 

// Post Ratings Fade In Text
function rade_fadein_text() {
	if(rate_fadein_opacity < 100) {
		rate_fadein_opacity += 10;
		if(is_opera)  {
			rate_fadein_opacity = 100;
		} else	 if(is_ie) {
			if(ratings_show_fading) {
				document.getElementById('post-ratings-' + post_id).style.filter = 'alpha(opacity=' + rate_fadein_opacity + ')';
			} else {
				rate_fadein_opacity = 100;
			}
		} else	 if(is_moz) {
			if(ratings_show_fading) {
				document.getElementById('post-ratings-' + post_id).style.MozOpacity = (rate_fadein_opacity/100);
			} else {
				rate_fadein_opacity = 100;
			}
		}
		setTimeout("rade_fadein_text()", 100); 
	} else {
		rate_fadein_opacity = 100;
		rate_unloading_text();
		is_being_rated = false;
	}
}


// When User Mouse Over Ratings
function current_rating(id, rating, rating_text) {
// alert('in the current_rating js function');
	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(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';
					document.images['rating_' + post_id + '_' + i].src = ratings_plugin_url + '/images/' + ratings_image + '/rating_off.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';
					document.images['rating_' + post_id + '_' + i].src = ratings_plugin_url + '/images/' + ratings_image + '/rating_off.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 = 'inline';
			document.getElementById('ratings-' + post_id + '-text').innerHTML = '<strong>Click stars to vote</strong>';
		}
	}
}


// 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';
	}
	totalpages = featuredcontentslider.settingcaches["slider1"].contentdivs.length;
	if(photo_voted < totalpages) {
		featuredcontentslider.jumpTo('slider1', String(photo_voted+1));
	}
}


// Process Post Ratings
function rate_post(num) {	
	if(!is_being_rated) {
		photo_voted = photo_voted + 1;
		is_being_rated = true;
		rate_loading_text();
		rate_process();	
		// document.getElementById("vote"+String(post_id)).innerHTML="You voted: <strong>"+String(post_rating)+"</strong>";
		//document.getElementById("vote"+String(post_id)).innerHTML='<div class="graph"><strong class="bar yourbar" style="width: ' + String(post_rating*10) + '%;">'+String(post_rating)+
		//	'</strong><strong class="bar averagebar" style="width: '+String(post_rating*10)+'%;">'+String(post_rating)+'</strong>Votes: <strong>'+String(post_rating)+'</strong></div>';
		//if (photo_voted >0) {
		// document.nextForm.submit_next.disabled=false;
		//}
	} else {		
		alert(ratings_text_wait);
	}
}
// 
 function getVoteResult(xml) {
     average_rate = $("average_rate",xml).text();
     total_vote = $("total_vote",xml).text();
	 document.getElementById("vote"+String(post_id)).innerHTML='<div class="graph"><strong class="bar yourbar" style="font-size: 12px;width: ' + String(post_rating*10) + '%;">'+String(post_rating)+
		'</strong><strong class="bar averagebar" style="font-size: 12px; width: '+String(average_rate*10)+'%;">'+String(average_rate)+'</strong>Total Votes: <strong>'+String(total_vote)+'</strong></div>';
   } 

// 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) {
			if(ratings_show_fading) {
				document.getElementById('post-ratings-' + post_id).style.filter = 'alpha(opacity=' + rate_fadeout_opacity + ')';
			} else {
				rate_fadein_opacity = 0;
			}
		} else if(is_moz) {
			if(ratings_show_fading) {
				document.getElementById('post-ratings-' + post_id).style.MozOpacity = (rate_fadeout_opacity/100);
			} else {
				rate_fadein_opacity = 0;
			}
		}
		setTimeout("rate_process()", 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 = rade_fadein_text;
		ratings.runAJAX();
		rate_fadein_opacity = 0;
		rate_fadeout_opacity = 100;
		$.post("http://www.hotglazz.com/hg/wp-content/themes/hotglazz-theme/vote_backend.php",{
		   post_id: post_id,
		   rate: post_rating,
		   action: "voteresult"
		 }, function(xml) {
			 getVoteResult(xml);
		}); 
	}
}
