<commit_before>/**
* Supports the parsing of ads
*/
( function( mw, $ ) {
//Global mw.addKAd manager
mw.addKalturaAds = function( embedPlayer, callback ) {
	embedPlayer.ads = new mw.KAds( embedPlayer, callback );
};

mw.sendBeaconUrl = function( beaconUrl ){
	$('body').append( 
		$( '<img />' ).attr({
			'src' : beaconUrl,
			'width' : 0,
			'height' : 0
		})
	);
};

mw.KAds = function( embedPlayer, $uiConf, callback) {
	// Create a Player Manager
	return this.init( embedPlayer, $uiConf ,callback );
};

mw.KAds.prototype = {
	// The ad types 
	namedAdTimelineTypes : [ 'preroll', 'postroll', 'midroll', 'overlay' ],
	// Ad attribute map 
	adAttributeMap: {
		"Interval": 'frequency',
		"StartAt": 'start'
	},
	displayedCuePoints: [],
	
	init: function( embedPlayer, callback ){
		var _this = this; 
		this.embedPlayer = embedPlayer;
		
		// setup local pointer: 
		var $uiConf = embedPlayer.$uiConf;
		this.$notice = $uiConf.find( 'label#noticeMessage' );
		this.$skipBtn = $uiConf.find( 'button#skipBtn' );

		// Build out the selection of kAds
		var configSet = ['htmlCompanions' , 'flashCompanions' ];
		$.each( this.namedAdTimelineTypes, function( inx, adType ){
			$.each( _this.adAttributeMap, function( adAttributeName,  displayConfName ){
				// Add all the ad types to the config set: 
				configSet.push( adType + adAttributeName);
			});
			// Add the ad type Url
			configSet.push( adType + 'Url');
		});

		this.adConfig = embedPlayer.getKalturaConfig(
			'vast',
			configSet
		);
		
		// Load the Ads from uiConf
		_this.loadAds( function(){
			mw.log( "KAds::All ads have been loaded" );
			callback();
		});

		// We can add this binding here, because we will always have vast in the uiConf when having cue points
		// Catch Ads from adOpportunity event
		$( this.embedPlayer ).bind('KalturaSupport_AdOpportunity', function( event, cuePointWrapper ) {
			_this.loadAd( cuePointWrapper );
		});
	},

	// Load the ad from cue point
	loadAd: function( cuePointWrapper ) {
		var _this = this;
		var embedPlayer = this.embedPlayer;
		var adType = this.embedPlayer.kCuePoints.getAdSlotType( cuePointWrapper );
		var cuePoint = cuePointWrapper.cuePoint;
		// Check if cue point already displayed
		if( $.inArray( cuePoint.id, _this.displayedCuePoints) >= 0 ) {
			return ;
		}

		// If ad type is midroll pause the video
		if( adType == 'midroll' ) {
			// Pause the video
			_this.embedPlayer.pauseLoading();
		}
		
		if( cuePoint.sourceUrl ) {
			mw.AdLoader.load( cuePoint.sourceUrl, function( adConf ){
				
				var adCuePointConf = {
					duration: ( (cuePoint.endTime - cuePoint.startTime) / 1000 ),
					start: ( cuePoint.startTime / 1000  )
				};

				var adsCuePointConf = {
					ads: [
						$.extend( adConf.ads[0], adCuePointConf )
					],
					skipBtn: {
						'text' : "Skip ad", // TODO i8ln 
						'css' : {
							'right': '5px',
							'bottom' : '5px'
						}
					},
					type: adType
				};

				var originalSrc = embedPlayer.getSrc();
				var seekTime = ( parseFloat( cuePoint.startTime / 1000 ) / parseFloat( embedPlayer.duration ) );
				var oldDuration = embedPlayer.duration;

				// Set restore function
				var restorePlayer = function() {
					embedPlayer.restoreEventPropagation();
					embedPlayer.enableSeekBar();
				};

				// Set switch back function
				var doneCallback = function() {
					// Add cuePoint Id to displayed cuePoints array
					_this.displayedCuePoints.push( cuePoint.id );
					
					var vid = embedPlayer.getPlayerElement();
					// Check if the src does not match original src if
					// so switch back and restore original bindings
					if ( originalSrc != vid.src ) {
						embedPlayer.switchPlaySrc(originalSrc, function() {
							mw.log( "AdTimeline:: restored original src:" + vid.src);
							// Restore embedPlayer native bindings
							// async for iPhone issues
							setTimeout(function(){
								restorePlayer();
							}, 100 );

							// Sometimes the duration of the video is zero after switching source
							// So i'm re-setting it to it's old duration
							embedPlayer.duration = oldDuration;
							if( adType == 'postroll' ) {
								// Run stop for now.
								setTimeout( function() {
									embedPlayer.stop();
								}, 100);

								mw.log( "AdTimeline:: run video pause ");
								if( vid && vid.pause ){
									// Pause playback state
									vid.pause();
									// iPhone does not catch synchronous pause
									setTimeout( function(){ if( vid && vid.pause ){ vid.pause(); } }, 100 );
								}
							} else {
								
								$( embedPlayer ).bind('seeked.ad', function() {
									alert('good');
									embedPlayer.play();
									setTimeout( function() {
										embedPlayer.play();
									}, 250);
								});

								// Seek to where we did the switch<commit_msg>* removed alert message<commit_after>/**
* Supports the parsing of ads
*/
( function( mw, $ ) {
//Global mw.addKAd manager
mw.addKalturaAds = function( embedPlayer, callback ) {
	embedPlayer.ads = new mw.KAds( embedPlayer, callback );
};

mw.sendBeaconUrl = function( beaconUrl ){
	$('body').append( 
		$( '<img />' ).attr({
			'src' : beaconUrl,
			'width' : 0,
			'height' : 0
		})
	);
};

mw.KAds = function( embedPlayer, $uiConf, callback) {
	// Create a Player Manager
	return this.init( embedPlayer, $uiConf ,callback );
};

mw.KAds.prototype = {
	// The ad types 
	namedAdTimelineTypes : [ 'preroll', 'postroll', 'midroll', 'overlay' ],
	// Ad attribute map 
	adAttributeMap: {
		"Interval": 'frequency',
		"StartAt": 'start'
	},
	displayedCuePoints: [],
	
	init: function( embedPlayer, callback ){
		var _this = this; 
		this.embedPlayer = embedPlayer;
		
		// setup local pointer: 
		var $uiConf = embedPlayer.$uiConf;
		this.$notice = $uiConf.find( 'label#noticeMessage' );
		this.$skipBtn = $uiConf.find( 'button#skipBtn' );

		// Build out the selection of kAds
		var configSet = ['htmlCompanions' , 'flashCompanions' ];
		$.each( this.namedAdTimelineTypes, function( inx, adType ){
			$.each( _this.adAttributeMap, function( adAttributeName,  displayConfName ){
				// Add all the ad types to the config set: 
				configSet.push( adType + adAttributeName);
			});
			// Add the ad type Url
			configSet.push( adType + 'Url');
		});

		this.adConfig = embedPlayer.getKalturaConfig(
			'vast',
			configSet
		);
		
		// Load the Ads from uiConf
		_this.loadAds( function(){
			mw.log( "KAds::All ads have been loaded" );
			callback();
		});

		// We can add this binding here, because we will always have vast in the uiConf when having cue points
		// Catch Ads from adOpportunity event
		$( this.embedPlayer ).bind('KalturaSupport_AdOpportunity', function( event, cuePointWrapper ) {
			_this.loadAd( cuePointWrapper );
		});
	},

	// Load the ad from cue point
	loadAd: function( cuePointWrapper ) {
		var _this = this;
		var embedPlayer = this.embedPlayer;
		var adType = this.embedPlayer.kCuePoints.getAdSlotType( cuePointWrapper );
		var cuePoint = cuePointWrapper.cuePoint;
		// Check if cue point already displayed
		if( $.inArray( cuePoint.id, _this.displayedCuePoints) >= 0 ) {
			return ;
		}

		// If ad type is midroll pause the video
		if( adType == 'midroll' ) {
			// Pause the video
			_this.embedPlayer.pauseLoading();
		}
		
		if( cuePoint.sourceUrl ) {
			mw.AdLoader.load( cuePoint.sourceUrl, function( adConf ){
				
				var adCuePointConf = {
					duration: ( (cuePoint.endTime - cuePoint.startTime) / 1000 ),
					start: ( cuePoint.startTime / 1000  )
				};

				var adsCuePointConf = {
					ads: [
						$.extend( adConf.ads[0], adCuePointConf )
					],
					skipBtn: {
						'text' : "Skip ad", // TODO i8ln 
						'css' : {
							'right': '5px',
							'bottom' : '5px'
						}
					},
					type: adType
				};

				var originalSrc = embedPlayer.getSrc();
				var seekTime = ( parseFloat( cuePoint.startTime / 1000 ) / parseFloat( embedPlayer.duration ) );
				var oldDuration = embedPlayer.duration;

				// Set restore function
				var restorePlayer = function() {
					embedPlayer.restoreEventPropagation();
					embedPlayer.enableSeekBar();
				};

				// Set switch back function
				var doneCallback = function() {
					// Add cuePoint Id to displayed cuePoints array
					_this.displayedCuePoints.push( cuePoint.id );
					
					var vid = embedPlayer.getPlayerElement();
					// Check if the src does not match original src if
					// so switch back and restore original bindings
					if ( originalSrc != vid.src ) {
						embedPlayer.switchPlaySrc(originalSrc, function() {
							mw.log( "AdTimeline:: restored original src:" + vid.src);
							// Restore embedPlayer native bindings
							// async for iPhone issues
							setTimeout(function(){
								restorePlayer();
							}, 100 );

							// Sometimes the duration of the video is zero after switching source
							// So i'm re-setting it to it's old duration
							embedPlayer.duration = oldDuration;
							if( adType == 'postroll' ) {
								// Run stop for now.
								setTimeout( function() {
									embedPlayer.stop();
								}, 100);

								mw.log( "AdTimeline:: run video pause ");
								if( vid && vid.pause ){
									// Pause playback state
									vid.pause();
									// iPhone does not catch synchronous pause
									setTimeout( function(){ if( vid && vid.pause ){ vid.pause(); } }, 100 );
								}
							} else {
								
								$( embedPlayer ).bind('seeked.ad', function() {
									embedPlayer.play();
									setTimeout( function() {
										embedPlayer.play();
									}, 250);
								});

								// Seek to where we did the switch
{'fraction_non_alphanumeric': 0.12979966611018365, 'fraction_numerical': 0.006469115191986645, 'mean_word_length': 2.6115297663903543, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 7, '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': '4492060', 'n_tokens_mistral': 3978, 'n_tokens_neox': 2923, 'n_words': 821}