
if (!window['$hnj'] || !$hnj.registry.included('/scripts/components/webRecipeSearchComponent.js')) { 
$hni.components.webRecipeSearchComponent = function (elementId, pagingComponentId, performSearchCallbackUrl) {
    var base = new $hni.components.manager(elementId);
    var currentPage = 1;

    var displayResults = function (results) {
        var container = $hnj('#' + elementId + 'SearchResults');
        var parentContainer = $hnj('#' + elementId);
        container.empty();
        if (results.InitialState == true) {
            updateResultsCount(0);
            $hnj('#' + elementId + 'ResultsInitial').show();
            $hnj('#' + pagingComponentId).hide();
            $hnj('#' + elementId + 'RefineResults').hide();
            parentContainer.unblock();
            return;
        }
        else if (results == undefined || results.Items.length == 0) {
            updateResultsCount(0);
            $hnj('#' + elementId + 'NoResults').show();
            $hnj('#' + pagingComponentId).hide();
            $hnj('#' + elementId + 'RefineResults').hide();
            $hnj('#' + elementId + 'ResultsInitial').hide();
            parentContainer.unblock();
            return;
        }
        $hnj('#' + elementId + 'ResultsInitial').hide();
        $hnj('#' + elementId + 'NoResults').hide();
        $hnj('#' + elementId + 'RefineResults').show();
        updateResultsCount(results.TotalNumberOfItems);
        for (var i = 0; i < results.Items.length; i++) {
            var collectionTitle = '';
            collectionTitle = (results.Items[i].Result.CollectionName != '' && results.Items[i].Result.CollectionName != undefined) ? ' (from ' + results.Items[i].Result.CollectionName + ')' : '';
            var budgetFriendly = results.Items[i].Result.IsBudgetFriendly ? '<span class="hni_IconBudget" title="Budget Friendly">&#160;&#160;</span>' : '';
            var heartHealthy = results.Items[i].Result.IsHeartHealthy ? '<span class="hni_IconHeart" title="Heart Healthy">&#160;&#160;</span>' : '';
            var quickAndEasy = results.Items[i].Result.IsQuickEasy ? '<span class="hni_IconEasy" title="Quick & Easy">&#160;&#160;</span>' : '';
            if (results.Items[i].Link != null) {
                container.append('<div class="hni_RecipeItemContainer"><div class="hni_RecipeIcons">' + budgetFriendly + heartHealthy + quickAndEasy + '</div><div class="hni_RecipeResultItem"><a href="' + results.Items[i].Link + '">' + results.Items[i].Result.Title + collectionTitle + '</a></div></div>');
            }
        }
        $hni.components.manager.get(pagingComponentId).init(results.TotalNumberOfItems, 1);
        parentContainer.unblock();
    };

    var updateResultsCount = function (resultCount) {
        var resultsTab = $hnj('#' + elementId + 'ViewResults');
        resultsTab.empty().append('View Results (' + resultCount + ')');
        var searchResults = $hnj('#' + elementId + 'Results');
        searchResults.empty().append('(' + resultCount + ') Search Results:');
    }

    var findSelectedCheckboxes = function (markSelectedOnly) {
        var allSelected = [];
        var count = 0;
        $hnj('.hni_RecipeSearchCategoryContainer').each(function () {
            count++;
            var selected = [];
            $hnj(this).find('input').each(function () {
                if ($hnj(this).attr('checked')) {
                    selected.push(this.id);
                }
            });
            var categorySelected = $hnj('#' + 'categorySelected' + count);
            categorySelected.empty();
            if (selected.length > 0) {
                categorySelected.append('(' + selected.length + ' selected)');
            }
            allSelected.push(selected);
        });
        if (markSelectedOnly) {
            return;
        }

        allSelected = $hnj.toJSON(allSelected);
        $hnj.cookie("selections", allSelected);
        return allSelected;
    }

    return $hnj.extend(
		base,
		{
		    initialSearch: function () {
		        var selectionsStr = $hnj.evalJSON($hnj.cookie("selections"));
		        if (selectionsStr != undefined) {
		            for (var i = 0; i < selectionsStr.length; i++) {
		                if (selectionsStr[i] != "") {
		                    this.addSelection(selectionsStr[i]);
		                }
		            }
		            findSelectedCheckboxes(true);
		            var storedPage = $hnj.cookie("searchSelectedPage");
		            if (storedPage != undefined) {
		                this.gotoPage(parseInt(storedPage));
		            }
		            else {
		                this.performSearch($hnj.cookie("selections"));
		            }
		        }
		    },

		    addSelection: function (conceptIDList) {
		        for (var i = 0; i < conceptIDList.length; i++) {
		            $hnj('#' + conceptIDList[i]).attr('checked', true);
		        }
		    },

		    performSearch: function (selectedItems) {
		        $hnj.cookie("searchSelectedPage", null);
		        //json selectedItems to the server
		        var container = $hnj('#' + elementId);
		        container.block();
		        var callbackUrl = performSearchCallbackUrl
		            .replace('$0', selectedItems)
		            .replace('$1', 1);
		        $hnj.ajax2.getJSONP(
					callbackUrl,
					function (searchResults) {
					    displayResults(searchResults);
					});
		    },

		    toggleSelection: function () {
		        this.performSearch(findSelectedCheckboxes(false));
		    },

		    toggleCategory: function (categoryID) {
		        $hnj('#' + 'searchContainer' + categoryID).toggleClass('alternate');
		        $hnj('#' + 'categoryArrow' + categoryID).toggleClass('alternate');
		    },

		    refineSearch: function () {
		        $hnj.scrollTo($hnj('#' + elementId), 500);
		    },

		    viewResults: function () {
		        $hnj.scrollTo($hnj('#' + elementId + 'Results'), 500);
		    },

		    gotoPage: function (page) {
		        $hnj.cookie("searchSelectedPage", page);
		        var callbackUrl = performSearchCallbackUrl
					.replace('$0', findSelectedCheckboxes(false))
		            .replace('$1', page);
		        var container = $hnj('#' + elementId);
		        container.block();
		        $hnj.ajax2.getJSONP(
					callbackUrl,
					function (results) {
					    displayResults(results);
					    $hni.components.manager.get(pagingComponentId).init(results.TotalNumberOfItems, page);
					    currentPage = page;
					});
		        $hnj.scrollTo($hnj('#' + elementId + 'Results'), 500);
		    }
		});
};

}
if (window['$hnj']) { $hnj.registry.register('/scripts/components/webRecipeSearchComponent.js', false); };


