﻿
$(function() {
    $("form").submit(function() {
        return ($("#txtKeywords").data("lastkeypress") == null) ? true : false;
    });
    UpdateContainerHeight();
    $("#searchSimple input").bind("click", function() {
        window.location = '#1';
        UpdateCategories(this);
        UpdateResults(false);
    });
    setInterval(function() {
        var keywords = $('#leftSearchContainer').data('keywords');
        var categories = $('#leftSearchContainer').data('categories');
        if (IsUseCookie()) {
            keywords = (GetLastCookie('keywordslast') == null) ? keywords : GetLastCookie('keywordslast');
            categories = (GetLastCookie('categorieslast') == null) ? categories : eval('[' + GetLastCookie('categorieslast') + ']');
        }
        if (keywords == '') keywords = "Enter Ingredient(s) or Keyword(s)";
        if ($('#leftSearchContainer').data('keywords') != null) {
            $("#txtKeywords").val(keywords);
            $('#leftSearchContainer').data('keywords', null);
        }
        if ($('#leftSearchContainer').data('categories') != null) {
            $("#searchSimple .checkwrap input:checked").each(function(i, o) {
                $(o).removeAttr("checked");
            });
            $.each(categories, function() {
                var objChk = $("#searchSimple .checkwrap input[value='" + this + "']");
                if (objChk.length > 0) {
                    objChk.attr("checked", true);
                }
                else {
                    $("#searchSimple select").val(this);
                }
            });
            $('#leftSearchContainer').data('categories', null);
        }
        if ($("#txtKeywords").data("lastkeypress") == null && $("#leftSearchContainer").data("IsOpen") != true) {
            if ($(".viewinglist input:checked").length == 0) {
                $("#searchSimple .checkwrap input:checked").each(function(i, o) {
                    UpdateCategories(o);
                });
            }
            UpdateResults(true);
        }
        else {
            var sPastView = $("#searchSimple").data("searched");
            if (($("#leftSearchContainer").data("IsOpen") == true) && (sPastView == null || sPastView == "")) {
                $("#searchSimple").data("searched", "/Meals-Tools/vwSearchResults.aspx?ingredients=&categories=&page=");
            }
        }
    }, 100);
});
function Keywords_OnKeyDown() {
    var now = new Date().getTime();
    $("#txtKeywords").data("lastkeypress", now);
    setTimeout(function() {
        if ($("#txtKeywords").data("lastkeypress") == now) {
            UpdateResults(false);
            $("#txtKeywords").data("lastkeypress", null);
        }
    }, 750);
}
function UpdateCategories(oCheck) {
    var catId = $(oCheck).attr("catId");
    var divList = $("#" + catId);
    var objItem = new Object();
    objItem.ChkId = oCheck.value;
    objItem.CatId = catId;
    objItem.Id = oCheck.value;
    objItem.Name = $(oCheck).next().text();
    var collItems = $(divList).data("items");
    if (collItems == null) collItems = new Array();
    if ($(oCheck).attr("checked"))
        collItems.push(objItem);
    else
        collItems = $.grep(collItems, function(o, i) { return o.Id != objItem.Id; });
    $(divList).data("items", collItems);
    $(divList).empty();
    if (collItems.length > 0) {
        collItems = collItems.sort(function(a, b) { return a.Name > b.Name });
        $.each(collItems, function(i, o) {
            $(divList).append("<input type='checkbox' value='" + o.Id + "' checked='checked' onclick=\"RemoveSearch(this);\" chkId='" + o.ChkId + "' catId='" + o.CatId + "' />");
            $(divList).append("<label>" + o.Name + "</label><br />");
        });
    }
    else {
        $(divList).append("<strong>Viewing All</strong>");
    }
    UpdateContainerHeight();
}
function UpdateResults(bDoKeepCurrentPage) {
    var collCatIds = "";
    $("#searchSimple .checkwrap input:checked").each(function(i, o) {
        if (collCatIds != "") collCatIds += ", ";
        collCatIds += o.value;
    });
    var ddlVal = $("#searchSimple select").val();
    if (ddlVal != "") {
        if (collCatIds != "") collCatIds += ", ";
        collCatIds += ddlVal;
    }
    var sKeywords = $("#txtKeywords").val();
    if (sKeywords == "Enter Ingredient(s) or Keyword(s)") sKeywords = "";
    if (!bDoKeepCurrentPage) {
        SetLastCookie('categorieslast', collCatIds);
        SetLastCookie('keywordslast', sKeywords);
        SetLastCookie('searchedlast', new Date().toUTCString());
    }
    collCatIds = escape(collCatIds);
    sKeywords = escape(sKeywords);
    var iNumToReturn = (GetLastCookie("rsrntr") != null) ? GetLastCookie("rsrntr") : 10;
    var sSortKeyValue = ($.query.get("sort") != "") ? "&sort=" + $.query.get("sort") : "";
    var sDirKeyValue = ($.query.get("dir")) != "" ? "&dir=" + $.query.get("dir") : "";
    var sCategoriesKeyValue = (collCatIds != "") ? "&categories=" + collCatIds : "";
    var sKeywordsKeyValue = (sKeywords != "") ? "&ingredients=" + sKeywords : "";
    var sViewUrl = "/Meals-Tools/vwSearchResults.aspx?return=" + iNumToReturn + sKeywordsKeyValue + sCategoriesKeyValue + sSortKeyValue + sDirKeyValue + "&page=";
    var sPastView = $("#searchSimple").data("searched");
    if (sPastView != sViewUrl) {
        $("#searchSimple").data("searched", sViewUrl);
        sPastView = sViewUrl;
        var waitForInit = setInterval(function() {
            if ($._isFirstLoadComplete) {
                $.RefreshView(sViewUrl, bDoKeepCurrentPage);
                clearInterval(waitForInit);
            }
        }, 100);
    }
}
function IsUseCookie() {
    var dtSearchedLast = $('#leftSearchContainer').data('datetime');
    if (GetLastCookie('searchedlast') != null) dtSearchedLast = Date.parse(GetLastCookie('searchedlast'));
    if (dtSearchedLast > $('#leftSearchContainer').data('datetime'))
        return true;
    else
        return false;
}
function ExpandToggle() {
    var opts = new Object();
    if ($("#leftSearchContainer").data("IsOpen")) {
        opts.fadeOut = "searchAdvanced";
        opts.fadeIn = "searchSimple";
        opts.width = 215;
        opts.IsOpen = false;
        $("#leftSearchContainer").data("IsOpen", false);
        CopyAdvancedToSimple();
    }
    else {
        opts.fadeOut = "searchSimple";
        opts.fadeIn = "searchAdvanced";
        opts.width = 735;
        opts.IsOpen = true;
        $("#leftSearchContainer").data("IsOpen", true);
        CopySimpleToAdvanced();
    }
    $(".expandToggle").hide("blind", { direction: "horizontal" }, 300);
    var origHeight = $("#searchExpand").height();
    $("#" + opts.fadeOut).fadeOut(300, function() {
        $("#searchExpand").height(origHeight);
        $("#searchExpand").animate({ width: opts.width + "px" }, 600, "easeOutBounce", function() {
            $("#" + opts.fadeIn).fadeIn(300);
            $("#searchExpand").animate({ height: $("#" + opts.fadeIn).height() + "px" }, 300, function() {
                UpdateContainerHeight();
                $(".expandToggle").show("blind", { direction: "horizontal" }, 300);
            });
        });
    });
}
function UpdateContainerHeight() {
    var absHeight = $("#searchExpand").height();
    $("#leftSearchContainer").height(absHeight);
}
function CopySimpleToAdvanced() {
    $("#searchSimple .checkwrap input:checked").each(function(i, o) {
        $("#searchAdvanced input[value='" + o.value + "']").attr("checked", true);
    });
    $("#txtKeywords2").val($("#txtKeywords").val());
    $("#searchAdvanced select").val($("#searchSimple select").val());
    ClearAllSimpleSelections(false);
}
function CopyAdvancedToSimple() {
    $("#searchAdvanced input:checked").each(function(i, o) {
        $("#searchSimple input[value='" + o.value + "']").attr("checked", true);
    });
    $("#txtKeywords").val($("#txtKeywords2").val());
    $("#searchSimple select").val($("#searchAdvanced select").val());
    ClearAllAdvancedSelections(true);
}
function ClearAllSimpleSelections(bDoUpdateResults) {
    $("#searchSimple input").attr("checked", false);
    $("#searchSimple select").val("");
    $("#searchSimple .viewinglist").empty().append("<strong>Viewing All</strong>");
    $("#txtKeywords").val("Enter Ingredient(s) or Keyword(s)");
    $(".viewinglist").data("items", null);
    if (bDoUpdateResults) UpdateResults(false);
}
function ClearAllAdvancedSelections(bDoUpdateResults) {
    $("#searchAdvanced input").attr("checked", false);
    $("#searchAdvanced select").val("");
    $("#txtKeywords2").val("Enter Ingredient(s) or Keyword(s)");
    if (bDoUpdateResults) UpdateResults(false);
}
function RemoveSearch(oCheck) {
    $("#" + $(oCheck).attr("chkId")).removeAttr("checked");
    UpdateCategories(oCheck);
    UpdateResults(false);
}
function GetLastCookie(name) {
    var val = $.cookie(name);
    if (val == "empty") val = "";
    return val;
}
function SetLastCookie(name, val) {
    if (val == "") val = "empty";
    $.cookie(name, val, { path: '/' });
}