var recipes = new Array();
var recipeLabels = new Array();
var playerOpen = false;
var recipe;
var gillsans = { src: '/consumerfiles/pageitems/master/campaignpages/Robust/fonts/Gillsans_Light_Italic.swf' };
function loadRecipesXML(url){
	sIFR.activate(gillsans);
	$.ajax(
	{
		type: "GET",
		url: url,
		dataType: "xml",
		success: function(xml) {
			$(xml).find('recipe').each(function()
			{
				var id = $(this).find("recipeId").text();
				recipes[id] = $(this);
			});
			$(xml).find('label').each(function()
			{
				var id = $(this).attr("id");
				var str = $(this).text().replace(/<!\[CDATA\[/i,'');
				recipeLabels[id] = str;
			});
			$("#label_nutritionPerServing").html(recipeLabels["nutritionPerServing"]);
			$("#thisDish .title").html(recipeLabels["thisDish"]);
			$("#label_preparationTime").html(recipeLabels["preparationTime"]);
			$("#label_cookingTime").html(recipeLabels["cookingTime"]);
			$("#label_readyToEatIn").html(recipeLabels["readyToEatIn"]);
			$("#label_difficulty").html(recipeLabels["difficulty"]);
			$("#label_cookingInstructions").html(recipeLabels["cookingInstructions"]);
			$("#label_showInstructionsVideo").html(recipeLabels["showInstructionsVideo"]);
			
			$("#label_cookingTips").html(recipeLabels["cookingTips"]);
			$("#label_seeMoreTips").html(recipeLabels["seeMoreTips"]+" &raquo;");
		}
	});
};

function hideRecipe()
{
	$("#recipeContent").hide();
}
function showRecipe(id)
{
	/*if(id.indexOf("1001") == -1) {
		if(id.length==1) id = "0"+id;
		id = "1001"+id;
		//alert("showRecipe id : "+ id + " (converted to id "+id+")");
	}*/
	recipe = recipes[id];
	if(recipe)
	{
		$("#label_recipeBy").html(recipeLabels["recipeBy"] + " "+ recipe.find('authorFirstName').text() + " " + recipe.find('authorLastName').text());
		//Recipe Image
		var imageUrl = recipe.find('imageUrl').text();
		if(imageUrl)
		{
			$("#RecipeImageRobust").html("<img src='/consumerfiles/pageitems/master/campaignpages/Robust/img/recipes/dishes/"+imageUrl+"' width='250'/>");
		}else{
			$("#RecipeImageRobust").html("");
		}
		//Full up general Recipe Info
		$("#recipeTitle").html(recipe.find('title').text());
		var description = recipe.find('authorRecipeDescription').text();
		if (description=="" || description==" ") $("#RecipeDescription").css("display","none");//description = "No Description";
		else $("#RecipeDescription").html(description);
		$("#RecipePrepTime").html("<b>"+recipe.find('prepTime').text()+"</b>");
		$("#RecipeCookingTime").html("<b>"+recipe.find('cookTime').text()+"</b>");
		$("#RecipeReadyToEat").html("<b>"+recipe.find('totalTime').text()+"</b>");
		var difficulty = recipe.find('difficulty').text();
			$("#RecipeDifficulty").html("<b>"+difficulty+"</b>");
		//Ingredients
		ingredientsList = $(document.createElement("ul"));
		var ingredient;
		var ingedients = recipe.find('ingredient').each(function(){
			ingredient = $(document.createElement("li"));
			ingredient.html("<b>"+$(this).find("metricQuantity").text()+" "+$(this).find("metricMeasure").text()+"</b> <span class='highlight'>"+$(this).find("name").text()+"</span> " + $(this).find("fullIngredientLine").text());
			ingredientsList.append(ingredient);
		});
		$("#RecipeIngredients").html(ingredientsList);
		$("#RecipeIngredients li:last").addClass("last");
		//Nutritions
		$("#RecipeNutritions .content").html("");
		
		var nutritions = recipe.find('nutritions').find('nutrition');
		var nutritionstr = "";
		
		var nutrition = $(document.createElement("div"));
		nutrition.addClass("nutrition");
		
		
		
		nutritions.each(function()
		{
			var table = $(document.createElement("table"));
			
			
			var td = $(document.createElement("td"));
			td.addClass("title");
			var td2 = $(document.createElement("td"));
			td2.addClass("content");
			var tr = $(document.createElement("tr"));
			
			/*nutrition.append(title);
			nutrition.append(content);*/
			tr.append(td);
			tr.append(td2);
			table.append(tr);
			nutrition.append(table);
			
			var name = $(this).find('name').text();
			var unitName = $(this).find('unit').text();
			var className = "";
			var shortname = $(this).find('displayName').text();
			switch(name)
			{
				case "Kilocalories":
					
					className = "cals";
					break;
				case "Protein":
					
					className = "protein";
					break;
				case "Fat":
					
					className = "fat";
					break;
				case "Saturated Fat":
					
					className = "satfat";
					break;
				case "Carbohydrates":
					
					className = "carbohyd";
					break;
				case "Fiber":
					
					className = "fiber";
					break;
			}
			
			//if(unitName=="kcal") unitName = "<br/>" + unitName;
			//else unitName += "<br/><br/>";
			td.html(shortname);
			//content.html("455<span class='percentage'>23%</span>");
			td2.html($(this).find('roundedValue').text()+unitName);
			//+"<span class='percentage'>"+$(this).find('percentDailyValue').text()+"%</span>");
			td.addClass(className);
			
			//Instructions
			instructionsList = $(document.createElement("ul"));
			var instruction;
			var num=0;
			var instruction = recipe.find('step').each(function()
			{
				num++;
				
				instruction = $(document.createElement("li"));
				instruction.css("background","url(/consumerfiles/pageitems/master/campaignpages/Robust/img/recipes/dots/dot"+num+".jpg)  no-repeat 0px 3px");
				instruction.html($(this).find("stepDetail").text());
				
				instructionsList.append(instruction);
			});
			
			$("#RecipeInstructions").html(instructionsList);
			
			//createRecipePlayer();
		});
		
		nutritionstr += nutrition.outerHTML();
		
		$("#RecipeNutritions .content").html(nutritionstr);
		
		$("#RecipePortions").html(recipeLabels["ingredientsFor"]+" "+recipe.find('yieldDisplayText').text());
		
		if(recipe.find('playerId').text()){
			$("#label_showInstructionsVideo").css("display","inline");
		}else{
			$("#label_showInstructionsVideo").css("display","none");
		}
		
		$("#recipeContent").show();
		
		removeRecipePlayer();
		
		
			$("#recipeTitle").removeClass("sIFR-replaced");
			$("#thisDish .title").removeClass("sIFR-replaced");
			
			//$("#recipeTitle").html("test");
		
		
		sIFR.replace(gillsans, {
			selector: '#recipeTitle',
			wmode : "transparent",
			
			css: ['.sIFR-root {color:#9cb37d;font-style:italic;}' ] 
		});
		
		sIFR.replace(gillsans, {
			selector: '#thisDish .title',
			wmode : "transparent",
			
			css: ['.sIFR-root {color:#666666;font-style:italic;}' ] 
		});
			
		//window.location.hash="recipeAnchor"
		$(window).scrollTop(1045);
	}
}

function createRecipePlayer()
{
	if(!playerOpen){
		createBrightcovePlayer({
			id:"RecipeVideoPlayer",
			containerId:"RecipeVideo",
			playerId:recipe.find('playerId').text(),
			publisherID:1875254528,
			width:320,
			height:240,
			videoPlayer:recipe.find('videoplayerId').text(),
			autoStart:"false"
		});
		
		$("#RecipeVideoContainer .title").removeClass("sIFR-replaced");
		
		$("#RecipeVideoDescription").html('<span class="highlight">'+recipe.find('videoTitle').text()+'</span>: '+recipe.find('videoDescription').text());
		$("#RecipeVideoTips").show();
		$("#RecipeVideoContainer").show();
		
		sIFR.replace(gillsans, {
			selector: '#RecipeVideoContainer .title',
			wmode : "transparent",
			
			css: ['.sIFR-root {color:#666666;font-style:italic;}' ] 
		});
		
		playerOpen = true;
	}
}

function removeRecipePlayer()
{
	if(playerOpen){
		$("#RecipeVideoContainer").hide();
		removeBrightcovePlayer("RecipeVideoPlayer");
		playerOpen = false;
	}
}

jQuery.fn.outerHTML = function(s) { return (s)? this.before(s).remove() : jQuery("<p>").append(this.eq(0).clone()).html(); }
