function showBetslip(options) {
// If no feed ID is set, do nothing
if (options.feedId.trim().length == 0) return;
var feed;
var expanded = false;
var autoExpandOnLoad = false;
// Pagination
var eventsPerPage;
var currentPage = 0;
var maxPages = 0;
var totalPages = 0;
var isMobile;
// Load CSS and detect path
var path = document.location.href.indexOf('/render/') > -1 ? 'https://bannerflow.blob.core.windows.net/lpb-publish/unibet/55dacb16e347271ec0d5101b/widget/betslip/' : '/widget/betslip/';
var head = document.head || document.getElementsByTagName("head")[0];
var css = document.createElement('link');
css.href = path + 'style.css';
css.type = 'text/css';
css.rel = 'stylesheet';
head.appendChild(css);
// Load feed script
var feedJS = document.createElement('script');
feedJS.src = path + 'feed.js';
head.appendChild(feedJS);
// Wait for DOM ready to add betslip to DOM
if (document.readyState != 'loading') document.addEventListener('DOMContentLoaded', onDomReady);
else onDomReady();
function onDomReady() {
if (window.Feed) {
init();
} else {
var t;
t = setInterval(function() {
if (window.Feed) {
clearInterval(t);
init();
}
}, 100);
}
}
function init() {
// Copy
if (!options.dictionary) {
options.dictionary = {
headline: 'Our Live Odds',
cta: 'Register today'
};
}
// Refresh interval
if (!options.autoRefresh) {
options.autoRefresh = 60;
}
if (options.autoExpand != undefined) {
autoExpandOnLoad = options.autoExpand;
}
// CTA link
var ctaOnClickEvent = '';
if (options.dictionary.ctaLink && options.dictionary.ctaLink.trim().length > 0) {
ctaOnClickEvent = ' onclick="window.open(\'' + options.dictionary.ctaLink.trim() + '\');"';
}
// Pagination
eventsPerPage = options.pagination.eventsPerPage ? options.pagination.eventsPerPage : 5;
maxPages = options.pagination.maxPages ? options.pagination.maxPages : 1;
options.limit = eventsPerPage * maxPages;
// On complete callback
if (!options.onComplete) {
options.onComplete = onFeedUpdate;
} else {
var customCallback = options.onComplete;
options.onComplete = function() {
onFeedUpdate();
customCallback();
}
}
// Create template DOM
var divBetslip = document.createElement('div');
divBetslip.id = 'betslip';
divBetslip.innerHTML = '
{Competition?League}
{Team1?Player1?TeamHome}
- {Team2?Player2?TeamAway}
{Odds1?Odd1}
{OddsX?OddX}
{Odds2?Odd2}
' + options.dictionary.cta + '
';
document.querySelector('body').appendChild(divBetslip);
// Determine device on init and window resize
window.addEventListener('resize', onWindowResize, true);
onWindowResize();
// Bind template to feed
feed = new Feed(options.feedId, '.match', options);
// Collapse/Expand state
var pageChangeTimer = autoExpandOnLoad ? window.setInterval(showNextPage, options.pagination.pageDisplayTime * 1000) : null;
document.querySelector('#betslip .headline').addEventListener('click', function() {
expanded = !expanded;
document.querySelector('#betslip').className = expanded ? 'expanded' : 'collapsed';
// Reset page change timer
if (options.pagination.pageDisplayTime) {
if (expanded) {
if (pageChangeTimer != null) {
window.clearInterval(pageChangeTimer);
}
pageChangeTimer = window.setInterval(showNextPage, options.pagination.pageDisplayTime * 1000);
} else {
if (pageChangeTimer != null) {
window.clearInterval(pageChangeTimer);
}
}
}
});
}
/*
var eventsPerPage = 5;
var currentPage = 0;
var maxPages = 2;
var totalPages = 0;
*/
function showNextPage() {
// Determine next page
var prevPage = currentPage;
currentPage++;
if (currentPage > totalPages - 1) {
currentPage = 0;
}
// Render page
onPageChange(prevPage, currentPage);
}
function onPageChange(prevPage, newPage) {
var matches = document.querySelector('#betslip').querySelectorAll('.match');
var pageChanged = prevPage != null && prevPage != newPage;
for (var i = 0; i < matches.length; i++) {
var match = matches[i];
var onCurrentPage = i >= newPage * eventsPerPage && i < (newPage + 1) * eventsPerPage;
var onPreviousPage = pageChanged && (i >= prevPage * eventsPerPage && i < (prevPage + 1) * eventsPerPage);
var onOtherPage = !onCurrentPage && !onPreviousPage;
if (onCurrentPage) {
var isFirstMatch = (i == newPage * eventsPerPage);
// Match is on current page
if (pageChanged) {
Tween.to(match, 0.3, {
opacity: 1,
delay: 0.1,
onStart: function(isFirst) {
this.style.display = 'block';
if (isFirst) {
this.classList.add('first');
}
},
onStartParam: isFirstMatch
});
} else {
if (isFirstMatch) {
match.classList.add('first');
}
match.style.display = 'block';
}
} else if (onPreviousPage) {
// Match is on previous page
Tween.to(match, 0.1, {
opacity: 0,
onComplete: function() {
if (this.classList.contains('first')) {
this.classList.remove('first');
}
this.style.display = 'none';
}
});
} else if (onOtherPage) {
match.style.display = 'none';
}
}
}
function onFeedUpdate() {
var slip = document.querySelector('#betslip');
// Show betslip if matches are available
slip.style.opacity = feed.items().length > 0 ? 1 : 0;
if (autoExpandOnLoad) {
expanded = true;
autoExpandOnLoad = false;
window.setTimeout(function() {
slip.querySelector('.body').classList.add('init-animation');
slip.className = 'expanded';
window.setTimeout(function() {
slip.querySelector('.body').classList.remove('init-animation');
}, 1.1 * 1000);
}, 800);
} else {
slip.className = expanded ? 'expanded' : 'collapsed';
}
// Hide odds with empty content (eg. Odd X for Tennis matches)
var oddsButtons = slip.querySelectorAll('.odd');
for (var i=0; i < oddsButtons.length; i++) {
oddsButtons[i].style.opacity = oddsButtons[i].innerHTML == '' ? 0 : 1;
}
// Pagination
var matches = slip.querySelectorAll('.match');
var numMatches = matches.length;
if (isMobile) {
eventsPerPage = 1
}
totalPages = Math.floor(numMatches / eventsPerPage);
if (totalPages == 0 && numMatches > 0) {
totalPages = 1;
}
if (currentPage > totalPages - 1) {
currentPage = 0;
}
// Render page
onPageChange(null, currentPage);
onWindowResize();
}
function onWindowResize() {
var width = document.body.clientWidth;
isMobile = (width <= 800);
document.querySelector('.matches').style.height = 'auto';
var matchHeight = document.querySelector('.matches').clientHeight;
document.querySelector('.matches').style.height = matchHeight + 'px';
}
// Tweening library
var Tween = {
tweens: [],
kill: function(el) {
var tweenData = this.tweens.filter(function(tw) {
return tw.el === el;
});
if (tweenData.length > 0) {
for (var i = 0; i < tweenData.length; i++) {
tweenData[i].stop = true;
}
}
},
to: function(el, duration, options) {
var tweenData = {
el: el,
stop: false
}
this.tweens.push(tweenData);
duration *= 1000;
var requestAnimFrame = (function() {
return window.requestAnimationFrame || function(callback, element) {
return window.setTimeout(callback, 1000 / 60);
};
})();
window.setTimeout((function() {
if (options.onStart) {
options.onStart = options.onStart.bind(el);
if (options.onStartParam != undefined) {
options.onStart(options.onStartParam);
} else {
options.onStart();
}
}
var properties = [];
for (var prop in options) {
if (!/onComplete|onCompleteParam|onStart|onStartParam|easing|delay/.test(prop)) {
var unit = 'px';
if (typeof options[prop] == 'string') {
unit = options[prop].replace(/[0-9\.\-]+/g, '');
options[prop] = Number(options[prop].replace(/[^0-9\.\-]+/g, ''));
if (unit.length == 0) unit = 'px';
}
if (typeof options[prop] == 'number') {
unit = null;
}
properties.push({
key: prop,
from: el.style[prop] ? Number(el.style[prop].replace(/[^0-9\.\-]+/g, '')) : 0,
to: options[prop],
unit: unit
});
}
}
//console.log(properties[0].from, properties[0].to)
var startTime = new Date().getTime();
function loop() {
var currentTime = new Date().getTime() - startTime;
var easing = options.easing ? options.easing : Tween.easeOutCubic;
for (var i = 0; i < properties.length; i++) {
var prop = properties[i];
var val = easing(currentTime, prop.from, prop.to - prop.from, duration);
el.style[prop.key] = val + prop.unit;
}
if (currentTime >= duration || tweenData.stop) {
Tween.tweens.remove(tweenData);
if (!tweenData.stop) {
tweenData.stop = true;
for (var i = 0; i < properties.length; i++) {
var prop = properties[i];
el.style[prop.key] = prop.to + (prop.unit ? prop.unit : 0);
}
if (options.onComplete) {
options.onComplete = options.onComplete.bind(el);
if (options.onComplete != undefined) {
options.onComplete(options.onCompleteParam);
} else {
options.onComplete();
}
}
}
} else {
requestAnimFrame(loop);
}
};
requestAnimFrame(loop);
}).bind(this), options.delay ? options.delay * 1000 : 0);
},
// https://github.com/danro/jquery-easing/blob/master/jquery.easing.js
easeOutBack: function(t, b, c, d, s) {
if (s == undefined) s = 1.70158;
return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
},
easeOutCubic: function(t, b, c, d) {
return c * ((t = t / d - 1) * t * t + 1) + b;
},
easeInOutQuart: function(t, b, c, d) {
if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b;
return -c / 2 * ((t -= 2) * t * t * t - 2) + b;
},
easeInOutElastic: function(t, b, c, d) {
var s = 1.70158;
var p = 0;
var a = c;
if (t == 0) return b;
if ((t /= d / 2) == 2) return b + c;
if (!p) p = d * (.3 * 1.5);
if (a < Math.abs(c)) {
a = c;
var s = p / 4;
} else var s = p / (2 * Math.PI) * Math.asin(c / a);
if (t < 1) return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
return a * Math.pow(2, - 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b;
},
easeInOutQuad: function(t, b, c, d) {
if ((t /= d / 2) < 1) return c / 2 * t * t + b;
return -c / 2 * ((--t) * (t - 2) - 1) + b;
}
}
Array.prototype.remove = function() {
var what, a = arguments,
L = a.length,
ax;
while (L && this.length) {
what = a[--L];
while ((ax = this.indexOf(what)) !== -1) {
this.splice(ax, 1);
}
}
return this;
};
}