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 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 initRecord(){
	var param = {};
	param.historystateid = id;
	param.page = 1;
	param.rows = 1;
	
	$.ajax({
		type:'post',
		dataType : 'json',
		url:'ywvideohistorystateAction!get_Ywvideohistorystate.do',
		data:param,
		success:function(data){
			if(data ){
				record = data;
				videodata = data;
				fillDetailInfo(record);
			}
			
		}
	});
}
function eventTabChange(type){
	if(type=="eventDiv"){   //事件图片
		//console.log("$('#recordBtn')",$("#recordBtn"))
		$("#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");
	}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");
		$("#planDiv").css("display","none");
		$("#pictureNav").removeClass("active");
		$("#videoNav").removeClass("active");
		$("#recordNav").addClass("active");
		$("#planNav").removeClass("active");
		//关闭实时视频
		videoblock.stopMonitor(); 
		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();
		$("#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");
		//关闭录像
		recordblock.stopPlayback();

		webrtc_startVideo(record.videonum,record.channelnum,record.channelname);
//		webrtc_startVideo("201808091236525951","1","窗口高架-01");
	}
}
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);
}