﻿/*
* Inline search for text in page content.
*/
if (!tacoBell.InlineSearchManager) {
    tacoBell.InlineSearchManager = (function(options) {
        var _options, _searchInput, _searchString;
        function _setUp(options) {
            // check for passed _options obj and set defaults
            _options = (typeof options == 'undefined') ? {} : options;
            _options.searchElsContainSelector = (typeof _options.searchElsContainSelector == 'undefined') ? '#ingredSetsContainer' : _options.searchElsContainSelector;
            _options.searchElsClass = (typeof _options.searchElsClass == 'undefined') ? 'ingredSet' : _options.searchElsClass;
            _options.searchInputID = (typeof _options.searchInputID == 'undefined') ? 'ingredsSearch' : _options.searchInputID;
            _searchInput = $('#' + _options.searchInputID);
            _options.searchSubmitID = (typeof _options.searchSubmitID == 'undefined') ? 'ingredsSearchSubmit' : _options.searchSubmitID;
            _options.foundHighlightClass = (typeof _options.foundHighlightClass == 'undefined') ? 'ingredHighlight' : _options.foundHighlightClass;
            // if no results found
            _options.noResultsContainerID = (typeof _options.noResultsContainerID == 'undefined') ? 'ingredsNoResults' : _options.noResultsContainerID;
            _options.noResultsTermID = (typeof _options.noResultsTermID == 'undefined') ? 'noResultsTerm' : _options.noResultsTermID;
            _options.noResultsTextID = (typeof _options.noResultsTextID == 'undefined') ? 'noResultsText' : _options.noResultsTextID;
            _options.noResultsMsg = (typeof _options.noResultsMsg == 'undefined') ? 'was not found in the ingredients list.' : _options.noResultsMsg;
            _addEventHandlers();
        }
        function _addEventHandlers() {
            _searchInput.get(0).blur();
            _searchInput.one('click', _clearSearch);
            // search on enter or clicking magnifying glass icon
            _searchInput.bind('keypress', function(event) {
                // non-IE vs. IE browsers
                var code = (event.keyCode ? event.keyCode : event.which);
                if (code == 13) { //Enter keycode
                    //event.preventDefault();
                    _doSearch();
                    return false;
                }
            });
            $('#' + _options.searchSubmitID).bind('click', _doSearch);
        }
        function _doSearch() {
            _searchString = $('#' + _options.searchInputID).val();
            if (_searchString.length > 0) {
				$(window).trigger('pdp:see_inside:search');
                var searchResults = '.' + _options.searchElsClass + ':containsCI(' + _searchString + ')';
                var searchResultsObj = $(searchResults);
                if (searchResultsObj.length) {
                    searchResultsObj.addClass(_options.foundHighlightClass);
                    if (typeof $.jScrollPane != 'undefined') {
                        // scroll to first result if using jScrollPane
                        var _matchedScroller = false;
                        $.each($.jScrollPane.active, function(index, value) {
                            if ($(value).get(0).id == $(_options.searchElsContainSelector).get(0).id) {
                                _matchedScroller = true;
                                value.scrollTo(searchResults + ':first');
                            }
                            if (!_matchedScroller) {
                                _goToIngredSet(searchResults + ':first');
                            }
                        });
                    } else {
                        _goToIngredSet(searchResults + ':first');
                    }
                } else {
                    if (_options.noResultsTermID.length) {
                        $('#' + _options.noResultsTermID).text(_searchString);
                    }
                    $('#' + _options.noResultsTextID).text(_options.noResultsMsg);
                    $('#' + _options.noResultsContainerID).show();
                }
            }
            _searchInput.get(0).blur();
            _searchInput.one('click', _clearSearch);
        }
        function _goToIngredSet(selector) {
            $('html, body').animate({
                scrollTop: $(selector).offset().top
            }, 100);
        }
        function _clearSearch() {
            _searchInput.attr('value', '');
            $('.' + _options.searchElsClass).removeClass(_options.foundHighlightClass);
            $('#' + _options.noResultsContainerID).hide();
            if (_options.noResultsTermID.length) {
                $('#' + _options.noResultsTermID).text('');
            }
            $('#' + _options.noResultsTextID).text('');
        }
        return {
            setUp: function(options) {
                _setUp(options);
            },
            clearSearch: function() {
                _clearSearch();
            }
        }
    })();
}
