/*
+----------------------------------------------------------------+
|																							|
|	WordPress 2.0 Plugin: WP-PostRatings 1.05								|
|	Copyright (c) 2006 Lester "GaMerZ" Chan									|
|																							|
|	File Written By:																	|
|	- Lester "GaMerZ" Chan															|
|	- http://www.lesterchan.net													|
|																							|
|	File Information:																	|
|	- Post Ratings Javascript File													|
|	- wp-content/plugins/postratings/postratings-js.js						|
|																							|
+----------------------------------------------------------------+
*/


// Variables
var ratings = new sack(ratings_ajax_url);
var ratings_delete = new sack(ratings_delete_ajax_url);
var comments = new sack(comments_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 rated_score = 0;
//var blnPosting = false;
//var ajax_timer;
var reset_timer;

// 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) {
			document.getElementById('post-ratings-' + post_id).filters.alpha.opacity = rate_fadein_opacity;
		} else	 if(is_moz) {
			document.getElementById('post-ratings-' + post_id).style.MozOpacity = (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) {
  clearResetTimer();
	//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 = post_rating;
		}
	//}
}


// Change displayed ratings dynamically
function show_current_rating(rating, insert_half) {
		/*for(i = 1; i <= rating; i++) {
			document.images['show_ratings_' + i].src = eval("ratings_mouseover_image.src");
		}*/

		for(i = 1; i <= 5; i++) {
			if(i <= rating) {
				document.images['show_ratings_' + i].src = site_url + '/wp-content/plugins/postratings/images/' + ratings_image + '/rating_on.gif';
				document.images['show_ratings_' + i].title = 'Rating: ' + rating + ' out of 5';
				document.images['show_ratings_' + i].alt = 'Rating: ' + rating + ' out of 5';
			} else if(i == insert_half) {
				document.images['show_ratings_' + i].src = site_url + '/wp-content/plugins/postratings/images/' + ratings_image + '/rating_half.gif';
				document.images['show_ratings_' + i].title = 'Rating: ' + rating + ' out of 5';
				document.images['show_ratings_' + i].alt = 'Rating: ' + rating + ' out of 5';
			} else {
				document.images['show_ratings_' + i].src = site_url + '/wp-content/plugins/postratings/images/' + ratings_image + '/rating_off.gif';
				document.images['show_ratings_' + i].title = 'Rating: ' + rating + ' out of 5';
				document.images['show_ratings_' + i].alt = 'Rating: ' + rating + ' out of 5';
			}
		}
}

// 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 = '';
		}
		reset_timer = setTimeout("reset_stars()", 1);
	//}
}


// Post Ratings Loading Text
function rate_loading_text() {
	document.getElementById('post-ratings-' + post_id + '-loading').style.display = 'block';
}


// Post Ratings Finish Loading Text
function rate_unloading_text() {
	document.getElementById('post-ratings-' + post_id + '-loading').style.display = 'none';
}


// Process Post Ratings
function rate_post() {
  clearResetTimer();
	//if(!is_being_rated) {
		is_being_rated = true;
		rated_score = post_rating;
		//rate_loading_text();
		//rate_process();
	//} else {		
		//alert(ratings_text_wait);
	//}
}

// Check form data
function check_form(comment, stylesheet_directory, oldcomment) {
	var agreetos = document.getElementById('agreetos');
	if (agreetos) {
		if (agreetos.checked) {
			if (post_rating > 0) {
    			 if ($('comment').value.length > 500){
    			 
              				//Elements appearance
              				if ($('nocomment')) { Element.hide('nocomment'); }
              				//if ($('nocomment')) { var myFx = new Fx.Style('nocomment', 'opacity', {duration:0}).start(1,0); }
              				$('submit').disabled = true;
              				$('comment').disabled = true;
              				Element.show('loading');
              				//var myFx = new Fx.Style('loading', 'opacity', {duration:0}).start(0,1);
              				//rate_process(comment, stylesheet_directory, oldcomment);
              				comment_process(comment, stylesheet_directory, oldcomment);
				    } else { alert("Your review is shorter than the minimum of 500 characters!"); }
			} else {
				alert("Please click on the stars to give the business a rating.");
				//fail(__('Sorry, '));
			}
		} else {
			alert("You must agree with Yebber.com's Terms of Use by checking the box to proceed with the registration.");
		}
	} else {
		if (post_rating > 0) {
		  if ( $('comment').value.length > 500){
                			//Elements appearance
                			if ($('nocomment')) { Element.hide('nocomment'); }
                			//if ($('nocomment')) { var myFx = new Fx.Style('nocomment', 'opacity', {duration:0}).start(1,0); }
                			$('submit').disabled = true;
                			$('comment').disabled = true;
                			Element.show('loading');
                			//var myFx = new Fx.Style('loading', 'opacity', {duration:0}).start(0,1);
                			//rate_process(comment, stylesheet_directory, oldcomment);
                			comment_process(comment, stylesheet_directory, oldcomment);  
                } else { alert("Your review is shorter than the minimum of 500 characters!"); }
    } else {
			alert("Please click on the stars to give the business a rating.");
			
			//fail(__('Sorry, '));
		}
	}
	return false;
}

// Process Post Ratings
function rate_process(comment, stylesheet_directory, oldcomment) {
	ratings.setVar("pid", post_id);
	ratings.setVar("rate", post_rating);
	ratings.method = 'GET';
	//ratings.onCompletion = function(){comment_process(comment, stylesheet_directory, oldcomment)};
	ratings.runAJAX();
  return false;
}

// Delete post rating
function rate_delete(post_id) {
	ratings_delete.setVar("postid", post_id);
	//ratings_delete.setVar("userid", post_rating);
	ratings_delete.method = 'GET';
	//ratings_delete.onCompletion = alert('asd');
	//ratings_delete.element = 'post-ratings-' + post_id;
	ratings_delete.runAJAX();
}

function comment_process(comment, stylesheet_directory, oldcomment) {
  if ($('jal_edit_this').value > 0) {
    //new Ajax.Updater({success: 'commentlist'}, stylesheet_directory + '/comments-ajax-edit.php', {asynchronous: true, evalScripts: true, insertion: Insertion.Bottom, onComplete: function(request){rade_fadein_text();complete(request, 'edit', comment, stylesheet_directory, oldcomment)}, onFailure: function(request){failure(request, 'edit')}, onLoading: function(request){loading()}, parameters: comment});
    new Ajax.Updater({success: 'commentlist'}, stylesheet_directory + '/comments-ajax-edit.php', {asynchronous: true, evalScripts: true, insertion: Insertion.Bottom, onComplete: function(request){rade_fadein_text();complete(request, 'edit', comment, stylesheet_directory, oldcomment)}, onFailure: function(request){failure(request, 'edit')}, onLoading: function(request){loading()}, parameters: comment + '&post_rating=' + post_rating});
  } else {
    //new Ajax.Updater({success: 'commentlist'}, stylesheet_directory + '/comments-ajax.php', {asynchronous: true, evalScripts: true, insertion: Insertion.Bottom, onComplete: function(request){rade_fadein_text();complete(request, 'write', comment, stylesheet_directory, oldcomment)}, onFailure: function(request){failure(request, 'write')}, onLoading: function(request){loading()}, parameters: comment});
    new Ajax.Updater({success: 'commentlist'}, stylesheet_directory + '/comments-ajax.php', {asynchronous: true, evalScripts: true, insertion: Insertion.Bottom, onComplete: function(request){rade_fadein_text();complete(request, 'write', comment, stylesheet_directory, oldcomment)}, onFailure: function(request){failure(request, 'write')}, onLoading: function(request){loading()}, parameters: comment + '&post_rating=' + post_rating});
  }
}

function commentAdded2() {
  Element.hide('loading');
  //var myFx = new Fx.Style('loading', 'opacity', {duration:0}).start(1,0);
	Element.show('commentform');
	//var myFx = new Fx.Style('commentform', 'opacity', {duration:0}).start(0,1);
	document.body.style.cursor = 'auto'
	
	if ($('errors')) { Element.remove('errors'); }
	new Insertion.After(document.getElementById('commentlist'),'test');
	alert(document.getElementById('commentlist').lastChild);
	//new Effect.Appear($('commentlist').lastChild);
	$('comment').value = '';
	$('comment').disabled = true;
	$('submit').disabled = true;
	if ($('nocomment')) { Element.remove('nocomment'); }
	if ($('hidelist')) { Element.remove('hidelist'); }
}

function clearResetTimer() {
	if (reset_timer) {
		clearTimeout(reset_timer);
		reset_timer = null;
	}
}

function reset_stars() {
	clearResetTimer();

	current_rating(post_id, rated_score, "")
}
