// wrapper.js
function get(q)
{
try {
var s = s ? s : window.location.search;
var a = new RegExp(q+"=([^=]*)");
var rv = a.exec(s);
if (!rv) return undefined;
return rv[1];
} catch (e) {
console.log("got exception parsing url: " + e.message);
}
return undefined;
}
if (get("test1") == "true") window.prioritiseCodecs = true;
if (get("test2") == "true") window.mp4Test = true;
window.flags = false;
window.sessionId = 0;
window.videoId = "0";
window.playlistId = "0";
window.channelId = "";
window.bookmarkId = "0";
window.autoplay = "false";
window.sharingBlockedMsg = "Sharing videos from this domain has been disabled";
window.siteId = "277";
window.skinLocation = "skins/vcms.js";
window.apiServer = "https://api-v3.switchmedia.asia";
window.shortUrl = "";
window.profile = "regular";
window.playerId = "playerregular";
window.allowSharing = "false";
window.allowDownloading = "false";
window.clientType = "";
window.subs = "";
window.messageProtocol = "1";
window.messageId = false;
if (!window.clientType) window.clientType = 1;
// API calls
window.playlistUrl = "/{SITEID}/playlists/get-data?playlist={PLAYLISTID}&detail=verbose&format=json";
window.assetUrl = "/{SITEID}/assets/get-data?asset={VIDEOID}&detail=verbose&format=json";
window.channelUrl = "/{SITEID}/channels/get-data?channel={CHANNELID}&detail=verbose&format=json";
// Universal player configuration url
window.playerConfigUrl = "/{SITEID}/playback/getUniversalPlayerConfig?videoID={VIDEOID}&playlistID={PLAYLISTID}" +
"&skinType=vcms&profile=" + window.profile + "&playerID=" + window.playerId +
"&format=json&bookmarkID={BOOKMARKID}&autoplay={AUTOPLAY}";
window.extraParameters = "?channelID=400&siteID=277&tint=CDE5DD";
if (window.bookmarkId === "0" || window.bookmarkId === 0) window.bookmarkId = false;
if (window.playlistId === "0" || window.playlistId === 0) window.playlistId = false;
if (window.videoId === "0" || window.videoId === 0) window.videoId = false;
var sendMessage = function(msg, origin)
{
if (window.messageProtocol == "1") {
parent.postMessage(msg, origin);
} else if (window.messageProtocol == "2") {
var obj = {
id: window.messageId,
version: window.messageProtocol,
msg: msg
}
parent.postMessage(JSON.stringify(obj), origin);
}
}
var generateShortUrl = function()
{
var type = false;
var assetId = false;
if (window.bookmarkId * 1) {
type = "b";
assetId = window.bookmarkId;
} else if (window.playlistId * 1) {
type = "p";
assetId = window.playlistId;
} else if (window.videoId * 1) {
type = "v";
assetId = window.videoId;
}
if (type == false || assetId == false) {
console.log("could not generate short url");
return false;
}
var token = window.btoa(type+assetId);
var url = window.shortUrl.replace("{TOKEN}", token);
var player = document.getElementById("wrapperPlayer");
if (player) player.setAttribute("data-shareUrl", url);
return url;
}
var parseShortUrl = function(url)
{
var pathname = url.substr(url.lastIndexOf('/'));
if (pathname[1] != 't') return false;
try {
var token = pathname.substring(2);
var asset = window.atob(token);
var type = asset.substring(0, 1);
var id = parseInt(asset.substring(1));
if(type === 'c') {
var json = asset.substring(1);
var obj = JSON.parse(json);
if(typeof obj.v !== "undefined") {
window.videoId = obj.v;
window.bookmarkId = 0;
window.playlistId = 0;
}
if(typeof obj.p !== "undefined") {
window.videoId = 0;
window.bookmarkId = 0;
window.playlistId = obj.p;
}
if(typeof obj.a !== "undefined") {
window.autoplay = obj.a;
}
}
if (!isNaN(parseFloat(id)) && isFinite(id)) {
switch (type) {
case "v":
window.videoId = id;
window.bookmarkId = 0;
window.playlistId = 0;
break;
case "p":
window.videoId = 0;
window.bookmarkId = 0;
window.playlistId = id;
break;
case "b":
window.videoId = 0;
window.bookmarkId = id;
window.playlistId = 0;
break;
}
if (window.videoId || window.bookmarkId || window.playlistId) {
getConfig();
return true;
}
}
} catch (e) {}
return false;
}
var getPlaylistIdFromChannelId = function(id)
{
var url = window.apiServer + window.channelUrl.replace("{SITEID}", window.siteId).replace("{CHANNELID}", id);
var loc = document.createElement("a");
loc.href = window.parentLocation;
url += "&referrer=" + loc.hostname;
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
try {
var channelData = JSON.parse(request.responseText);
var playlists = channelData.playlistslist.playlistslist;
for (var i = 0; i < playlists.length; i++) {
var playlist = playlists[i];
if (playlist.assetslist.assetslist.length > 0) {
window.playlistId = playlist.playlist;
getConfig();
break;
}
}
} catch (e) {}
}
}
request.send(null);
}
var parseUrl = function()
{
var siteID = get("siteID");
var videoID = get("videoID");
var playlistID = get("playlistID");
var bookmarkID = get("bookmarkID");
var sessionID = get("sessionID");
var flags = get("flags");
var autoplay = get("autoplay");
var channelID = get("channelID");
if (siteID) window.siteId = siteID;
if (videoID) window.videoId = videoID;
if (playlistID) window.playlistId = playlistID;
if (bookmarkID) window.bookmarkId = bookmarkID;
if (sessionID) window.sessionId = sessionID;
if (flags) window.flags = decodeURIComponent(flags);
if (autoplay) window.autoplay = autoplay;
if (channelID) {
window.playlistId = getPlaylistIdFromChannelId(channelID);
return true;
} else if (window.videoId || window.bookmarkId || window.playlistId) {
getConfig();
return true;
}
return false;
}
var getStoredResumeTime = function()
{
if (undefined == window.localStorage) return false;
var rv = window.localStorage.getItem(window.videoId);
if (null == rv) rv = 0;
return rv;
}
var storeCurrentPosition = function(currentTime)
{
if (undefined == window.localStorage) return false;
if (currentTime < 30000) return true;
if (currentTime > 0) {
if (tv.switch.progresstracking) {
var delta = tv.switch.progresstracking.getDeltaForOffset("wrapperPlayer", currentTime);
if (delta) currentTime -= delta;
}
window.localStorage.setItem(window.videoId, currentTime);
}
return true;
}
var deleteStoredResumeTime = function()
{
if (undefined == window.localStorage) return false;
window.localStorage.removeItem(window.videoId);
}
// tell the hosting page that we have loaded
window.onload = function()
{
var url = (window.location != window.parent.location) ? document.referrer: document.location;
if (url) {
window.parentLocation = url;
if (!window.shortUrl) window.shortUrl = window.parentLocation;
// parse the url for the VCMS format - check if first entry in path is an integer
var a = document.createElement("a");
if (a) {
a.href = url;
if (a.pathname) {
var elems = a.pathname.split("/");
try {
var possibleSiteId = elems[1];
var i = parseInt(possibleSiteId);
if (i.toString() == possibleSiteId) window.siteId = i;
} catch (e) {}
}
}
}
if (!parseShortUrl(url)) {
url = window.location.toString();
if (!parseShortUrl(url)) {
parseUrl();
}
}
window.sendMessage("loaded", "*");
}
window.onbeforeunload = function()
{
/* this will destroy the player and allow it to perform any teardown tasks
* prior to the browser navigating away from the current page
*/
tv.switch.deInitialize();
}
var handleResize = function(e)
{
if (window.inResize) return;
window.inResize = true;
window.realBody = document.getElementsByTagName("body")[0];
var parentNode = window.realBody.parentNode;
window.realBody.style.visibility = "hidden";
window.realBody.style.display = "none";
var emptyBody = document.createElement("body");
emptyBody.setAttribute("id", "emptyBody");
parentNode.appendChild(emptyBody);
var triggerReflow = window.realBody.offsetHeight;
window.realWidth = window.innerWidth;
window.realHeight = window.innerHeight;
var emptyBody = document.getElementById("emptyBody");
if (emptyBody) emptyBody.parentNode.removeChild(emptyBody);
window.realBody.style.visibility = "";
window.realBody.style.display = "";
triggerReflow = window.realBody.offsetHeight;
resizePlayer(window.realWidth, window.realHeight);
window.inResize = false;
}
var resizePlayer = function(w, h)
{
var player = document.getElementById("universalPlayerContainer");
var playlist = document.getElementById("playlistContainer");
if (playlist) {
var box = playlist.getBoundingClientRect();
w -= box.width;
if (w <= 0) {
// if the container is too small, hide the playlist
playlist.style.visibility = "hidden";
w += box.width;
}
}
player.style.width = w + "px";
player.style.height = h + "px";
if (playlist) playlist.style.height = (h - 1) + "px";
}
var handleResizeOld = function(e, reentrant)
{
// 1. get the container width and height
// 2. do we have a playlist? yes, subtract the playlist width from container width
// 3. set universalPlayerContainer width and accordingly
// 4. set the playlist height to match the player height
if (undefined == reentrant) {
var player = document.getElementById("universalPlayerContainer");
var playlist = document.getElementById("playlistContainer");
player.style.width = "";
player.style.height = "";
if (playlist) {
playlist.style.width = "";
playlist.style.height = "";
}
window.setTimeout(function() {
handleResize(e, true);
}, 100);
return;
}
var w = window.innerWidth;
var h = window.innerHeight;
var player = document.getElementById("universalPlayerContainer");
var playlist = document.getElementById("playlistContainer");
if (playlist) {
var box = playlist.getBoundingClientRect();
w -= box.width;
if (w <= 0) {
// if the container is too small, hide the playlist
playlist.style.visibility = "hidden";
w += box.width;
}
}
player.style.width = w + "px";
player.style.height = h + "px";
if (playlist) playlist.style.height = h + "px";
// report back to the parent if our minimum height has changed
if (h != document.body.currentHeight) {
document.body.currentHeight = h;
window.sendMessage("height " + h, "*");
}
}
// handle messages from the hosting page. This allows the page
// to set initial video or playlist entries
var handleMessage = function(e)
{
var args = e.data.split(" ");
switch (args[0]) {
case "set":
switch (args[1]) {
case "iframeRects":
args.shift(); args.shift();
window.iframeRects = JSON.parse(args.join(" "));
break;
case "protocol":
window.messageProtocol = args[2];
window.messageId = args[3];
break;
case "playerConfig":
window.playlistId = 0;
window.videoId = 0;
window.bookmarkId = 0;
args.shift(); args.shift();
setPlayerConfig(args.join(" "));
break;
case "playlist":
window.playlistId = args[2];
window.videoId = 0;
window.bookmarkId = 0;
getConfig();
break;
case "video":
window.playlistId = 0;
window.videoId = args[2];
window.bookmarkId = 0;
//getConfig();
break;
case "bookmark":
window.playlistId = 0;
window.videoId = 0;
window.bookmarkId = args[2];
getConfig();
break;
case "cuePoints":
var cuePoints = args[2].split(",");
tv.switch.skin.setProgressBarCuePoints("wrapperPlayer", cuePoints);
break;
case "markers":
var type = args[2];
var color = args[3];
var markers = args[4].split(",");
tv.switch.skin.setProgressBarMarkers("wrapperPlayer", type, color, markers);
break;
case "volume":
var vol = args[2];
tv.switch.player.setVolume("wrapperPlayer", vol);
break;
default:
console.log("unhandled set: " + args[1]);
break;
}
break;
case "player":
switch (args[1]) {
case "play":
tv.switch.player.play("wrapperPlayer", (+args[2]));
break;
case "seek":
tv.switch.player.skipTo("wrapperPlayer", (+args[2]));
tv.switch.skin.invalidateUI("wrapperPlayer");
break;
case "pause":
tv.switch.player.pause("wrapperPlayer");
break;
case "stop":
tv.switch.player.stop("wrapperPlayer");
break;
case "playpause":
tv.switch.player.togglePlayPause("wrapperPlayer");
break;
}
break;
case "get":
switch (args[1]) {
case "playerConfig":
window.sendMessage("playerConfig " + JSON.stringify(window.playerConfig), "*");
break;
case "playerPosition":
window.sendMessage("playerPosition " + tv.switch.player.getPosition("wrapperPlayer"), "*");
break;
case "currentFrameAsPNG":
var res = tv.switch.player.getFramePNG("wrapperPlayer", null, args[2], args[3], args[4]);
window.sendMessage("framePNG " + args[4] + " " + res, "*");
break;
}
break;
default:
//console.log("unhandled command: " + args[0]);
break;
}
}
var addParam = function(url, param)
{
var loc = document.createElement("a");
loc.href = url;
if (!loc.search) url += "?" + param;
else url += "&" + param;
return url;
}
var patchConfig = function()
{
if (window.playerConfig.trimpoints) {
if (window.playerConfig.trimpoints.start === "0" ||
window.playerConfig.trimpoints.start === 0) {
if (window.playerConfig.trimpoints.end === "0" ||
window.playerConfig.trimpoints.end === 0) {
delete window.playerConfig.trimpoints;
delete window.playerConfig.trimMode;
}
}
}
if (window.sessionId) {
if (window.playerConfig.media && window.playerConfig.media.renditions) {
for (var i = 0; i < window.playerConfig.media.renditions.length; i++) {
if (window.playerConfig.media.renditions[i].url instanceof Array) {
for (var j = 0; j < window.playerCofig.media.renditions[i].url.length; j++) {
var url = addParam(window.playerConfig.media.renditions[i].url[j], "sessionID=" + window.sessionId);
window.playerConfig.media.renditions[i].url[j] = url;
}
} else {
var url = addParam(window.playerConfig.media.renditions[i].url, "sessionID=" + window.sessionId);
window.playerConfig.media.renditions[i].url = url;
}
}
}
}
if (window.playerConfig.media) {
window.showDownloadButton = false;
window.showShareButton = false;
window.automaticallyPlay = false;
if (window.playerConfig.media.downloadButton === "true" || window.playerConfig.media.downloadButton === true) {
window.showDownloadButton = true;
}
if (window.playerConfig.media.shareButton === "true" || window.playerConfig.media.shareButton === true) {
window.showShareButton = true;
}
if (window.playerConfig.media.autoPlay === "true" || window.playerConfig.media.autoPlay === true) {
window.playerConfig.media.autoPlay = false;
window.automaticallyPlay = true;
}
}
// also check playlist entries for share, download, and autoPlay
if (window.playerConfig.playlist) {
for (var i = 0; i < window.playerConfig.playlist.length; i++) {
var entry = window.playerConfig.playlist[i];
if (!entry) continue;
if (entry.media.downloadButton === "true" || entry.media.downloadButton === true) {
window.showDownloadButton = true;
}
if (entry.media.shareButton === "true" || entry.media.shareButton === true) {
window.showShareButton = true;
}
if (entry.media.autoPlay === "true" || entry.media.autoPlay === true) {
window.automaticallyPlay = true;
}
}
}
try {
if (window.playerConfig.media.resumePlayback === "true" || window.playerConfig.media.resumePlayback === true) {
var playPosition = getStoredResumeTime() * 1;
if (playPosition) {
console.log("INFO: going to automatically resume playback at offset " + playPosition);
window.playerConfig.media.rawPlayPosition = playPosition;
} else {
window.playerConfig.media.rawPlayPosition = 0;
}
}
} catch (e) {}
return window.playerConfig;
}
var setPlayerConfig = function(text)
{
window.playerConfig = JSON.parse(text);
window.playerConfig = patchConfig();
loadPlaylistData(window.playlistId);
loadVideoData(window.videoId);
tv.switch.initializeWithConfig(window.playerConfig);
}
var checkAdBlocking = function()
{
if (tv.switch.blockMode) {
console.log("adblocking detected, blockMode = true");
window.clientType = 2;
} else {
var obj = document.getElementById("advertising");
if (obj) {
var style = window.getComputedStyle(obj, null);
if (style.display == "none") {
console.log("adblocking detected, clientType = adblock");
window.clientType = 2;
} else {
console.log("no adblocking detected");
}
}
}
}
var getConfig = function()
{
var url = window.apiServer + window.playerConfigUrl.replace("{SITEID}", window.siteId).replace("{VIDEOID}", window.videoId || "0").replace("{PLAYLISTID}", window.playlistId || "0").replace("{BOOKMARKID}", window.bookmarkId || "0").replace("{AUTOPLAY}", window.autoplay);
if (window.flags) url = addParam(url, window.flags);
if (window.sessionId) url = addParam(url, "sessionID=" + window.sessionId);
if (window.videoData) delete window.videoData;
if (window.playlistData) delete window.playlistData;
var loc = document.createElement("a");
loc.href = window.parentLocation;
if (window.flags && window.flags.indexOf("ad_ease") > 0) {
url += "&referrer=ad_ease";
} else if (loc.hostname) {
url += "&referrer=" + window.parentLocation.replace(/\?.*$/, '');
}
// Check for the debug flag
var debug = false;
if (get("debug") != null) {
debug = true;
// Create an instance of the debugger if it doesn't already exist
if (window.adeaseDebugInstance == null && window.adeaseDebug != null) {
window.adeaseDebugInstance = new adeaseDebug.AdeaseDebugger();
var debugEL = document.getElementById('adease-debug');
if (debugEL != null) {
window.adeaseDebugInstance.attach(debugEL);
}
}
}
if (window.extraParameters) {
// remove the question mark if needed
if (window.extraParameters.indexOf('?') == 0) {
url += "&" + window.extraParameters.slice(1);
} else if (window.extraParameters.indexOf("&") == 0) {
url += window.extraParameters;
} else {
url += "&" + window.extraParameters;
}
}
checkAdBlocking();
url += "&cl=" + window.clientType;
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
try {
window.playerConfig = JSON.parse(request.responseText);
// Add the config to the adease debugging system
if (debug && window.adeaseDebugInstance != null) {
adeaseDebugInstance.setConfig(JSON.parse(JSON.stringify(window.playerConfig)));
}
} catch (e) {
window.playerConfig = {
ErrorCode : tv.switch.errorCodes.networkConfigurationError,
Message : "Failed to receive configuration from network",
}
}
if (window.playerConfig.ErrorCode) {
var title = "Oops";
if (window.playerConfig.Title) title = window.playerConfig.Title;
displayErrorMessage(title, window.playerConfig, true);
return;
}
loadPlaylistData(window.playlistId);
loadVideoData(window.videoId);
window.playerConfig = patchConfig();
// we no longer want to automatically play bookmarks
//if (window.bookmarkId && window.bookmarkId !== "0") window.playerConfig.media.autoplay = true;
if (window.skinLocation && window.playerConfig.skin) {
console.log("patching config for skin location", window.skinLocation);
window.playerConfig.skin.HTML=window.skinLocation;
}
tv.switch.initializeWithConfig(window.playerConfig);
var shareUrl = generateShortUrl();
var player = document.getElementById("wrapperPlayer");
if (window.playerConfig.media) {
if (window.playerConfig.media.downloadButton === "true" || window.playerConfig.media.downloadButton ===true) {
window.allowDownloading = true;
}
if (window.playerConfig.media.shareButton === "true" || window.playerConfig.media.shareButton ===true) {
window.allowSharing = true;
}
}
if (window.allowSharing === true || window.allowSharing === "true") {
if (player) {
player.setAttribute("data-shareUrl", shareUrl);
player.setAttribute("data-allowSharing", true);
player.setAttribute("data-shortUrl", window.shortUrl);
}
} else {
if (player) {
player.removeAttribute("data-shareUrl");
player.removeAttribute("data-allowSharing");
player.removeAttribute("data-shortUrl");
}
}
if (window.allowDownloading === true || window.allowDownloading === "true") {
if (player) player.setAttribute("data-allowDownloading", true);
} else {
if (player) player.removeAttribute("data-allowDownloading");
}
}
}
request.send(null);
}
var loadPlaylistData = function(playlistId)
{
var playlistContainer = document.getElementById("playlistContainer");
if (!playlistId || playlistId == "0") {
if (playlistContainer) {
playlistContainer.style.visibility = "hidden";
playlistContainer.style.display = "none";
}
//handleResize();
return;
} else {
if (playlistContainer) {
playlistContainer.style.visibility = "visible";
playlistContainer.style.display = "block";
}
}
var url = window.apiServer + window.playlistUrl.replace("{SITEID}", window.siteId).replace("{PLAYLISTID}", playlistId);
var loc = document.createElement("a");
loc.href = window.parentLocation;
if (loc.hostname) url += "&referrer=" + loc.hostname;
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
window.playlistData = JSON.parse(request.responseText);
if (window.playlistData.playlistinfo) {
//synchronisePlaylistData();
populatePlaylist();
}
//handleResize();
}
}
request.send(null);
}
var synchronisePlaylistData = function()
{
try {
if (window.playerConfig.playlist && window.playerConfig.playlist.length > 0) {
var c1 = window.playerConfig.playlist.length;
var c2 = window.playlistData.assetslist.length;
if (c1 != c2) {
console.log("API discrepancy");
var newlist = [];
for (var i = 0; i < window.playlistData.assetslist.length; i++) {
var entry = window.playlistData.assetslist[i];
if (entry.state == "APPROVED") newlist.push(entry);
}
window.playlistData.assetslist = newlist;
}
}
} catch (e) {
}
}
var populatePlaylist = function()
{
var text = window.playlistData.playlistinfo.image;
if (window.playlistHeadingImg) {
if (undefined == text) {
text = "about:blank";
}
playlistHeadingImg.setAttribute("src", text);
}
text = window.playlistData.playlistinfo.title;
if (!text) text = window.playlistData.playlistinfo.name;
if (undefined != text) {
if (window.playlistHeadingText) {
var child = playlistHeadingText.firstChild;
while (child) {
try {
if (child.nodeType == 3) {
child.nodeValue = text;
break;
}
} catch (e) {}
child = child.nextSibling;
}
}
}
var videoId = false;
if (window.playlistData.assetslist) {
if (playlist) {
playlist.innerHTML = "";
var totalVideos = window.playlistData.assetslist.length;
var totalDuration = 0;
for (var i = 0; i < window.playlistData.assetslist.length; i++) {
var entry = window.playlistData.assetslist[i];
var div = document.createElement("div");
totalDuration += (entry.duration * 1);
if (videoId === false) videoId = entry.asset;
if (div) {
div.setAttribute("class", "playlistEntry");
div.setAttribute("id", "playlistEntry" + i);
div.setAttribute("onclick", "onPlaylistEntryPressed(this);");
div.setAttribute("index", i);
var innerHTML = "";
if (entry.image) {
innerHTML += "";
}
var text = entry.title;
var description = entry.shortdescription;
if (!description) description = "";
if (text || description) {
innerHTML += "