;
function ch_parseInt(data){
if(!data){ return undefined; }
try {
return parseInt(data);
} catch (e){
return undefined;
}}
(function(){
var w=window;
if(w.ChannelIO){
return (window.console.error||window.console.log||function(){})('ChannelIO script included twice.');
}
var ch=function(){
ch.c(arguments);
};
ch.q=[];
ch.c=function(args){
ch.q.push(args);
};
w.ChannelIO=ch;
function loadSdk(){
if(w.ChannelIOInitialized){
return;
}
w.ChannelIOInitialized=true;
var s=document.createElement('script');
s.type='text/javascript';
s.async=true;
s.src='https://cdn.channel.io/plugin/ch-plugin-web.js';
s.charset='UTF-8';
var x=document.getElementsByTagName('script')[0];
x.parentNode.insertBefore(s, x);
}
function buildBaseSettings(){
return {
"pluginKey": channel_io_options.channel_io_plugin_key,
"hideChannelButtonOnBoot": channel_io_options.channel_io_hide_default_launcher==='on',
"customLauncherSelector": channel_io_options.channel_io_custom_launcher_selector,
"mobileMessengerMode": channel_io_options.channel_io_mobile_messenger_mode==='on' ? 'iframe':undefined,
"zIndex": ch_parseInt(channel_io_options.channel_io_z_index),
"scriptProvider": "channel",
"scriptPlatform": "wordpress",
"scriptVersion": "2.0.0"
};}
function bootAnonymous(){
ChannelIO('boot', buildBaseSettings());
}
function bootWithProfile(me){
var settings=buildBaseSettings();
if(me&&me.login&&me.memberId){
settings.memberId=me.memberId;
if(me.memberHash){
settings.memberHash=me.memberHash;
}
settings.profile=me.profile||{};}
ChannelIO('boot', settings);
}
function fetchProfileAndBoot(){
if(!channel_io_options||!channel_io_options.profile_url){
bootAnonymous();
return;
}
var settled=false;
var timeoutId=window.setTimeout(function(){
if(settled){
return;
}
settled=true;
bootAnonymous();
}, 5000);
function finish(callback){
if(settled){
return;
}
settled=true;
window.clearTimeout(timeoutId);
callback();
}
if(typeof window.fetch==='function'){
window.fetch(channel_io_options.profile_url, {
credentials: 'same-origin'
})
.then(function(res){ return res.ok ? res.json():null; })
.then(function(me){
finish(function(){ bootWithProfile(me); });
})
.catch(function(){ finish(bootAnonymous); });
return;
}
try {
var xhr=new XMLHttpRequest();
xhr.open('GET', channel_io_options.profile_url, true);
xhr.withCredentials=true;
xhr.onreadystatechange=function(){
if(xhr.readyState!==4) return;
if(xhr.status >=200&&xhr.status < 300){
try {
var me=JSON.parse(xhr.responseText);
finish(function(){ bootWithProfile(me); });
} catch (e){
finish(bootAnonymous);
}}else{
finish(bootAnonymous);
}};
xhr.onerror=function(){ finish(bootAnonymous); };
xhr.send();
} catch (e){
finish(bootAnonymous);
}}
var started=false;
function start(){
if(started){
return;
}
started=true;
loadSdk();
fetchProfileAndBoot();
}
if(document.readyState==='complete'){
start();
}else if(window.attachEvent){
window.attachEvent('onload', start);
}else{
window.addEventListener('DOMContentLoaded', start, false);
window.addEventListener('load', start, false);
}})();