function BlockUtil(option) {
	$.extend(this, options);
}
BlockUtil.setCurrentBlock = function(block){
	this.currentblock = block || null;
	return this;
}

BlockUtil.getCurrentBlock = function(block){
	return this.currentblock;
}

BlockUtil.getNextIdleBlock = function(index) {
	for (var i = (1 * index || 0); i < BlockCacher.blockCache.length; i++) {
		var o = BlockCacher.blockCache[i];
		if (!o.isRunning) {
			BlockUtil.setCurrentBlock(o);
			return o;
		}
	}
	var firstblock = BlockCacher.blockCache[0];
	// firstblock.stopMonitor();
	return BlockCacher.blockCache[0];
}


BlockUtil.getBlockByUuid = function(uuid) {
	for (var i = 0; i < BlockCacher.blockCache.length; i++) {
		var o = BlockCacher.blockCache[i];
		if (o.uuid == uuid){
			BlockUtil.setCurrentBlock(o);
			return o;
		}
	}
	return null;
}

BlockUtil.getBlockByIndex = function(idx) {
	for (var i = 0; i < BlockCacher.blockCache.length; i++) {
		var o = BlockCacher.blockCache[i];
		if (o.index == idx){
			BlockUtil.setCurrentBlock(o);
			return o;
		}
	}
	return null;
}

BlockUtil.getBlockByMonId = function(monid) {
	for (var i = 0; i < BlockCacher.blockCache.length; i++) {
		var o = BlockCacher.blockCache[i];
		if (o.monid == monid){
			BlockUtil.setCurrentBlock(o);
			return o;
		}
	}
	return null;
}

BlockUtil.getBlockByPeerConnection = function(peerConnection) {
	for (var i = 0; i < BlockCacher.blockCache.length; i++) {
		var o = BlockCacher.blockCache[i];
		if (o.peerConnection == peerConnection){
			BlockUtil.currentblock = o ;
			return o;
		}
	}
	return null;
}

BlockUtil.addBlock = function(number) {
	var x = BlockCacher.blockCache.length;
	while (x < number) {

		BlockCacher.addBlock($.recordblock({
			uuid : $.uniqueId(),
			title : "",
			cssClass : 'num-' + number,
			$baseContainer : baseContainer,
			serverConnection : serverConnection,
			index : x++,
			monid : null,
			isRunning : false,
			onRightClick : $.noop()
		}).init());
	}
}

BlockUtil.changeBlockStatus = function(index) {
	var block = this.getBlockByIndex(index);
	block.setTitle(block.getHideTitle());
	block.setRunning(true);
	// console.log(block.getContent().html());
	block.$baseContainer.find(".con-head").removeClass("current");
	block.getContent().children(".con-head").eq(0).addClass("current");
	BlockUtil.setCurrentBlock(block);
}

BlockUtil.changeDirection = function(index, direct, dom) {
	var block = this.getBlockByIndex(index);
	!!direct && block && block.changeDirection({
		"up" : 11,
		"down" : 12,
		"left" : 13,
		"right" : 14
	}[String(direct)] || "");
}

BlockUtil.stopChangeDirection = function(index, direct, dom) {
	var block = this.getBlockByIndex(index);
	block && block.stopChangeDirection();
}

BlockUtil.stopMonitor = function(index) {
	var block = this.getBlockByIndex(index);
	if (block) {
		block.stopMonitor().setRunning(false).setHideTitle("").setTitle("")
				.setFdId("").setChannelId("").setMonId(null);
		block.setPeerConnection(null);
		block.getVideoComp().src = "";
	}
	return block;
}

BlockUtil.fullScreen = function(index) {
	var block = this.getBlockByIndex(index);
	if (block) {
		var status = (document.fullscreenElement||document.webkitFullscreenElement);
		if(!status) block.getVideoComp().webkitRequestFullScreen();
		else {
			document.webkitCancelFullScreen();
		}
	}
	return block;
}

BlockUtil.changeLayout = function(number) {
	$("#screen" + BlockCacher.blockCache.length).removeClass("block-"+ BlockCacher.blockCache.length +"-selected");
	$("#screen" + number).addClass("block-"+ number +"-selected");
	
	if (BlockCacher.blockCache.length > number) {

		for (var i = number; i < BlockCacher.blockCache.length; i++) {
			baseContainer.children("div[index=" + i + "]").remove();
		}

		BlockCacher.blockCache = BlockCacher.blockCache.splice(0, number);

	} else if (BlockCacher.blockCache.length < number) {
		BlockUtil.addBlock(number);
	}
	baseContainer.children("div").removeAttr("class").attr('class',
			"num-" + number);
	
	return this;
}

BlockUtil.showChannelIcon = function(rs) {
	if (rs != null) {
		var tdzt = rs.tdzt;
		var recordState = rs.lxzt;
		var ldzt = rs.tdlx;
		var icon = "../../frame/assets/images/device/ballCameraColor.png";
		if (tdzt == "1" && (ldzt == "1" || ldzt == "2" || ldzt == "3" ||

		ldzt == "4")) {
			if ("1" == recordState) {
				icon = "../../frame/assets/images/device/ballCameraVCR.png";
			} else {
				icon = "../../frame/assets/images/device/ballCameraColor.png";
			}
			rs.icon = icon;
		}
		if (tdzt == "0" && (ldzt == "1" || ldzt == "2" || ldzt == "3" ||

		ldzt == "4")) {
			icon = "../../frame/assets/images/device/ballCameraGray.png";
			rs.icon = icon;
		}
		if (tdzt == "1" && (ldzt == "5" || ldzt == "6" || ldzt == "7" ||

		ldzt == "8" || ldzt == "15" || ldzt == "20")) {
			if ("1" == recordState) {
				icon = "../../frame/assets/images/device/gunCameraVCR.png";
			} else {
				icon = "../../frame/assets/images/device/gunCameraColor.png";
			}
			rs.icon = icon;
		}
		if (tdzt == "0" && (ldzt == "5" || ldzt == "6" || ldzt == "7" ||

		ldzt == "8" || ldzt == "15" || ldzt == "20")) {
			icon = "../../frame/assets/images/device/gunCameraGray.png";
			rs.icon = icon;
		}
		// GPS
		if (tdzt == "1" && (ldzt == "9" || ldzt == "10")) {
			if ("1" == recordState) {
				icon = "../../frame/assets/images/device/gpsCarVCR.png";
			} else {
				icon = "../../frame/assets/images/device/gpsCarColor.png";
			}
			rs.icon = icon;
		}
		if (tdzt == "0" && (ldzt == "9" || ldzt == "10")) {
			icon = "../../frame/assets/images/device/gpsCarGray.png";
			rs.icon = icon;
		}
		// ¾¯Ô±
		if (tdzt == "1" && (ldzt == "11")) {
			rs.icon = "../../frame/assets/images/device/map_policeColor.png";
			rs.mapIcon = "../../frame/assets/images/iconMap/map_police"
					+ rs.ssParam1 + ".png";
		}
		if (tdzt == "0" && (ldzt == "11")) {
			rs.icon = "../../frame/assets/images/device/map_policeGray.png";
			rs.mapIcon = "../../frame/assets/images/iconMap/map_police"
					+ rs.ssParam1 + ".png";
		}
	}
	return rs;
}

BlockUtil.showChannelIcon_xztree = function(rs) {

	// console.log(rs);
	if (rs != null) {
		var tdzt = rs.tdzt;
		var recordState = rs.lxzt;
		var tdlx = rs.tdlx;
		var icon = "icon-node-ballCameraColor";
		if (tdzt == "1" && (tdlx == "1" || tdlx == "2" || tdlx == "3" ||

		tdlx == "4")) {
			if ("1" == recordState) {
				icon = "icon-node-ballCameraVCR";
			} else {
				icon = "icon-node-ballCameraColor";
			}
			rs.icon = icon;
		}
		if (tdzt == "0" && (tdlx == "1" || tdlx == "2" || tdlx == "3" ||

		tdlx == "4")) {
			icon = "icon-node-ballCameraGray";
			rs.icon = icon;
		}
		if (tdzt == "1" && (tdlx == "5" || tdlx == "6" || tdlx == "7" ||

		tdlx == "8" || tdlx == "15" || tdlx == "20")) {
			if ("1" == recordState) {
				icon = "icon-node-gunCameraVCR";
			} else {
				icon = "icon-node-gunCameraColor";
			}
			rs.icon = icon;
		}
		if (tdzt == "0" && (tdlx == "5" || tdlx == "6" || tdlx == "7" ||

		tdlx == "8" || tdlx == "15" || tdlx == "20")) {
			icon = "icon-node-gunCameraGray";
			rs.icon = icon;
		}
		// GPS
		if (tdzt == "1" && (tdlx == "9" || tdlx == "10")) {
			if ("1" == recordState) {
				icon = "icon-node-gpsCarVCR";
			} else {
				icon = "icon-node-gpsCarColor";
			}
			rs.icon = icon;
		}
		if (tdzt == "0" && (tdlx == "9" || tdlx == "10")) {
			icon = "icon-node-gpsCarGray";
			rs.icon = icon;
		}
		// ¾¯Ô±
		if (tdzt == "1" && (tdlx == "11" || tdlx == "12")) {
			// rs.icon = "images/device/map_policeColor.png";
			// rs.mapIcon = "images/iconMap/map_police"+rs.ssParam1+".png";
			if ("1" == recordState) {
				icon = "icon-node-PoliceColorWork";
			} else {
				icon = "icon-node-PoliceColor";
			}
			rs.icon = icon;
		}
		if (tdzt == "0" && (tdlx == "11" || tdlx == "12")) {
			// rs.icon = "images/device/map_policeGray.png";
			// rs.mapIcon = "images/iconMap/map_police"+rs.ssParam1+".png";
			icon = "icon-node-PoliceGray";
			rs.icon = icon;
		}
	}
	return rs;

}
BlockUtil.historyGridCache = function(sbbh, tdbh, tdmc, tdzt,tdlx,lxzt) {
	var obj = {
			sbbh : sbbh,
			tdbh : tdbh,
			tdmc : tdmc,
			tdzt : tdzt,
			tdlx : tdlx,
			lxzt : lxzt,
			sbtdbh:String(sbbh +"-"+tdbh)
	};
	
	var rows = historyGrid.datagrid("getData").rows;
	
	if(rows && rows.length != 0){
		for(var i = 0;i<rows.length;i++){
			if(rows[i].sbtdbh == String(obj.sbbh +"-"+obj.tdbh) ){
				return;
			}
		}
	}
	
	historyGrid.datagrid('insertRow', {
		index : 0,
		row   : obj
	});
}

BlockUtil.queryRecord=function(begintime,endtime,secondsstart,fdid,channelid,subid,uuid){
	serverConnection.send(JSON.stringify({'queryrecord': {'fdid':fdid, 'channelid':(channelid-1), 'subid':subid || 0, 'begintime':begintime, 'endtime':endtime}, 'uuid': secondsstart}));
	return this;
}

BlockUtil.searchRecordResult=function(endtime,searchDay,recordType,sbtdobject){
	$('#timeline-container').empty();
	var recorddayarray = this.queryRecordQueue(endtime,searchDay);
	for(var i = 0 ; i <recorddayarray.length;i++){
		var day = recorddayarray[i];
		BlockUtil.queryRecord(day[0],day[1],day[0],sbtdobject.sbbh,sbtdobject.tdbh,0,$.uniqueId());
	}
}

BlockUtil.renderRecordResult=function(data,recordDate){
	var $ctx = $("<div class='timeLine'>") ,canvasbar = $.uniqueId() ,$recordbar = $("<div class='timeLine-right' id="+canvasbar+"></div>")  ;
		$ctx.append("<div class='timeLine-left'>"+($.formatDate(new Date(recordDate*1000)))+"</div>").append($recordbar);
		$('#timeline-container').append($ctx);
		$.recordTimeline({
		    wrapperId: canvasbar,
		    begintime: recordDate,
		    timeArr: data,
		    currObj: $(window).data('currobj')
		}).drawData();
}

BlockUtil.queryRecordQueue = function(endTime,len){
	var timeGroup = [];
	var start =(new Date((endTime + " 00:00:00").replace(/-/g,"/"))).getTime();
	var end = (new Date((endTime + " 23:59:59").replace(/-/g,"/"))).getTime();
	for(var i = 0; i < len;i++){
		var item = [];
		item.push((start - 24*60*60*1000*i)/1000);
		item.push((end - 24*60*60*1000*i)/1000);
		timeGroup.push(item);
	}
	return timeGroup;
}
BlockUtil.prepareRecordBack = function(begintime,endtime,currobj,currtime,playbackurl){
	var block = BlockUtil.getCurrentBlock();
	if(!block) block = BlockUtil.getNextIdleBlock();
	console.log("prepareRecordBack:"  + block);
	if(block){
		block.setFdId(currobj.sbbh).setChannelId(currobj.tdbh).setHideTitle(currobj.tdmc).startPlayback(
				begintime ,0,currtime,playbackurl,currobj.sbbh,currobj.tdbh,0);
		//BlockUtil.historyGridCache(row.sbbh,row.tdbh,row.tdmc,row.tdzt,row.tdlx,row.lxzt);
	}
}

BlockUtil.changeSpeed = function(index,speed){
	var block = BlockUtil.getBlockByIndex(index);
	alert(block.getMonId() +" -- " + block.getUuId())
}

BlockUtil.stopPlayback = function(index) {
	var block = this.getBlockByIndex(index);
	if (block) {
		block.stopPlayback().setRunning(false).setHideTitle("").setTitle("")
				.setFdId("").setChannelId("").setMonId(null);
		block.setPeerConnection(null);
		block.getVideoComp().src = "";
	}
	return block;
}

BlockUtil.pauseMonitor = function(index,obj) {
	var block = this.getBlockByIndex(index);
	if (block) {
		if($(obj).html() == "ÔÝÍ£"){
			block.pauseStopMonitor();
			$(obj).html("²¥·Å");
		}else{
			block.pauseStartMonitor();
			$(obj).html("ÔÝÍ£");
		}
	}
	return block;
}

BlockUtil.changeSpeed = function(index,speed){
	var block = this.getBlockByIndex(index);
	if (block) {
		block.changeSpeed(speed.toString());
	}
	return block;
}