 function saveRecipe( rId, offsite ){
 	foaUsageLog(window.location, "Save Recipe From Recipe Page");
	$.post("/members/saveRecipe?async=true", { r: rId, type: "data" }, 
		function(data){
			switch(data){
				case '0':	$('#save-recipe-box #save-recipe a').hide();
							$('#save-recipe-box #save-ok').show();
						 	break;
				case '1': 	$('#save-recipe-box #save-recipe a').hide();
							$('#save-recipe-box #save-ok').show();
						  	break;
				case 'A1':	
				case 'A2':	if (offsite == undefined){
								registerOrLogin("Save Recipe From Recipe Page");
							}else{
								window.top.location.href="/accounts/register?redirectURI=" + escape( window.top.location );
							}
							break;
			}
		}
	);			
}

//deprecated
function showModalOrRedirect( offsite ) {
	if( offsite == undefined ) {
		showAccountsMod('register');
	} else {
		window.top.location.href="/accounts/register?redirectURI=" + escape( window.top.location ) 
	}
}
function chompRecipe( rId ){
	foaUsageLog(window.location, "Chomp Recipe");
	$.post("/members/recipes/chompRecipe?async=true", { recipe: rId, type: "data" }, 
		function(data){
			switch(data){
				case 'A1':	registerOrLogin("Chomp Recipe");
							break;
				case 'A2':	registerOrLogin("Chomp Recipe");
							break;
				default:	$('#recipe-chomp-cont-' + rId + ' #chomp-it').hide();
							$('#recipe-chomp-cont-' + rId + ' #chomped').show();
							$('#recipe-chomp-cont-' + rId + ' #num-of-chomps').html(data + '');
						 	break;
			}
		}
	);			
}

function addRecipeComment(form, rId, objName){
	for (var i=0; i<form.comment_type.length; i++)  { 
		if (form.comment_type[i].checked)  {
			commentType = form.comment_type[i].value
		} 
	} 
	addRecipeCommentByType(rId, objName, commentType );
}

function addRecipeCommentByType(rId, objName, commentType ){
	
	$commentObj = $('#' + objName );
	$commentTypeObj = $('#' + objName + "-type" );
	$commentSizeObj = $('#recipe-comments-size');
	$addButtonObj = $('#user-comment-button');
	$addingCommentObj = $('#user-comment-adding');
	var comment = $commentObj.val();
	if ($.trim(comment) == "" || $('#recent_comment').val() == comment){
		return false;
	}
	
	$addButtonObj.hide();
	$addingCommentObj.show();
	foaUsageLog(window.location, "Add Recipe Comment");

	$.post("/members/recipes/addRecipeComment?async=true", { comment_type: commentType, recipe: rId, comment:comment, type: "data" }, 
		function(data){
			switch(data){
				case 'A1':	registerOrLogin("Add Recipe Comment");
							break;
				case 'A2':	registerOrLogin("Add Recipe Comment");
							break;
				default:	$('#no-recipe-comments').hide();
							$('#recipe-comments').prepend( data );
							$commentObj.val("");
							var currCount = parseInt($('#recipe-comments-size').text());
							var newCount = eval("currCount+1");
							$commentSizeObj.text(newCount+"");
							$('#recent_comment').val(comment);
							$('#add-comment-container').hide();
						 	break;
			}
			$addButtonObj.show();
			$addingCommentObj.hide();
		}
	);			
}

function deleteRecipeComment( cId, uId ){
	$commentSizeObj = $('#recipe-comments-size');
	$.post("/members/recipes/deleteRecipeComment?async=true", { commentId: cId, userId: uId, type: "data" }, 
		function(data){
			switch(data){
				case 'A1':	//error
							break;
				default:	$('#recipe-comment-' + cId).remove();
							var currCount = parseInt($('#recipe-comments-size').text());
							var newCount = eval("currCount-1");
							$commentSizeObj.text(newCount+"");
						 	break;
			}
		}
	);
}


function addShoppingListItem(rId) {
	$.post("/members/recipes/addToShoppingList?async=true", { r: rId,i:$('#shopping_list_ingredient').val(), type: "data" } ,
		function(data){
			switch(data){
				case 'A1':	registerOrLogin("Add To Shopping List");
							break;
				case 'A2':	registerOrLogin("Add To Shopping List");
							break;
				default:	document.onclick=null;
							$("#shopping-list-form").hide();
							var $listComplete = $("#shopping-list-complete");
							$listComplete.show();
							$("#shopping-list-cont").fadeOut(2000);
						 	break;
			}
		}
	);				
}

function addRecipeToShoppingList(rId) {
	$("#add-all-to-shopping-list").hide();
	$.post("/members/recipes/addRecipeToShoppingList?async=true", { r: rId, type: "data" } ,
		function(data){
			$("#adding-shopping-list").hide();	
			switch(data){
				case 'A1':	registerOrLogin("Add To Shopping List");
							break;
				case 'A2':	registerOrLogin("Add To Shopping List");
							break;
				default:	var $listComplete = $("#shopping-list-all-complete");
							$listComplete.show();
							$("#shopping-list-all-complete").fadeOut(6000);
						 	break;
			}
		}
	);			

}

function showShoppingListForm(event, obj, ingId) {
	$("#shopping-list-complete").hide();

	var $shoppingListForm = $("#shopping-list-cont");

	$shoppingListForm.css( {top: ( getAbsoluteTop(obj.id) - 7) + "px", left: (getAbsoluteLeft(obj.id) + 18) + "px" } );
	$("#shopping_list_ingredient").attr("value", $("#ingredient" + ingId).html() );
	$shoppingListForm.show();	
	$("#shopping-list-form").show();
		
	document.onclick=function(event){		
		toggleForm(event);
	};
	
	stopBubble(event);
}
function toggleForm(event) {
		$("#shopping-list-cont").toggle();	
		document.onclick=null;
}

function moveBeverage(bev, id){
	//make ajax to add directly to db
	var container = $(bev).parent().attr("id");
	if ( container == 'beverage-selector' ){
		$(bev).fadeOut("fast", function(){
				$(bev).clone().appendTo('#beverage-choices')
				.append("<input type='hidden' name='beverages' value='" + id + "'")
				.fadeIn("slow", function(){
					$(bev).remove();
				});
		});
		
	}else{
		$(bev).fadeOut("fast", function(){
				$(bev).clone().appendTo('#beverage-selector')
				.fadeIn("slow", function(){
					$(bev).remove();
				})
				.find("input").remove();
		});
	}
}

function recipeVote(cId, voteType){
	var $voteObj = $("#vote-" + voteType + "-" + cId);
	var $voteValObj = $("#vote-" + voteType + "-" + cId + "-val");
	var src = $voteObj.find("img").attr('src');
	var vId = $voteValObj.val();
	$.post("/members/recipes/commentVote?async=true", { cId: cId, voteType: voteType, vId: vId, type: "data" }, 
		function(data){
			switch(data){
				case 'A1':	registerOrLogin("Chomp Recipe");
							break;
				case 'A2':	registerOrLogin("Chomp Recipe");
							break;
				default:	var $count = $('#vote-' + voteType + '-' + cId + '-count');
							if (vId == ""){
								src = src.replace("off.gif", 'on.gif');
								$count.html((parseInt($count.text()) + 1)+ "");
							}else{
								src = src.replace("on.gif", 'off.gif');
								$count.html((parseInt($count.text()) - 1) + "");
							}
							$voteObj.find("img").attr("src", src);
							$voteValObj.val(data);
						 	break;
			}
		}
	);			
}


isInteger = function( s ) {
return !isNaN( parseInt( s ) );
}

function reduce(An, Ad) {
  ng=An+'';
  dg=Ad+''
  var factorX //highest common factor
  An = Math.abs( An );
  Ad = Math.abs( Ad );
  var factorX = 1;

  //Find common factors of Numerator and Denominator
  for ( var x = 2; x <= Math.min( An, Ad ); x ++ ) {
    var check1 = An / x;
    if ( check1 == Math.round( check1 ) ) {
      var check2 = Ad / x;
      if ( check2 == Math.round( check2 ) ) {
        factorX = x;
      }
    }
  }

  An=An/factorX;  //divide by highest common factor to reduce fraction then multiply by neg to make positive or negative
  Ad=Ad/factorX;  //divide by highest common factor to reduce fraction
  if(An > Ad){
  	//this logic is wrong need to make recursive and mod Ad
  	remainder = Math.round(An % Ad);
  	wholeNumber = Math.floor(An/Ad);
  	if (Math.round(remainder*10) > 0){
  		return "<span style='font-size:1.1em'> " + wholeNumber + "</span> <span style='font-size:.9em'> " + reduce(remainder,Ad) + "</span>";	
  	}else{
  		return wholeNumber;
  	}
  }else if (An == Ad){
	return "1";
  }else if(Ad == 1){
  	return (An);
  }else{
	return ("<span style='font-size:.9em'>" + An + "/" + Ad + "</span>");
  }
}	

function getFactoredServings(str, factor){
	var cnt = 0;
	var startInt = 0;
	var endInt = 0;
	var cursor = 0;
	var isFraction = false;
	var intFound = false;
	var newStr = "";
	var re = /(\d+)[\/](\d+)/;
	var m;
	m1 = Math.round( factor * 10 );
	m2 = 10;
	for(i=0;i<str.length;i++){
		chr = str.charAt(i);
		if(isInteger(chr) && intFound == false){
			startInt = i;
			intFound = true;
		}
		if(!isInteger(chr) && intFound == true){
			if(chr == "/" && isInteger(str.charAt(i+1)) ){
				isFraction = true;
			}else{
				endInt = i;
				if(isFraction){
					frac = str.substring(startInt, endInt);
					m = re.exec(frac);
					numer = parseInt(m[1]);
					denom = parseInt(m[2]);
						numer = parseInt(m[1] * m1);
						denom = parseInt(m[2] * m2);
					newFrac = reduce(numer,denom);
					newStr += str.substring(cursor, startInt) + newFrac;
				}else{
					origSize = (parseInt(str.substring(startInt, endInt)));
					numer = origSize * m1;
					denom = 10;
					newStr += str.substring(cursor, startInt) + reduce(numer,denom);
				}
				cursor = endInt;
				intFound = false;
				isFraction = false;
			}
		}
	}
	newStr += str.substring(cursor, str.length);
	return newStr;
}

function changeServings(origServings){
	resetServings();
	var newServings = $('#new-servings').val();
	var factor = newServings / origServings;
	$( "#ingredients span.ing-desc" ).each(
	
		function(){
			$(this).html( getFactoredServings($(this).html(),factor) );
		}
	);
}

function resetServings(){
	$( "#ingredients li" ).each(
		function(){
			$(this).find("span.ing-desc").html($(this).find("span.ing-desc-orig").html());
		}
	);
}

function getComplimentText(text, textBox){
	$('#user-comment').val(text);
	$('#user-comment').select();
}

function showComplimentOpt(){
	$('#add-comment-container').show();
	$('#make-comment').hide();
	$('#add-complimet').show();
	$('#default-compliment').click();
	
}

function showCommentOpt(){
	$('#add-comment-container').show();
	$('#add-complimet').hide();
	$('#make-comment').show();
	$('#default-comment').click();
	
}
