// This global flag is used for detect the active tab "List View" or "Detail View"
var tabFlag = true;
var tabFeaturedRecipe = true;
var tabSelected = false;
var substituteHeight;
var substituteOffset;
function getRecipeId(element) {
    if (!$(element).hasClass("recipeView")) {
        element = $(element).closest("div.recipeView")[0];
    }
    return $("input:first", element).attr("value");
}

function recipeShowComments() {
    var postId = getRecipeId(this);
    var commentsContainer = $('.commentsContainer', this.parentNode.parentNode.parentNode.parentNode);
    commentsContainer.show();
    $("iframe", commentsContainer[0]).attr("src", basePath + "ajax/posts/comments.aspx?id=" + postId);
    return false;
}

function adjustIFrameHeight(iframe) {
    var rv = null;
    if (iframe.contentDocument)
        rv = iframe.contentDocument;
    else
        rv = document.frames[iframe.id].document;
	if (rv) {
		var newHeight = rv.body.offsetHeight + 25;
		iframe.height = newHeight;
		if (iframe.id == "suggestion") {
			if (document.getElementById("modalPopUpContainer")) {
				document.getElementById("modalPopUpContainer").style.height = newHeight + "px";
				document.getElementById("modalPopUpContainer").style.top = "50%"
				document.getElementById("modalPopUpContainer").style.marginTop = (newHeight) / 2 * -1 + "px";
			}
		}
    }
}

function addToRecipeBox(postID) {
    recipeBoxBaseFunction({ Action: "add", postId: postID });
}

function removeFromRecipeBox(postID) {
    recipeBoxBaseFunction({ Action: "remove", itemsToDelete: postID });
}

function recipeBoxBaseFunction(data) {
    $.get(basePath + "MyStuff/setRecipeBox.aspx", data, function() {
        updateMyRecipeBoxCount();
    });
}

function updateRecipeBox(element) {
    var added = $("input[type='hidden']:last", element).attr("value") == "true";
    if (added) {
        $("div.recipeBoxAdded", element).show();
        $("div.recipeBoxNotAdded", element).hide();
    } else {
        $("div.recipeBoxNotAdded", element).show();
        $("div.recipeBoxAdded", element).hide();
    }
}

function linkRecipeBoxAdd() {
    var parent = $(this).closest("div.recipeBox");
    var postID = $("input[type='hidden']:first", parent).attr("value");
    addToRecipeBox(postID);
    $("input[type='hidden']:last", parent).attr("value", "true");
    updateRecipeBox(parent);
    return false;
}

function linkRecipeBoxRemove() {
		var parent = $(this).closest("div.recipeBox");
		$("#confirmationPopup").modal();
		$('#modalPopUpContainer').css({height:"auto"});
		$("#confirmationPopup a.btnNo").click(function(){
			$("#modalPopUpContainer .simplemodal-close").trigger('click');
			return false;
		});
		$("#confirmationPopup a.btnYes").click(function(){
			$("#modalPopUpContainer .simplemodal-close").trigger('click');
			removeRecipe();
			return false;
		});
    function removeRecipe() {
			var postID = $("input[type='hidden']:first", parent).attr("value");
	    removeFromRecipeBox(postID);
  	  $("input[type='hidden']:last", parent).attr("value", "false");
    	updateRecipeBox(parent);
		}
    return false;
}

function updateMyRecipeBoxCount() {
    $("#myRecipeBoxCount, #topNavMyRecipeBoxCount").load(basePath + "ajax/recipes/recipeBoxCount.aspx");
}

function addToShoppingCart(postID, ingredients) {
    shoppingCartBaseFunction({ action: "add", postId: postID, ingredientString: ingredients });
}

function removeFromShoppingCart(postID, ingredients) {
    shoppingCartBaseFunction({ action: "delete", postId: postID, ingredientString: ingredients });
}

function shoppingCartBaseFunction(data) {
    $.get(basePath + "MyStuff/setIngredients.aspx", data, function() {
        updateShoppingCartCount();
    });
}

function updateShoppingCart(element) {
    var added = $("input[type='hidden']:last", element).attr("value") == "true";
    if (added) {
        $("div.rshoppingCartAdded", element).show();
        $("div.shoppingCartNotAdded", element).hide();
    } else {
        $("div.shoppingCartNotAdded", element).show();
        $("div.rshoppingCartAdded", element).hide();
    }
}

function linkShoppingCartAdd() {
    var parent = $(this).closest("div.shoppingCart");
    var postID = $("input[type='hidden']:first", parent).attr("value");
    ingredientString = linkShoppingCartGetIngredients(parent);
    addToShoppingCart(postID, ingredientString);
    $("input[type='hidden']:last", parent).attr("value", "true");
    updateShoppingCart(parent);
    return false;
}

function linkShoppingCartRemove() {
    var parent = $(this).closest("div.shoppingCart");
    var postID = $("input[type='hidden']:first", parent).attr("value");
    ingredientString = linkShoppingCartGetIngredients(parent);
    removeFromShoppingCart(postID, ingredientString);
    $("input[type='hidden']:last", parent).attr("value", "false");
    updateShoppingCart(parent);
    return false;
}

function linkShoppingCartGetIngredients(element) {
    var delimiter = "";
    var ingredientString = "";
    $("div.preparationContainer div.ingredientsBox li", $(element).closest("div.recipeView")[0]).each(function() {
        ingredientString = ingredientString + delimiter + $(this).text();
        if (delimiter == "" ) delimiter = "|";
    });
    return ingredientString;
}

function updateShoppingCartCount() {
    $("#topNavShoppingCartCount").load(basePath + "ajax/recipes/shoppingCartCount.aspx");
}

function recipesDetailsLink() {
    $('div.extendedProperties', this.parentNode.parentNode.parentNode).toggle();
    $(this).toggleClass('expanded');
    return false;
}

function globalRecipesReset (parent) {
	setupRecipes(parent);
	setupTabs();
	//itemTitleReplace();
}

function showTooltip(){
	// Rollover functions for the made with link
	$(".madeWith div a").live("mouseover", function(){
		var ctn = $(this).siblings("div.madeWithCtn").html();
		var position = $(this).position();
		$("#prodTooltip").html(ctn);
		$("#prodTooltip").css({left: position.left - 206, top: (position.top - 56)}).fadeIn("fast");
	});
	$(".madeWith div a").live("mouseout", function(){
		$("#prodTooltip").hide().html("&nbsp;");
	});
	// Rollover functions for the substitutes link
	$(".madeWithAndSubstitutes .substitutes a").live("mouseover", function(){
		var ctn = $(this).siblings("div.subsCtn").html();
		var position = $(this).position();
		$("#substitutesBox .boxCtn").html(ctn);
		var topPosFix = $("#substitutesBox").height() / 2;
		$("#substitutesBox").css({left: position.left - 130, top: (position.top - topPosFix + 7)}).fadeIn("fast");
	});
	$(".madeWithAndSubstitutes .substitutes a").live("mouseout", function(){
		$("#substitutesBox").hide().find(".boxCtn").html("&nbsp;");
	});
	$(".madeWithAndSubstitutes .substitutes a").live("click", function(){ return false; });
}

function setupRecipes(parentElement) {
    parentElement = parentElement ? parentElement + " " : "";
    $(parentElement + "div.recipeView a.recipeDetailsLink").click(recipesDetailsLink);
    $(parentElement + "div.recipeView a.recipeShowCommentsLink").click(recipeShowComments);
    if(isAuthenticated) {
			$(parentElement + "div.recipeBox a.recipeBoxAdd").click(linkRecipeBoxAdd);
  	  $(parentElement + "div.recipeBox a.recipeBoxRemove").click(linkRecipeBoxRemove);
			$(parentElement + "div.recipeBox").each(function() {
					updateRecipeBox(this);
			});
			$(parentElement + "div.shoppingCart a.shoppingCartAdd").click(linkShoppingCartAdd);
			$(parentElement + "div.shoppingCart a.shoppingCartRemove").click(linkShoppingCartRemove);
			$(parentElement + "div.shoppingCart").each(function() {
					updateShoppingCart(this);
			});
		} else {
			$(parentElement + "div.recipeBox a.recipeBoxAdd").click(function(){alert("You are not logged in!");});
  	  $(parentElement + "div.recipeBox a.recipeBoxRemove").click(function(){alert("You are not logged in!");});
			$(parentElement + "div.shoppingCart a.shoppingCartAdd").click(function(){alert("You are not logged in!");});
			$(parentElement + "div.shoppingCart a.shoppingCartRemove").click(function(){alert("You are not logged in!");});
		}
    setupRatingControl(parentElement);
}

$(document).ready(showTooltip);
// Spotlight tagging for the print recipe links
function printRecipePing() {
	//random number generation
	var axel = Math.random() + "";
	var a = axel * 1000000000000000000;
	var doPing = new Image();
	doPing.src ='http://ad.doubleclick.net/activity;src=1419543;type=hills544;cat=print446;ord=1;num=' + a +	'?';
}
// Spotlight tagging for the add to recipe box links
function addToRecipeBoxPing() {
	//random number generation
	var axel = Math.random() + "";
	var a = axel * 1000000000000000000;
	var doPing = new Image();
	doPing.src ='http://ad.doubleclick.net/activity;src=1419543;type=hills544;cat=addto006;ord=1;num=' + a +	'?';
}
// Spotlight tagging for the add to shopping list links
function addToShoppingPing() {
	//random number generation
	var axel = Math.random() + "";
	var a = axel * 1000000000000000000;
	var doPing = new Image();
	doPing.src ='http://ad.doubleclick.net/activity;src=1419543;type=hills544;cat=addto215;ord=1;num=' + a +	'?';
}