var winId	= $.getUrlParam("winId");
var id = top.$("#"+winId).panel("options").data;
var videodata = {};
var record = {};
$(function(){
	initRecord();
	//初始化视频播放控件
	initBlock();
	initRecordBlock();
	$(window).on("focus",function(){
		(window !== top.WebRtcServer.reqPage) && top.WebRtcServer.setReqPage(window);
	});
	parent.$(".panel-tool-close").on("click",function(){
//		videoblock.stopMonitor();
//		recordblock.stopPlayback();
		unloadWindow();
	})
	parent.$(".l-btn-text").on("click",function(){
//		videoblock.stopMonitor();
//		recordblock.stopPlayback();
		unloadWindow();
	})
});
function formatter1(date){
	if (!date){return '';}
	return $.fn.datebox.defaults.formatter.call(this, date);
}
function parser1(s){
	if (!s){return null;}
	return $.fn.datebox.defaults.parser.call(this, s);
}
function initRecord(){
	var param = {};
	param.currentstateid = id;
	param.page = 1;
	param.rows = 1;
	
	$.ajax({
		type:'post',
		dataType : 'json',
		url:'ywvideocurrentstateAction!get_Ywvideocurrentstate.do',
		data:param,
		success:function(data){
			if(data ){
				record = data;
				videodata = data;
				fillDetailInfo(record);
			}
			
		}
	});
}
function searchVideo(){
	unloadWindow();
	var serdate = $('#searchDate').val();
	var serdateArr=serdate.split("-");
	serdateArr[1]=serdateArr[1].substring(1,2);
	serdate=serdateArr.join("-");
	var startTime = serdate+" 00:00:00";
	var endTime = serdate+" 23:59:59";
	/*		var start ="00:10:50";
	var end ="00:11:50";*/
	barInit(startTime,endTime);
	webrtc_startRecord(record.videonum,record.channelnum,record.channelname,startTime,endTime);//设备编号$通道编号$通道名称信息
}
function eventTabChange(type){
	if(type=="eventDiv"){   //事件图片
	//	console.log("$('#recordBtn')",$("#recordBtn"))
		unloadWindow();
		$("#recordBtn").hide();
		$("#eventDiv").css("display","block");
		$(".pc-recordvideo").css("display","none");
		$(".pc-img").css("display","block");
		$(".pc-video").css("display","none");
		$("#planDiv").css("display","none");
		$("#pictureNav").addClass("active");
		$("#videoNav").removeClass("active");
		$("#recordNav").removeClass("active");
		$("#planNav").removeClass("active");
		
		$(".detail").css("display","none");
		$("#showDetail").removeClass("active");
		$(".div-info").css("display","block");
		$(".div-img").removeClass("width100");
	}else if(type=="recordDiv"){   //录像播放
		$("#searchDate").datebox("setValue",$.getTodayStart(0,"00:00:00"));
		$("#recordBtn").show();
		$("#eventDiv").css("display","block");
		$(".pc-recordvideo").css("display","block");
		$(".pc-img").css("display","none");
		$(".pc-video").css("display","none");
		unloadWindow();
		$("#planDiv").css("display","none");
		$("#pictureNav").removeClass("active");
		$("#videoNav").removeClass("active");
		$("#recordNav").addClass("active");
		$("#planNav").removeClass("active");
		
		$(".detail").css("display","none");
		$("#showDetail").removeClass("active");
		$(".div-info").css("display","block");
		$(".div-img").removeClass("width100");
		
		//关闭实时视频
		var myDate = new Date();
		var currDate = myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+myDate.getDate();
		var startTime = currDate+" 00:00:00";
		var endTime = currDate+" 23:59:59";
		barInit(startTime,endTime);
		webrtc_startRecord(record.videonum,record.channelnum,record.channelname,startTime,endTime);//设备编号$通道编号$通道名称信息
	}else if(type=="videoDiv"){   //视频播放
		//console.log("$('#recordBtn')",$("#recordBtn"))
		$("#recordBtn").hide();
		unloadWindow();
		$("#eventDiv").css("display","block");
		$(".pc-recordvideo").css("display","none");
		$(".pc-img").css("display","none");
		$(".pc-video").css("display","block");
		$("#planDiv").css("display","none");
		$("#pictureNav").removeClass("active");
		$("#videoNav").addClass("active");
		$("#recordNav").removeClass("active");
		$("#planNav").removeClass("active");
		
		$(".detail").css("display","none");
		$("#showDetail").removeClass("active");
		$(".div-info").css("display","block");
		$(".div-img").removeClass("width100");
		//关闭录像

		webrtc_startVideo(record.videonum,record.channelnum,record.channelname);
//		webrtc_startVideo("201808091236525951","1","窗口高架-01");
	}else if(type=="detailDiv"){
		// 详情页面
		$("#recordBtn").hide();
		$("#eventDiv").css("display","block");
		$(".pc-recordvideo").css("display","none");
		$(".pc-img").css("display","none");
		$(".pc-video").css("display","none");
		$("#planDiv").css("display","none");
		$("#pictureNav").removeClass("active");
		$("#videoNav").removeClass("active");
		$("#recordNav").removeClass("active");
		$("#planNav").removeClass("active");
		
		$(".detail").css("display","block");
		$("#showDetail").addClass("active");
		$(".div-info").css("display","none");
		$(".div-img").addClass("width100");
	}
}
function barInit(start, end){
	ProgressBar.init({
		startTime:new Date(start),
		endTime:new Date(end),
		totalSecond:86400
	});
}
function fillDetailInfo(data){
//	$("#taskname").text(data.taskname);
//	$("#tasktypename").text(data.tasktypename);
	$("#channelname").text(data.channelname);
	$("#videonum").text(data.videonum);
	$("#channelnum").text(data.channelnum);
	$("#sbip").text(data.sbip);
	$("#cjmc").text(data.cjmc);
	$("#currentstate").text(data.currentstatename);
//	$("#videostatusname").text(data.videostatusname);
//	$("#failurecause").text(data.failurecause);
//	$("#videolabelstatusname").text(data.videolabelstatusname);
//	$("#runstatusname").text(data.runstatusname);
//	$("#runtime").text(data.runtime);
	var pciUrl = data.extend1;
	if (data.extend1 == null || data.extend1 == '' || data.extend1 == '无') {
		pciUrl = data.extend2;
	}
	var proName = window.location.pathname.split("/")[1];
	var sysUrl = "http://"+window.location.host+"/"+proName;
	var picProxyGetUrl = sysUrl+"/sourceProxy?fileType=picUrlProxy&picUrl="+data.extend1;
	$(".pc-img .pc-img-topimg img").attr("src",picProxyGetUrl);
	// 详情
	var isonline = data.isonline;
	show("#isonline",isonline);
	var vqcstate = data.vqcstate;
	show("#vqcstate",vqcstate);
	var nosignalstatus = data.nosignalstatus;
	nosignalTransStatus("#nosignalstatus",nosignalstatus);

	var imgclearstatus = data.imgclearstatus;
	show("#imgclearstatus",imgclearstatus);
	var imgcontraststatus = data.imgcontraststatus;
	show("#imgcontraststatus",imgcontraststatus);
	var imgoverbrightstatus = data.imgoverbrightstatus;
	show("#imgoverbrightstatus",imgoverbrightstatus);

	var imgtoodarkstatus = data.imgtoodarkstatus;
	show("#imgtoodarkstatus",imgtoodarkstatus);
	var imgcolorstatus = data.imgcolorstatus;
	show("#imgcolorstatus",imgcolorstatus);
	var videonoisestatus = data.videonoisestatus;
	show("#videonoisestatus",videonoisestatus);

	var videorollstripestatus = data.videorollstripestatus;
	show("#videorollstripestatus",videorollstripestatus);
	var blackwhiteimgstatus = data.blackwhiteimgstatus;
	show("#blackwhiteimgstatus",blackwhiteimgstatus);
	var videofreezestatus = data.videofreezestatus;
	show("#videofreezestatus",videofreezestatus);

	var videojitterstatus = data.videojitterstatus;
	show("#videojitterstatus",videojitterstatus);
	var videoupheavalstatus = data.videoupheavalstatus;
	show("#videoupheavalstatus",videoupheavalstatus);
	var scenechangestatus = data.scenechangestatus;
	show("#scenechangestatus",scenechangestatus);

	var videoocclusionstatus = data.videoocclusionstatus;
	show("#videoocclusionstatus",videoocclusionstatus);
	var recordstate = data.recordstate;
	show("#recordstate",recordstate);
}

function show(id,zt){
	if(zt==1){
		$(id).html('<div style="height:13px;width:13px;background-color:#60b631;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin-bottom: 0;margin-top: -13px;margin-left: 42%;margin-right: 2px;"></div>');
	}else if(zt==0){
		$(id).html('<div style="height:13px;width:13px;background-color:#fe0000;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin-top: -13px;margin-left: 42%;margin-right: 2px;"></div>');
	}
}

function nosignalTransStatus(id,data){
	if(data=="1"){
		$(id).html("<div style='height:13px;width:13px;background-color:#60b631;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin-bottom: 0;margin-top: -13px;margin-left: 42%;margin-right: 2px;'></div>");
	} else if(data=="2") {
		$(id).html("<div class='tip' title='码流中断' style='height:13px;width:13px;background-color:#ff7a05;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin-bottom: 0;margin-top: -13px;margin-left: 42%;margin-right: 2px;'></div>");
	} else if(data=="3") {
		$(id).html("<div class='tip' title='帧率过低' style='height:13px;width:13px;background-color:#fe0000;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin-bottom: 0;margin-top: -13px;margin-left: 42%;margin-right: 2px;'></div>");
	} else if(data=="4") {
		$(id).html("<div class='tip' title='丢包' style='height:13px;width:13px;background-color:#fe0000;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin-bottom: 0;margin-top: -13px;margin-left: 42%;margin-right: 2px;'></div>");
	} else {
		return "";
	}
}