﻿
function RemoveRecipe_Click(recipeBoxId, recipeId, containerId) {
    var data = new Object();
    data.RecipeBoxId = recipeBoxId;
    data.RecipeId = recipeId;
    CallWebService(wsRecipeBox, "RemoveRecipeFromRecipeBox", data,
function() {
    var objLink = $("a[recipeboxid='" + recipeBoxId + "']").fadeOut();
    objLink.attr("count", parseInt(objLink.attr("count")) - 1);
    BuildRecipeBoxLink(objLink);
    objLink.fadeIn();
    $("#" + containerId).parents("tr").fadeOut(600,
function() {
    if (objLink.attr("count") == "0") {
        setTimeout(function() { window.location = window.location; }, 250);
    }
}
);
}
, null);
}
function RemoveRecipeBox_Click(recipeBoxId, containerId, imgLoadingId) {
    var yes = confirm("Are you sure you want to delete this folder?");
    if (!yes) { return; }
    $("#" + imgLoadingId).fadeIn(500);
    var data = new Object();
    data.RecipeBoxId = recipeBoxId;
    CallWebService(wsRecipeBox, "RemoveRecipeBox", data,
function() {
    $("#" + imgLoadingId).fadeOut(500,
function() {
    $("#" + containerId).hide("drop", { direction: "right" }, 1000,
function() {
    var objRemovedLink = $("a[recipeboxid='" + recipeBoxId + "']");
    $(this).remove();
    if (objRemovedLink.attr("class") == "current" || GetRecipeBoxLinkCount() == 0) {
        window.location = "Recipe-Box.aspx";
        return;
    }
    RebindRecipeBoxes();
}
);
}
);
}
, null);
}
function GetRecipeBoxLinkCount() {
    return $("a[recipeboxid]").length;
}
function AddNewRecipeBox_Click(textboxId, textboxErrorId, containerId, thankyouId, labelFolderNameId, errorId, imgLoadingId) {
    var objTextbox = $('#' + textboxId);
    var iRecipeBoxId = 0;
    if ($.trim(objTextbox.val()) == "") {
        objTextbox.attr("class", "inputError");
        $('#' + textboxErrorId).fadeIn(500);
        return;
    }
    objTextbox.val(Sanitize(objTextbox.val()))
    var data = new Object();
    data.RecipeBoxName = objTextbox.val();
    $("#" + imgLoadingId).fadeIn();
    CallWebService(wsRecipeBox, "AddNewRecipeBox", data,
function(iRecipeBoxId) {
    $("#" + imgLoadingId).fadeOut();
    if (iRecipeBoxId == 0) {
        $("#" + containerId).fadeOut(function() {
            $("#" + errorId).fadeIn();
        });
        return 0;
    }
    BuildNewRecipeBoxItem(iRecipeBoxId, data.RecipeBoxName, 0);
    if (GetRecipeBoxLinkCount() > 0) {
        $("#spNoRecipeBoxes").fadeOut().remove();
    }
    $('#' + labelFolderNameId).text(objTextbox.val());
    objTextbox.val('');
    RebindRecipeBoxes();
    $("#" + containerId).fadeOut(function() {
        $("#" + thankyouId).fadeIn();
    });
}
, null);
    return iRecipeBoxId;
}
function RefreshCurrentRecipeBox(hlRecipeBoxId, spCurrentRecipeBoxNameId) {
    var hl = $("#" + hlRecipeBoxId);
    if (hl.length == 0) return;
    $.RefreshView("/Meals-Tools/vwRecipeBoxRecipes.aspx?recipebox_id=" + hl.attr("recipeboxid") + "&page=", false);
    if ($("#" + spCurrentRecipeBoxNameId).length == 0) { return; }
    $("#" + spCurrentRecipeBoxNameId).fadeOut(500,
function() {
    var objCurrentLinks = $("a[id*='hlRecipeBox'][class='current'][recipeboxid!='" + hl.attr("recipeboxid") + "']");
    objCurrentLinks.each(
function() {
    $(this).removeClass();
}
);
    if (hl.attr("recipeboxid") != '')
        $("a[recipeboxid='" + hl.attr("recipeboxid") + "']").attr("class", "current");
    $(this).text(hl.attr("boxname")).fadeIn(500);
}
);
}
function RebindRecipeBoxes() {
    if ($("#tblRecipes select").length <= 0) return;
    CallWebService(wsRecipeBox, "GetAllRecipeBoxes", null,
function(colRecipeBoxes) {
    $("#tblRecipes select").each(
function() {
    var objDropdown = $(this);
    objDropdown.empty("option").append("<option value=\"\">Select a Folder</option>");
    $.each(colRecipeBoxes,
function(i, rb) {
    var objItem = $("<option/>");
    if (rb.RecipeBoxName.length > 15)
        rb.RecipeBoxName = rb.RecipeBoxName.substring(0, 15) + "...";
    objItem.attr("value", rb.RecipeBoxId).text(rb.RecipeBoxName);
    objDropdown.append(objItem);
}
);
    objDropdown.removeAttr("disabled");
}
);
}
, null);
}
function GetRecipeBoxList(recipeId) {
    var sRecipeBoxDropdown = "<select id='ddlRecipeBoxes" + recipeId + "'><option value=''>Select a Folder</option>";
    $("div.links a[id*='hlRecipeBox']").each(
function() {
    sRecipeBoxDropdown += "<option value='" + $(this).attr("recipeboxid") + "'>" + $(this).attr("boxname") + "</option>";
}
);
    return sRecipeBoxDropdown + "</select>";
}
function AddRecipeToRecipeBox_Click(ddlRecipeBoxesId, txtRecipeBoxNameId, txtRecipeBoxNameErrorId, recipeId, containerId, thankYouId, labelFolderNameId, imgLoadingId, errorId) {
    var recipeBoxId = $('#' + ddlRecipeBoxesId + ' option:selected').val();
    var recipeBoxName = $('#' + ddlRecipeBoxesId + ' option:selected').text();
    if (recipeBoxId == "") {
        recipeBoxName = $.trim($('#' + txtRecipeBoxNameId).val());
        if (recipeBoxName == "") {
            $('#' + txtRecipeBoxNameErrorId).fadeIn(500);
            $('#' + txtRecipeBoxNameId).attr("class", "inputError");
            return;
        }
        recipeBoxId = 0;
    }
    var data = new Object();
    data.RecipeBoxId = recipeBoxId;
    data.RecipeId = recipeId;
    data.RecipeBoxName = recipeBoxName;
    _sRecipeBoxName = recipeBoxName;
    $("#" + imgLoadingId).fadeIn(500,
function() {
    CallWebService(wsRecipeBox, "AddRecipeToRecipeBox", data,
function(recipeBoxId) {
    if (recipeBoxId == 0) {
        $("#" + containerId).fadeOut(function() {
            $("#" + errorId).fadeIn();
        });
        return 0;
    }
    $("#" + txtRecipeBoxNameId).val("");
    $("#" + imgLoadingId).fadeOut(500,
function() {
    var hlRecipeBox = $("a[recipeboxid='" + recipeBoxId + "']");
    if (hlRecipeBox.length == 0) {
        BuildNewRecipeBoxItem(recipeBoxId, data.RecipeBoxName, 0);
        RebindRecipeBoxes();
        hlRecipeBox = $("a[recipeboxid='" + recipeBoxId + "']");
    }
    if (hlRecipeBox.length > 0) {
        hlRecipeBox.fadeOut(1000,
function() {
    hlRecipeBox.attr("count", parseInt(hlRecipeBox.attr("count")) + 1);
    BuildRecipeBoxLink(hlRecipeBox);
    hlRecipeBox.fadeIn(1000);
}
);
    }
    $("#" + labelFolderNameId).text(recipeBoxName);
    $("#" + containerId).fadeOut(function() {
        $("#" + ddlRecipeBoxesId).remove();
        $("#" + thankYouId).fadeIn();
    });
}
);
}
, null);
}
);
}
function RenameRecipeBox_Click(recipeBoxId, textbox, hlRecipeBoxId, containerId, imgLoadingId) {
    var data = new Object();
    var objTextbox = $(textbox);
    objTextbox.val($.trim(Sanitize(objTextbox.val())));
    if (objTextbox.val() == "") {
        $("#spEditBoxRequireName").fadeIn(500);
        objTextbox.attr("class", "inputError");
        return;
    }
    data.RecipeBoxId = recipeBoxId;
    data.NewRecipeBoxName = objTextbox.val();
    $('#' + imgLoadingId).fadeIn(750);
    $('#' + hlRecipeBoxId).fadeOut(750,
function() {
    CallWebService(wsRecipeBox, "RenameRecipeBox", data,
function() {
    RebindRecipeBoxes();
    $('#' + hlRecipeBoxId).attr("boxname", objTextbox.val());
    BuildRecipeBoxLink($('#' + hlRecipeBoxId));
    objTextbox.val('');
    $('#' + imgLoadingId).fadeOut(750,
function() {
    $('#' + hlRecipeBoxId).fadeIn(750,
function() {
    if ($("#" + hlRecipeBoxId).attr("class") == "current") {
        $("#spCurrentRecipeBoxName").fadeOut(
function() {
    $(this).text(data.NewRecipeBoxName).fadeIn();
}
);
    }
}
);
}
);
}
, null);
    $("#" + containerId).fadeOut(750,
function() {
    objTextbox.text("");
}
);
}
);
}
function BuildRecipeBoxLink(objLink) {
    var name =
$(objLink).text($(objLink).attr("boxname") + " (" + $(objLink).attr("count") + ")");
}
function BuildEditRecipeBox(containerId, recipeBoxId, hlRecipeBoxId, imgLoadingId) {
    if ($("div [id^='divEditRecipeBoxItem']").length > 0) {
        $("div [id^='divEditRecipeBoxItem']").each(
function() {
    $(this).slideToggle(500, function() { $(this).remove() });
}
)
    }
    var divName = "divEditRecipeBoxItem" + recipeBoxId;
    var divMain = $("#MockdivEditRecipeBoxItem").clone(true).attr("id", divName);
    $("#" + containerId).append(divMain);
    divMain.slideToggle(500,
function() {
    $("#" + divName + " #hlEditCloseWindow").click(function() { ToggleRemove(divName) });
    $("#" + divName + " #hlEditCancel").click(function() { ToggleRemove(divName) });
    $("#" + divName + " #hlEditFolder").click(
function() {
    RenameRecipeBox_Click(recipeBoxId, "#" + divName + " #txtNewRecipeBoxName", hlRecipeBoxId, divName, imgLoadingId);
}
);
}
);
}
function ToggleRemove(containerId) {
    $("#" + containerId).slideToggle(
function() {
    $(this).remove()
}
);
}
function GetImage(src, onmouseover, onmouseout, alt) {
    return $(new Image()).attr("src", src).attr("onmouseover", "this.src='" + onmouseover + "'").attr("onmouseout", "this.src='" + onmouseout + "'").attr("alt", alt);
}
function BuildNewRecipeBoxItem(iRecipeBoxId, sRecipeBoxName, recipeCount) {
    if ($("#divRecipeBoxes").length <= 0) return;
    if (sRecipeBoxName.length > 15)
        sRecipeBoxName = sRecipeBoxName.substring(0, 15);
    var divRecipeBoxItem = $("<div></div>")
.attr("id", "divRecipeBoxItem" + iRecipeBoxId)
.attr("class", "recipeboxitem");
    var divFolderPop = $("<div></div>")
.attr("id", "divFolderPop" + iRecipeBoxId)
.attr("class", "folderpopcontain");
    var hlRecipeBox = $("<a>" + sRecipeBoxName + " (" + recipeCount + ")</a>")
.attr("onclick", "RefreshCurrentRecipeBox('hlRecipeBox" + iRecipeBoxId + "','spCurrentRecipeBoxName'); return false;")
.attr("recipeboxid", iRecipeBoxId)
.attr("count", recipeCount)
.attr("boxname", sRecipeBoxName)
.attr("href", "/Meals-Tools/Recipe-Box.aspx?recipebox_id=" + iRecipeBoxId)
.attr("id", "hlRecipeBox" + iRecipeBoxId);
    var imgLoading = new Image();
    $(imgLoading).attr("src", "/app_themes/tools/images/recipebox/loader.gif")
.attr("width", "12")
.attr("height", "12")
.attr("style", "display:none; position:absolute; margin:0 0 0 105px;")
.attr("alt", "Loading...")
.attr("id", "imgLoading" + iRecipeBoxId);
    var imgDelete = $("<img>")
.attr("src", "/App_Themes/Tools/images/recipebox/remove_folder_icon.gif")
.click(
function() {
    RemoveRecipeBox_Click(iRecipeBoxId, "divRecipeBoxItem" + iRecipeBoxId, "imgLoading" + iRecipeBoxId);
}
);
    var imgEdit = $("<img>")
.attr("src", "/App_Themes/Tools/images/recipebox/edit_foloder_icon.gif")
.click(
function() {
    BuildEditRecipeBox("divFolderPop" + iRecipeBoxId, iRecipeBoxId, "hlRecipeBox" + iRecipeBoxId, "imgLoading" + iRecipeBoxId);
}
);
    var pImageContainer = $("<p></p>").attr("class", "editlinks").append(imgDelete).append(" ").append(imgEdit);
    var pLinkContainer = $("<p></p>").attr("class", "folderlink").append(imgLoading).append(hlRecipeBox);
    divRecipeBoxItem.append(pImageContainer).append(pLinkContainer).show("drop", { direction: "left" }, 1000);
    divRecipeBoxItem.insertBefore('#divRecipeBoxes');
    divFolderPop.insertBefore('#divRecipeBoxes');
}
function ShowAddToFolder(thankyouId, recipeId, lblAddToRecipeBoxId, containerId) {
    var ddl = $('#ddlRecipeBoxes' + recipeId);
    if (ddl.length != 0) {
        ddl.remove();
    }
    var thankyou = $('#' + thankyouId);
    if (thankyou.css('display') != 'none') {
        thankyou.slideToggle(1000);
    }
    $('#' + lblAddToRecipeBoxId).after(GetRecipeBoxList(recipeId));
    $('#' + containerId).slideToggle(1000);
    return false;
}