var Alerts = (function () {
    function Alerts() {
        var _this = this;
        this.cycleTimeForAllAlerts = 0;
        this.$alertsBox = $('#alertsBox');
        this.$alertsBoxBody = this.$alertsBox.find('ul');
        this.forceAlertRefresh = function () {
            _this.getAlerts(_this.updateAlerts, _this.updateGlobalEmergencyPopup);
        };
        this.getAlerts = function (updateAlerts, updateEmergencyAlert) {
            var langSelected = Cookies.get("_culture") ? Cookies.get("_culture") : "";
            $.ajax({
                url: '/Alert/GetUpdatedAlerts?lang=' + langSelected,
                dataType: 'json'
            }).done(function (data) {
                if (data.alerts) {
                    updateAlerts(data.alerts);
                }
                if (data.emergencyAlertHash !== 0) {
                    $('.emergencyPopupNotification').show();
                    $('footer').addClass('pad45');
                    if (Cookies.get('emergencyPopup') != data.emergencyAlertHash) {
                        Cookies.set('emergencyPopup', data.emergencyAlertHash, { expires: 365, path: '/' });
                        updateEmergencyAlert();
                    }
                }
                else {
                    $('footer').removeClass('pad45');
                    $('.emergencyPopupNotification').hide();
                    Cookies.remove("emergencyPopup", { path: "/" });
                }
            });
        };
        this.updateAlerts = function (alerts) {
            var easyTicker = _this.$alertsBox.data('easyTicker');
            if (easyTicker) {
                easyTicker.stop();
            }
            ;
            var $this = _this;
            _this.$alertsBoxBody.empty();
            if (alerts.length > 0) {
                if (resources.UseAlertBellIcon != "true") {
                    var counter = 0;
                    var cookiecheck = Cookies.get('_culture');
                    var region = Cookies.get('_region') ? Cookies.get('_region') : "ALL";
                    var culture = (cookiecheck != null) ? cookiecheck.toLowerCase() : 'en';
                    culture = (culture.indexOf('-') == -1) ? culture : culture.split("-")[0];
                    var styleClass = '""';
                    $.each(alerts, function () {
                        if (!this.filterRegions || (region == "ALL" || this.regions.indexOf(region) >= 0)) {
                            var messageLang;
                            if (culture === 'fr') {
                                messageLang = this.messages.messageLang2;
                            }
                            else if (culture === 'es') {
                                messageLang = this.messages.messageLang3;
                            }
                            messageLang = messageLang != null && messageLang.message != "" ? messageLang : this.messages.messageLang1;
                            var message = messageLang.message;
                            if (message) {
                                if (this.highImportance)
                                    styleClass = "highImportanceAlert";
                                else
                                    styleClass = "";
                                if (resources.FloodgateAlertBarAsLink === "true") {
                                    $this.$alertsBoxBody.append('<li id="alertsLi' + counter + '" data-type="' + styleClass + '"><a href="/List/Alerts">' + message + '</a></li>');
                                }
                                else {
                                    $this.$alertsBoxBody.append('<li id="alertsLi' + counter + '" data-type="' + styleClass + '">' + message + '</li>');
                                }
                            }
                            counter++;
                        }
                    });
                    _this.splitLongAlerts();
                    _this.cycleTimeForAllAlerts = $this.$alertsBoxBody[0].childElementCount * globalVars.tickerInterval;
                    if (_this.cycleTimeForAllAlerts > _this.currentFetchAlertsFreqTime) {
                        clearInterval(_this.fetchAlertsInterval);
                        _this.currentFetchAlertsFreqTime = _this.cycleTimeForAllAlerts + 1000;
                        _this.fetchAlertsInterval = setInterval(function () {
                            _this.forceAlertRefresh();
                        }, _this.currentFetchAlertsFreqTime);
                    }
                    $('#emptyAlertLi').remove();
                    $this.toggleAlertColor();
                    if ($this.$alertsBoxBody.children().length > 1 && easyTicker) {
                        easyTicker.start();
                    }
                    else {
                        $this.$alertsBoxBody.append('<li id="emptyAlertLi">&nbsp;</li>');
                    }
                }
                else {
                    $(".alertIcon").attr("title", resources.Alerts);
                    $(".alertIcon .dot").show();
                }
            }
            else {
                if (resources.UseAlertBellIcon != "true") {
                    $this.$alertsBoxBody.append('<li id="emptyAlertLi">&nbsp;</li>');
                    $this.toggleAlertColor();
                }
                else {
                    $(".alertIcon").attr("title", resources.NoAlert);
                    $(".alertIcon .dot").hide();
                }
            }
        };
        this.updateGlobalEmergencyPopup = function () {
            var langSelected = Cookies.get("_culture") ? Cookies.get("_culture") : "";
            $.ajax({
                url: '/Alert/GetEmergencyAlert?lang=' + langSelected,
                dataType: 'json'
            }).done(function (data) {
                $('.emergencyPopupNotification').attr('data-alert', data.content);
                if (data.content.length > 0 && $(".modal.in").length == 0) {
                    _this.showPopup(data.content);
                }
            });
        };
        this.showPopup = function (content) {
            bootbox.dialog({
                title: '<i style="padding-right: 5px;" class="far fa-exclamation-triangle"></i> ' + resources.EmergencyAlertPopUp.toUpperCase(),
                onEscape: true,
                className: 'emergencyAlertModal',
                message: content
            });
        };
        this.toggleAlertColor = function () {
            if (_this.$alertsBoxBody.children().first().data("type") == "highImportanceAlert") {
                $(".scrollRow").addClass("highImportanceAlert");
            }
            else {
                $(".scrollRow").removeClass("highImportanceAlert");
            }
        };
        this.startEasyTicker();
    }
    Alerts.prototype.splitLongAlerts = function () {
        var $this = this;
        this.$alertsBoxBody.children().each(function (index) {
            var clientHeight = $this.$alertsBox.get(0).clientHeight;
            var dataType = $(this).attr("data-type");
            var s = $(this).text();
            var smallerRows = "";
            var words = s.split(' ');
            var parts = [];
            var part = "";
            var partCounter = 0;
            words.forEach(function (word) {
                part += !part ? word : " " + word;
                $(".tempAlert").html(part);
                var isOverflow = $(".tempAlert").height() > clientHeight;
                if (isOverflow) {
                    part = part.substring(0, part.lastIndexOf(" "));
                    parts.push({ key: partCounter, value: part });
                    part = word;
                    partCounter++;
                }
            });
            parts.push({ key: partCounter, value: part });
            parts.forEach(function (p) {
                if (p.value.length > 0) {
                    smallerRows += '<li id="alertsLi' + index + '_' + p.key + '" data-type="' + dataType + '">' + p.value + '</li>';
                }
            });
            $(this).replaceWith(replaceUrlsWithLinks(smallerRows));
        });
    };
    Alerts.prototype.hashCode = function (str) {
        var hash = 0;
        for (var i = 0; i < str.length; i++) {
            hash = ~~(((hash << 5) - hash) + str.charCodeAt(i));
        }
        return hash;
    };
    Alerts.prototype.startEasyTicker = function () {
        $('.siteWideAlerts').easyTicker({
            direction: 'up',
            easing: 'swing',
            speed: 'slow',
            interval: globalVars.tickerInterval,
            height: 'auto',
            visible: 1,
            mousePause: 1,
            controls: {
                up: '',
                down: '',
                toggle: '',
                playText: 'Play',
                stopText: 'Stop'
            }
        });
        $('.siteWideAlerts').on('contentChanged', this.toggleAlertColor);
    };
    return Alerts;
}());
$(document).ready(function () {
    var alerts = new Alerts();
    alerts.forceAlertRefresh();
    alerts.currentFetchAlertsFreqTime = resources.AlertRefreshInterval ? parseInt(resources.AlertRefreshInterval) : 60000;
    alerts.fetchAlertsInterval = setInterval(function () {
        alerts.forceAlertRefresh();
    }, alerts.currentFetchAlertsFreqTime);
    $(".emergencyPopupNotification").on("click", function (event) {
        if (!$('.emergencyPopupNotification').attr('data-alert')) {
            alerts.updateGlobalEmergencyPopup();
        }
        else {
            alerts.showPopup($('.emergencyPopupNotification').attr('data-alert'));
        }
    });
    $(window).on('regionChanged orientationchange', function (evt, information) {
        alerts.forceAlertRefresh();
    });
});

