<commit_before>( function( mw, $ ) { "use strict";

mw.DoubleClick = function( embedPlayer, callback, pluginName ){
	this.init( embedPlayer, callback, pluginName);
};
mw.DoubleClick.prototype = {
	// The bind postfix to keep track of doubleclick bindings. 
	bindPostfix: '.DoubleClick',
	
	// The content video element.
	content: null,
	
	// Global volume holder.
	globalVolume : 1,

	// IMA SDK Ad Loader.
	adsLoader : null,
	// IMA SDK Ad Manager.
	adsManager: null,

	// Status variables for ad and content playback.
	adActive: false,
	// if the current ad is paused: 
	adPaused: false, 
	
	// The monitor interval index:
	adMonitor: null,
	
	// store the ad start time
	adPreviousTimeLeft: null,
	contentPlaying: false,
	adDuration: null,
	demoStartTime: null,
	
	// Flags for a fallback check for all ads completed .
	contentDoneFlag: null,
	
	// Flag for startting ad playback sequence:
	startedAdPlayback: null,
	
	allAdsCompletedFlag: null,
	
	
	// The current ad Slot type by default "managed" i.e doubleClick manages the player sequence. 
	currentAdSlotType : null,

	init: function( embedPlayer, callback, pluginName ){
		var _this = this;
		
		this.embedPlayer = embedPlayer;
		
		// Inherit BaseAdPlugin
		mw.inherit( this, new mw.BaseAdPlugin( embedPlayer, callback ) );
		
		// Set the plugin name
		this.pluginName = pluginName;
		
		// reset the contentDoneFlag flags:
		this.contentDoneFlag = null;
		this.allAdsCompletedFlag = null;
		
		// remove any old bindings: 
		embedPlayer.unbindHelper( this.bindPostfix );
		
		// make sure any old ad Manager is unloaded:
		var globalAdsManger = $(_this.embedPlayer).data( 'doubleClickAdsMangerRef' );
		if( globalAdsManger ){
			mw.log( "DoubleClick::unload old adManger" );
			if ( $.isFunction( globalAdsManger.unload ) ) {
				globalAdsManger.unload();
			}
			if( $('#' + this.getAdContainerId() ).length ){
				$('#' + this.getAdContainerId() ).remove();
			}
		}
		
		// Load double click ima per doc:
		this.loadIma(function(){
			// Determine if we are in managed or kaltura point based mode. 
			if( _this.getConfig( "preSequence" ) && _this.getConfig( "adTagUrl" ) ){
				// Check for adPattern 
				if( _this.getConfig( 'adPattern' ) ){
					var adIndex = _this.getAdPatternIndex();
					mw.log( "DoubleClick:: adPattern: " + _this.getConfig( 'adPattern' ) +
							" on index: " + adIndex );
					if( adIndex == 'A' ){
						// Managed bindings
						_this.addManagedBinding();
					}
				} else{
					// No defined ad pattern always use managed bindings
					_this.addManagedBinding();
				}
			} else {
				// Add cuepoint bindings
				_this.addKalturaCuePointBindings();
			}
			// Issue the callback to continue player build out: 
			callback();
		}, function( errorCode ){
			mw.log( "Error::DoubleClick Loading Error: " + errorCode );
			// Don't add any bindings directly issue callback: 
			callback();
		});
	},
	/**
	 * Get the global adPattern index: 
	 */
	getAdPatternIndex:function(){
		var adPattern = this.getConfig( 'adPattern' );
		var currentAdIndex = $( this.embedPlayer ).data('DcAdPatternIndex');
		if( typeof currentAdIndex === 'undefined' ){
			currentAdIndex = 0;
		} else{
			// increment the index
			currentAdIndex++
			// index is past adPattern length reset to 0
			if( currentAdIndex > adPattern.length -1 ){
				currentAdIndex = 0;
			}
		}
		// update add index:
		$( this.embedPlayer ).data('DcAdPatternIndex', currentAdIndex);
		// return the adPattern index: 
		return adPattern[currentAdIndex];
	},
	/**
	 * Load the google IMA library: 
	 */
	loadIma:function( successCB, failureCB ){
		// http://code.google.com/apis/ima/docs/sdks/googlehtml5_ads_v3.html#loadsdk
		$.getScript('http://s0.2mdn.net/instream/html5/ima.js', function(){
			google.ima.SdkLoader.setCallbacks( function(){
				successCB();
			}, function( errorCode ){
				failureCB( errorCode );
			});
			google.ima.SdkLoader.load("3");
		});
	},
	addManagedBinding: function(){
		var _this = this;
		mw.log( "DoubleClick::addManagedBinding" );
		_this.embedPlayer.bindHelper( 'AdSupport_preroll' + _this.bindPostfix, function( event, sequenceProxy ){
			// Add the slot to the given sequence proxy target target
			sequenceProxy[ _this.getSequenceIndex( 'preroll' ) ] = function( callback ){
				// if a preroll set it as such: 
				_this.currentAdSlotType = 'preroll';
				// Setup the restore callback
				_this.restorePlayerCallback = callback;
				// Request ads
				mw.log( "DoubleClick:: addManagedBinding : requestAds:" +  _this.getConfig( 'adTagUrl' )  );
				_this.requestAds( _this.getConfig( 'adTagUrl' ) );	
			};
		});
		_this.embedPlayer.bindHelper( 'AdSupport_postroll' + _this.bindPostfix, function( event, sequenceProxy ){
			sequenceProxy[ _this.getSequenceIndex( 'postroll' ) ] = function( callback ){
				// Setup the restore callback
				_this.restorePlayerCallback = callback;
				
				// set content complete flag
				_this.contentDoneFlag = true;
				
				// set current slot to postRoll
				_this.currentAdSlotType = 'postroll';
				
				// trigger the double click end sequence:
				_this.adsLoader.contentComplete();
			};
		});
	},
	/**
	 * Get the content video tag
	 */
	getContent:function(){
		// Set the content element to player element: 
		return this.embedPlayer.getPlayerElement();
	},
	addKalturaCuePointBindings: function(){
		var _this = this;
		mw.log("DoubleClick::addKalturaCuePointBindings");
		// Add a binding for cuepoints:
		_this.embedPlayer.bindHelper( 'KalturaSupport_AdOpportunity' + _this.bindPostfix, function( event,  cuePointWrapper ){
			mw.log( "DoubleClick:: evaluate ad oppertunity");
			var cuePoint = cuePointWrapper.cuePoint;
			// Check if trackCuePoints has been disabled 
			if( _this.getConfig( 'trackCuePoints') === false){
				mw.log( "DoubleClick:: trackCuePoints is false");
				return ;
			}
			
			// Check that the cue point is protocolType = 0 and cuePointType == adCuePoint.Ad
			if( cuePoint.protocolType !== 0 || cuePoint.cuePointType != 'adCuePoint.Ad' ){
				mw.log( "DoubleClick:: cuePoint protocol != 0 or type != adCuePoint.ad" );
				return ;
			}
			// Check if we have a provider filter:
			var providerFilter = _this.getConfig('provider');
			if( providerFilter && cuePoint.tags.toLowerCase().indexOf( providerFilter.toLowerCase() ) === -1 ){
				// skip the cuepoint that did not match the provider filter
				mw.log( "DoubleClick:: skip cuePoint with tag: " + cuePoint.tags + ' != ' + providerFilter );
				return ;
			}
			
			// Get the ad type for each cuepoint
			var adType = _this.embedPlayer.kCuePoints.getRawAdSlotType( cuePoint );
			
			mw.log( "DoubleClick:: AdOpportunity:: " + cuePoint.startTime + ' ad type: ' + adType );
			if( adType == 'overlay' ){
				_this.loadAndDisplayOverlay( cuePoint );
				return true; // continue to next cue point
			}
			// Update the ad slot type: 
			_this.currentAdSlotType = adType;
			
			if( adType == 'preroll' || adType == 'postroll' ){
				_this.embedPlayer.bindHelper( 'AdSupport_' + adType + _this.bindPostfix, function( event, sequenceProxy ){
					// Add the slot to the given sequence proxy target target
					sequenceProxy[ _this.getSequenceIndex( adType ) ] = function( callback ){
						// Setup the restore callback
						_this.restorePlayerCallback = callback;
						// Request ads
						mw.log( "DoubleClick:: addManagedBinding : cuePoint:" +  adType );
						_this.requestAds( cuePoint.sourceUrl );
					};
				});
			}
			// If cuepoint ad type is midroll request inline: 
			if( adType == 'midroll' ){
				// All cuepoints act as "midrolls" 
				mw.log( "DoubleClick:: addKalturaCuePointBindings: midroll -> requestAds" );
				// pause the player while requesting adds
				_this.embedPlayer.pauseLoading();
				// request the ads: 
				_this.requestAds( cuePoint.sourceUrl ) ;
			}
		});
	},
	/**
	 * Load and display an overlay 
	 * @param cuePoint
	 * @return
	 */
	loadAndDisplayOverlay: function( cuePoint ){
		var _this = this;
		// Don't display overlays if in an ad: 
		if( this.embedPlayer.evaluate('{sequenceProxy.isInSequence}') ){
			return ;
		}
		// Request the ad ( will trigger the currentAdCallback and onResumeRequestedCallback when done )
		_this.requestAds( cuePoint.sourceUrl, 'overlay' );
	},
	getAdContainer: function(){
		if( !$('#' + this.getAdContainerId() ).length ){
			this.embedPlayer.$interface.append( 
				$('<div />')
					.attr( 'id',  this.getAdContainerId() )
					.css({
						'position' : 'absolute',
						'top' : '0px',
						'left' : '0px'
					})
			)
		}
		return $('#' + this.getAdContainerId() ).get(0);
	},
	getAdContainerId: function(){
		return 'adContainer' + this.embedPlayer.id;
	},
	getAdDisplayContainer: function(){
		//  Create the ad display container. Use an existing DOM element
		//	to house the ad display container. Ideally, the element is
		//	positioned above the content video player, so the ads are
		//	displayed correctly.
		//
		if( ! this.adDisplayContainer ){
			this.adDisplayContainer = new google.ima.AdDisplayContainer(
				this.getAdContainer(),
				this.getContent()
			);
		}
		return this.adDisplayContainer;
	},
	// Various initialization activities of the content video element.
	initContent:function() {
		// It is required to send the content complete event at the end of
		// content playback.
		//content.addEventListener('ended', function() {
		//	adsLoader.contentComplete();
		//});

		// Initialize the monitoring of the video playback progress.
		//setInterval(onVideoTimeUpdate, 300);
	},
	/**
	 * Adds custom params to ad url.
	 */
	addCustomParams: function( adUrl ){debugger;
		var postFix = this.getConfig( 'customParams' ) ? 
				'cust_params=' + encodeURIComponent( this.getConfig( 'customParams' ) ) : '';
		if( postFix ){
			var paramSeperator = adUrl.indexOf( '?' ) === -1 ? '?' : 
				adUrl[ adUrl.length -1 ] == '&' ? '': '&';
			
			return unescape( adUrl ) + paramSeperator + postFix;
		} else {
			return unescape( adUrl );
		}
	},
	 // This function requests the ads.
	requestAds: function( adTagUrl, adType ) {
		var _this = this;
		// add any custom params: <commit_msg>removed debugger line fixes broken commit in ae2317db4b12ac50c5990a4138612182974a8122<commit_after>( function( mw, $ ) { "use strict";

mw.DoubleClick = function( embedPlayer, callback, pluginName ){
	this.init( embedPlayer, callback, pluginName);
};
mw.DoubleClick.prototype = {
	// The bind postfix to keep track of doubleclick bindings. 
	bindPostfix: '.DoubleClick',
	
	// The content video element.
	content: null,
	
	// Global volume holder.
	globalVolume : 1,

	// IMA SDK Ad Loader.
	adsLoader : null,
	// IMA SDK Ad Manager.
	adsManager: null,

	// Status variables for ad and content playback.
	adActive: false,
	// if the current ad is paused: 
	adPaused: false, 
	
	// The monitor interval index:
	adMonitor: null,
	
	// store the ad start time
	adPreviousTimeLeft: null,
	contentPlaying: false,
	adDuration: null,
	demoStartTime: null,
	
	// Flags for a fallback check for all ads completed .
	contentDoneFlag: null,
	
	// Flag for startting ad playback sequence:
	startedAdPlayback: null,
	
	allAdsCompletedFlag: null,
	
	
	// The current ad Slot type by default "managed" i.e doubleClick manages the player sequence. 
	currentAdSlotType : null,

	init: function( embedPlayer, callback, pluginName ){
		var _this = this;
		
		this.embedPlayer = embedPlayer;
		
		// Inherit BaseAdPlugin
		mw.inherit( this, new mw.BaseAdPlugin( embedPlayer, callback ) );
		
		// Set the plugin name
		this.pluginName = pluginName;
		
		// reset the contentDoneFlag flags:
		this.contentDoneFlag = null;
		this.allAdsCompletedFlag = null;
		
		// remove any old bindings: 
		embedPlayer.unbindHelper( this.bindPostfix );
		
		// make sure any old ad Manager is unloaded:
		var globalAdsManger = $(_this.embedPlayer).data( 'doubleClickAdsMangerRef' );
		if( globalAdsManger ){
			mw.log( "DoubleClick::unload old adManger" );
			if ( $.isFunction( globalAdsManger.unload ) ) {
				globalAdsManger.unload();
			}
			if( $('#' + this.getAdContainerId() ).length ){
				$('#' + this.getAdContainerId() ).remove();
			}
		}
		
		// Load double click ima per doc:
		this.loadIma(function(){
			// Determine if we are in managed or kaltura point based mode. 
			if( _this.getConfig( "preSequence" ) && _this.getConfig( "adTagUrl" ) ){
				// Check for adPattern 
				if( _this.getConfig( 'adPattern' ) ){
					var adIndex = _this.getAdPatternIndex();
					mw.log( "DoubleClick:: adPattern: " + _this.getConfig( 'adPattern' ) +
							" on index: " + adIndex );
					if( adIndex == 'A' ){
						// Managed bindings
						_this.addManagedBinding();
					}
				} else{
					// No defined ad pattern always use managed bindings
					_this.addManagedBinding();
				}
			} else {
				// Add cuepoint bindings
				_this.addKalturaCuePointBindings();
			}
			// Issue the callback to continue player build out: 
			callback();
		}, function( errorCode ){
			mw.log( "Error::DoubleClick Loading Error: " + errorCode );
			// Don't add any bindings directly issue callback: 
			callback();
		});
	},
	/**
	 * Get the global adPattern index: 
	 */
	getAdPatternIndex:function(){
		var adPattern = this.getConfig( 'adPattern' );
		var currentAdIndex = $( this.embedPlayer ).data('DcAdPatternIndex');
		if( typeof currentAdIndex === 'undefined' ){
			currentAdIndex = 0;
		} else{
			// increment the index
			currentAdIndex++
			// index is past adPattern length reset to 0
			if( currentAdIndex > adPattern.length -1 ){
				currentAdIndex = 0;
			}
		}
		// update add index:
		$( this.embedPlayer ).data('DcAdPatternIndex', currentAdIndex);
		// return the adPattern index: 
		return adPattern[currentAdIndex];
	},
	/**
	 * Load the google IMA library: 
	 */
	loadIma:function( successCB, failureCB ){
		// http://code.google.com/apis/ima/docs/sdks/googlehtml5_ads_v3.html#loadsdk
		$.getScript('http://s0.2mdn.net/instream/html5/ima.js', function(){
			google.ima.SdkLoader.setCallbacks( function(){
				successCB();
			}, function( errorCode ){
				failureCB( errorCode );
			});
			google.ima.SdkLoader.load("3");
		});
	},
	addManagedBinding: function(){
		var _this = this;
		mw.log( "DoubleClick::addManagedBinding" );
		_this.embedPlayer.bindHelper( 'AdSupport_preroll' + _this.bindPostfix, function( event, sequenceProxy ){
			// Add the slot to the given sequence proxy target target
			sequenceProxy[ _this.getSequenceIndex( 'preroll' ) ] = function( callback ){
				// if a preroll set it as such: 
				_this.currentAdSlotType = 'preroll';
				// Setup the restore callback
				_this.restorePlayerCallback = callback;
				// Request ads
				mw.log( "DoubleClick:: addManagedBinding : requestAds:" +  _this.getConfig( 'adTagUrl' )  );
				_this.requestAds( _this.getConfig( 'adTagUrl' ) );	
			};
		});
		_this.embedPlayer.bindHelper( 'AdSupport_postroll' + _this.bindPostfix, function( event, sequenceProxy ){
			sequenceProxy[ _this.getSequenceIndex( 'postroll' ) ] = function( callback ){
				// Setup the restore callback
				_this.restorePlayerCallback = callback;
				
				// set content complete flag
				_this.contentDoneFlag = true;
				
				// set current slot to postRoll
				_this.currentAdSlotType = 'postroll';
				
				// trigger the double click end sequence:
				_this.adsLoader.contentComplete();
			};
		});
	},
	/**
	 * Get the content video tag
	 */
	getContent:function(){
		// Set the content element to player element: 
		return this.embedPlayer.getPlayerElement();
	},
	addKalturaCuePointBindings: function(){
		var _this = this;
		mw.log("DoubleClick::addKalturaCuePointBindings");
		// Add a binding for cuepoints:
		_this.embedPlayer.bindHelper( 'KalturaSupport_AdOpportunity' + _this.bindPostfix, function( event,  cuePointWrapper ){
			mw.log( "DoubleClick:: evaluate ad oppertunity");
			var cuePoint = cuePointWrapper.cuePoint;
			// Check if trackCuePoints has been disabled 
			if( _this.getConfig( 'trackCuePoints') === false){
				mw.log( "DoubleClick:: trackCuePoints is false");
				return ;
			}
			
			// Check that the cue point is protocolType = 0 and cuePointType == adCuePoint.Ad
			if( cuePoint.protocolType !== 0 || cuePoint.cuePointType != 'adCuePoint.Ad' ){
				mw.log( "DoubleClick:: cuePoint protocol != 0 or type != adCuePoint.ad" );
				return ;
			}
			// Check if we have a provider filter:
			var providerFilter = _this.getConfig('provider');
			if( providerFilter && cuePoint.tags.toLowerCase().indexOf( providerFilter.toLowerCase() ) === -1 ){
				// skip the cuepoint that did not match the provider filter
				mw.log( "DoubleClick:: skip cuePoint with tag: " + cuePoint.tags + ' != ' + providerFilter );
				return ;
			}
			
			// Get the ad type for each cuepoint
			var adType = _this.embedPlayer.kCuePoints.getRawAdSlotType( cuePoint );
			
			mw.log( "DoubleClick:: AdOpportunity:: " + cuePoint.startTime + ' ad type: ' + adType );
			if( adType == 'overlay' ){
				_this.loadAndDisplayOverlay( cuePoint );
				return true; // continue to next cue point
			}
			// Update the ad slot type: 
			_this.currentAdSlotType = adType;
			
			if( adType == 'preroll' || adType == 'postroll' ){
				_this.embedPlayer.bindHelper( 'AdSupport_' + adType + _this.bindPostfix, function( event, sequenceProxy ){
					// Add the slot to the given sequence proxy target target
					sequenceProxy[ _this.getSequenceIndex( adType ) ] = function( callback ){
						// Setup the restore callback
						_this.restorePlayerCallback = callback;
						// Request ads
						mw.log( "DoubleClick:: addManagedBinding : cuePoint:" +  adType );
						_this.requestAds( cuePoint.sourceUrl );
					};
				});
			}
			// If cuepoint ad type is midroll request inline: 
			if( adType == 'midroll' ){
				// All cuepoints act as "midrolls" 
				mw.log( "DoubleClick:: addKalturaCuePointBindings: midroll -> requestAds" );
				// pause the player while requesting adds
				_this.embedPlayer.pauseLoading();
				// request the ads: 
				_this.requestAds( cuePoint.sourceUrl ) ;
			}
		});
	},
	/**
	 * Load and display an overlay 
	 * @param cuePoint
	 * @return
	 */
	loadAndDisplayOverlay: function( cuePoint ){
		var _this = this;
		// Don't display overlays if in an ad: 
		if( this.embedPlayer.evaluate('{sequenceProxy.isInSequence}') ){
			return ;
		}
		// Request the ad ( will trigger the currentAdCallback and onResumeRequestedCallback when done )
		_this.requestAds( cuePoint.sourceUrl, 'overlay' );
	},
	getAdContainer: function(){
		if( !$('#' + this.getAdContainerId() ).length ){
			this.embedPlayer.$interface.append( 
				$('<div />')
					.attr( 'id',  this.getAdContainerId() )
					.css({
						'position' : 'absolute',
						'top' : '0px',
						'left' : '0px'
					})
			)
		}
		return $('#' + this.getAdContainerId() ).get(0);
	},
	getAdContainerId: function(){
		return 'adContainer' + this.embedPlayer.id;
	},
	getAdDisplayContainer: function(){
		//  Create the ad display container. Use an existing DOM element
		//	to house the ad display container. Ideally, the element is
		//	positioned above the content video player, so the ads are
		//	displayed correctly.
		//
		if( ! this.adDisplayContainer ){
			this.adDisplayContainer = new google.ima.AdDisplayContainer(
				this.getAdContainer(),
				this.getContent()
			);
		}
		return this.adDisplayContainer;
	},
	// Various initialization activities of the content video element.
	initContent:function() {
		// It is required to send the content complete event at the end of
		// content playback.
		//content.addEventListener('ended', function() {
		//	adsLoader.contentComplete();
		//});

		// Initialize the monitoring of the video playback progress.
		//setInterval(onVideoTimeUpdate, 300);
	},
	/**
	 * Adds custom params to ad url.
	 */
	addCustomParams: function( adUrl ){
		var postFix = this.getConfig( 'customParams' ) ? 
				'cust_params=' + encodeURIComponent( this.getConfig( 'customParams' ) ) : '';
		if( postFix ){
			var paramSeperator = adUrl.indexOf( '?' ) === -1 ? '?' : 
				adUrl[ adUrl.length -1 ] == '&' ? '': '&';
			
			return unescape( adUrl ) + paramSeperator + postFix;
		} else {
			return unescape( adUrl );
		}
	},
	 // This function requests the ads.
	requestAds: function( adTagUrl, adType ) {
		var _this = this;
		// add any custom params: 
{'fraction_non_alphanumeric': 0.1271382795168844, 'fraction_numerical': 0.003746610796154794, 'mean_word_length': 3.2457095018836335, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5270328', 'n_tokens_mistral': 7545, 'n_tokens_neox': 6301, 'n_words': 1716}