var oldTitle = document.title;
var iframe = document.createElement('iframe');
var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
var mobileWidth = '100%';
var mobileHeight = '100%';
var desktopWidth = '500px';
var desktopHeight = '640px';
var mobileIconWidth = '120px';
var mobileIconHeight = '120px';
var desktopIconWidth = '74px';
var desktopIconHeight = '74px';
var widgetPosition = 'right';
iframe.frameBorder = 0;
iframe.style.width = isMobile ? mobileIconWidth : desktopIconWidth;
iframe.style.height = isMobile ? mobileIconHeight : desktopIconHeight;
iframe.id = 'iframe-container';
iframe.src = 'https://cannafarms.nectardesk.io/chat/embed/load-window/IS319357460c254d96adaa2adc3e9b3ccf/1';
iframe.style.position = 'fixed';
iframe.style.bottom = '0px';
if (widgetPosition === 'left') {
iframe.style.left = '0px';
} else {
iframe.style.right = '0px';
}
iframe.style.zIndex = 9999999;
if (/complete|interactive/.test(document.readyState)) {
loadChat();
}
document.addEventListener('DOMContentLoaded', function () {
loadChat();
let isChatClosed = localStorage.getItem('livechat-chat-opened') === 'false';
if (isChatClosed) {
iframe.style.width = isMobile ? mobileIconWidth : desktopIconWidth;
iframe.style.height = isMobile ? mobileIconHeight : desktopIconHeight;
} else {
iframe.style.width = isMobile ? mobileWidth : desktopWidth;
iframe.style.height = isMobile ? mobileHeight : desktopHeight;
}
});
function loadChat() {
localStorage.setItem('livechat-chat-opened', false);
document.body.appendChild(iframe)
window.addEventListener('message', function (e) {
var iframeEl = document.getElementById('iframe-container');
iframeEl.contentWindow.postMessage({
'client_url': window.location.href,
'livechat_params': localStorage.getItem('livechat_params')
}, '*');
if (e.data.blink_title !== undefined) {
blinkTab(e.data.blink_title);
}
var t = window.document.getElementById('iframe-container');
if (e.data.showchat == 'yes') {
t.style.height = isMobile ? mobileHeight : desktopHeight;
t.style.width = isMobile ? mobileWidth : desktopWidth;
} else if (e.data.showchat == 'no') {
t.style.height = isMobile ? mobileIconHeight : desktopIconHeight;
t.style.width = isMobile ? mobileIconWidth : desktopIconWidth;
}
if (e.data.nectarboxhide == 'yes') {
('320px' == t.style.height || '' == t.style.height) ? t.style.height = '74px' : t.style.height = '320px';
}
if (window.ndChatSettings) {
if (e.data.liveChatEvent === 'onChatLoaded' && window.ndChatSettings.onChatLoaded) {
window.ndChatSettings.onChatLoaded();
}
if (e.data.liveChatEvent === 'onChatBoxOpened' && window.ndChatSettings.onChatBoxOpened) {
window.ndChatSettings.onChatBoxOpened();
}
if (e.data.liveChatEvent === 'onChatBoxClosed' && window.ndChatSettings.onChatBoxClosed) {
window.ndChatSettings.onChatBoxClosed();
}
if (e.data.liveChatEvent === 'onMessageSent' && window.ndChatSettings.onMessageSent) {
window.ndChatSettings.onMessageSent();
}
if (e.data.liveChatEvent === 'onMessageReceived' && window.ndChatSettings.onMessageReceived) {
window.ndChatSettings.onMessageReceived();
}
}
});
}
function blinkTab(message) {
var timeoutId = localStorage.getItem('blinkTimeoutId');
var blink = function () {
document.title = document.title == message ? oldTitle : message;
},
clear = function () {
clearInterval(timeoutId);
localStorage.setItem('blinkTimeoutId', '');
document.title = oldTitle;
window.onmousemove = null;
timeoutId = null;
};
if (timeoutId) {
clearInterval(timeoutId);
}
timeoutId = setInterval(blink, 1000);
localStorage.setItem('blinkTimeoutId', timeoutId);
window.onmousemove = clear;
}