var Search;
(function (Search) {
    var SearchTextBox = /** @class */ (function () {
        function SearchTextBox(textboxOptions) {
            this.searchSuggestionsCount = 0;
            this.contentType = "application/json; charset=utf-8";
            this.options = textboxOptions;
            this.bindEvents();
        }
        SearchTextBox.prototype.bindEvents = function () {
            var me = this;
            // Only submit the form if it's not empty.
            $("#" + me.options.searchFormId).submit(function () {
                // Encode content before submiting the form so the symbol '&' could be entered.
                var textboxContent = encodeURIComponent($("#" + me.options.textBoxId).val());
                $("#" + me.options.textBoxId + "_hidden").val(textboxContent);
                return !me.isSearchTextboxEmpty();
            });
        };
        SearchTextBox.prototype.initAutocomplete = function () {
            $("#" + this.options.autoCompleteDivId).empty();
            $("#" + this.options.autoCompleteDivId + " .search-ul-autocomplete").empty();
            $("#" + this.options.autoCompleteDivId).append(this.options.ulInjected);
            $("#" + this.options.autoCompleteDivId).hide();
            this.searchSuggestionsCount = 0;
        };
        SearchTextBox.prototype.searchAutocomplete = function (options) {
            var me = this;
            me.searchSuggestionsCount = 0;
            // Options.
            var settings = $.extend({
                numberOfBestBets: 3,
                numberOfKeyword: 3,
                titleLength: 15,
                summaryLength: 40
            }, options);
            // This is the selector itself.
            $('#' + me.options.searchFormId + ' input').keyup(function (e) {
                var code = e.keyCode;
                // do nothing if it's an arrow key.
                if ([37, 38, 39, 40].indexOf(code) >= 0) {
                    return;
                }
                // Evaluation of the state of pending requests.
                if (typeof (me.ajaxRequest) !== 'undefined') {
                    me.ajaxRequest.abort();
                }
                // If intro key.
                if (code === 13 && !me.isSearchTextboxEmpty()) {
                    $("#" + me.options.searchFormId + " button").click();
                    return;
                }
                me.processWaitingMessage(true);
                // If the textbox isn´t empty the search starts. If empty the results are cleared.
                if ($(this).val() !== '' && $(this).val().length > 2) {
                    me.loadBestBetsAsync(encodeURIComponent($(this).val()), settings);
                }
                else {
                    $("#" + me.options.autoCompleteDivId).hide();
                }
                me.processWaitingMessage(false);
            });
            return this;
        };
        // Ajax call to obtain the keywords suggestions that matches with the introduced keyword
        SearchTextBox.prototype.loadBestBetsAsync = function (keyword, settings) {
            var me = this;
            var payload = {};
            payload.SearchedKeyword = keyword;
            var ajaxOptions = {
                url: "/SearchAPI/SearchTextBox/AX_LoadBestBetsSuggestions/" + me.options.pageId,
                type: 'POST',
                dataType: "json",
                async: true,
                data: JSON.stringify(payload),
                contentType: this.contentType,
                success: function (items) {
                    var bestBets = items;
                    for (var i = 0; i < settings.numberOfBestBets; i++) {
                        if (bestBets[i] !== undefined) {
                            me.renderBestBet(bestBets[i], i);
                        }
                        else {
                            $("#" + me.getLiIdentifier("elementBest", i)).remove();
                        }
                    }
                    me.searchSuggestionsCount += bestBets.length;
                    // Disables menu items and personal area to make auto suggestion look good
                    DisableMenuItemsAndPA();
                },
                error: function () {
                }
            };
            me.ajaxRequest = $.ajax(ajaxOptions);
            me.ajaxRequest.done(function () {
                //Load of the bestbets
                me.loadKeywordsAsync(keyword, settings);
            });
        };
        //Ajax call to obtain the bestbets suggestions that matches with the introduced keyword
        SearchTextBox.prototype.loadKeywordsAsync = function (keyword, settings) {
            var me = this;
            var payload = {};
            payload.SearchedKeyword = keyword;
            var ajaxOptions = {
                url: "/SearchAPI/SearchTextBox/AX_LoadKeywordSuggestions/" + me.options.pageId,
                type: "POST",
                dataType: "json",
                contentType: this.contentType,
                async: true,
                data: JSON.stringify(payload),
                success: function (items) {
                    var keywords = items;
                    for (var i = 0; i < settings.numberOfKeyword; i++) {
                        if (keywords[i] !== undefined) {
                            me.renderKeyword(keywords[i], i);
                        }
                        else {
                            $("#" + me.getLiIdentifier("elementKey", i)).remove();
                        }
                    }
                    me.searchSuggestionsCount += keywords.length;
                },
                error: function () {
                }
            };
            me.ajaxRequest = $.ajax(ajaxOptions);
            me.ajaxRequest.done(function () {
                if (me.searchSuggestionsCount === 0) {
                    $("#" + me.options.autoCompleteDivId).hide();
                    $("#" + me.options.autoCompleteDivId + " .search-ul-autocomplete").empty();
                }
                else {
                    $("#" + me.options.autoCompleteDivId).show();
                }
                me.searchSuggestionsCount = 0;
            });
        };
        SearchTextBox.prototype.renderBestBet = function (bestBet, index) {
            var me = this;
            var li = "";
            var liIdentifier = me.getLiIdentifier("elementBest", index);
            if (bestBet.Thumbnail.trim() !== "") {
                li = me.options.liImageInjected.replace("elementId", liIdentifier);
            }
            else {
                li = me.options.liInjected.replace("elementId", liIdentifier);
            }
            li = li.replace("className", "elementBest");
            li = li.replace("ItemName", bestBet.Title);
            li = li.replace("ItemDescription", "<br />" + bestBet.Summary.substr(0, 80) + "...");
            li = li.replace("IsKeyword", "");
            if (bestBet.Thumbnail.trim() !== "") {
                li = li.replace("ItemThumbnail", bestBet.Thumbnail);
            }
            if ($("#" + liIdentifier).length === 0) {
                if (index > 0) {
                    $(li).insertAfter("#" + me.getLiIdentifier("elementBest", index - 1));
                }
                else {
                    $("#" + me.options.autoCompleteDivId + " .search-ul-autocomplete").prepend(li);
                }
            }
            else {
                $("#" + liIdentifier).html(li);
            }
            $("#" + liIdentifier).attr('onclick', "openLink('" + bestBet.Url + "', " + bestBet.IsUrlExternal + ");");
        };
        SearchTextBox.prototype.renderKeyword = function (keyword, index) {
            var me = this;
            var liIdentifier = me.getLiIdentifier("elementKey", index);
            var li = me.options.liInjected.replace("elementId", liIdentifier);
            li = li.replace("className", "elementKey");
            li = li.replace("ItemName", keyword.Keyword);
            li = li.replace("ItemDescription", "");
            li = li.replace("IsKeyword", "<i class='icon-font icon-search icon--larger'></i>");
            if ($("#" + liIdentifier).length === 0) {
                $("#" + me.options.autoCompleteDivId + " .search-ul-autocomplete").append(li);
            }
            else {
                $("#" + liIdentifier).html(li);
            }
            //Event dinamically created for click event on keywords
            $("#" + liIdentifier).click(function () {
                me.searchKeyword($(this).attr("id"));
            });
        };
        SearchTextBox.prototype.processWaitingMessage = function (show) {
            if (show)
                $("#" + this.options.loadingIndicatorId).show();
            else {
                $("#" + this.options.loadingIndicatorId).hide();
                this.searchSuggestionsCount = 0;
            }
        };
        /* Check if the textbox is empty */
        SearchTextBox.prototype.isSearchTextboxEmpty = function () {
            return $.trim($("#" + this.options.textBoxId).val()) === "";
        };
        /**
        *  Do the search of the selected keyword
        * @param {String} sKeyword The searched keyword
        */
        SearchTextBox.prototype.searchKeyword = function (sKeyword) {
            $("#" + this.options.textBoxId).val($("#" + sKeyword).text());
            $("#" + this.options.searchFormId + " button").click();
        };
        /* Get the li identifier taking into account the options */
        SearchTextBox.prototype.getLiIdentifier = function (identifier, index) {
            if (this.options.useDynamicIdentifiers) {
                return "" + identifier + index + "_" + this.options.searchFormId;
            }
            else {
                return "" + identifier + index;
            }
        };
        return SearchTextBox;
    }());
    Search.SearchTextBox = SearchTextBox;
})(Search || (Search = {}));
/**
 * Open link in the same tab or a new one
 * @param {String} url to open
 * @param {Boolean} inNewTab If it should open on a new tab or not
 */
function openLink(url, inNewTab) {
    var target = (inNewTab === true) ? "_blank" : "_self";
    var win = window.open(url, target);
    if (inNewTab) {
        win.focus();
    }
}
function DisableMenuItemsAndPA() {
    //Pillar Navigation
    var pillarNavigation = document.querySelectorAll('input[data-selector="pillar-navigation"]');
    for (var i = 0; i < pillarNavigation.length; i++) {
        pillarNavigation[i].checked = false;
    }
    var pillar = document.querySelectorAll('.pillar');
    for (var i = 0; i < pillar.length; i++) {
        pillar[i].classList.remove('active');
        pillar[i].classList.remove('inactive');
    }
    // PA DESKTOP//
    var personalAreaNavInput = document.querySelector('input[data-selector="personal-area-navigation"]');
    if (personalAreaNavInput) {
        personalAreaNavInput.checked = false;
    }
    var personalAreaOld = document.querySelector('label[data-selector="desktop-personal-area-menu-navigation"]');
    if (personalAreaOld) {
        personalAreaOld.classList.remove('icon-x');
        personalAreaOld.classList.add('icon-user-2');
        if (personalAreaOld.dataset.logged)
            personalAreaOld.classList.add('personal-area-menu__avatar--logged-in');
    }
}
//# sourceMappingURL=SearchTextbox.js.map;
var Search;
(function (Search) {
    function SearchResultInit() {
        $("#aSeeMore").click(function () {
            $("#divShowMore").slideDown(1600);
            $("#aSeeMore").hide();
        });
        if ($(".hdnFamilyItems").val() === "0") {
            $(".mfp-content").hide();
        }
    }
    Search.SearchResultInit = SearchResultInit;
})(Search || (Search = {}));
//# sourceMappingURL=SearchResult.js.map;
var Search;
(function (Search) {
    function InitSearchStopBox() {
        // Bind buttons to open the second popup
        $(".jq-search-popup").magnificPopup({
            items: {
                src: "#search-popup-info-product",
                type: "inline"
            },
            modal: false,
            closeOnBgClick: true,
            closeBtnInside: true,
            showCloseBtn: true,
            enableEscapeKey: true
        });
    }
    Search.InitSearchStopBox = InitSearchStopBox;
})(Search || (Search = {}));
//# sourceMappingURL=SearchStopBox.js.map;
