const EXPAND_IN_PLACE=0;
const EXPAND_NEW_WINDOW=1;
var _live={
players:[],
windows:[],
format:FORMAT_JPEG,
camInfo:1,
cropMode:0,
hiRes:0,
expand:EXPAND_IN_PLACE,
expandedCam:null,
fps:'',
ptzPicker:null,
};
function LiveLoad(){
const sidebar=document.getElementById('liveSidebar');
const content=document.getElementById('liveContent');
sidebar.style.display='flex';
content.style.display='block';
if(!_live.ptzPicker)_live.ptzPicker=new PtzPicker();
LiveCreateGroupPicker();
LiveCreateCamPicker();
window.onresize=()=>{LiveConfigurePlayers();};
window.addEventListener('keydown',LiveKeyPress);
}
function LiveUnload(){
const sidebar=document.getElementById('liveSidebar');
const content=document.getElementById('liveContent');
sidebar.style.display='none';
content.style.display='none';
window.onresize=null;
for(let i=0;i<_live.players.length;i++)if(_live.players[i]){
_live.players[i].dispose();
_live.players[i]=null;
}
_live.players=[];
window.removeEventListener('keydown',LiveKeyPress);
}
function LiveGetCookies(){
let cVal=allCookies.getItem('liveFormat');
if(cVal!=null)_live.format=parseInt(cVal);
cVal=allCookies.getItem('liveCropMode');
if(cVal==null){
_live.cropMode=_defaultCropMode;
allCookies.setItem('liveCropMode',_defaultCropMode,Infinity);
}else _live.cropMode=parseInt(cVal);
cVal=allCookies.getItem('liveFps');
if(cVal!=null)_live.fps=cVal;
cVal=allCookies.getItem('liveExpand');
if(cVal!=null)_live.expand=parseInt(cVal);
cVal=allCookies.getItem('liveCamInfo');
if(cVal!=null)_live.camInfo=parseInt(cVal);
cVal=allCookies.getItem('liveHiRes');
if(cVal!=null)_live.hiRes=parseInt(cVal);
}
function LiveCreateGroupPicker(){
var html="";
for(let i=0;i<_groups.length;i++)html+='<option value="'+_groups[i].id+'">'+_groups[i].name+'</option>';
const sel=document.getElementById('liveGroup');
sel.innerHTML=html;
var liveGroup=allCookies.getItem('liveGroup');
if(liveGroup && SelectValueIsValid(sel,liveGroup))sel.value=liveGroup;
}
function LiveGroupSelectionChanged(){
_live.expandedCam=null;
allCookies.setItem('liveGroup',document.getElementById("liveGroup").value,Infinity);
LiveCreateCamPicker();
}
function LiveCreateCamPicker(){
var html="",isOneChecked=false,firstCam=null;
const members=_groups[document.getElementById('liveGroup').selectedIndex].members;
let list=allCookies.getItem('liveChecks'+document.getElementById('liveGroup').value);
let checkedArr=list?list.split(',').map(Number):[];
let ptzFlag=false;
for(let i=0;i<_cams.length;i++){
if(!_cams[i].demo && _cams[i].perm&PERM_LIVEVIDEO && members.includes(_cams[i].num)){
let checked=checkedArr.includes(_cams[i].num);
if(_cams[i].ptz && _cams[i].perm&PERM_CAMCONTROL)ptzFlag=true;
if(checked)isOneChecked=true;
if(firstCam==null)firstCam=_cams[i].num;
html+='<label><input type="checkbox" id="liveCam'+_cams[i].num+'" class="camCheck" onclick="LiveCamSelectionChanged(event)"'+(checked?' checked':'')+'> '+_cams[i].name+'</label>';
}
}
if(firstCam==null){
document.getElementById('liveCamSelectDiv').innerHTML='<span>'+Loc('No cameras are available')+'</span>';
}else{
document.getElementById('liveCamSelectDiv').innerHTML=html;
if(!isOneChecked)document.getElementById('liveCam'+firstCam).checked=true;
}
document.getElementById('ptzDivWrapper').style.display=ptzFlag?'':'none';
LiveCamSelectionChanged();
}
function LiveCamSelectionChanged(event){
_live.expandedCam=null;
let members=_groups[document.getElementById('liveGroup').selectedIndex].members;
let list='';
if(event && event.altKey)for(m of members)if(document.getElementById('liveCam'+m))document.getElementById('liveCam'+m).checked=event.target.checked;
for(m of members)if(document.getElementById('liveCam'+m)&& document.getElementById('liveCam'+m).checked)list+=(list?',':'')+m;
allCookies.setItem('liveChecks'+document.getElementById('liveGroup').value,list,Infinity);
LiveConfigurePlayers();
}
function LiveConfigurePlayers(){
let selectedCams=[];
let firstPtzCam=null;
let ptzCamIncluded=false;
LiveGetCookies();
for(let i=0;i<_live.players.length;i++){
if(_live.players[i] &&(!(document.getElementById('liveCam'+i)&& document.getElementById('liveCam'+i).checked)|| _live.players[i].format!=_live.format || _live.players[i].fps!=_live.fps)){
_live.players[i].dispose();
_live.players[i]=null;
}
}
for(let i=0;i<_cams.length;i++){
let checkbox=document.getElementById('liveCam'+_cams[i].num);
if(checkbox && checkbox.checked)selectedCams.push(_cams[i]);
}
const clickHandler=function(cam,isDoubleClick){
if(isDoubleClick){
LiveExpandCam(cam);
}else{
_live.ptzPicker.setCam(cam);
}
}
const liveContent=document.getElementById('liveContent');
const rects=OptimumGridLayout(liveContent.offsetWidth,liveContent.offsetHeight,_live.camInfo?INFO_HEIGHT:0,selectedCams);
for(let i=0;i<selectedCams.length;i++){
let camNum=selectedCams[i].num;
if(!_live.players[camNum])_live.players[camNum]=new Player(selectedCams[i],'liveContent',_live.format,_live.fps,clickHandler,'');
if(_live.expandedCam==null){
_live.players[camNum].show();
_live.players[camNum].setFrame(rects[i],_live.camInfo,_live.hiRes,_live.cropMode);
if(_live.ptzPicker.cam!=null && _live.ptzPicker.cam.num==camNum)ptzCamIncluded=true;
if(firstPtzCam==null && selectedCams[i].ptz && selectedCams[i].perm&PERM_CAMCONTROL)firstPtzCam=selectedCams[i];
document.getElementById('liveContent').style.background='';
}else{
if(_live.expandedCam==camNum){
_live.players[camNum].show();
_live.players[camNum].setFrame({x:3,y:3,w:liveContent.offsetWidth-6,h:liveContent.offsetHeight-6},_live.camInfo,_live.hiRes,_live.cropMode);
if(selectedCams[i].ptz && selectedCams[i].perm&PERM_CAMCONTROL)firstPtzCam=selectedCams[i];
}else{
_live.players[camNum].hide();
}
document.getElementById('liveContent').style.background='red';
}
}
if(!ptzCamIncluded)_live.ptzPicker.setCam(firstPtzCam);
document.getElementById('liveInfo').style.display=selectedCams.length?'none':'';
}
function LiveExpandCam(cam){
if(_live.expand==EXPAND_IN_PLACE){
if(_live.expandedCam==cam.num)_live.expandedCam=null;
else _live.expandedCam=cam.num;
LiveConfigurePlayers();
}else{
if(_live.windows[cam.num] && !_live.windows[cam.num].closed)_live.windows[cam.num].focus();
else _live.windows[cam.num]=window.open('live-cam?cameraNum='+cam.num,'_blank','width=800,height=600');
}
}
function LiveKeyPress(event){
if(event.key=="Escape" && _live.expandedCam!=null){
_live.expandedCam=null;
LiveConfigurePlayers();
}
}
