var selected_rating;
var selected_listingID;
var selected_module;
var modKey;

function sendReview(rating,listID,mod,review_title,review_msg,modKey){


	//alert ('sending review ' + review_msg );
	$.ajax({
			url  : 'index.php?ado=reviewLogRatingAndComment&ajax=true&layout=empty',
			type : 'POST',
			data : { moduleType : mod, listingID : listID, rating : rating, title : review_title, message : review_msg },
			success : function(response){

				var responseObj = JSON.parse(response);
				
				rating = parseInt((responseObj.average / .5) - 1);

				starkey = "star_" + modKey + "_" + listID;

				$('input[name='+starkey+']').rating('select', rating);

				myRating = responseObj.yourRating;
				if ($('#my_rating_' + listID).length > 0) {
					var myRating = 'your rating: '+ myRating +' star'+ (myRating == '1.0' ? '' : 's');
					$('#my_rating_' + listID).text(myRating);
				}
				
				if ($('#review_count_' + listID).length > 0){
					var reviewCount = '(' + responseObj.reviewCount + ' review'+ (responseObj.reviewCount == '1' ? '' : 's') +')';
					$('#review_count_' + listID).text(reviewCount);
				}

				if (rating == 0){
					$('#new_comment_'+listID).slideUp();
				}else{

					ratekey = "rate_" + modKey + "_add_comment_" + listID;

					
					if($('#'+ratekey).length > 0){
						$('#'+ratekey).slideUp();
					}

					
				}

				
			}
		});

}


$(document).ready(function() {
	
	$('.rating-cancel').remove(); // no cancel button
	

	$('.submitComment').click(function(){

		if (selected_rating > 0){
			r_title = escape($('#rate_'+modKey+'_add_comment_'+selected_listingID  + ' input[name=title]' ).val());
			r_msg = escape($('#rate_'+modKey+'_add_comment_'+selected_listingID  + ' textarea[name=review]' ).val());
		}else {
			r_title = escape($('#new_comment_'+selected_listingID  + ' input[name=title]' ).val());
			r_msg = escape($('#new_comment_'+selected_listingID  + ' textarea[name=review]' ).val());
		}

		//alert (' submit comment ');
		//console.debug("msg = " + r_msg);
		sendReview(selected_rating,selected_listingID,selected_module,r_title,r_msg,modKey);
		
		selected_rating = "";
		selected_listingID = "";
		selected_module = "";

	});

	$('.submitVote').click(function(){

		if (selected_rating > 0){
			sendReview(selected_rating,selected_listingID,selected_module,'','',modKey);
			$('#rate_'+modKey+'_add_comment_'+selected_listingID).slideUp();
		}else {
			$('#new_comment_'+selected_listingID).slideUp();
		}
		
		
		selected_rating = "";
		selected_listingID = "";
		selected_module = "";

	});

	$('.ask_for_comment_only').click(function(){

		var id = $(this).attr("id").split(":");
		selected_listingID = id[0];

		modKey = id[1];
		selected_module = modKey.split("-")[0];
		selected_rating = 0;

		if($('#new_comment_'+selected_listingID).length > 0){

			$('.ask_for_comment').each(function(){

				if ( $(this).css("display") != "none" ){
					//console.debug("slideup");
					$(this).slideUp();
				}


			});

			$('#new_comment_'+selected_listingID).slideDown();
			//console.debug("found");
		}else {
			//console.debug("not found");
		}

	});

	$('.star-rating a').click(function(){
		var rating = $(this).text();


		var dis = $(this).parent().parent().parent().attr("alt");
		if (dis == "locked") return;
		
		var id = $(this).parent().parent().parent().attr("id").split(":");
		modKey = id[0];
		mod = modKey.split("-")[0];

		var listID = id[1];

		selected_rating = rating;
		selected_listingID = listID;
		selected_module = mod;

		var ratekey = '#rate_'+modKey+'_add_comment_'+listID;
		
		if( $( ratekey ).length > 0){

			$('.ask_for_comment').each(function(){

				if ( $(this).css("display") != "none" ){
					//console.debug("slideup");
					$(this).slideUp();
				}
				

			});

			$('#rate_'+modKey+'_add_comment_'+listID).slideDown();
			//console.debug("found");
		}else {

			if (modKey.length > 0){
				
				sendReview(rating,listID,mod,"","",modKey);
			}

			
			//console.debug("not found");
		}


		

/*
		$.ajax({
			url  : 'index.php?ado=reviewLogRating&ajax=true&layout=empty',
			type : 'POST',
			data : { moduleType : mod, listingID : listID, rating : rating },
			success : function(response){
				//var myRating = 'your rating: '+ rating +' star'+ (rating == '1.0' ? '' : 's');
				//$('.my-rating').text(myRating);

				var responseObj = JSON.parse(response);

				//var reviewCount = '(' + responseObj.reviewCount + ' review'+ (responseObj.reviewCount == '1' ? '' : 's') +')';
				//$('.review-count').text(reviewCount);

				$('input[name=star]').rating('select', parseInt((responseObj.average / .5) - 1));
			}
		});
*/

	});
});