var TBS_PLAYER="TBS_PLAYER";
var findPlayer=xmp.baseplayer.BasePlayer.findPlayer;
var eligibleVideoCount = 0;
/********************************************************************************
TBS PLAYER FUNCTIONS 
********************************************************************************/
function TBSPlayer(options, parameters, listener){
var categoryFilterArray = ['BandwidthRecorder' /*,'AjaxRequestManager', 'VideoContentMetaFileLoader'*/]; // allows filtering out of categories with too many messages.
TBSPlayer.ctor.call(this, TBS_PLAYER, options, parameters);
this.listener=listener;
}
xmp.DERIVE_CLASS(xmp.baseplayer.BasePlayer,TBSPlayer);
TBSPlayer.prototype.finiEx = function() {
TBSPlayer.base.fini.call(this);
};
TBSPlayer.prototype.handleRegisterNodeListeners = function(playerCtx) {
if (playerCtx === this.getMainPlayerContext()) {
var listeners = [{object: new AdNodeListener(this), nodes:["PreRoll", "PostRoll", "Promo", "SponsoredAd", "HouseAd", "AdInsertNotForSale"]},
{object: new VideoNodeListener(this), nodes:["Content"]},
{object: new NextUpSlateNodeListener(this), nodes:["NextUpSlate"]}];
for (var i = 0; i < listeners.length; i++){
this.registerNodeListener(listeners[i].object, listeners[i].nodes, playerCtx );
}
} else { 
// Overlay
}
};
TBSPlayer.prototype.handleMediaPlayerReady = function(playerCtx){
if (playerCtx === this.getMainPlayerContext()) {
this.setVolume(50, playerCtx);
callScrubbarMethod('handleSetVolume', 50);
this.setMute(false, playerCtx);
}
this.setViewportConfig(xmp.SWF_MIME_TYPE, xmp.DHTML_VIEWPORT, playerCtx);
this.setViewportConfig(xmp.FLASH_MIME_TYPE, xmp.DHTML_VIEWPORT, playerCtx);
this.setViewportConfig(xmp.WINDOWS_MEDIA_MIME_TYPE, xmp.DHTML_VIEWPORT, playerCtx);
this.setViewportConfig(xmp.IMAGE_MIME_TYPE, xmp.DHTML_VIEWPORT, playerCtx);
this.setViewportConfig(xmp.DHTML_MIME_TYPE, xmp.DHTML_VIEWPORT, playerCtx);
};
TBSPlayer.prototype.handleCreateViewport = function(type, name, parameters, settings, playerCtx ) {
if (playerCtx !== this.getMainPlayerContext()) {
return null;
} else if (type === xmp.FLASH_VIEWPORT) {
return this.createFlashViewport(type, name, parameters, settings);
} else if (type === xmp.DHTML_VIEWPORT) {
return this.createDHTMLViewport(type, name, parameters, settings);
}
return null;
};
TBSPlayer.prototype.handleDestroyViewport = function(type, name, playerCtx){
var viewport = ((navigator.appName.indexOf(xmp.MICROSOFT_BROWSER) !== -1 || 
navigator.appName.indexOf(xmp.OPERA_BROWSER) !== -1) ? 
window[name] : document[name]);
if (viewport) {
var parent = ((navigator.appName.indexOf(xmp.MICROSOFT_BROWSER) !== -1 || 
navigator.appName.indexOf(xmp.OPERA_BROWSER) !== -1) ? 
viewport.parentElement : viewport.parentNode);
parent.removeChild(viewport);
}
};
TBSPlayer.prototype.handleBootstrapped = function(success) {
this.listener.onBootstrapped(success);
};
TBSPlayer.prototype.handleFatalException = function(err) {
if (err.name === "PlugInNotFoundError" || err.name === "InvalidPlugInVersionError") {
window.location = err.plugIn.downloadUrl;
}
};
TBSPlayer.prototype.createFlashViewport = function(type, name, parameters, settings) {
var viewport = new SWFObject(xmp.getSWFRoot() + "TestFlashViewport.swf", name, "320", "240", 8, "#FFFFFF");
for (var i = 0; i < parameters.length; i++) {
viewport.addParam(parameters[i].name, parameters[i].value);
}
for (i = 0; i < settings.length; i++) {
viewport.addVariable(settings[i].name, settings[i].value);
}
// removed per Chad and 113007 XMP release
//viewport.addVariable(xmp.FLASH_NATIVE_PLAYER_COMPONENT, xmp.makeSWFPath() + "FlashMediaPlayer.swf");
viewport.write(document.getElementById("TBS_CONTAINER").id);
// Store reference to flash container to use later to set container flash content.
flash_container = xmp.findDomElement(name);
flash_container.style.cssText = "position: absolute";
return flash_container;	
};
TBSPlayer.prototype.createDHTMLViewport = function(type, name, parameters, settings) {
var viewport = document.createElement("div");
viewport.setAttribute(xmp.ID_ATTRIBUTE, name);
viewport.setAttribute(xmp.NAME_ATTRIBUTE, name);
// removed per Chad and 113007 XMP release
//viewport.setAttribute(xmp.FLASH_NATIVE_PLAYER_COMPONENT, xmp.makeSWFPath()+"FlashMediaPlayer.swf");
var sample_container = document.getElementById("TBS_CONTAINER");
viewport.style.position = "absolute";
viewport.style.backgroundColor = "#000000";
viewport.style.width = "320px";
viewport.style.height = "240px";	
// Add viewport to basic container.
tkutils.insertDomChildBefore(sample_container, viewport);
return viewport;	
};
TBSPlayer.prototype.seekEx = function() {
var time = callScrubbarMethod('handleGetTime', false);
this.seek(time, this.getMainPlayerContext());
};
TBSPlayer.prototype.setVolumeEx = function(){
var volume = callScrubbarMethod('handleGetVolume', false);
if (volume != undefined) {
this.setVolume(volume, this.getMainPlayerContext());
}
};
TBSPlayer.prototype.setMuteEx = function() {
this.setMute(true, this.getMainPlayerContext());
callScrubbarMethod('handleSetVolume', 0);
};
TBSPlayer.prototype.setFullScreenEx = function() {
if (this.isAvailable(xmp.FULLSCREEN, this.getMainPlayerContext())) {
alert("Entering FullScreen. Press ESC to exit FullScreen.");
this.setFullscreen(true, this.getMainPlayerContext());
} else {
// for flash video
alert("Double-click the video to enter FullScreen. Press ESC to exit FullScreen.");
}
};
TBSPlayer.prototype.playEx = function() {
this.getViewport(this.getMainPlayerContext()).hideOverlays(xmp.ALL_OVERLAYS);	
callScrubbarMethod('handleScrubEnabled', true);
this.play(this.getMainPlayerContext());
};
TBSPlayer.prototype.pauseEx = function() {
callScrubbarMethod('handleScrubEnabled', false);
this.pause(this.getMainPlayerContext());
};
TBSPlayer.prototype.disableAllCommands = function() {
callScrubbarMethod('handleScrubEnabled', false);
callScrubbarMethod('handleTogglePlay', false);
callScrubbarMethod('handleSetTRT', 0);
callScrubbarMethod('handleTimelineChange', 0);
};
TBSPlayer.prototype.handlePlayListEnded = function() {
nextVideo();
};
/********************************************************************************
BASE NODE LISTENER 
********************************************************************************/
function BaseNodeListener(name, player, disable_commands) {
BaseNodeListener.ctor.call(this, name, player);
this.disable_commands = disable_commands;
}
xmp.DERIVE_CLASS(xmp.baseplayer.listeners.AbstractNodeListener, BaseNodeListener);
BaseNodeListener.prototype.handleDynamicRules = function(node){};
BaseNodeListener.prototype.handleRender = function(node){
this.getPlayer().open(node, this.getPlayerContext());
};
BaseNodeListener.prototype.handleOpen = function(node) {
if (true === this.disable_commands){
this.getPlayer().disableAllCommands();
}
this.setStatus(node,"Opened");
};
BaseNodeListener.prototype.handleConnecting = function(node) {};
BaseNodeListener.prototype.handleConnected = function(node) {};
BaseNodeListener.prototype.handlePlay = function(node){};
BaseNodeListener.prototype.handlePause = function(node){};
BaseNodeListener.prototype.handleStop = function(node){};
BaseNodeListener.prototype.handleRewind = function(node){};
BaseNodeListener.prototype.handleFastForward = function(node){};
BaseNodeListener.prototype.handleSeekable = function(node){};
BaseNodeListener.prototype.handleEnded = function(node) {
callScrubbarMethod('handleScrubEnabled', false);
callScrubbarMethod('handleTogglePlay', false);
callScrubbarMethod('handleSetTRT', 0);
callScrubbarMethod('handleTimelineChange', 0);
this.getPlayer().advance();
};
BaseNodeListener.prototype.handleBuffering = function(node, buffStateObj) { 
var amount = 0.01 * buffStateObj.percent;
this.setStatus(node, amount);
};
BaseNodeListener.prototype.handleTimelineChange = function(node, position, duration){
callScrubbarMethod('handleTogglePlay', true);
callScrubbarMethod('handleSetTRT', duration);
callScrubbarMethod('handleTimelineChange', position);
};
BaseNodeListener.prototype.handleError = function(node, err) {
this.getPlayer().advance();
};
BaseNodeListener.prototype.setStatus = function(node, status) {};
BaseNodeListener.prototype.loadSynchUnit = function(node) {
var res = node.getMetaResource('synch_unit');	
if (!res) {
return;
}
if(res.isError()) {
return;
}	
var data = res.getDataObject();
if(res.getMetadata('played_synch_unit',false)) {
return;
}
res.setMetadata('played_synch_unit', true);
this._createBannerAd("banner_ad_iframe" , data);
};
BaseNodeListener.prototype._createBannerAd = function(adId, cnnad_url){
var adBanner = document.getElementById("banner_ad_div");
var html = '<iframe hspace="0" vspace="0" marginHeight="0" marginWidth="0" src="'
+ cnnad_url + '&page.allowcompete=yes&domId=' 
+ adId + '" border="0" frameBorder="0" width="300" height="250" scrolling="no" id="'
+ adId + '" style="position: absolute;" ></iframe>';
adBanner.innerHTML = html;
};
/********************************************************************************
AdNodeListener
- drives ad videos
*******************************************************************************/
var AD_NODE_LISTENER = "Ad";
function AdNodeListener(player) {
AdNodeListener.ctor.call(this, AD_NODE_LISTENER, player, true);
this.PreRollFound = false;
}
xmp.DERIVE_CLASS(BaseNodeListener, AdNodeListener);
AdNodeListener.prototype.canUserControl=function(node){
var canControl = xmp.baseplayer.BasePlayer.getSettingsManager().getContextNode().getNodeForPath('BasePlayer').getBoolean('user can control ad', false);
return canControl;
};
AdNodeListener.prototype.handleInitialize = function(node) {};
AdNodeListener.prototype.handlePlay = function(node) {
callScrubbarMethod('handleScrubEnabled', false);
};
AdNodeListener.prototype.handleComplete = function(node) {
var data = node.getPlayableData();
node.setURI( data.getPlayableId() );
node.setMimeTypes( data.getMimeTypeArray() );
node.setStreamingMode( xmp.AD_STREAMING_MODE ); 
this.loadSynchUnit(node);
callScrubbarMethod('handlePlayEnabled', true);
callScrubbarMethod('handleScrubEnabled', false);
callScrubbarMethod('handleSetTRT', node.getPlayableData().getDataObject().trt);
};
AdNodeListener.prototype.handleRender = function(node) {
var res = node.getMetaResource('primary');
AdNodeListener.base.handleRender.call(this, node);
};
AdNodeListener.prototype.handleDynamicRules = function(node){
var nodeTypeId = node.getNodeTypeId();
if ((nodeTypeId === 'PreRoll') && !this.PreRollFound) {
this.PreRollFound=true;
if (TBSRules.DontPlayRule.isActive()) {
var ruleContext = null;
this.getPlayer().registerRuleClass("DontPlayRule", "TBSRules.DontPlayRule");
node.addNewRule("DontPlayRule",null);
node.addRule(ruleContext);
}
}
};
AdNodeListener.prototype.handleOverrideAdResources = function(node, overrideContext) {
var contextName = overrideContext.expandString('${player.context_name}');
var nodeTypeId = node.getNodeTypeId();
if (contextName.indexOf('Mosaic') !== -1){
if (nodeTypeId === 'PreRoll') {
overrideContext.getResource('primary').setId('');
overrideContext.getResource('synch_unit').setId('');
}
}
};
/********************************************************************************
NextupSlateNodeListener
- drives DHTML object between video content pieces
*******************************************************************************/
var NEXTUP_SLATE_NODE_LISTENER = "NextUpSlate";
var HEADLINE = "headline";
function NextUpSlateNodeListener(player) {
NextUpSlateNodeListener.ctor.call(this, NEXTUP_SLATE_NODE_LISTENER, player);
}
xmp.DERIVE_CLASS(xmp.baseplayer.listeners.DefaultSlateNodeListener, NextUpSlateNodeListener);
NextUpSlateNodeListener.prototype.handleComplete = function(node) {
xmp.baseplayer.listeners.DefaultNextUpSlateNodeListener.base.handleComplete.call(this, node);
var op = this.i_$18C();
if(op !== null){
node.setMetadata("headline", op.getPlayableData().getDataObject().headline);
}
callScrubbarMethod('handleScrubEnabled', false);
callScrubbarMethod('handlePlayEnabled', false);
callScrubbarMethod('handleSetTRT', 0);
callScrubbarMethod('handleTimelineChange', 0);
};
/********************************************************************************
VideoNodeListener
- drives all video content nodes
*******************************************************************************/
var VIDEO_NODE_LISTENER = "Video";
function VideoNodeListener(player) {
VideoNodeListener.ctor.call(this, VIDEO_NODE_LISTENER, player, false);
}
xmp.DERIVE_CLASS(BaseNodeListener, VideoNodeListener);
VideoNodeListener.prototype.handleEnded = function(node) {
callScrubbarMethod('handleScrubEnabled', false);
callScrubbarMethod('handleTogglePlay', false);
callScrubbarMethod('handleSetTRT', 0);
callScrubbarMethod('handleTimelineChange', 0);
// check trt and category to see if this is an ad eligible clip
// doing this check here because a video must also finish to be eligible
var trt = node.getPlayableData().getDataObject().trt;
var category = node.getPlayableData().getDataObject().category;
if (trt > 50 && category == 'clip') {
eligibleVideoCount++;
node.setMetadata('eligibleVideoCount', eligibleVideoCount); 
} 
this.getPlayer().advance();
};
VideoNodeListener.prototype.handleInitialize = function(node) {
var item = node.getPlayableData();
node.setMimeTypes(item.getMimeTypeArray());
node.setURI(item.getDataObject().location);
node.setStreamingMode(item.getDataObject().streamingMode);
//node.setSizes(item.getDataObject().sizes);
var sizes = item.getDataObject().sizes;
if (sizes && sizes.length > 0) {
node.i_$UZ(item.getDataObject().sizes);
}
document.getElementById("DRM_ERROR_MSG").style.visibility = 'hidden';
document.getElementById("TBS_CONTAINER").style.visibility = 'visible';
};
VideoNodeListener.prototype.handleComplete = function(node) {
this.handleInitialize(node);
callScrubbarMethod('handlePlayEnabled', true);
callScrubbarMethod('handleScrubEnabled', true);
callScrubbarMethod('handleSetTRT', node.getPlayableData().getDataObject().trt);
if (navigator.platform.indexOf("Mac") != -1 
&& node.getPlayableData().getDataObject().isDRM == 'true'
&& node.getPlayableData().getMimeTypeArray()[0] == 'video/x-ms-wmv') {
document.getElementById("TBS_CONTAINER").style.visibility = 'hidden';
document.getElementById("DRM_ERROR_MSG").style.visibility = 'visible';
}
};
VideoNodeListener.prototype.handleError = function(node, err) {
if (navigator.platform.indexOf("Mac") != -1 
&& node.getPlayableData().getDataObject().isDRM == 'true'
&& node.getPlayableData().getMimeTypeArray()[0] == 'video/x-ms-wmv') {
// should have been caught in handleComplete, but just in case..
document.getElementById("TBS_CONTAINER").style.visibility = 'hidden';
document.getElementById("DRM_ERROR_MSG").style.visibility = 'visible';
} else {
if (err.name === "PlugInNotFoundError" || err.name === "InvalidPlugInVersionError") {
window.location = err.plugIn.downloadUrl;
} else {
this.getPlayer().advance();
}
}
};
