﻿/*
Helper object for the recipe search
*/

//This is MS version of document.ready... this however fires after jquery document.ready.
//This is required to bind the add_navigate event, if it is not bound in this method then navigate
//does not occur on page load!
Sys.Application.add_init(function(sender, e) {

    recipeSearchHelper.RecipeSearcher.SetPager({ PageSize: 10 });
    recipeSearchHelper.RecipeSearcher.hasResultContainer = false;

    recipeSearchHelper.historyTracker.SetHandler(recipeSearchHelper, recipeSearchHelper.onPageNavigate);

});

var recipeSearchHelper = {

    RecipeSearcher: new ViewManagedList(),

    historyTracker: new HistoryTracker(),

    notFoundMsg: "<p>Sorry your search returned no results, please try again. There are over 300 recipes available, so try broadening your search criteria.</p>",

    //if user id is set, it will only search user favourites
    searchByFilter: function(userId, productcategory, mealtime, doValidate) {

        //if these are null, set them to the values of the drop down lists
        productcategory = (productcategory == null ? $(".productdropdown option:selected").val() : productcategory);
        mealtime = (mealtime == null ? $(".typedropdown option:selected").val() : mealtime);

        //if its not set, or it is true, do the validation
        if (doValidate == null || doValidate == true)
            Page_ClientValidate();

        if (Page_IsValid) {
            this.RecipeSearcher.SetService(Sunbeam.Web.Services.RecipeScriptService.GetRecipesAndFilter, [productcategory, mealtime, userId, false], ".indexResults");

            //create delegate to maintain scope in our callback.
            var _this = this;
            this.RecipeSearcher.onComplete = function(result) { _this.displaySearchFilterCallback.call(_this, result) };

            this.RecipeSearcher.Load();
            //serviceMethod.call(this, completetime, productcategory, mealtime, this.displaySearchFilterCallback);
            $('.feature').hide();
            $('.recipeList').show();
        }
    },

    //if user id is set, it will only search user favourites
    searchByKeyword: function(userId, keywords) {

        //if its null, get it from the search field
        keywords = (keywords == null ? $(".searchKeywords").val() : keywords);

        this.RecipeSearcher.SetService(Sunbeam.Web.Services.RecipeScriptService.GetRecipesForKeywordSearch, [keywords, userId], ".indexResults");

        //create delegate to maintain scope in our callback.
        var _this = this;
        this.RecipeSearcher.onComplete = function(result) { _this.displaySearchKeywordCallback.call(_this, result) };

        this.RecipeSearcher.Load();
        //serviceMethod.call(this, keywords, this.displaySearchKeywordCallback);
        $('.feature').hide();
        $('.recipeList').show();

    },

    onPageNavigate: function(sender, e) {

        var history = this.historyTracker.GetHistoryByName("s");

        if (history == null) {
            //if it's null then were at the start page!
            $('.feature').show();
            $('.recipeList').hide();
        }
        else if (history.key == "filter" && history.values.length == 4) {
            //do the search
            this.searchByFilter((history.values[2] == "" ? null : history.values[2]), history.values[0], history.values[1], false);
        }
        else if (history.key == "keyword" && history.values.length == 2) {
            //do the search
            this.searchByKeyword((history.values[1] == "" ? null : history.values[1]), history.values[0]);
        }
    },

    //fired after the pager has loaded all the content for  the filter search
    displaySearchFilterCallback: function(result) {

        //Set a history point in the client.
        //combine the values of the drop down lists into array
        var val = new Array(
            this.RecipeSearcher.data[0],
            this.RecipeSearcher.data[1],
            this.RecipeSearcher.data[2],
            this.RecipeSearcher.data[3]);

        this.historyTracker.AddHistory("s", "filter", val);

        //if nothing is found, set the not found message
        if (this.RecipeSearcher.itemCount == 0) {
            $('.indexResults').empty().hide().fadeIn().append(this.notFoundMsg);
            if (val[0] == "3" && val[1] == "15100" && val[2] == "15003" && $(".searchKeywords").val() != "") { $('.indexResults').html("<div class='productItem'><a href='#'><img style='border:0px' src='/Images/layout/megadeathbaby.jpg'/>Pureed steak with megadeath sauce<span>Prep: 60 + mins<br/>Serves: 4-6 mega-babies;</span></a><div class='cleaner'></div></div>") };
        }
    },

    //fired after the pager has loaded all the content for the keyword search
    displaySearchKeywordCallback: function(result) {

        //Set a history point in the client.
        //combine the values of the drop down lists into array
        var val = new Array(
            this.RecipeSearcher.data[0],
            this.RecipeSearcher.data[1]);

        this.historyTracker.AddHistory("s", "keyword", val); 

        if (this.RecipeSearcher.itemCount == 0) {
            $('.indexResults').empty().hide().fadeIn().append(this.notFoundMsg);
        }
    }

}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
