﻿function returnAction(action) {
    var passbackModal = action;
    if (action != null) {
        var linkId = action.substring(0, action.indexOf("_"));
        if (action == "save_modal") {
            showWindows($("#" + action), $("#" + linkId + "_topbar"), $("a.modalLink[rel=" + linkId + "]"));
            saveRecipe();
        } else if (action == "email") {
            showWindows(null, $("#" + action + "_topbar"), null);
            populateEmailTopBar()
        } else if (action == "review_modal") {
            showWindows($("#" + action), $("#" + linkId + "_topbar"), $("a.modalLink[rel=" + linkId + "]"));
        }
    }
}

function dotNETConverter(json) {
    var jsonstr = "{";
    for (var i = 0; i < json.length; i++) {
        jsonstr += "\"" + json[i].name + "\":\"" + json[i].value + "\",";
    }
    jsonstr += "}";
    jsonstr = jsonstr.replace(",}", "}");
    return jsonstr;
}
function closeTopBar(topbar) {
    $(topbar).parent().animate({ height: "20px" }).find("a.close").hide().end().find("div:visible").hide();
    $(topbar).parent().find("div#default_topbar").show();
    if ($(".modals").is(":visible")) {
        $(".modals").animate({ top: $(".modals").position().top - 50 });
    }
}
function showTopBar(topbar) {
    if ($(topbar).parent().height() != 70) {
        $(topbar).parent().animate({ height: "70px" }, function() {
            $(this).find("a.close").show().unbind().click(function() { closeTopBar($(this)); });
        });
        $(".modals").animate({ top: $(".modals").position().top + 50 });
    }
    $(topbar).parent().find("div:visible").hide().end().end().show();
}
function showModal(modal) {
    $(modal).parent().find("div[id*=modal]:visible").hide().end().fadeIn("fast").end().show();
    $(modal).parent().find("a.close").show().unbind().click(function() {
        closeModal(modal);
    });
}
function closeModal(modal) {
    $(modal).parent().find("div[id*=modal]:visible").fadeOut("fast");
    $(modal).parent().fadeOut("fast", function() {
        if ($("img.imgButton[src*=_green]").size() > 0) {
            $("img.imgButton[src*=_green]").each(function() {
                $(this).attr("src", $(this).attr("src").replace(/_green/ig, ""));
            });
        }
    });
}
function showWindows(modal, topbar, link) {
    if ($(modal).size() > 0 && modal != null) {
        var modalX = $(link).position().left + $(link).width() / 3;
        var modalY = $(link).position().top + 2 + $(link).height();
        $(modal).parent().css({ "left": modalX, "top": modalY });
        showModal(modal);
    }
    if ($(topbar).size() > 0 && topbar != null) {
        showTopBar(topbar);
    }
}

function submitAJAX(dataStr, destination, successFunction, failureFunction) {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: destination,
        data: dataStr,
        dataType: "json",
        success: successFunction,
        error: failureFunction
    });
}

function highlightButton(button) {
    var imgsrc = $(button).children("img").attr("src");
    if (!imgsrc.match(/_green/)) {
        $(button).children("img").attr("src", imgsrc.replace(/.gif$/ig, "_green.gif"));
    }
}

function fadeButton(button) {
    var imgsrc = $(button).children("img").attr("src");
    if (imgsrc.match(/_green/) && !$("#" + $(button).attr("rel") + "_modal").is(":visible")) {
        $(button).children("img").attr("src", imgsrc.replace(/_green/ig, ""));
    }

}

function toggleBigSmall(button) {
    var imgsrc = $(button).children("img").attr("src");
    if (imgsrc.match(/bigger/)) {
        $(button).children("img").attr("src", imgsrc.replace(/bigger/ig, "smaller"));
    }
    else {
        $(button).children("img").attr("src", imgsrc.replace(/smaller/ig, "bigger"));
    }
}

function OpenPrintWindow(recipeId, text) {

    var queryString = '/Recipes/RecipePrint.aspx?id=' + recipeId;
    var review = '&revs=';
    var txt = '&text=' + text;
    $("input:checkbox").each(function() {
        if (this.value != null && this.id.indexOf("printcheckbox") > -1 && this.checked) {
            if (review.length > 6) {
                review += ',';
            }
            review += this.value;
        }
    });
    queryString += review;
    queryString += txt;
    window.open(queryString, "PrintRecipe", "location=1,status=1,scrollbars=1,resizable=1,toolbar=1,directories=1,width=750");
}
