
showingProduct = false;
rated = false;
commentSubmitted = false;

function showTips(way){
	
	if (way == true) {
		$('#recipeTips p').show();
		src = ABSOLUTE_PATH+'/images/btnMinus.png';
	}else{
		$('#recipeTips p').hide();
		src = ABSOLUTE_PATH+'/images/btnPlus.png';
	}
	
	$('#recipeTips .btnPlusMinus').attr('src', src);
	$('#recipeTips a').attr('href', 'javascript:showTips('+((way==true)?'false':'true')+');void(0);');
}

function showNutrition(way){
	
	if (way == true) {
		$('#nutritionalInfo table').show();
		src = ABSOLUTE_PATH+'images/btnMinus.png';
	}else{
		$('#nutritionalInfo table').hide();
		src = ABSOLUTE_PATH+'images/btnPlus.png';
	}
	
	$('#nutritionalInfo .btnPlusMinus').attr('src', src);
	$('#nutritionalInfo a').attr('href', 'javascript:showNutrition('+((way==true)?'false':'true')+');void(0);');
	
}

function rateRecipe(rating){
	
	if(userid == undefined) return;
	if(rated != false) return;
	
	api = API_URL+"rate_recipe_api.php";
	rated = rating;
	
	recipeRating(rated);
	startAnimation();
	
	$.post(
			api, 
			{userID:userid, recipeID:recipeID, rating:rated},
			rateCallback
			);
			
	$.post(
			ABSOLUTE_PATH+'recipe-club/saverating.php', 
			{recipeID:recipeID, rating:rated},
			saveRatingCallback
			);
	
}

function startAnimation(){
	
	int = setInterval("animateRating()", 100);
	animCount = 0;
	
}

function animateRating(){
	if(animCount == 11) clearInterval(int);
	
	src = (animCount % 2 == 0) ? "iconStarGrey.png" : "iconStar.png";
	for(i=0; i<5; i++){
		if(i>(rated-1)){
			$("#ratingStar"+i).attr("src", ABSOLUTE_PATH+"images/iconStarGrey.png");
		}else{
			$("#ratingStar"+i).attr("src", ABSOLUTE_PATH+"images/"+src);
		}
	}
	animCount++;
	
}

function rateRecipeRoll(rating, on){
	
	if(rated != false) return;
	
	for(i=0; i<5; i++){
		grey = $("#ratingStar"+i).hasClass("greyStar");
		if((on == false || i>rating) && grey==true){
			$("#ratingStar"+i).attr("src", ABSOLUTE_PATH+"images/iconStarGrey.png");
		}else{
			$("#ratingStar"+i).attr("src", ABSOLUTE_PATH+"images/iconStar.png");
		}
	}
	
}

function submitComment(){
	
	if(userid == undefined || commentSubmitted == true) return;
	
	api = API_URL+"comment_recipe_api.php";
	$("#commentTA").attr("disabled", "disabled");
	comm = $("#commentTA").val();
	$("#commentTA").val("Your comment has been submitted");
	
	commentSubmitted = true;
	
	$.post(api, 
			{userID:userid, recipeID:recipeID, comment:comm},
		    commentCallback
			);
}

commentsOn = false;
function toggleComments(){
	commentsOn = !commentsOn;
	
	$(".commentDiv").each(
		function(){
			
			if(commentsOn == true){
				$(this).show();
			}else{
				i = $(this).attr("id");
				i = Number(i.substring(7, i.length));
				if(i > 3){
					$(this).hide();
				}else{
					$(this).show();
				}
			}
			
			
		}
	);
	
	str = (commentsOn == true) ? "less comments" : "more comments";
	$("#moreComments").html(str);
	
}

function reportAbuse(id){
	
	if(userid == undefined) return;
	
	api = API_URL+"report_abuse_api.php";
	
	$.post(api, 
			{userID:userid, commentID:id},
		    reportCallback
			);
	
	$("#reportLink"+id).html("reported");
	$("#reportLink"+id).css("color", "#cccccc");
	$("#reportLink"+id).attr("href", "javascript:void(0);");
	
}

function rateCallback(data){
	if(data.matches == undefined) data = JSON.parse(data);
	// do nothing
}

function commentCallback(data){
	if(data.success == undefined) data = JSON.parse(data);
	// do nothing
}

function saveRatingCallback(data){
	if(data.success == undefined) data = JSON.parse(data);
	// do nothing
}

function reportCallback(data){
	if(data.success == undefined) data = JSON.parse(data);
	// do nothing
}



