﻿if (typeof (GMI.AddRecipe) == 'undefined') {
    GMI.AddRecipe = {};
}

GMI.AddRecipe.CurrentWeekStart = new Date();  //GMI.GetDateOfCurrentWeekSunday();
GMI.AddRecipe.CurrentRecipe;
GMI.AddRecipe.CurrentRecipeTitle;
GMI.AddRecipe.UserKey;

GMI.AddRecipe.MoveAddRecipeToNextWeek = function() {
    // Figure out what the new week start will be
    var currentWeekStart = $('#AddRecDialog th:nth-child(2)').attr('id');
    var currentWeekDate = GMI.GetDateFromStandardStringFormat('-', currentWeekStart);
    GMI.AddRecipe.CurrentWeekStart = GMI.AddDaysToDate(7, currentWeekDate);

    // Close the AddRecipeDialog
    $('.dialog-overlay').click();

    // Open the dialog with the new week start
    GMI.AddRecipe.ShowAddRecipeDialog();

    return false;
}

GMI.AddRecipe.MoveAddRecipeToPreviousWeek = function() {
    // Figure out what the new week start will be
    var currentWeekStart = $('#AddRecDialog th:nth-child(2)').attr('id');
    var currentWeekDate = GMI.GetDateFromStandardStringFormat('-', currentWeekStart);
    GMI.AddRecipe.CurrentWeekStart = GMI.AddDaysToDate(-7, currentWeekDate);

    // Close the AddRecipeDialog
    $('.dialog-overlay').click();

    // Open the dialog with the new week start
    GMI.AddRecipe.ShowAddRecipeDialog();

    return false;
}

GMI.AddRecipe.SetCurrentRecipeAndShowDialog = function(evt) {
    evt.preventDefault();
    GMI.AddRecipe.CurrentRecipe = $(this).attr('rel');
    GMI.AddRecipe.CurrentRecipeTitle = $(this).attr('title');
    GMI.AddRecipe.ShowAddRecipeDialog();
}

GMI.AddRecipe.ShowAddRecipeDialog = function() {
    $('#AddRecDialog').html('');
    $('#AddRecDialog').css({ 'background': 'url(/images/ajax-loader.gif) no-repeat center center', 'width': '100%', 'height': '100%' });
    // Open the progress spinner and dialog, but hide the dialog

    $('#AddRecDialog').ModalDialog({ width: 1000, title: "Add this recipe to your meal plan calendar", id: "AddRecipeDialog", position: 'absolute', deferredDisplay: true });

    // Grab the contents of the "Add Recipe Control" and add to the AddRecipeDialog
    GMI.SendXHRToWCF('GetAddRecipeDialog',
                    { userKey: GMI.AddRecipe.UserKey, recipeId: GMI.AddRecipe.CurrentRecipe, weekStart: GMI.GetDateStringFromDate(GMI.AddRecipe.CurrentWeekStart) },
                    function(data) {

                        // Get the contents of the dialog
                        $('#AddRecDialog').html(data.d);

                        $('#AddRecDialog').ModalDialog.Show();

                        $('#AddRecDialog').css('background', '');

                        $('#AddRecDialog .recipe').each(function() {
                            if ($(this).attr('id') == GMI.AddRecipe.CurrentRecipe) {
                                $(this).closest('td').removeClass('occupied');
                            }
                        });
                    },
    // Error function
                    function() {
                        ModalAlert('There was an opening the "Add Recipe" dialog. Please try "reloading" the page to try again.');
                    }
    );
}

GMI.AddRecipe.AddRecipeToCalendar = function(evt) {
    evt.preventDefault();

    // Private Function AddToCalendar(ByVal userKey As String, ByVal recipeId As String, ByVal mealTypeId As Integer, ByVal planDate As String) As Boolean
    var data = GMI.Calendar.GetContainerData($(this));
    var container = $(this).closest('td');
    container.removeClass('occupied');

    GMI.SendXHRToWCF('AddToCalendar',
                    { userKey: GMI.AddRecipe.UserKey, recipeId: GMI.AddRecipe.CurrentRecipe, mealTypeId: data.MealId, planDate: data.CellDate },
                    function(data) {
                        container.append('<div id="' + GMI.AddRecipe.CurrentRecipe + '" class="recipe min-recipe"><h5>' + GMI.AddRecipe.CurrentRecipeTitle + '</h5></div>');
                    },
    // Error function
                    function() {
                        container.addClass('occupied');
                        ModalAlert('There was an error opening the "Add Recipe" dialog. Please try "reloading" the page to try again.');
                    }
    );

}

$(document).ready(function() {
    $('.add-recipe').click(GMI.AddRecipe.SetCurrentRecipeAndShowDialog);
    $('body').delegate('.recipe-add-to-recipe-box-button, .btn-sprite-recipe-box', 'click', GMI.Calendar.SaveRecipeToRecipeBox); // Function in commonplan.js
    $('#add-to-recipebox').click(GMI.AddToRecipeBoxFolderFromSearch);    // Confirm add to recipe box
    $('#AddRecDialog').delegate('.add-next-week-button', 'click', GMI.AddRecipe.MoveAddRecipeToNextWeek);
    $('#AddRecDialog').delegate('.add-previous-week-button', 'click', GMI.AddRecipe.MoveAddRecipeToPreviousWeek);
    $('body').delegate('.btn-sprite-calendar', 'click', GMI.AddRecipe.SetCurrentRecipeAndShowDialog);
    $('body').delegate('.add-recipe-view .cell-context-button', 'click', GMI.AddRecipe.AddRecipeToCalendar);
    GMI.AddRecipe.UserKey = $('span.uk').html();
});
