/*
 *  InUC Web Client Video Window v4.0.1 by NEC Platforms,Ltd.
 */
var debuglog=0,l={log:function(a){0<debuglog&&console.log(a)},dir:function(a){1<debuglog&&console.dir(a)},warn:function(a){console.warn(a)},error:function(a){0<=debuglog&&console.error(a)}},EXPAND_NONE=0,EXPAND_SELF=1,EXPAND_OTHER=2,VQ_LOW=1,VQ_MID=2,VQ_HIGH=3,VVM=null,viewModel=null;
function koInitialize(){VVM=window.opener.VVM;window.opener.ko.applyBindings(VVM,document.getElementById("video_hdr"));window.opener.ko.applyBindings(VVM,document.getElementById("video_area"));viewModel=window.opener.viewModel;window.opener.ko.applyBindings(viewModel,document.getElementById("video_hdr_ex"));document.getElementById("hdr_vqt").selectedIndex=VVM.localVideo()?VVM.localVideo().vqval()-VQ_LOW:VQ_MID-VQ_LOW;setDocTitle()}
$(document).ready(function(){koInitialize();$(window).on("orientationchange resize",function(a){adjustFrames();setTimeout(function(){window.opener.muteVideoElements()},500)});$(window).focus(function(){setTimeout(function(){window.opener.muteVideoElements()},500)});$(".barrageBlock").on("click",function(a){$(this).hasClass("block_clickevt")||($(".barrageBlock").addClass("block_clickevt"),setTimeout(function(){$(".barrageBlock").removeClass("block_clickevt")},1E3))});$(window).on("unload",function(){if(window.opener.viewModel.videoSubWin()==
window){window.opener.viewModel.videoSubWin(null);try{!1===window.opener.switchToVoice(window.opener.viewModel.primeCall())&&window.opener.SIP_getStream(window.opener.viewModel.primeCall().cid),stopScreenShare()}catch(a){}}window.opener.ko.cleanNode(document);window.opener.ko.removeNode(viewModel);window.opener.ko.removeNode(VVM);VVM=viewModel=null;$(window).off();$(window).documentElement.removeData();$(window).documentElement.innerHTML=""});$(window).on("message",function(a){a=a.originalEvent;l.dir(a);
a.source!==window||void 0===a.data?l.log("ignore message"):"GetScreenShare"==a.data.type&&"share-screen"===a.data.callback&&(l.log(a.data.response),a.data.response&&getScreenShareStream(a.data.response))});muteAllVideoElements();adjustFrames()});function setDocTitle(){for(var a="",b,c=0,e=VVM.viewVideoList().length;c<e;c++)b=VVM.viewVideoList()[c],b!=VVM.localVideo()&&(a&&(a+=" / "),a+=VVM.viewVideoList()[c].name());document.title=a}
function clickEndCall(){try{window.opener.endCall(window.opener.viewModel.primeCall().cid)}catch(a){}}function clickExBtn(){"none"==$("#video_hdr_ex").css("display")?$("#video_hdr_ex").css("display","block"):$("#video_hdr_ex").css("display","none")}function closeExBtns(){$("#video_hdr_ex").css("display","none")}function clickVideoBtn(){viewModel.isVideoDevAttached()&&(0==VVM.cameraEnabled()?window.opener.switchToVideo():window.opener.switchToVoice())}
function clickAudioBtn(){viewModel.isAudioDevAttached()&&(1==VVM.localMuted()?window.opener.unmuteAudio():window.opener.muteAudio())}function clickEyeBtn(){var a=!VVM.visibleLocal(),b=VVM.localVideo();b&&(b.expand()==EXPAND_SELF&&expandVideo(b),b.visible(a))}function clickScreenBtn(){VVM.ScShareStream()?stopScreenShare():startScreenShare()}function clickHoldCall(){try{window.opener.clickHoldBtn(window.opener.viewModel.primeCall())}catch(a){}}
function clickBTransCall(){try{window.opener.clickBTransBtn()}catch(a){}}function clickTransCall(){try{window.opener.clickTransBtn()}catch(a){}}function clickSwapCall(){try{window.opener.swapCall()}catch(a){}}function clickAddCall(){}function clickVoiceCall(){window.close()}
function getDeviceStream(a){if(navigator.mediaDevices&&"getUserMedia"in navigator.mediaDevices)return l.log("navigator.mediaDevices.getUserMadia"),navigator.mediaDevices.getUserMedia(a);if("undefined"!==typeof Promise)return l.log("wrap getUserMadia with Promise"),new Promise(function(b,c){getUserMedia(a,b,c)});l.log("navigator.getUserMadia with No Promise");return null}
function expandVideo(a){if(a.expand()==EXPAND_SELF)for(var b=0,c=VVM.videoList().length;b<c;b++){var e=VVM.videoList()[b];e.expand(EXPAND_NONE)}else for(b=0,c=VVM.videoList().length;b<c;b++)e=VVM.videoList()[b],e===a?e.expand(EXPAND_SELF):e.expand(EXPAND_OTHER);adjustFrames()}function getExpandIdx(){for(var a=0,b=VVM.viewVideoList().length;a<b;a++)if(VVM.viewVideoList()[a].expand()==EXPAND_SELF)return a;return-1}
function muteAllVideoElements(){for(var a,b=0,c=VVM.viewVideoList().length;b<c;b++)a=".video#vid"+b,$(a)[0]&&($(a)[0].muted=!0)}var svadd_timer=[];
function addSrcVideoElement(a){svadd_timer[a]&&(clearTimeout(svadd_timer[a]),delete svadd_timer[a]);svadd_timer[a]=setTimeout(addSrcVideoElement,100,a);l.log("sub:addSrcVideoElement["+a+"]");if(void 0==VVM)return l.warn("sub:not ready VVM["+a+"]"),null;if(void 0===VVM.viewVideoList()[a])return l.warn("sub:addSrcVideoElement no list ["+a+"]"),null;var b=VVM.viewVideoList()[a].stream();if(void 0==b||null==b)return l.warn("sub:not ready stream["+a+"]"),null;var c=$(".video#vid"+a)[0];if(void 0===c||
null==c)return l.warn("sub:not ready videoElement["+a+"]"),null;if(void 0===c.srcObject)return l.warn("sub:not support srcObject["+a+"]"),clearTimeout(svadd_timer[a]),delete svadd_timer[a],window.URL.createObjectURL(b);setTimeout(function(b){c.srcObject=null;c.srcObject=b;l.log("sub:set srcObject["+a+"]");l.dir(b);setTimeout(function(){window.opener.muteVideoElements()},500)},100,b);clearTimeout(svadd_timer[a]);delete svadd_timer[a];return null}
function frameResize(a,b,c,e,k){if(a<VVM.viewVideoList().length){var g="vid"+a,f="#vf"+a,d=VVM.viewVideoList()[a],m=e-2,h=k-2,n=640,q=480,p=document.getElementById(g);if(null!=p&&0!==p.videoWidth&&0!==p.videoHeight)n=p.videoWidth,q=p.videoHeight;else{if(d.stream()&&0<d.stream().getVideoTracks().length&&d.stream()&&d.stream().getVideoTracks()[0].enabled){l.log(g+":waiting to recieve video stream");setTimeout(function(a,b,c,d,e){frameResize(a,b,c,d,e)},100,a,b,c,e,k);return}l.log(g+":cannot get video size default[640,480]")}a=
m/n;g=h/q;a<g?h=q*a:m=n*g;b=b+e/2-m/2;c=c+k/2-h/2;$(f).css("top",d.top+"px");$(f).css("left",d.left+"px");$(f).css("width",d.width+"px");$(f).css("height",d.height+"px");$(f).css("font-size",d.fontSz);$(f).animate({top:c,left:b,width:m,height:h,fontSize:m<h?m/10+"px":h/10+"px"},300);d.top=c;d.left=b;d.width=m;d.height=h;d.fontSz=m<h?m/10+"px":h/10+"px";$(f).off("mouseenter");$(f).mouseenter(function(){adjustFrames();window.opener.muteVideoElements()})}}var rsz_timer=null;
function adjustFrames(){null!=rsz_timer&&(clearTimeout(rsz_timer),rsz_timer=null);rsz_timer=setTimeout(function(){var a=getExpandIdx();0<=a?2>=VVM.viewVideoList().length?adjustFrames_full(a):adjustFrames_expand(a):adjustFrames_uniformaly();rsz_timer=null},100)}
function adjustFrames_uniformaly(){var a=VVM.viewVideoList().length;l.log("adjustFrames count="+a);if(a){var b=$("#video_area").innerWidth(),c=$("#video_area").innerHeight();if(0>=b||0>=c)l.log("### cannot get area size vaw="+b+",vah="+c);else{b>=c?(va_l=b,va_s=c):(va_l=c,va_s=b);var e=0,k=0,g=1,f=a;temp_l=f;for(temp_s=g;temp_l>=temp_s;){var d=va_l/temp_l;var m=va_s/temp_s;var h=d<m?d:m;d*=m;if(h>e||h===e&&d>k)e=h,k=d,f=temp_l,g=temp_s;temp_s++;temp_l=Math.ceil(a/temp_s)}l.log(a+":"+f+"\u00d7"+g);
for(i=g;0<i;i--)for(j=f=Math.ceil(a/i);0<j;j--)b>=c?(h=b*(j-1)/f,e=c*(i-1)/g,k=b/f,d=c/g):(h=b*(i-1)/g,e=c*(j-1)/f,k=b/g,d=c/f),l.log(a-1+": ("+h+","+e+","+k+","+d+")"),frameResize(a-1,h,e,k,d),a--}}}
function adjustFrames_expand(a){var b=VVM.viewVideoList().length;l.log("adjustFrames count="+b);if(!(2>=b)){var c=$("#video_area").innerWidth(),e=$("#video_area").innerHeight();if(0>=c||0>=e)l.log("### cannot get area size vaw="+c+",vah="+e);else for(var k,g,f,d,m=0,h=0,n=VVM.viewVideoList().length;h<n;h++)VVM.viewVideoList(),h===a?(g=k=0,c<e?(f=c,d=.85*e):(f=.85*c,d=e)):(c<e?(k=c/(b-1)*m,g=.85*e,f=c/(b-1),d=e*(1-.85)):(k=.85*c,g=e/(b-1)*m,f=c*(1-.85),d=e/(b-1)),m++),frameResize(h,k,g,f,d)}}
function adjustFrames_full(a){var b=VVM.viewVideoList().length;l.log("adjustFrames count="+b);if(b&&!(2<b)){b=$("#video_area").innerWidth();var c=$("#video_area").innerHeight();if(0>=b||0>=c)l.log("### cannot get area size vaw="+b+",vah="+c);else for(var e,k,g,f,d=0,m=VVM.viewVideoList().length;d<m;d++)d===a?(k=e=0,g=b,f=c):(e=.85*b,k=.85*c,g=b*(1-.85),f=c*(1-.85)),frameResize(d,e,k,g,f)}}
function startScreenShare(){VVM.sup_check()&&VVM.cap_check()&&viewModel.videoSubWin()&&getScreenShareStream()}function stopScreenShare(){VVM.sup_check()&&VVM.cap_check()&&dettachScreenShare()}
function getScreenShareStream(){VVM.cap_check()?navigator.mediaDevices.getDisplayMedia({audio:!1,video:{mandatory:{maxWidth:1280,maxHeight:720,maxFrameRate:60}}}).then(function(a){l.log("start Capture.");attachScreenShare(a)}).catch(function(a){l.log("[NG] Media is failed.");l.error("An error occurred: ["+a+"]")}):l.log("[NG] cap_check is false.")}function attachScreenShare(a){try{window.opener.attachScreenShare(a)}catch(b){}}
function dettachScreenShare(){try{window.opener.dettachScreenShare()}catch(a){}}function vqt_change(a){window.opener.sendEvtVQChange(VQ_LOW+a.selectedIndex)};
