﻿var loadInProgress = false;

function SearchRecipes() {

	if (!loadInProgress) {
		var categoryID = $(".recipes .category select option:selected").val();
		var peopleID = $(".recipes .people select option:selected").val();
		var timeID = $(".recipes .time select option:selected").val();

		var keyword1 = $(".recipes .keyword1 input").val();
		var keyword2 = $(".recipes .keyword2 input").val();
		var keyword3 = $(".recipes .keyword3 input").val();
		var attributes = "";
		if ($(".recipes input#attr231").attr("checked"))
			attributes += "231,";
		if ($(".recipes input#attr232").attr("checked"))
			attributes += "232,";
		if ($(".recipes input#attr233").attr("checked"))
			attributes += "233,";
		if ($(".recipes input#attr234").attr("checked"))
			attributes += "234,";
		if ($(".recipes input#attr235").attr("checked"))
			attributes += "235,";
		if ($(".recipes input#attr236").attr("checked"))
			attributes += "236,";
		loadInProgress = true;
				
		MadurLifandi.Services.Recipes.GetRecipes(categoryID, peopleID, timeID, keyword1, keyword2, keyword3, attributes, LoadRecipesComplete, LoadRecipesFailed);
	}
}

function LoadRecipesComplete(data, pageIndex, methodName) {
	if (data != null) {
		if (data.length > 0) {
			var recipe;
			var html = '';
			for (var i = 0; i < data.length; i++) {
				recipe = data[i];
				html += '<div class="item"><h4>' + recipe.Name + '</h4><p>' + recipe.Content + '</p><a href="/Uppskrift/' + recipe.RecipeID + '/" title="Skoða nánar" class="more">Nánar</a></div>';
			}
			
			$(".recipes .recipeList").html(html);
			
			recipe = null;
			html = null;
			data = null;
		}
		else {
			$(".recipes .recipeList").html('<p style="color: #e91f35; font-weight: bold;">Engar uppskriftir fundust við þessi leitarskilyrði.</p>');
		}
	}
	else {
		$(".recipes .recipeList").html('<p style="color: #e91f35; font-weight: bold;">Engar uppskriftir fundust við þessi leitarskilyrði.</p>');
	}

	loadInProgress = false;
}

function LoadRecipesFailed(exception, pageIndex, methodName) {
	alert('Villa: Birting uppskrifta.');
	loadInProgress = false;
}

function ClearRecipeSearchForm() {
	$(".recipes .category select option:first").attr("selected", "selected").parent('select');
	$(".recipes .people select option:first").attr("selected", "selected").parent('select');
	$(".recipes .time select option:first").attr("selected", "selected").parent('select');
	$(".recipes .field input").val("");
	$(".recipes .attributes input").attr("checked", "");
}

function ShowMenuSpinner() {
}

function HideMenuSpinner() { 

}

function MenuLoad() {
	if (!loadInProgress) {
		loadInProgress = true;
		ShowMenuSpinner();

		var day = $(this).html();

		MadurLifandi.Services.Recipes.GetMenuDay(day, MenuLoadComplete, MenuLoadFailed, $(this));
	}
}

function MenuLoadComplete(data, handle, methodName) {
	if (data != null) {
		$(".sideCol .menu ul li.selected").removeClass("selected");
		handle.addClass("selected");

		$(".sideCol .menu span.day").html(data.Name);
		$(".sideCol .menu a").attr("href", data.Url);

		$(".sideCol .menu p:first").html('<span>' + data.MealName1 + ': </span>' + data.MealContent1);
		if (data.MealName2 != null && data.MealName2 != '') {
			$(".sideCol .menu p:last").html('<span>' + data.MealName2 + ': </span>' + data.MealContent2).show();
		}
		else
			$(".sideCol .menu p:last").hide();
	}
	else {
		//Gat ekki sótt matseðil dagsins.		
	}

	loadInProgress = false;
	HideMenuSpinner();
}

function MenuLoadFailed(exception, handle, methodName) {
	alert('Villa: Birting matseðils.');
	loadInProgress = false;
	HideMenuSpinner();
}
