if (typeof optio == 'undefined') optio = { apiVersion: '4.2', language: 'en' }; optio._getContent = function(data, callback) { var url = 'https://www.optiopublishing.com/embed/?'; for (var key in data) { if (data[key] != null) { url += key + '=' + encodeURIComponent(data[key]) + '&'; } } // Use XDR for IE8 and 9 if ('XDomainRequest' in window && window.XDomainRequest !== null) { var xdr = new XDomainRequest(); xdr.open('GET', url); xdr.onload = function () { callback(xdr.responseText); }; xdr.send(); } else { var xhr = new XMLHttpRequest(); xhr.open('GET', url); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { callback(xhr.responseText); } }; xhr.send(); } }; optio._setInnerHtml = function(el, html) { el.innerHTML = html; var scripts = el.querySelectorAll('script'); for (var i = 0; i < scripts.length; i++) { var scriptEl = document.createElement('script'); scriptEl.text = scripts[i].text; el.appendChild(scriptEl); el.removeChild(scriptEl); } } optio._forEachEl = function(selector, callback) { var els = document.querySelectorAll(selector, callback); for (var i = 0; i < els.length; i++) { callback(els[i]); } } optio._toggleClass = function(el, className, state) { if (typeof el.classList !== 'undefined') { if (state) { el.classList.add(className); } else { el.classList.remove(className); } } else { var classList = el.className.split(' '); if (state) { if (classList.indexOf(className) > -1) return; classList.push(className); } else { var index = classList.indexOf(className); if (index < 0) return; classList.splice(index, 1); } el.className = classList.join(' '); } } optio._closest = function(el, selector) { if (typeof el.closest === 'function') { return el.closest(selector); } else { if (!Element.prototype.matches) { Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector; } var current = el.parentNode; while (current) { if (current.matches(selector)) { return current; } else { current = current.parentNode; } } return false; } } /** * Open lightbox */ optio.openLightbox = function(videoId, language) { if (language === undefined) language = 'en'; // Close open lightbox optio._forEachEl('.optioLightboxWrapper', function(el) { el.parentNode.removeChild(el); }); optio._getContent({ v: optio.version, url: window.location.hostname, control: 'lightbox', video: videoId, language: language, autoplay: true, format: 'html' }, function(response) { // Insert lightbox var el = document.createElement('div'); el.className = 'optioLightboxWrapper'; el.innerHTML = response; document.body.appendChild(el); // Close lightbox optio._forEachEl('.optioLightbox', function(el) { el.addEventListener('click', function(event) { event.stopPropagation(); }); }); optio._forEachEl('.optioLightboxWrapper, .optioLightboxClose', function(el) { el.addEventListener('click', function() { optio._forEachEl('.optioLightboxWrapper', function(el) { el.parentNode.removeChild(el); }); }); }); }); }; /** * Replace optio tags */ optio._init = function() { // Optio library tag optio._forEachEl( '.optio-video-library, .optio-library', function(el) { optio._getContent({ url: window.location.hostname, control: 'video_library', format: 'html', language: el.getAttribute('data-language') || optio.language, autoplay: el.getAttribute('data-autoplay') || 'false', captions: el.getAttribute('data-captions') || 'false', filter: el.getAttribute('data-filter'), video: el.getAttribute('data-video-id') }, function(response) { optio._setInnerHtml(el, response); }); } ); // Optio video player optio._forEachEl( '.optio-video-player[data-video-id], .optio-video[data-video-id]', function(el) { optio._getContent({ url: window.location.hostname, control: 'video_player', format: 'html', language: el.getAttribute('data-language') || optio.language, autoplay: el.getAttribute('data-autoplay') || 'false', captions: el.getAttribute('data-captions') || 'false', video: el.getAttribute('data-video-id') }, function(response) { optio._setInnerHtml(el, response); }); } ); // Optio thumbnail link optio._forEachEl( '.optio-thumbnail[data-video-id], .optio-thumbnail-link[data-video-id]', function(el) { optio._getContent({ url: window.location.hostname, control: 'thumbnail_link', format: 'html', language: el.getAttribute('data-language') || optio.language, autoplay: el.getAttribute('data-autoplay') || 'false', captions: el.getAttribute('data-captions') || 'false', video: el.getAttribute('data-video-id') }, function(response) { optio._setInnerHtml(el, response); }); } ); // Optio Kids Zone optio._forEachEl( '.optio-kidszone', function(el) { optio._getContent({ url: window.location.hostname, control: 'kidszone', format: 'html' }, function(response) { optio._setInnerHtml(el, response); }); } ); // Partner landing page optio._forEachEl( '.optio-landing-page', function(el) { optio._getContent({ url: window.location.hostname, control: 'landing-page', format: 'html' }, function(response) { optio._setInnerHtml(el, response); }); } ); }; if (document.readyState === 'loading') { window.addEventListener('DOMContentLoaded', optio._init); } else { optio._init(); } /** * Maintain backwards compatibility with V2 */ optio.VideoLibrary = function(domElement) { optio._forEachEl( '#' + domElement, function(el) { optio._getContent({ url: window.location.hostname, control: 'video_library', format: 'html' }, function(response) { optio._setInnerHtml(el, response); }); } ); } optio.VideoPlayer = function(domElement, video, autoPlay) { optio._forEachEl( '#' + domElement, function(el) { optio._getContent({ url: window.location.hostname, control: 'video_player', format: 'html', video: video, autoPlay: autoPlay ? autoPlay : 'false' }, function(response) { optio._setInnerHtml(el, response); }); } ); }