﻿
Type.registerNamespace("PlainJoeStudios.MediaSocial.WebServer.Controls");PlainJoeStudios.MediaSocial.WebServer.Controls.VideoPlayer=function(parameters){this._ctor(parameters);};PlainJoeStudios.MediaSocial.WebServer.Controls.VideoPlayer.prototype={MIN_TIME_NEEDED_FOR_SYNC_TEST:7*1000,SYNC_TEST_DELAYED_START:1*1000,SYNC_TEST_NUM_TESTS:5,MIN_TIME_NEEDED_FOR_INTERMISSION_BANNERS:3*1000,END_OF_VIDEO_SLACK:3*1000,TIMELINE_TICK_INTERVAL:100,PRELOAD_PERIOD:60,EARLY_LATE_JOIN_PERIOD:60,ACTION_BANNERS:"b",ACTION_PRELOAD_VIDEO:"p",ACTION_VIDEO:"v",SCHEDULE_UPDATE_RATE:10*1000,VIDEO_TYPE_PRERECORDED:1,VIDEO_TYPE_LIVE:2,_EventId:null,_FlashVideoPlayerUrl:null,_FlashVideoPlayerContainerId:null,_bFlashPlayerReady:false,_jqFlashPlayerContainer:null,_jqFlashPlayer:null,_jqLiveEmbedContainer:null,_oFunctionQueue_FlashVideoPlayer:null,_arr_oVideoShowings:null,_oTimeline:null,_iFirstVideoStartTime:null,_oScheduleUpdateTimoutId:null,_bDisableScheduleUpdateCheck:false,_bSynchronizationComplete:false,_iTimelineClockId:null,_iActiveOrNextShowingId:null,_iPreloadingVideo:null,_iPlayingVideo:null,_iShowingIdSentToCountdown:null,_bIsShowingBanners:false,_iPreviousShowingId:null,_iBannerHoldSeconds:null,_ctor:function(parameters){var This=this;this._EventId=parameters["EventId"];this._FlashVideoPlayerUrl=parameters["FlashVideoPlayerUrl"];this._FlashVideoPlayerContainerId=parameters["FlashVideoPlayerContainerId"];this._jqFlashPlayerContainer=$(".videoplayer");this._jqLiveEmbedContainer=$(".videoplayerexternalembed");this._jqRotatingBannerContainer=$(".videorotatingbanner");if(this._FlashVideoPlayerUrl==null||this._FlashVideoPlayerContainerId==null){throw new Error("Missing parameters.");}
this._iBannerHoldSeconds=parameters["BannerHoldSeconds"]||5;this._oFunctionQueue_FlashVideoPlayer=new PlainJoeStudios.Web.FunctionQueue();this._oTimeline=new PlainJoeStudios.MediaSocial.WebServer.Controls.Timeline();this._oTimeline.addListener(function(oUserData){This._onVideoTimelineEvent(oUserData);});var strPreviewHtml=parameters["VideoEmbedHtml"];var strPreviewUrl=parameters["VideoSourceUrl"];if(strPreviewHtml!=null||strPreviewUrl!=null){this._arr_oVideoShowings=null;this._updateVideoTimelineForPreview(strPreviewUrl,strPreviewHtml);}
else{var oBus=$PJP.GetControl("ChannelBus");oBus.attachListener(oBus.LISTENER_TYPE_SCHEDULE,function(oPacket){This._arr_oVideoShowings=oPacket;This._updateVideoTimeline(oPacket);});}
var iFirstShowing=(parseInt(parameters["FirstShowingDateTime"])*1000);if(isNaN(iFirstShowing)){iFirstShowing=this._iFirstVideoStartTime||null;}
var iNow=g_TimeSynchronization.getServerTime(new Date().getTime());if(iFirstShowing===null||iNow+this.MIN_TIME_NEEDED_FOR_SYNC_TEST<iFirstShowing){g_TimeSynchronization.RegisterTestCompleteListener(function(){This._onSynchronizationComplete();});window.setTimeout(function(){g_TimeSynchronization.CalculateSynchronization(This.SYNC_TEST_NUM_TESTS);},This.SYNC_TEST_DELAYED_START);}
else{This._onSynchronizationComplete();}
this._iTimelineClockId=window.setInterval(function(){This._oTimeline.tick(g_TimeSynchronization.getServerTime());},this.TIMELINE_TICK_INTERVAL);},_onVideoTimelineEvent:function(oTimelineEvent){var This=this;var strAction=oTimelineEvent.action;var iVideoId=oTimelineEvent.videoId;var iShowingId=oTimelineEvent.showingId||0;var iNow=g_TimeSynchronization.getServerTime(new Date().getTime());var iStart=oTimelineEvent.showtimeStart||null;if(iVideoId===0||strAction===this.ACTION_BANNERS){if(!this._bIsShowingBanners){this.ShowBannerRotation();this._iPreloadingVideo=null;this._iPlayingVideo=null;if(this._iActiveOrNextShowingId!==null){this._iPreviousShowingId=this._iActiveOrNextShowingId;this._iActiveOrNextShowingId=null;}}}
else if(strAction===this.ACTION_PRELOAD_VIDEO){if((iVideoId!==this._iPreloadingVideo&&iVideoId!==this._iPlayingVideo)||(iShowingId!==this._iActiveOrNextShowingId&&iShowingId!==this._iPreviousShowingId)){if(oTimelineEvent.streamUrlLate==null||(iNow<iStart-this.EARLY_LATE_JOIN_PERIOD*1000)){this.preloadVideo(oTimelineEvent.streamUrlEarly);this._iPreloadingVideo=iVideoId;this._iPlayingVideo=null;this._iActiveOrNextShowingId=iShowingId;}
else if(oTimelineEvent.streamUrlLate!=null){this.preloadVideo(oTimelineEvent.streamUrlLate);this._iPreloadingVideo=iVideoId;this._iPlayingVideo=null;this._iActiveOrNextShowingId=iShowingId;}}}
else if(strAction===this.ACTION_VIDEO){if(iShowingId!==this._iPreviousShowingId&&(iVideoId!==this._iPlayingVideo||iShowingId!==this._iActiveOrNextShowingId)){if(oTimelineEvent.videoType===this.VIDEO_TYPE_PRERECORDED){if(this._iPreloadingVideo!==iVideoId){this.preloadVideo(oTimelineEvent.streamUrlLate||oTimelineEvent.streamUrlEarly);}
var iOffset=iNow-iStart;if(iOffset<0){iOffset=0;}
if(oTimelineEvent.offset>0){iOffset+=oTimelineEvent.offset;}
this.playVideo(iOffset);}
else if(oTimelineEvent.videoType===this.VIDEO_TYPE_LIVE){this.playEmbed(oTimelineEvent.streamHtml);}
if(iShowingId!==0){window.setTimeout(function(){This._sendPlaybackNotification(iShowingId,iVideoId);},Math.floor(Math.random()*5000+1));}
this._iPreloadingVideo=null;this._iPlayingVideo=iVideoId;this._iActiveOrNextShowingId=iShowingId;}}
window.setTimeout(function(){This.updateCountdown();},1);},_sendPlaybackNotification:function(iShowingId,iVideoId){PlainJoeStudios.MediaSocial.WebServer.Services.Channel.PlaybackStarted(Math.abs(iShowingId),iVideoId,this._EventId);},_onSynchronizationComplete:function(){var This=this;this._bSynchronizationComplete=true;this.LoadFlashVideoPlayer();},_updateVideoTimelineForPreview:function(strSourceUrl,strEmbedHtml){this._oTimeline.removeEventsAll();var iNow=g_TimeSynchronization.getServerTime(new Date().getTime());var oEvent={action:this.ACTION_VIDEO,streamUrlEarly:strSourceUrl||null,streamUrlLate:strSourceUrl||null,streamHtml:strEmbedHtml||null,showtimeStart:iNow};if(strSourceUrl){oEvent.videoType=this.VIDEO_TYPE_PRERECORDED;}
else{oEvent.videoType=this.VIDEO_TYPE_LIVE;}
this._oTimeline.addEvent(iNow,oEvent);this._iFirstVideoStartTime=iNow;},_updateVideoTimeline:function(arr_oVideos){var iNowServer=g_TimeSynchronization.getServerTime(new Date().getTime());var bHasVideos=!(arr_oVideos==null||arr_oVideos.length===0);this._oTimeline.removeEventsAll();var bAddedBanners=false;if(!bHasVideos||(iNowServer+this.MIN_TIME_NEEDED_FOR_INTERMISSION_BANNERS<arr_oVideos[0].StartDateTimeSec*1000)){var oEvent={action:this.ACTION_BANNERS};this._oTimeline.addEvent(iNowServer,oEvent);bAddedBanners=true;}
if(!bHasVideos){return;}
var iPreviousVideo_Start=null;var iPreviousVideo_End=null;var oPreviousVideo_Type=null;var iPreviousShowingId=null;var iPreviousVideoId=null;iPreviousVideo_Start=arr_oVideos[0].StartDateTimeSec*1000;iPreviousVideo_End=arr_oVideos[0].StartDateTimeSec*1000+arr_oVideos[0].ShowingDurationMS;oPreviousVideo_Type=arr_oVideos[0].SourceType;iPreviousShowingId=arr_oVideos[0].ShowingId;iPreviousVideoId=arr_oVideos[0].VideoId;if(arr_oVideos[0].SourceType===this.VIDEO_TYPE_PRERECORDED){var iStartTime=arr_oVideos[0].StartDateTimeSec*1000;var iPreloadTime=iStartTime-(this.PRELOAD_PERIOD*60*1000);if(bAddedBanners&&iPreloadTime<iNowServer){iPreloadTime=iNowServer+1;}
if(iPreloadTime<iStartTime){var oEvent={action:this.ACTION_PRELOAD_VIDEO,videoType:arr_oVideos[0].SourceType,streamUrlEarly:arr_oVideos[0].SourceStreamUrlEarly,streamUrlLate:arr_oVideos[0].SourceStreamUrlLate,showingId:arr_oVideos[0].ShowingId,videoId:arr_oVideos[0].VideoId,showtimeStart:iStartTime,offset:arr_oVideos[0].SourceOffsetMS};this._oTimeline.addEvent(iPreloadTime,oEvent);}}
var oEvent={action:this.ACTION_VIDEO,videoType:oPreviousVideo_Type,streamUrlEarly:arr_oVideos[0].SourceStreamUrlEarly,streamUrlLate:arr_oVideos[0].SourceStreamUrlLate,streamHtml:arr_oVideos[0].SourceHtmlEmbedding,showingId:iPreviousShowingId,videoId:iPreviousVideoId,showtimeStart:iPreviousVideo_Start,offset:arr_oVideos[0].SourceOffsetMS};this._oTimeline.addEvent(iPreviousVideo_Start,oEvent);this._iFirstVideoStartTime=iPreviousVideo_Start;for(var i=1;i<arr_oVideos.length;++i){var oVideo=arr_oVideos[i];var iStartTime=oVideo.StartDateTimeSec*1000;var iEndTime=oVideo.StartDateTimeSec*1000+oVideo.ShowingDurationMS;if(iPreviousVideo_End
+this.END_OF_VIDEO_SLACK
+this.MIN_TIME_NEEDED_FOR_INTERMISSION_BANNERS<iStartTime){var oEvent={action:this.ACTION_BANNERS};this._oTimeline.addEvent(iPreviousVideo_End+this.END_OF_VIDEO_SLACK,oEvent);}
if(iStartTime<iNowServer){this._oTimeline.removeEventsAll();this._iFirstVideoStartTime=iStartTime;}
if(oVideo.SourceType===this.VIDEO_TYPE_PRERECORDED){var iPreloadTime=iStartTime-(this.PRELOAD_PERIOD*60*1000);if(iPreloadTime<iPreviousVideo_End){iPreloadTime=iPreviousVideo_End+this.END_OF_VIDEO_SLACK;}
if(iPreloadTime>=iStartTime){iPreloadTime=iStartTime-1*1000;}
var oEvent={action:this.ACTION_PRELOAD_VIDEO,videoType:oVideo.SourceType,streamUrlEarly:oVideo.SourceStreamUrlEarly,streamUrlLate:oVideo.SourceStreamUrlLate,showingId:oVideo.ShowingId,videoId:oVideo.VideoId,showtimeStart:iStartTime,offset:oVideo.SourceOffsetMS};this._oTimeline.addEvent(iPreloadTime,oEvent);}
var oEvent={action:this.ACTION_VIDEO,videoType:oVideo.SourceType,streamUrlEarly:oVideo.SourceStreamUrlEarly,streamUrlLate:oVideo.SourceStreamUrlLate,streamHtml:oVideo.SourceHtmlEmbedding,showingId:oVideo.ShowingId,videoId:oVideo.VideoId,showtimeStart:iStartTime,offset:oVideo.SourceOffsetMS};this._oTimeline.addEvent(iStartTime,oEvent);iPreviousVideo_Start=iStartTime;iPreviousVideo_End=iEndTime;iPreviousShowingId=oVideo.ShowingId;iPreviousVideoId=oVideo.VideoId;}
var oEventFinish={action:this.ACTION_BANNERS};this._oTimeline.addEvent(iPreviousVideo_End,oEventFinish);return;},updateCountdown:function(){var This=this;if(typeof(g_Countdown)==='undefined'||g_Countdown===null){return;}
if(this._iPlayingVideo!==null){g_Countdown.ShowLiveNow();This._iShowingIdSentToCountdown=null;}
else{var bWasFound=false;var fnCallback=function(oData){if(oData.action===This.ACTION_BANNERS||oData.action===This.ACTION_PRELOAD_VIDEO){return;}
if(oData.showingId!==This._iShowingIdSentToCountdown){g_Countdown.ShowCountingDown(g_TimeSynchronization.getServerTime(),oData.showtimeStart);This._iShowingIdSentToCountdown=oData.showingId;}
bWasFound=true;return false;};this._oTimeline.forEachEvent(fnCallback);if(!bWasFound){g_Countdown.Hide();This._iShowingIdSentToCountdown=null;}}},ShowBannerRotation:function(){var This=this;this._jqLiveEmbedContainer.empty();this._jqLiveEmbedContainer.hide();this._jqFlashPlayerContainer.show();this._oFunctionQueue_FlashVideoPlayer.Run(function(){This._jqFlashPlayer[0].js_stopVideo();This._jqFlashPlayer[0].js_showBanners("/Handlers/RotatingBanners.ashx?eid="+This._EventId,This._iBannerHoldSeconds);});this._bIsShowingBanners=true;},LoadFlashVideoPlayer:function(){this._jqLiveEmbedContainer.hide();this._jqFlashPlayerContainer.show();swfobject.embedSWF(this._FlashVideoPlayerUrl,this._FlashVideoPlayerContainerId,"626","351","9.0.124","/Flash/expressInstall.swf",{videoBufferTime:1},{wmode:"transparent",allowFullScreen:"true"},null,function(e){if(!e.success){$(".noflash").show();}});this._jqFlashPlayer=$("#"+this._FlashVideoPlayerContainerId);if(this._jqFlashPlayer.length==0){throw new Error("VideoPlayer flash object not found.");}},preloadVideo:function(strUrl){var This=this;this._oFunctionQueue_FlashVideoPlayer.Run(function(){This._jqFlashPlayer[0].js_preloadVideo(strUrl,0.0);});},playVideo:function(iSeekToMs){var This=this;this._jqLiveEmbedContainer.empty();this._jqLiveEmbedContainer.hide();this._jqFlashPlayerContainer.show();if(typeof(iSeekToMs)==="number"&&iSeekToMs!=0){this._oFunctionQueue_FlashVideoPlayer.Run(function(){This._jqFlashPlayer[0].js_hideBanners();This._jqFlashPlayer[0].js_seekVideo(iSeekToMs/1000);});}
else{this._oFunctionQueue_FlashVideoPlayer.Run(function(){This._jqFlashPlayer[0].js_hideBanners();This._jqFlashPlayer[0].js_playVideo(0);});}
this._bIsShowingBanners=false;},playEmbed:function(strEmbedCode){var This=this;if(this._oFunctionQueue_FlashVideoPlayer.GetIsSynchronous()){this._oFunctionQueue_FlashVideoPlayer.Run(function(){This._jqFlashPlayer[0].js_stopVideo();});}
this._jqFlashPlayerContainer.hide();this._jqLiveEmbedContainer.show();this._jqLiveEmbedContainer.html(strEmbedCode);var cl="videoplayerexternalembed";$("."+cl).removeClass(cl).find("embed, object").eq(0).addClass(cl);this._bIsShowingBanners=false;},fjs_Message:function(strMessage){var This=this;if(strMessage==="applet_loaded"){this._bFlashPlayerReady=true;this._jqFlashPlayer=$("#"+this._FlashVideoPlayerContainerId);window.setTimeout(function(){This._oFunctionQueue_FlashVideoPlayer.Run();},1);}
else if(strMessage==="video_stopped"){this._iPlayingVideo=null;this._iPreloadingVideo=null;if(this._iActiveOrNextShowingId!==null){this._iPreviousShowingId=this._iActiveOrNextShowingId;this._iActiveOrNextShowingId=null;}}}};