(function() {
var css = '#breezy-container .breezy-launcher{bottom:20px;height:60px;right:20px;position:fixed;width:60px;border:none}#breezy-container .breezy-launcher button{background-color:rgb(24, 110, 240);border:none;border-radius:6px;color:#fff;font-size:32px;height:60px;outline:0;position:relative;width:60px}#breezy-container .breezy-launcher button i{height:32px;left:14px;position:absolute;top:14px;transition:all .3s ease;width:32px}#breezy-container .breezy-launcher button:hover{box-shadow:0 0 20px 1px rgba(0,0,0,.2)}#breezy-container .breezy-messenger-frame{display:none;z-index:2147483000!important;transition:all .3s ease;position:fixed;right:20px;height:calc(100% - 20px - 75px - 20px);min-height:250px;max-height:450px;bottom:calc(20px + 75px);width:360px;border-radius:6px;overflow:hidden;opacity:1}#breezy-container.nudge .breezy-messenger-frame{right:80px}#breezy-container .breezy-messenger-frame iframe{border:none;height:100%;width:100%}#breezy-container.open .breezy-messenger-frame{height:calc(100% - 20px - 75px - 20px)}#breezy-container .breezy-launcher button i.fa-comments{opacity:1;transform:rotate(0)}#breezy-container .breezy-launcher button i.fa-times{opacity:0;transform:rotate(-30deg)}#breezy-container.open .breezy-launcher button i.fa-comments{opacity:0;transform:rotate(30deg)}#breezy-container.open .breezy-launcher button i.fa-times{opacity:1;transform:rotate(0)}#breezy-container.closing .breezy-messenger-frame,#breezy-container.opening .breezy-messenger-frame{opacity:0;transform:translate(0,20px)}',
    head = parent.document.head || parent.document.getElementsByTagName('head')[0],
    style = document.createElement('style');

    // responsive CSS
    css += '@media (max-width:599px){#breezy-container .breezy-messenger-frame{background-color:rgb(24, 110, 240);border-radius:0;bottom:0;max-height:100%;right:0;top:0;width:100%}#breezy-container.open .breezy-messenger-frame{height:100%}#breezy-container.open .breezy-launcher{bottom:auto;right:0;top:5px;z-index:2147483001!important}#breezy-container.open .breezy-launcher button{background-color:transparent;box-shadow:none}}';

    style.type = 'text/css';
    style.id = 'bzy-style';

    if (style.styleSheet){
      style.styleSheet.cssText = css;
    } else {
      style.appendChild(document.createTextNode(css));
    }

    head.appendChild(style);

    link = document.createElement('link');
    link.rel = 'stylesheet';
    link.href = 'https://assets-cdn.breezy.hr/breezy-hr/dependencies/fontawesome/v6.4.2-pro/css/all.css';
    link.type = 'text/css';
    link.id = 'bzy-fa';
    head.appendChild(link);

    var companyId = 'ba0d1c147393';

    var div = document.createElement('div');
    div.id = 'breezy-container';
    div.style.position = 'fixed';
    div.style.left = '0';
    div.style.bottom = '0';
    div.style.height = '0';
    div.style.width = '0';
    div.style.zIndex = '2147483647';


    var app = document.createElement('div');
    app.className = 'breezy-messenger-frame';


    var frame = document.createElement('iframe');
    frame.setAttribute('id', 'bzIframe');
    frame.setAttribute('allowfullscreen', '');

    var widgetScript = document.querySelector('#widgetScript');
    var page = location.href;
    var bzT = new Date().getTime();
    if ((page.indexOf('localhost') > -1) || (page.indexOf('breezy.hr:8090') > -1) || (widgetScript && (widgetScript.src.indexOf('localhost') > -1))) {
        // running locally
        frame.src = 'http://localhost:8088/#/ba0d1c147393?page=' + page + '&t=' + bzT + '&socketUrl=undefined&APP_BASE_URL=https://app.breezy.hr/&isPF=undefined&portalDomain=breezy.hr';
    } else {
        var frmSrc = 'https://assets-cdn.breezy.hr/breezy-widget/index.html#/ba0d1c147393?page=' + page + '&t=' + bzT + '&socketUrl=undefined&APP_BASE_URL=https://app.breezy.hr/&isPF=undefined&portalDomain=breezy.hr';
        frame.src = frmSrc;
    }
    frame.setAttribute('name', 't'+bzT);
    app.appendChild(frame);

    div.appendChild(app);

    var bzyMessengerClosed = true;

    var clickLauncher = function() {
        var bzy = document.querySelector('#breezy-container');
        var timer = null;
        if (bzyMessengerClosed) {
            bzy.classList.remove('closing');
            bzy.classList.add('open', 'opening');
            app.style.display = "block";
            setTimeout(function(){
                bzy.classList.remove('opening');
            });
            toggleOpen(true);
        } else {
            bzy.classList.remove('opening');
            bzy.classList.add('closing');
            setTimeout(function(){
                app.style.display = "none";
            }, 250);
            setTimeout(function(){
                bzy.classList.remove('open', 'closing');
            }, 300);
            toggleOpen(false);
        }
    }

    var launcher = document.createElement('div');
    launcher.className = 'breezy-launcher';
    var button = document.createElement('button');
    button.className = 'breezy-launcher-button';
    button.addEventListener('click', clickLauncher);

    var iconOpen = document.createElement('i');
    iconOpen.className = 'fa fa-comments';

    var iconClose = document.createElement('i');
    iconClose.className = 'fa fa-times';

    button.appendChild(iconOpen);
    button.appendChild(iconClose);

    launcher.appendChild(button);
    div.appendChild(launcher);

    var container = document.querySelector('#BrzyHr_messenger');

    /* console.log(document.querySelector('#intercom-container'))
    if (document.querySelector('#intercom-container')) {
        container.classList.add('nudge');
        console.log('intercom found');
    } */

    container.appendChild(div);

    // events and stuff or something
    var bzyFirstVisit = false;
    var widgetRequest = function(event) {
        var request = event.data;
        var payload = request.payload;
        if (request.type === 'ready') {
            var data = {
                type: 'visitor:init',
                payload: {
                    breezy_portal: document.getElementById('BrzyHr_app') ? true : false,
                    current_page_url: document.location.href
                }
            };
            postMessage(data);
            if (payload.open) {
                //console.log('messenger is suppose to be OPEN')
                clickLauncher();
            } else if (payload.firstVisit && (document.body.clientWidth > 480)) {
                setTimeout(function() {
                    if (bzyMessengerClosed) {
                        bzyFirstVisit = true;
                        if (companyId !== '0fd565af622a') {
                            clickLauncher();
                        }
                    }
                }, 5000);
            } else {
                //console.log('messenger is suppose to be CLOSED')
            }

            //event.source.postMessage(JSON.stringify(data), event.origin);
        } else if (request.type === 'update') {
            //console.log(request.payload)
            if (widgetScript && (widgetScript.src.indexOf('localhost') > -1)) {
                document.querySelector('#visitorInfo pre').innerHTML = JSON.stringify(JSON.parse(payload), undefined, 2);
            }

        } else if (request.type === 'messenger:open') {
            clickLauncher();
        }
        // frame.contentWindow.postMessage(JSON.stringify(data), 'http://localhost:8086');
    };

    var bzyRoute = document.location.href;
    var checkRoute = function() {
        if (bzyRoute !== window.location.href) {
            bzyRoute = document.location.href;

            var data = {
                type: 'visitor:update',
                payload: {
                    breezy_portal: document.getElementById('BrzyHr_app') ? true : false,
                    current_page_url: document.location.href
                }
            };
            postMessage(data);
        }
    };

    var toggleOpen = function(payload) {
        bzyMessengerClosed = !payload;

        var data = {
            type: 'messenger:open',
            payload: {
                open: payload,
                firstVisit: bzyFirstVisit
            }
        };
        bzyFirstVisit = false;
        postMessage(data);
    };

    var postMessage = function(data) {
        if (widgetScript && (widgetScript.src.indexOf('localhost') > -1)) {
            document.querySelector('#BrzyHr_messenger iframe').contentWindow.postMessage(JSON.stringify(data), '*')
            //document.querySelector('#BrzyHr_widget iframe').contentWindow.postMessage(JSON.stringify(data), 'http://localhost:8088');
        } else {
            document.querySelector('#BrzyHr_messenger iframe').contentWindow.postMessage(JSON.stringify(data), 'https://breezy-assets-cdn.s3.amazonaws.com')
            //document.querySelector('#BrzyHr_widget iframe').contentWindow.postMessage(JSON.stringify(data), '*');
        }
    };

    window.setInterval(checkRoute, 1000);
    window.addEventListener('message', widgetRequest, false);

    var bzyIdleTimer,
        bzyInactive = false,
        title = document.title;
    var bzyDebug = false;

    var sendIsInactive = function(inactive) {
        inactive = (typeof inactive === 'undefined') ? true : false
        bzyInactive = inactive

        if (bzyDebug) console.log('bzyInactive = ' + inactive)

        if (inactive) clearInterval(bzyIdleTimer);

        if (bzyDebug) {
            if (inactive) {
                document.title = '(inactive) ' + title;
            } else {
                document.title = title;
            }
        }

        var data = {
            type: 'messenger:active',
            payload: {
                inactive: inactive
            }
        }
        postMessage(data);

    }

    function initReload() {
        clearInterval(bzyIdleTimer);
        bzyIdleTimer = setInterval(sendIsInactive, 30000);

    }
    window.addEventListener('load', function() {
        initReload();
    });
    window.addEventListener('focus', function() {
        if (bzyDebug) console.log('focused');
        if (bzyInactive) {
            sendIsInactive(false)
        }
        initReload();
    });
    window.addEventListener('blur', function() {
        sendIsInactive()
        //clearInterval(bzyIdleTimer);
    });
    window.addEventListener('mousemove',function() {
        if (bzyInactive) {
            sendIsInactive(false)
        }
        initReload()
    });


})();