var videoblock;
var recordblock;
//实时播放视频组件
function initBlock(){
	var uuid = $.uniqueId();
	videoblock = $.videoBlock({
		uuid : uuid,
		title : "",
		$baseContainer : $("#video"),
		$content:$("#video"),
		serverConnection : top.serverConnection,
		monid : null,
		isRunning : false,
		onRightClick : $.noop()
	});
	BlockCacher.addBlock(videoblock);
	$("#video video").attr("id",videoblock.videoId);
	//console.log('实时视频============',videoblock);
}
//实时视频
function initBodyEvent(){
	$(window).on("focus",function(){
		(window !== top.WebRtcServer.reqPage) && top.WebRtcServer.setReqPage(window);
	});
}
function webrtc_startVideo(sbbh,tdbh,tdmc){
	videoblock.setFdId(sbbh).setChannelId(tdbh).setHideTitle(tdmc).startMonitor();
}

//历史视频播放组件
function initRecordBlock(){
	var uuid = $.uniqueId();
	recordblock = $.recordblock({
		uuid : uuid,
		title : "",
		$baseContainer : $("#record"),
		$content:$("#record"),
		serverConnection : top.serverConnection,
		monid : null,
		isRunning : false,
		onRightClick : $.noop()
	});
	BlockCacher.addBlock(recordblock);
	$("#record video").attr("id",recordblock.videoId);
	//console.log('历史视频============',recordblock);
}
function webrtc_startRecord(){
	var sbbh = Plan.planRecord.vdId;
	var tdbh = Plan.planRecord.channelId;
	var tdmc = Plan.planRecord.tdmc;
	var playbackurl = "";
	var curDate = Plan.planRecord.occurTime;
	var start_occur= "";
	var end_occur = "";
	start_occur = start_occurTime(curDate);
	end_occur = end_occurTime(curDate);	
	console.log('回放视频参数=='+sbbh+'，'+tdbh+'，'+tdmc+'，'+curDate);
	setTimeout(function(){
		recordblock.setFdId(sbbh).setChannelId(tdbh).setHideTitle(tdmc).startPlayback(start_occur ,0,end_occur,playbackurl,sbbh,tdbh,0);
	},300)
}
//播放事故录像，按照设备信息和事件编号播放
function webrtc_startRecordByEventId(sbbh,tdbh,tdmc,accidentId,occurTime){
	//initRecordBlock();
	var playbackurl = "";
	//计算时间差，单位：分钟
	var currTime = new Date();
	var diffTime = currTime.getTime() - new Date(occurTime).getTime(); //时间差的毫秒数
	var minutes=Math.floor(diffTime / parseInt(1000*60)); 
	setTimeout(function(){ 
		if(minutes < 30) {  //时间差小于30分钟调用中心录像
			var start_occur= "";
			var end_occur = "";
			start_occur = start_occurTime(occurTime);
			end_occur = end_occurTime(occurTime);	
			recordblock.setFdId(sbbh).setChannelId(tdbh).setHideTitle(tdmc).startPlayback(start_occur ,0,end_occur,playbackurl,sbbh,tdbh,0);
		}else{//大于30分钟调用告警事件录像
			recordblock.setFdId(sbbh).setChannelId(tdbh).setHideTitle(tdmc).startPlaybackByEventId(accidentId , sbbh, tdbh, 0);
		}
	},300)
}
function webrtc_startRecordByAlarm(sbbh,tdbh,tdmc,curDate){
	initRecordBlock();
	var playbackurl = "";
	var start_occur= "";
	var end_occur = "";
	start_occur = start_occurTime(curDate);
	end_occur = end_occurTime(curDate);	
	
	console.log('回放视频参数=='+sbbh+'，'+tdbh+'，'+tdmc+'，'+curDate);
	console.log('=回放录像时间start_occur = ' , start_occur);
	console.log('=回放录像时间end_occur = ' , end_occur);
	setTimeout(function(){
		recordblock.setFdId(sbbh).setChannelId(tdbh).setHideTitle(tdmc).startPlayback(start_occur ,0,end_occur,playbackurl,sbbh,tdbh,0);
	},300)
	//recordblock.setFdId(sbbh).setChannelId(tdbh).setHideTitle(tdmc).startPlayback(start_occur ,0,end_occur,playbackurl,sbbh,tdbh,0);
}
//关闭视频组件
function unloadWindow(){
	/*for(var i = 0;i < BlockCacher.blockCache.length;i++){
		if(BlockCacher.blockCache[i].fdId && BlockCacher.blockCache[i].channelId != null){
			BlockUtil.stopMonitor(i);
		}
	}*/
	if(videoblock && videoblock.fdId && videoblock.channelId != null){
		//实时视频关闭
		videoblock.stopMonitor();
		videoblock.setRunning(false).setHideTitle("").setTitle("")
		.setFdId("").setChannelId("").setMonId(null);
		videoblock.setPeerConnection(null);
		videoblock.getVideoComp().src = "";
	}
	if(recordblock && recordblock.fdId && recordblock.channelId != null){
		//录像关闭
		recordblock.stopPlayback();
		recordblock.setRunning(false).setHideTitle("").setTitle("")
		.setFdId("").setChannelId("").setMonId(null);
		recordblock.setPeerConnection(null);
		recordblock.getVideoComp().src = "";
	}
}

//毫秒数转换
function timeChange(time){
	return Date.parse(new Date(time.replace(/-/g,"/")))/1000;
}
//事件开始事件，输入字符串时间前30秒
function start_occurTime(time){
	var dt=new Date(time.replace(/-/g,"/"));//将传入的日期格式的字符串转换为date
	var ndt=new Date(dt.getTime()-30000);//将转换之后的时间减去十秒
	return Date.parse(ndt)/1000;
}
//事件开始事件，输入字符串时间后30秒
function end_occurTime(time){
	var dt=new Date(time.replace(/-/g,"/"));//将传入的日期格式的字符串转换为date
	var ndt=new Date(dt.getTime()+30000);//将转换之后的时间减去十秒
	return Date.parse(ndt)/1000;
}

//获取配置文件信息
function getConfigPlanVideoInfo(){
	var obj = [];
	$.ajax({
		 type:'post',
		 dataType : 'json',
		 async:false,
		 url : 'trafficaccidentinfoAction!getConfigPlanVideoInfo.do',
		 success:function(data){
			obj = data;
		 }
	});
	return obj;
}