/// <reference path="uihelper.dialogs.min.js" />
/// <reference path="persistencehelper.min.js" />
/// <reference path="../datahelper.js" />

UIHelper.MainView={Init:function(){UIHelper.MainView.Events.Init();var action=$.query.get("action");switch(action){case DataHelper.Constants.Actions.INSERT_RECIPE:if($.query.get("recipeid")!=""&&$.query.get("day")!=""){PersistenceHelper.WeekPlan.SetDay(parseInt($.query.get("recipeid")),parseInt($.query.get("day")));}
break;}
UIHelper.MainView.UI.SwitchLoading($("#week-container li"),true);DataHelper.GetCurrentWeekPlan(function(weekplan){UIHelper.MainView.UI.PopulateWeek(weekplan);});this.UI.SetWeekNumber();},Events:{Init:function(){this.MakeDaysSortable();$("#week-container li img.close-button").hover(function(){if(!($.browser.msie&&parseInt($.browser.version)<7))
$(this).attr("src","/IMG/buttons/close-button-hover.png");},function(){if(!($.browser.msie&&parseInt($.browser.version)<7))
$(this).attr("src","/IMG/buttons/close-button.png");}),$("#week-container li .header a").click(function(event){event.preventDefault();var index=$("#week-container li .header a").index(this);UIHelper.MainView.UI.PopulateDay(null,index+1);});$("#debug").click(function(event){event.preventDefault();PersistenceHelper.WeekPlan.Debug();});$(".button.search").click(function(){$("#search-dialog").dialog("open");});$(".button.wizard").click(function(){var li=$(this).parent(":first");var dayIndex=$("#week-container li").index(li);UIHelper.Dialogs.WizardDialog.Vars.SelectedDay=dayIndex+1;$("#wizard-dialog").dialog("open");});$("#do-send-plan").click(function(){$("#send-dialog").dialog("open");});$("#do-print").click(function(){$("#print-dialog").dialog("open");});$("#show-shoppinglist-dialog").click(function(){$("#shoppinglist-dialog").dialog("open");});$("#show-archive-dialog").click(function(){$("#archive-dialog").dialog("open");});},MakeDaysSortable:function(){$("#week-container").sortable({cancel:"img",containment:"document",handle:".header",start:onSortStart,stop:onSortStop,placeholder:"day-placeholder",tolerance:"pointer"});function onSortStart(event,ui){UIHelper.MainView.UI.RemoveDayHeadlines();}
function onSortStop(event,ui){UIHelper.MainView.UI.UpdateOrderOfDays();$("#week-container li").each(function(){var field=$("input[name='recipeid']",this);var index=$("#week-container li").index(this);var val=field.val()!=""?field.val():0;PersistenceHelper.WeekPlan.SetDay(val,index+1);});}
function onSort(event,ui){}}},UI:{SetWeekNumber:function(){var today=new Date();var theHeader=$("#madplan-header h1");theHeader.html(theHeader.html()+" "+today.getWeek());},RemoveDayHeadlines:function(){},UpdateOrderOfDays:function(){$("#week-container li").each(function(i){$("h2",this).text(DataHelper.DaysOfWeek[i+1]);$(this).attr("id","day"+(i+1));})},PopulateWeek:function(recipes){$.each(recipes,function(i,recipe){UIHelper.MainView.UI.PopulateDay(recipe,i+1);})},SwitchLoading:function(elements,isLoading){$.each(elements,function(i,element){var recipeImage=$("img.recipe-image");var loadingImage=$("#week-container img.loading-image");if(isLoading){loadingImage.css("display","block");recipeImage.hide();}
else{loadingImage.hide();recipeImage.show();}});},PopulateDay:function(recipe,day){var dayLI=$("#day"+day);if(recipe==null||recipe.Id==0){$("input[name='recipeid']",dayLI).val("0");$(".header img.close-button",dayLI).hide();$(".recipe a.recipe-link",dayLI).hide();$(".recipe div.no-image",dayLI).show();$(".recipe p.description",dayLI).text("");$(".recipe p.preparation-time",dayLI).text("");}else{$(".header img.close-button",dayLI).show();$("input[name='recipeid']",dayLI).val(recipe.Id);$(".recipe .description",dayLI).text(recipe.Name);$(".recipe a.recipe-link",dayLI).show();$(".recipe div.no-image",dayLI).hide();var dayIMG=$("img.recipe-image",dayLI);dayIMG.show();dayIMG.attr("src",recipe.SmallImageUrl);dayIMG.attr("alt",recipe.Name);dayIMG.show();var recipeLink=$("a.recipe-link",dayLI);var linkParams={id:recipe.Id}
recipeLink.attr("href",DataHelper.MakeLink("RecipeView",linkParams));recipeLink.attr("title","Se opskrift");var time=recipe.PreparationTime;$("p.preparation-time",dayLI).text(time+" min");}
UIHelper.MainView.UI.SwitchLoading(dayLI,false);var recipeId=(recipe!=null?recipe.Id:0);PersistenceHelper.WeekPlan.SetDay(recipeId,day);},GetRecipeAndInsertOnDay:function(recipeId,day){UIHelper.MainView.UI.SwitchLoading($("#week-container li#day"+day),true);var requestData={Id:JSON.stringify(recipeId)}
JSONHelper.MakeRequest("GetRecipeById",requestData,function(responseData){UIHelper.MainView.UI.SwitchLoading($("#week-container li#day"+day),false);var recipe=responseData.d;UIHelper.MainView.UI.PopulateDay(recipe,day);});}}}
jQuery(function($){UIHelper.MainView.Init();});
