(function(){
function ready() {
var hashToken = '#thefloors',
hashValue = '',
domainId= '207',
widgetDefLoad = true,
gaTrackingId = 'UA-39273288-10',
widgetState = false,
widgetIcon = +6;
function setStyles() {
var cssId = 'thefloorsStyle';
if (!document.getElementById(cssId)) {
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.id = cssId;
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'https://infoservice.us-east-2.elasticbeanstalk.com/downloads/styles.css';
link.media = 'all';
head.appendChild(link);
link.onload = function () {
createWidgetBtn();
addListener(document.getElementById('widget-btn'), 'click', showAndCreateIframe);
};
}
}
function getHashValue() {
var widgetHash = window.location.hash;
widgetHash = widgetHash.replace(hashToken, '');
var arr = widgetHash.split('/');
arr.splice(0,2);
widgetHash = '/' + arr.join('/');
return widgetHash;
// todo consider there is alresy hash in url
}
function setHashValue(newHash) {
hashValue = hashToken + newHash;
if(widgetState) {
setWindowLocationHash();
}
}
function setWindowLocationHash() {
window.location.hash = hashValue;
}
function clearWindowLocationHash() {
window.location.hash = '';
}
function createWidgetBtn() {
var widgetBtn = document.createElement('div');
var widgetIconHtml;
switch(widgetIcon) {
case 1:
widgetIconHtml = "";
break;
case 2:
widgetIconHtml = "";
break;
case 3:
widgetIconHtml = "";
break;
case 4:
widgetIconHtml = "";
break;
case 5:
widgetIconHtml = "";
break;
case 6:
widgetIconHtml = "";
break;
case 7:
widgetIconHtml = "
";
break;
default:
widgetIconHtml = "";
break;
}
widgetBtn.innerHTML = "" + widgetIconHtml + "Выбрать квартиру
";
document.body.appendChild(widgetBtn);
}
function createIframe() {
let oldIframeWrap = document.getElementById('iframeWr');
if(oldIframeWrap){
oldIframeWrap.remove();
}
var widgetHash = getHashValue();
var iframeWr = document.createElement('div');
iframeWr.setAttribute('id', 'iframeWr');
iframeWr.style.position = 'fixed';
iframeWr.style.top = '0';
iframeWr.style.left = '0';
iframeWr.style.bottom = '0';
iframeWr.style.right = '0';
iframeWr.style.transform = 'translateX(100%)';
iframeWr.style.transition = '1s';
iframeWr.style.zIndex = '-1';
iframeWr.innerHTML = "";
document.body.appendChild(iframeWr);
}
function addListener(element, eventName, handler) {
if (element.addEventListener) {
element.addEventListener(eventName, handler, false);
}
else if (element.attachEvent) {
element.attachEvent('on' + eventName, handler);
}
else {
element['on' + eventName] = handler;
}
}
function closeIframe() {
var iframeWr = document.getElementById('iframeWr');
var iframe = document.getElementById('iframe');
document.body.ontouchmove = function(e){ return true; }
document.body.style.position = 'inherit';
document.body.style.overflow = 'inherit';
iframeWr.style.transform = 'translateX(100%)';
iframeWr.style.transition = '1s';
clearWindowLocationHash();
widgetState = false;
setTimeout(function () { iframeWr.style.zIndex = '-1' }, 1000);
}
function showAndCreateIframe(){
if(!widgetDefLoad){
openIframe();
}else{
createIframe();
openIframe();
}
}
function showWidgetOnLinkClicked() {
setTimeout(() => {
createIframe();
openIframe();
}, 500);
}
function openIframe() {
var iframeWr = document.getElementById('iframeWr');
var iframe = document.getElementById('iframe');
document.body.ontouchmove = function(e){ e.preventDefault(); }
document.body.style.position = 'fixed';
document.body.style.minWidth = '100%';
document.body.style.maxWidth = '100%';
document.body.style.overflow = 'hidden';
iframeWr.style.zIndex = '10000002';
iframeWr.style.transform = 'translateX(0%)';
iframeWr.style.transition = '1s';
widgetState = true;
if(ga && ga.length) {
var tracker = getGaTracker();
if (tracker) {
ga(tracker.get('name') + '.send',
'event',
'widgetButtonClick',
'TFMainWidgetClicked');
}
}
if(ahoy){
trackAhoyEvent('show_domain');
}
}
function getQueryParams(){
queryParams = window.location.search.substr(1).split('&');
if (queryParams == '') return {};
var res = {};
for (var i = 0; i < queryParams.length; ++i) {
var p = queryParams[i].split('=', 2);
if (p.length == 1)
res[p[0]] = '';
else
res[p[0]] = decodeURIComponent(p[1].replace(/\+/g, ' '));
}
return res;
}
function sendClientIds() {
var ahoyVisitId, ahoyVisitorId;
if (ga && ga.length && getGaClientID) {
var clientId = getGaClientID();
if (clientId) {
var msg = { clientId: clientId };
senMessageToWidget(JSON.stringify(msg));
}
}
if (ahoy) {
ahoyVisitorId = ahoy.getVisitorId();
ahoyVisitId = ahoy.getVisitId();
senMessageToWidget(JSON.stringify({
visitorId: ahoyVisitorId,
visitId: ahoyVisitId
}));
}
}
function senMessageToWidget(msg){
var widgetUrl = 'https://infoservice.us-east-2.elasticbeanstalk.com/widget/';
document.getElementById('iframeTheFloors')
.contentWindow
.postMessage(msg, widgetUrl);
}
function openIframeIfLocationParamExist(){
var locationParams = window.location.hash.split('/');
if (locationParams == '') return {};
var res = {};
for (var i = 0; i < locationParams.length; ++i) {
if(locationParams[i] == '#thefloors') {
setTimeout(function () {
showAndCreateIframe();
}, 1000);
}
}
}
if(!widgetDefLoad){
createIframe();
}
openIframeIfLocationParamExist();
setStyles();
var elements = document.getElementsByClassName('thefloors-custom-widget-link');
for (var i = 0; i < elements.length; i++) {
elements[i].addEventListener('click', showWidgetOnLinkClicked);
}
function bindEvent(element, eventName, eventHandler) {
if (element.addEventListener){
element.addEventListener(eventName, eventHandler, false);
} else if (element.attachEvent) {
element.attachEvent('on' + eventName, eventHandler);
}
}
bindEvent(window, 'message', function (e) {
if(!e.origin || e.origin != 'https://infoservice.us-east-2.elasticbeanstalk.com') {
return;
}
if(e.data == 'close-thefloors') {
closeIframe();
return;
}
if(e.data === 'widget-initialized'){
sendClientIds();
return;
}
if(e.data) {
setHashValue(e.data);
}
});
if (getQueryParams()['thefloorswidget'] === 'show') {
showAndCreateIframe();
}
function addScript( src ) {
return new Promise(function(resolve, reject) {
var s = document.createElement('script');
s.onload = () => resolve(s);
s.onerror = () => reject(new Error(`Error while loading script ${src}`));
s.setAttribute('src', src);
s.setAttribute('async', true);
document.head.appendChild(s);
});
}
function getUtms(){
var queryParams = getQueryParams();
var res = {};
var params = ['utm_source', 'utm_medium', 'utm_term', 'utm_content', 'utm_campaign'];
for(var i=0; i < params.length; ++i){
if(queryParams[params[i]]){
res[params[i]] = queryParams[params[i]];
}
}
return res;
}
function trackAhoyEvent(name){
var opts = getUtms();
opts['domain_id']=domainId;
ahoy.track(name, opts);
}
function getGaTracker() {
var tracker = ga.getAll().filter(item => {
if(item.get('trackingId') === gaTrackingId) {
return item;
}
});
if (tracker) {
return tracker[0];
} else {
return null;
}
}
function getGaClientID(){
var tracker = getGaTracker();
if (tracker) {
var clientId = tracker.get('clientId');
return clientId;
} else {
return null;
}
}
function initAnalytics() {
var ahoyUrl = 'https://s3.us-east-2.amazonaws.com/infoservice-new/scripts/ahoy.js';
let loadAhoyScript = addScript(ahoyUrl);
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date(); a = s.createElement(o),
m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
var opts;
window.ahoy = {
urlPrefix: 'https://infoservice.us-east-2.elasticbeanstalk.com',
visitsUrl: '/ahoy/visits',
eventsUrl: '/ahoy/events',
cookieDomain: null,
page: window.location.href,
platform: 'Web',
useBeacon: false,
startOnReady: true,
trackVisits: true
};
loadAhoyScript.then(() => {
setTimeout(() => {
trackAhoyEvent('init_widget');
}, 500);
})
}
initAnalytics();
};
document.addEventListener('DOMContentLoaded', ready);
})();