/********************************************************************************
 ** BaseNodeListener 
 ** - Superclass for other listeners
 *******************************************************************************/

// Declare find player.
var findPlayer = xmp.baseplayer.BasePlayer.findPlayer;

function CNNPlayerListener( name, player ) {
	CNNPlayerListener.ctor.call( this, name, player );
}

// Derive "CNNPlayerListener" from "AbstractNodeListener".
xmp.DERIVE_CLASS( xmp.baseplayer.listeners.DefaultPlayerListener, CNNPlayerListener );

CNNPlayerListener.prototype.setStatus = function( node, status, details ) {
	CNNPlayerListener.base.setStatus.call(this, node, status, details);
};

CNNPlayerListener.prototype.handleEnded = function( node ) {
	callControllerMethod('setEnabled',false);
	callControllerMethod('setEmailEnabled',true);
	callControllerMethod('setTime',0);
	if (CNNPlaylistManager.getInstance().controller == null && node.getNodeTypeId() != "GenericSlate") {
		callControllerMethod('togglePlay',false);	
	}
};
CNNPlayerListener.prototype.handleBuffering = function( node, buffStateObj ) {
	callControllerMethod('setLoadedPercent',(0.01*buffStateObj.percent));
};
CNNPlayerListener.prototype.handleTimelineChange = function( node, position, duration ) {
	if (node.getNodeTypeId() != "GenericSlate") {
		if (CNNPlayer.getActivePlayer().isScrubbing == false) {
			if (CNNPlaylistManager.getInstance().controller == null) { callControllerMethod('togglePlay',true); }
			callControllerMethod('setTime',position);
		}
	}
};
CNNPlayerListener.prototype._findNextContentNode = function( ) {	
	var lookAhead = this.getPlayer().getLookAheadNodeArray();	
	for ( var i = 0; i < lookAhead.length; i++ ) {
		var node = lookAhead[i];
		if ( node.isContentType() ) {
			return node;
		}
	}	
	return null;
};
CNNPlayerListener.prototype._findPreviousContentNode = function( ) {	
	var lookBehind = this.getPlayer().getLookBehindNodeArray();
	for ( var i = 0; i < lookBehind.length; i++ )
	{
		var node = lookBehind[i];
		if ( node.isContentType() ) {
			return node;
		}
	}
	return null;
};

/********************************************************************************
 ** AdNodeListener
 ** - drives ad videos
 *******************************************************************************/

function AdNodeListener(name, player ) {
	AdNodeListener.ctor.call( this, name, player );
	this._logger = new xmp.util.internals.CategoryLogger( 'AdNodeListener' );
}

xmp.DERIVE_CLASS( xmp.baseplayer.listeners.DefaultAdNodeListener, AdNodeListener );

AdNodeListener.prototype.handleOpen = function( node ) {
	if (node.getNodeTypeId() != "AdInsertNotForSale" && CNNPlaylistManager.getInstance().SAConfiguration.loadSynchUnit != false) {
		this.getPlayer().loadSynchUnit(node);
	}
}

AdNodeListener.prototype.handleRender = function(node){
	AdNodeListener.base.handleRender.call(this, node);

	// atlas / clickable ad integration
	var dataObject = node.getPlayableData().getDataObject();
	if ((typeof(dataObject.clickURL) != 'undefined') && (dataObject.clickURL.length > 0)) {
		var divObj = document.createElement('div');
		var div_height = CNNPlaylistManager.getInstance().atlasSlateSize[1];
		var div_width = CNNPlaylistManager.getInstance().atlasSlateSize[0];
		divObj.setAttribute('id','atlasLayer');
		divObj.style.height = div_height;
		divObj.style.width = div_width;
		divObj.style.position = "absolute";
		divObj.style.top = "1px";
		divObj.style.left = "1px";
		divObj.style.cursor = "pointer";
		divObj.innerHTML = '<img src="http://i.cdn.turner.com/cnn/.e/img/2.0/global/1x1pixel.gif" width="' + div_width + '" height="' + div_height + '">';
		divObj.style.cursor = "pointer";
		divObj.onclick = function(){
			CNNPlaylistManager.getInstance().atlasClick();
		};
		CNNPlaylistManager.getInstance().atlasURL = dataObject.clickURL;
		CNNPlaylistManager.getInstance().viewport.appendChild(divObj);
		CNNPlaylistManager.getInstance().atlasActive = true;
	}

	// comscore tracking
	var is_video = false;
	CNN.ComscoreTriggerCommand.pingBeacon(is_video, '');
}

AdNodeListener.prototype.handlePlay = function( node ) {
	callControllerMethod('setScrubberEnabled',false);
};

AdNodeListener.prototype.handleComplete = function( node ) {	
	// atlas integration
	CNNPlaylistManager.getInstance().killAtlas();

	var item = node.getPlayableData();
	var mimeType = item.getMimeTypeArray()[0]; 
	var ext = ".flv";
	if (mimeType === 'video/x-ms-wmv') { ext = ".wmv"; }
	var relativeUri = item.getPlayableId() + ext;
	
	node.setURI( this.getPlayer().getAbsoluteURI( relativeUri, 'adVideo' ) );
	node.setMimeTypes( [mimeType] );
	node.setStreamingMode( xmp.AD_STREAMING_MODE );	
	
	callControllerMethod('setEnabled',true);
	callControllerMethod('setScrubberEnabled',false);
	callControllerMethod('setDuration', node.getPlayableData().getDataObject().trt);

	if (CNNPlaylistManager.getInstance().activePlaylist == 'saplaylist') {
		CNNPlaylistManager.getInstance().SAConfiguration.updateContentHandler(this.getPlayer()._findNextContentNode().getPlayableData().getDataObject());
	}
};

AdNodeListener.prototype.handleOverrideAdResources = function(node, overrideContext) {
	var primary_tag = overrideContext.getResource('primary').getId();
	var synch_tag = overrideContext.getResource('synch_unit').getId();
	
	overrideContext.getResource('primary').setId(primary_tag.replace(/cnn_video\/video.preroll_lg.ad$/, 'video.evp_preroll.ad'));
	overrideContext.getResource('synch_unit').setId(synch_tag.replace(/cnn_video\/video.336x280_sync.ad$/, 'video.300x60_evp.ad'));
};

/********************************************************************************
 **  qSlateNodeListener
 ** - shown if an error occurs in a video or if a video is expired
 *******************************************************************************/

function GenericSlateNodeListener( name, player ) {
	GenericSlateNodeListener.ctor.call( this, name, player );
}  

xmp.DERIVE_CLASS( xmp.baseplayer.listeners.DefaultSlateNodeListener, GenericSlateNodeListener );

GenericSlateNodeListener.prototype.handleComplete = function( node ) {	
	// atlas integration
	CNNPlaylistManager.getInstance().killAtlas();

	GenericSlateNodeListener.base.handleComplete.call( this, node );
	
};

/********************************************************************************
 ** VideoNodeListener
 ** - drives all video content nodes
 *******************************************************************************/

function VideoNodeListener( name, player ) {
	VideoNodeListener.ctor.call( this, name, player, false );		
}

xmp.DERIVE_CLASS( xmp.baseplayer.listeners.DefaultVideoNodeListener, VideoNodeListener );

VideoNodeListener.prototype.handleEnded = function( node ) {
	callControllerMethod('setEnabled',false);
	callControllerMethod('setTime',0);
	if (CNNPlaylistManager.getInstance().controller == null) { callControllerMethod('togglePlay',false); }
	CNNPlayer.getActivePlayer().callEnded();
}

VideoNodeListener.prototype.handleComplete = function( node ) {	
	// atlas integration
	CNNPlaylistManager.getInstance().killAtlas();

	VideoNodeListener.base.handleComplete.call( this, node );	

	callControllerMethod('setEnabled', true);
	callControllerMethod('setScrubberEnabled',true);
	callControllerMethod('setDuration', node.getPlayableData().getDataObject().trt);

	if (CNNPlaylistManager.getInstance().activePlaylist == 'saplaylist') {
		this.getPlayer().removeSynchUnit();
		CNNPlaylistManager.getInstance().SAConfiguration.updateContentHandler(node.getPlayableData().getDataObject());
	}
};

VideoNodeListener.prototype.handleRender = function( node ) {
	if ("Expired" == node.getPlayableData().getDataObject().isExpired || false == node.getPlayableData().getDataObject().isEmbeddable) { 
		callControllerMethod('setEnabled',false);
		callControllerMethod('setTime',0);
		if (CNNPlaylistManager.getInstance().controller == null) { callControllerMethod('togglePlay',false); }
		CNNPlaylistManager.getInstance().isVideoError = true;
		this.getPlayer().advance();
	} else {
		callControllerMethod('videoStarted', true);
		this.getPlayer().getMediaPlayer().open( node );
	}
};

VideoNodeListener.prototype.handleError = function( node, err ) {
	callControllerMethod('setEnabled',false);
	callControllerMethod('setTime',0);
	if (CNNPlaylistManager.getInstance().controller == null) { 
		callControllerMethod('togglePlay',false); 
	}
	CNNPlaylistManager.getInstance().isVideoError = true;
	CNNPlaylistManager.getInstance().errorObject = err;
	this.getPlayer().advance();
};