var SSWI = (function () { return { domain: null, url: 'widgets.serwersms.pl', path: 'generate.php', jQuery: null, brower: '', browser_ver: '', version: 1, iframe: [], setting: [], /** * Initalization function */ init: function () { SSWI.domain = SSWI.getDomain(); SSWI.browser = SSWI.getBrowser().name; SSWI.browser_ver = SSWI.getBrowser().version; if (window.jQuery === undefined || window.jQuery.fn.jquery !== '2.2.0') { var script_tag = document.createElement('script'); script_tag.setAttribute('type', 'text/javascript'); script_tag.setAttribute('src', (document.location.protocol == 'https:' ? 'https://' : 'https://') + 'code.jquery.com/jquery-2.2.0.min.js?v=' + SSWI.version); if (script_tag.readyState) { script_tag.onreadystatechange = function () { if (this.readyState == 'complete' || this.readyState == 'loaded') { SSWI.loadMethod(); (document.getElementsByTagName('head')[0] || document.documentElement).appendChild(script_tag); } }; } else { script_tag.onload = SSWI.loadMethod; (document.getElementsByTagName('head')[0] || document.documentElement).appendChild(script_tag); } } else { SSWI.loadMethod(); } }, /** * Load method widget */ loadMethod: function () { var _old_sswi = window._sswi; window._sswi = new function () { this.push = function () { for (var i = 0; i < arguments.length; i++) { switch (arguments[i][0]) { case 'setWidget': SSWI['setWidget'](arguments[i][1], arguments[i][2], arguments[i][3],arguments[i][4]); break; } } }; }; window._sswi.push.apply(window._sswi, _old_sswi); if (window.addEventListener) addEventListener('message', SSWI.listener, false); else attachEvent('onmessage', SSWI.listener); SSWI.jQuery = window.jQuery.noConflict(true); SSWI.jQuery(document).ready(function ($) { SSWI.generateWidget(); }); }, /** * Generate random uuid */ generateUUID: function () { var time = new Date().getTime(); var uuid = '1xxx-yxxx'.replace(/[xy]/g, function (c) { var r = (time + Math.random() * 16) % 16 | 0; time = Math.floor(time / 16); return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16); }); return uuid; }, /** * Get info from browser */ getBrowser: function () { var ua = navigator.userAgent, tem, M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; if (/trident/i.test(M[1])) { tem = /\brv[ :]+(\d+)/g.exec(ua) || []; return { name: 'IE', version: (tem[1] || '') }; } if (M[1] === 'Chrome') { tem = ua.match(/\bOPR\/(\d+)/) if (tem != null) { return { name: 'Opera', version: tem[1] }; } } M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?']; if ((tem = ua.match(/version\/(\d+)/i)) != null) { M.splice(1, 1, tem[1]); } return { name: M[0], version: M[1] }; }, /** * Listener function */ listener: function (e) { //if (e.url.indexOf(SSWI.url) === -1) return; if (e.data.indexOf(':') > -1) { var data = e.data.split(':'); if (data.length == 3) { var iframe = SSWI.jQuery('#' + data[0]); if (iframe.length > 0) { if(data[1] == 'scroll') { var top = parseInt(iframe.offset().top); top += parseInt(data[2]); top -= 40; if(top > 0) { SSWI.jQuery('html, body').animate({ scrollTop: top }, 250); } } else if(data[1] == 'delete') { iframe.remove(); } else { iframe.css({'width': '100%', 'height': data[2] + 'px'}); } } } } }, /** * Check is mobile browser */ isMobile: function () { return (navigator.userAgent.match(/iPhone|iPad|iPod|Android|BlackBerry|Opera Mini|IEMobile/i) != null); }, /** * Get domain from url address */ getDomain: function () { var domain = document.domain; var url = domain.replace(/https?:\/\/(www.)?/i, ''); url = url.replace('www.', ''); if (url.indexOf('/') === -1) { return url; } return url.split('/')[0]; }, /** * Get url params */ getUrlParams: function () { return window.location.search; }, /** * Set widget form code */ setWidget: function (code, container,phone,message) { phone = typeof phone !== 'undefined' ? phone : ''; message = typeof message !== 'undefined' ? message : ''; SSWI.setting.push({ 'id': '1123-' + code, 'code': code, 'container': container, 'phone':phone, 'message':message }); }, /** * Reset form input */ resetForm: function (el) { el.find(':input').not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected'); }, /** * Set iframe height */ setIframeHeight: function (height) { SSWI.iframe.css({'height': height + 'px'}); }, /** * Generate widget */ generateWidget: function () { var css = SSWI.jQuery('', { rel: "stylesheet", type: "text/css", href: (document.location.protocol == 'https:' ? 'https://' : 'https://') + SSWI.url + '/css/widget_iframe.css?v=' + SSWI.version }); css.appendTo('head'); for (var option in SSWI.setting) { var element = SSWI.setting[option]; if (element.id !== undefined && element.id != '' && !SSWI.jQuery('#' + element.id).length > 0) { var src = (document.location.protocol == 'https:' ? 'https://' : 'https://') + SSWI.url + '/app/' + SSWI.path + '?c=' + element.code + '&d=' + SSWI.domain; if(element.phone) src = src+'&p='+element.phone; if(element.message) src = src+'&m='+element.message; var iframe = null; var div = SSWI.jQuery('#' + element.container); if (div.length > 0) { iframe = SSWI.jQuery(''); iframe.css({'width': 340 + 'px', 'overflow': 'hidden'}); iframe.prop('id', element.id); div.append(iframe); } else { iframe = SSWI.jQuery(''); iframe.css({'width': 340 + 'px', 'overflow': 'hidden'}); iframe.prop('id', element.id); SSWI.jQuery('body').append(iframe); } SSWI.iframe.push(iframe); SSWI.showIframe(iframe); } } }, /** * Show iframe */ showIframe: function (iframe) { iframe.on('load', function () { setTimeout(function () { iframe.css({'visibility': 'visible'}); }, 500); }); } }; }()); SSWI.init();