!function(t, e, o) { let i = ()=>window.innerWidth < 925; "use strict"; XF.ChatPopup = XF.ChatPopup || XF.create({ options: { url: "", eventPrefix: "RTC" }, loadingPopup: !1, $container: null, $popup: null, state: "closed", __construct(e) { this.options = t.extend({}, this.options, e), t(o).on("chat:open-popup", (t,e)=>{ (e ||= {}).eventPrefix === this.options.eventPrefix && this.open(e) } ), this.$container = t('
'), this.$container.on("chat:new-message", ".real-time-chat", XF.proxy(this, "onNewMessage")).on("chat:messages-seen", ".real-time-chat", XF.proxy(this, "updateUnreadCountBadge")) }, onNewMessage() { this.$popup && this.getChatTarget().one("chat:room-reloaded", ()=>{ this.updateUnreadCountBadge() } ) }, updateUnreadCountBadge() { if (!this.$popup) return; let t = this.getChatTarget() , e = this.getChat(); if (!e) return; let o = t.find(".js-badgePopup") , i = e.rooms.unreadCount; o.text(i), o.toggleClass("is-hidden", i <= 0 || !this.isCollapsed()) }, isOpened() { return "open" === this.state }, isCollapsed() { return "collapsed" === this.state }, async open(e) { e = t.extend({ query: {}, roomTag: "" }, e); let {query: o, roomTag: i} = e; i ? o.tag = i : this.getRoomTagCookie() && !o.c?.open && (o.tag = this.getRoomTagCookie()), XF.hideTooltips(); let s = ()=>{ this.updateState("open"), this.$container.trigger("chat-popup:open"), this.$popup.one("chat:initialized", ()=>{ let t = this.getChat(); t.roomTag && t.toggleLeftColumn(!1) } ) } ; this.$popup || await this.loadPopup({ query: o }); let p = this.getChatTarget() , a = this.getChat() , n = ()=>{ var t, e; let a = this.getChat(); ((t,e)=>{ e.rooms.creator.$button?.on("slide-menu:toggle", (o,i)=>{ t.hasClass("no-left-column") && e.toggleLeftColumn(!0), i || !e.roomTag || t.hasClass("no-left-column") || e.toggleLeftColumn(!1) } ) } )(p, a), t = p, e = a, i && t.toggleClass("no-left-column", e.rooms.open(i)), o.c?.open && e.rooms.creator.toggleForm(o.c.open), s(this.$popup) } ; a ? n() : p.one("chat:init", ()=>{ n() } ), this.setStateCookie("open"), i && this.setRoomTagCookie(i) }, updatePopupCoords() { let o = { top: parseInt(this.$popup.css("top"), 10), left: parseInt(this.$popup.css("left"), 10) } , i = this.$popup.outerHeight() , s = this.$popup.outerWidth() , p = t(e).width() - s , a = t(e).height() - i , n = o; if (this.isCollapsed()) { let r = this.getCoordsCookie(); r && (n.left = r.left, n.top = r.top) } let l = Math.max(0, Math.min(n.left, p)) , h = Math.max(0, Math.min(n.top, a)); o.top !== h && this.$popup.css("top", h), o.left !== l && this.$popup.css("left", l) }, close() { this.$container.trigger("chat-popup:close"), this.$popup.addClass("is-closing"), setTimeout(()=>{ this.$popup.find(".real-time-chat").trigger("chat:unmounted"), this.$popup.remove(), this.$popup = null, this.setStateCookie("closed") } , 250) }, collapse(e) { if (this.$container.trigger("chat-popup:collapse", [!this.isCollapsed()]), t(e.target).closest(".js-closePopup").length) return; let o = this.isCollapsed() ? "open" : "collapsed"; this.updateState(o), this.updateUnreadCountBadge() }, updateState(t) { let e = this.getChat(); e && (this.state = t, this.$popup.toggleClass("is-collapsed", "collapsed" === t), this.setStateCookie(t), "collapsed" === t ? (e.ignoreNewMessages = !0, e.visible = !1) : (e.ignoreNewMessages = !1, e.visible = !0, e.messages.update(()=>{ let t = e.messages.find({ unread: !0, visitor: !1 }).first(); t.length && (t.addClass("is-highlight"), e.messages.scrollTo(t, "start"), e.messages.setHighlightRemoveTimeout()) } ))) }, onRoomClick(e) { let o = t(e.currentTarget).data("room-tag"); o && this.setRoomTagCookie(o) }, onResetRoomClick() { this.setRoomTagCookie("") }, onDrag(t, e) { this.setCoordsCookie(e) }, restorePositionFromCookie() { let t = this.getCoordsCookie(); t && this.$popup.css(t) }, loadPopup(t) { t = Object.assign({ query: {}, wrapPopupCallback: null, onLoaded: null, global: !0 }, t); let {query: e, wrapPopupCallback: o, onLoaded: i, global: s} = t; return this.$popup ? Promise.resolve() : this.loadingPopup ? Promise.reject("Already loading") : (this.loadingPopup = !0, new Promise((t,p)=>{ XF.ajax("GET", this.options.url, e, ({html: e})=>{ e && e.content && XF.setupHtmlInsert(e, (e,s,p)=>{ p(!0), this.$popup = e, this.$popup.toggleClass("is-collapsed", this.isCollapsed()), o && o(this.$popup), this.$popup.on("click", ".js-closePopup", XF.proxy(this, "close")), this.$popup.on("click", ".js-collapsePopup", XF.proxy(this, "collapse")), this.$popup.on("click", ".js-room", XF.proxy(this, "onRoomClick")), this.$popup.on("click", ".js-resetRoom", XF.proxy(this, "onResetRoomClick")), this.$popup.on("drag", XF.proxy(this, "onDrag")), this.$popup.appendTo(this.$container), this.restorePositionFromCookie(); let a = this.getChatTarget(); a.one("chat:initialized", ()=>{ let t = this.getChat(); this.$popup.trigger("chat:initialized"), a.one("chat:rooms-refreshed", ()=>{ this.updateUnreadCountBadge(), t.roomTag && t.toggleLeftColumn(!1) } ), a.one("chat:room-restored", ()=>{ t.toggleLeftColumn(!1) } ), a.one("chat:default-room-reset", ()=>{ t.toggleLeftColumn(!0) } ) } ); let n = new ResizeObserver(()=>{ this.$popup ? (this.updatePopupCoords(), this.setSizeCookie({ width: this.$popup.outerWidth() })) : n.disconnect() } ); n.observe(this.$popup[0]), this.$container.appendTo("body"), this.$popup.removeClass("is-loading"), i && i(this.$popup), XF.activate(this.$popup), this.loadingPopup = !1, t(this.$popup) } ) } , { global: s }).catch(()=>{ this.loadingPopup = !1, p("Failed to load popup") } ) } )) }, setupFromCookie() { let t = this.getStateCookie() , e = this.getRoomTagCookie(); if (!t || "closed" === t) return; let o = {}; e && (o.tag = e), this.state = t, this.loadPopup({ query: o, global: !1, wrapPopupCallback: t=>{ let e = this.getSizeCookie(); e && t.css(e) } , onLoaded: e=>{ this.updateState(t) } }) }, setStateCookie(t) { XF.Cookie.set(this.getCookieName("state"), t) }, getStateCookie() { return XF.Cookie.get(this.getCookieName("state")) }, setRoomTagCookie(t) { XF.Cookie.set(this.getCookieName("room_tag"), t) }, getRoomTagCookie() { return XF.Cookie.get(this.getCookieName("room_tag")) }, setCoordsCookie(t) { XF.Cookie.setJson(this.getCookieName("coords"), t) }, getCoordsCookie() { return XF.Cookie.getJson(this.getCookieName("coords")) }, setSizeCookie(t) { XF.Cookie.setJson(this.getCookieName("size"), t) }, getSizeCookie() { return XF.Cookie.getJson(this.getCookieName("size")) }, getCookieName(t) { return this.options.eventPrefix.toLowerCase() + "_" + t }, getChatTarget() { return this.$popup.find(".real-time-chat") }, getChat() { return this.getChatTarget().length ? XF.Element.getHandler(this.getChatTarget(), "chat") : null } }), XF.ChatPopupBtn = XF.Element.newHandler({ options: { url: "", eventPrefix: "", activeOnMobile: !0 }, init() { if (!this.options.activeOnMobile && i()) return void this.$target.hide(); t('.real-time-chat[data-event-prefix="' + this.options.eventPrefix + '"]').length ? this.$target.hide() : (this.popup = new XF.ChatPopup({ url: this.options.url, eventPrefix: this.options.eventPrefix }), this.popup.setupFromCookie(), this.$target.toggleClass("is-popup-open", null !== this.popup.$popup || this.popup.loadingPopup), this.$target.on("click", XF.proxy(this, "onClick")), this.popup.$container.on("chat-popup:close", t=>{ this.$target.removeClass("is-popup-open") } )) }, onClick(t) { t.preventDefault(), s(this.$target), this.$target.addClass("is-popup-open") } }), XF.RtcDraggable = XF.Element.newHandler({ options: { container: "< .js-draggableContainer" }, init() { this.$container = this.options.container ? XF.findRelativeIf(this.options.container, this.$target) : this.$target; let e = ()=>{ this.$target.draggable({ container: this.$container }) } ; void 0 === t.fn.draggable ? t(o).one("jquery:rtc-plugins-loaded", e) : e() } }); let s = e=>{ let i = e.data("event-prefix") , s = e.data("query") || {}; t(o).trigger("chat:open-popup", { eventPrefix: i, query: s, roomTag: e.data("room-tag") }) } ; t(o).on("click", ".js-chatPopup", e=>{ !t(e.currentTarget).data("active-on-mobile") && i() || (e.preventDefault(), s(t(e.currentTarget))) } ), XF.Element.register("chat-popup-btn", "XF.ChatPopupBtn"), XF.Element.register("rtc-draggable", "XF.RtcDraggable"), XF.Element.initialize($('*[data-xf-init*="chat-popup-btn"]')), XF.Element.initialize($('*[data-xf-init*="rtc-draggable"]')); }(window.jQuery, window, document);