VideoWallLoading();
//<!-- 根据数据调整行距以及显示 
var _lastValueV,_lastValueH;
function VideoWallLoading() {	
	var _h = $('#ParametersWallSizeH').val();
	var _v = $('#ParametersWallSizeV').val();
	if (parseInt(_h)<=0||parseInt(_v)<=0||parseInt(_h)>5||parseInt(_v)>5) {
		$('.MessageBox').show();
		$('#ParametersWallSizeH').val(_lastValueH);
		$('#ParametersWallSizeV').val(_lastValueV);
		//alert('must be 1-5');
		return;
	}
	_lastValueV = _v;
	_lastValueH = _h;
	$('.listHeadRightCont').html('');
	PresetDataDetail = [];
	for (var i = 0; i < parseInt(_h); i++) {
		$('.listHeadRightCont').append("<ul class='listHeadRightContCavans' style='font-size:12px;'></ul>");
		PresetDataDetail.push([]);
	}
	$('.listHeadRightCont .listHeadRightContCavans').each(function(e){
		for (var j = 0; j < parseInt(_v); j++) {
			$(this).append("<li id='walldecodedrag"+((j+1)+parseInt(_v)*e)+"' class='listHeadLeftContDataLeft listHeadLeftContDataLeftMargin' draggable='true' ondragstart='videowalldragstart(event)' ondragover='dragover(event)' ondrop='drop(event)'></li>");
			PresetDataDetail[e][j]='';
		}
	});
	var _listnum = document.getElementsByClassName('listHeadRightCont')[0].children.length;
	var _num;
	for (var i = 0; i < _listnum; i++) {
		_num = document.getElementsByClassName("listHeadRightContCavans")[i].children.length;
		_num=169*_num;
		document.getElementsByClassName("listHeadRightContCavans")[i].style.width = _num;
		if (i!=0) {
			document.getElementsByClassName("listHeadRightContCavans")[i].style.marginTop = 143*i;
		}
	}
}
$('#ParametersWallSizeH').on('change',function(){
	VideoWallLoading();
});
$('#ParametersWallSizeV').on('change',function(){
	VideoWallLoading();
});
function dragover(e) {
	e.preventDefault();
}
var dragType='';
// decode drag 事件
function videowalldragstart(e) {
	//console.log(e);
	if (e.target.innerHTML=='') {
		return;
	}
	e.dataTransfer.setData('text',e.target.innerHTML);
	//nowDevicePosition = 
	videoPageChangeType = 'videoDecodeWalls';
	if (navigator.userAgent.indexOf('Firefox')>0) {
		nowPosition = e.target.id.replace('decodeencodeexist','');
	}else {
		nowPosition = e.path[0].id.replace('decodeencodeexist','');
	}
	
	// nowWallDevicePosition = 'wallDecodeChangePosition';
	dragType = 'decode';
}
// wall decode drop 事件
function drop(e) {	
	if (navigator.userAgent.indexOf('Firefox')>0) {
		e.stopPropagation();//阻止冒泡行为 firefox表现为弹出新标签页
		e.preventDefault();//阻止默认行为
	}
	
	var _h = parseInt($('#ParametersWallSizeH').val());
	var _v = parseInt($('#ParametersWallSizeV').val());
	var _arr = _data.videoswitching_encode_arr.split(',');
	var _number;
	checkParameterHasEncode = _txDeviceMAC;
	for (var i = 0; i <_arr.length-1; i++) {
		if (_arr[i]==checkParameterHasEncode) {
			_number=i;
		}
	}
	if (dragType=='decode'&&videoPageChangeType=='videoDecode') {
		var data = e.dataTransfer.getData('text');
		if(e.target.className=='DragToDropBtn'){//从decode向wall写入 与设备进行交换		
			if (navigator.userAgent.indexOf('Firefox')>0) {
				var _num = parseInt(e.target.offsetParent.id.replace('walldecodedrag',''));
			}else {
				var _num = parseInt(e.srcElement.offsetParent.id.replace('walldecodedrag',''));
			}
			
			$('#walldecodedrag'+_num).html(data);
			var _dataCheck = $('#walldecodedrag'+_num+' span:eq(1)').html();
			checkInWallDacodeData(_num,_dataCheck);
			$('.listHeadRightCont li').each(function(a){
				if ((_num-1)!=a) {
					if (_dataCheck==$('.listHeadRightCont li:eq('+a+') span:eq(1)').html()) {
						var _i = parseInt((a)/_h);
						var _j = (a)%_v;
						$('.listHeadRightCont li:eq('+a+')').html('');
						$('.listHeadRightCont li:eq('+a+')').css('background-color','#fcfcfc');	
						PresetDataDetail[_i][_j] = '';
					}
				}
			});
			if ($('#WallParameters span:eq(5)').html()!='&nbsp;') {
				console.log('decode1:'+_dataCheck);
				var _ResolutionData;
				var _i,_j;//parameterResolution
				var _dataCommand;
				var _h,_v,_i,_j;
				var _arr = _data.videoswitching_encode_arr.split(',');
				var _num;

				checkParameterHasEncode = _txDeviceMAC;
				for (var i = 0; i <_arr.length-1; i++) {		
					if (_arr[i]==checkParameterHasEncode) {
						_num=i;				
					}		
				}
				var _checkInArr='';
				$('.listHeadRightCont li').each(function(e){
					if ($(this).html()!='') {
						_checkInArr += (e+1)+',';
					}
				});
				if (_checkInArr=='') {
					return;
				}
				_h = parseInt($('#ParametersWallSizeH').val());
				_v = parseInt($('#ParametersWallSizeV').val());

				_checkInArr = _checkInArr.substr(0,_checkInArr.length-1).split(',');
				for (var i = 0; i < _checkInArr.length; i++) {
					_ResolutionData = returnResolution(_checkInArr[i],checkParameterHasEncode);
					_ResolutionData = _ResolutionData.split(',');
					//console.log(_ResolutionData[0]+':'+_ResolutionData[1]+':'+_ResolutionData[2]+':'+_ResolutionData[3]);
					_i = parseInt((parseInt(_checkInArr[i])-1)/_v);
					_j = (parseInt(_checkInArr[i])-1)%_v;
					if (PresetDataDetail[_i][_j]==_dataCheck) {

							_dataCommand = 'join '+checkParameterHasEncode+':HDMI:0 '
									+PresetDataDetail[_i][_j]+':HDMI:0 wall size '+EncodeVideoDataArr[_num][6]+' '+EncodeVideoDataArr[_num][7]+' fps 60 offset '+
									_ResolutionData[0]+' '+_ResolutionData[1]+' keep '+_ResolutionData[2]+' '+_ResolutionData[3]+
									' viewport '+_ResolutionData[4]+' '+_ResolutionData[5]+' '+_ResolutionData[6]+' '+_ResolutionData[7];
							sendCommand('data='+_dataCommand);
						
					}
					//console.log(_dataCommand);
				}
			}
		}else{//与空设备进行交换
			//e.target.innerHTML = data;
			if (navigator.userAgent.indexOf('Firefox')>0) {
				var _num = parseInt(e.target.id.replace('walldecodedrag',''));
			}else {
				var _num = parseInt(e.srcElement.id.replace('walldecodedrag',''));
			}
			
			$('#walldecodedrag'+_num).html(data);
			var _dataCheck = $('#walldecodedrag'+_num+' span:eq(1)').html();
			$('#walldecodedrag'+_num).css('background-color','#62c5fd');	
			var presetNum = $('.listHeadLeftContPresetSelect').val();
			//e.target.style.backgroundColor = '#62c5fd';
			checkInWallDacodeData(_num,_dataCheck);
			$('.listHeadRightCont li').each(function(a){
				if (_num!=(a+1)) {
					if($('.listHeadRightCont li:eq('+a+') span:eq(1)').html()==_dataCheck){
						$('.listHeadRightCont li:eq('+a+')').html('');
						$('.listHeadRightCont li:eq('+a+')').css('background-color','#fcfcfc');				
					}
				}		
			});
			if ($('#WallParameters span:eq(5)').html()!='&nbsp;') {
				console.log('decode2:'+_dataCheck);
				var _ResolutionData;
				var _i,_j;//parameterResolution
				var _dataCommand;
				var _h,_v,_i,_j;
				var _arr = _data.videoswitching_encode_arr.split(',');
				var _num;

				checkParameterHasEncode = _txDeviceMAC;
				for (var i = 0; i <_arr.length-1; i++) {		
					if (_arr[i]==checkParameterHasEncode) {
						_num=i;				
					}		
				}
				var _checkInArr='';
				$('.listHeadRightCont li').each(function(e){
					if ($(this).html()!='') {
						_checkInArr += (e+1)+',';
					}
				});
				if (_checkInArr=='') {
					return;
				}
				_h = parseInt($('#ParametersWallSizeH').val());
				_v = parseInt($('#ParametersWallSizeV').val());

				_checkInArr = _checkInArr.substr(0,_checkInArr.length-1).split(',');
				for (var i = 0; i < _checkInArr.length; i++) {
					_ResolutionData = returnResolution(_checkInArr[i],checkParameterHasEncode);
					_ResolutionData = _ResolutionData.split(',');
					//console.log(_ResolutionData[0]+':'+_ResolutionData[1]+':'+_ResolutionData[2]+':'+_ResolutionData[3]);
					_i = parseInt((parseInt(_checkInArr[i])-1)/_v);
					_j = (parseInt(_checkInArr[i])-1)%_v;
					if (PresetDataDetail[_i][_j]==_dataCheck) {
						_dataCommand = 'join '+checkParameterHasEncode+':HDMI:0 '
								+PresetDataDetail[_i][_j]+':HDMI:0 wall size '+EncodeVideoDataArr[_num][6]+' '+EncodeVideoDataArr[_num][7]+' fps 60 offset '+
								_ResolutionData[0]+' '+_ResolutionData[1]+' keep '+_ResolutionData[2]+' '+_ResolutionData[3]+
								' viewport '+_ResolutionData[4]+' '+_ResolutionData[5]+' '+_ResolutionData[6]+' '+_ResolutionData[7];
						sendCommand('data='+_dataCommand);
					}
					//console.log(_dataCommand);
				}
			}			
		}
		dragType = '';
		videoPageChangeType='';
	}
	else if(dragType=='decode'&&videoPageChangeType=='videoDecodeWalls'){
		var data = e.dataTransfer.getData('text');		
		
		if(e.target.className=='DragToDropBtn'){// 表示两个设备交换位置
			if (navigator.userAgent.indexOf('Firefox')>0) {
				var _num = parseInt(e.target.offsetParent.id.replace('walldecodedrag',''));
			}else {
				var _num = parseInt(e.srcElement.offsetParent.id.replace('walldecodedrag',''));
			}
			
			var _changeDeviceID = new Array();
			_changeDeviceID[0]=$('#walldecodedrag'+_num+' span:eq(1)').html()
			var _changeDeviceCont=$('#walldecodedrag'+_num).html();
			var _arr = _data.videoswitching_encode_arr.split(',');
			
			$('#walldecodedrag'+_num).html(data);
			$('#walldecodedrag'+_num).attr('draggable',true);
			var _dataCheck = $('#walldecodedrag'+_num+' span:eq(1)').html();
			_changeDeviceID[1] = _dataCheck;	
			checkInWallDacodeData(_num,_dataCheck);
			console.log('1.'+_dataCheck+':2.'+_changeDeviceID[0]);
			$('.listHeadRightCont li').each(function(a){
				if ((_num-1)!=a) {
					if (_dataCheck==$('.listHeadRightCont li:eq('+a+') span:eq(1)').html()) {
						var _i = parseInt((a)/_h);
						var _j = (a)%_v;
						$('.listHeadRightCont li:eq('+a+')').html(_changeDeviceCont);
						PresetDataDetail[_i][_j] = _changeDeviceID[0];
					}
				}
			});
			if ($('#WallParameters span:eq(5)').html()!='&nbsp;') {
				console.log(_changeDeviceID[0]);
				var _ResolutionData;
				var _i,_j;//parameterResolution
				var _h,_v,_i,_j;
				var _dataCommand;
				var _checkInArr='';
				$('.listHeadRightCont li').each(function(e){
					if ($(this).html()!='') {
						_checkInArr += (e+1)+',';
					}
				});
				if (_checkInArr=='') {
					return;
				}
				_h = parseInt($('#ParametersWallSizeH').val());
				_v = parseInt($('#ParametersWallSizeV').val());

				_checkInArr = _checkInArr.substr(0,_checkInArr.length-1).split(',');
				for (var j = 0; j < 2; j++) {
					for (var i = 0; i < _checkInArr.length; i++) {
						_ResolutionData = returnResolution(_checkInArr[i],checkParameterHasEncode);
						_ResolutionData = _ResolutionData.split(',');											
						_i = parseInt((parseInt(_checkInArr[i])-1)/_v);
						_j = (parseInt(_checkInArr[i])-1)%_v;
						if (PresetDataDetail[_i][_j]==_changeDeviceID[j]) {

							_dataCommand = 'join '+checkParameterHasEncode+':HDMI:0 '
									+PresetDataDetail[_i][_j]+':HDMI:0 wall size '+EncodeVideoDataArr[_number][6]+' '+EncodeVideoDataArr[_number][7]+' fps 60 offset '+
									_ResolutionData[0]+' '+_ResolutionData[1]+' keep '+_ResolutionData[2]+' '+_ResolutionData[3]+
									' viewport '+_ResolutionData[4]+' '+_ResolutionData[5]+' '+_ResolutionData[6]+' '+_ResolutionData[7];
							sendCommand('data='+_dataCommand);
						}
					}
				}					
			}				
		}else{// 空位置调位
			if (navigator.userAgent.indexOf('Firefox')>0) {
				var _num = parseInt(e.target.id.replace('walldecodedrag',''));
			}else {
				var _num = parseInt(e.srcElement.id.replace('walldecodedrag',''));
			}
			
			$('#walldecodedrag'+_num).html(data);
			$('#walldecodedrag'+_num).attr('draggable',true);
			var _dataCheck = $('#walldecodedrag'+_num+' span:eq(1)').html();
			$('#walldecodedrag'+_num).css('background-color','#62c5fd');	
			var presetNum = $('.listHeadLeftContPresetSelect').val();
			//e.target.style.backgroundColor = '#62c5fd';
			checkInWallDacodeData(_num,_dataCheck);
			console.log('1.'+_dataCheck+':2.'+_dataCheck);
			$('.listHeadRightCont li').each(function(a){
				if (_num!=(a+1)) {
					if($('.listHeadRightCont li:eq('+a+') span:eq(1)').html()==_dataCheck){
						$('.listHeadRightCont li:eq('+a+')').html('');
						$('.listHeadRightCont li:eq('+a+')').removeAttr('draggable');
						$('.listHeadRightCont li:eq('+a+')').removeClass('listContListLiHover');
						$('.listHeadRightCont li:eq('+a+')').css('background-color','#fcfcfc');				
					}
				}		
			});
			if ($('#WallParameters span:eq(5)').html()!='&nbsp;') {
				console.log(_dataCheck);
				var _ResolutionData;
				var _i,_j;//parameterResolution
				var _dataCommand;
				var _h,_v,_i,_j;
				var _checkInArr='';
				$('.listHeadRightCont li').each(function(e){
					if ($(this).html()!='') {
						_checkInArr += (e+1)+',';
					}
				});
				if (_checkInArr=='') {
					return;
				}
				_h = parseInt($('#ParametersWallSizeH').val());
				_v = parseInt($('#ParametersWallSizeV').val());

				_checkInArr = _checkInArr.substr(0,_checkInArr.length-1).split(',');
				for (var i = 0; i < _checkInArr.length; i++) {
					_ResolutionData = returnResolution(_checkInArr[i],checkParameterHasEncode);
					_ResolutionData = _ResolutionData.split(',');
					//console.log(_ResolutionData[0]+':'+_ResolutionData[1]+':'+_ResolutionData[2]+':'+_ResolutionData[3]);
					_i = parseInt((parseInt(_checkInArr[i])-1)/_v);
					_j = (parseInt(_checkInArr[i])-1)%_v;
					if (PresetDataDetail[_i][_j]==_dataCheck) {
						_dataCommand = 'join '+checkParameterHasEncode+':HDMI:0 '
								+PresetDataDetail[_i][_j]+':HDMI:0 wall size '+EncodeVideoDataArr[_number][6]+' '+EncodeVideoDataArr[_number][7]+' fps 60 offset '+
								_ResolutionData[0]+' '+_ResolutionData[1]+' keep '+_ResolutionData[2]+' '+_ResolutionData[3]+
								' viewport '+_ResolutionData[4]+' '+_ResolutionData[5]+' '+_ResolutionData[6]+' '+_ResolutionData[7];
						sendCommand('data='+_dataCommand);
					}
					//console.log(_dataCommand);
				}
			}			
		}
		dragType = '';
		videoPageChangeType='';

		var devicelistNum = 0;
		var dataTmp = "{'value':'"+$('#ParametersWallSizeH').val()+','
									+$('#ParametersWallSizeV').val()+','
									+$('#BezelTop').val()+','
									+$('#BezelBottom').val()+','
									+$('#BezelLeft').val()+','
									+$('#BezelRight').val()+"',"
						+"'device':[";
		var deviceNum = parseInt($('#ParametersWallSizeH').val())*parseInt($('#ParametersWallSizeV').val());
		for (var i = 0; i < deviceNum; i++) {
			(function(ii){
				if ($('#walldecodedrag'+(ii+1)).html()!='') {
					dataTmp+="{'index':"+(ii+1)+','
								+"'hostname':'"+$('#walldecodedrag'+(ii+1)+' span:eq(0)').html()+"',"
								+"'macaddress':'"+$('#walldecodedrag'+(ii+1)+' span:eq(1)').html()+"',"
								+"'ipaddress':'"+$('#walldecodedrag'+(ii+1)+' span:eq(2)').html()+"',"
								+"'version':'"+$('#walldecodedrag'+(ii+1)+' span:eq(3)').html()+"',"
								+"'mode':'"+$('#walldecodedrag'+(ii+1)+' span:eq(4)').html()+"',"
								+"'multicastaddress':'"+$('#walldecodedrag'+(ii+1)+' span:eq(5)').html()+"',"
								+"'status':'"+$('#walldecodedrag'+(ii+1)+' span:eq(6)').html()+"',"
								+"'resolution':'"+$('#walldecodedrag'+(ii+1)+' span:eq(7)').html()+"',"
								+"'colorspace':'"+$('#walldecodedrag'+(ii+1)+' span:eq(8)').html()+"'},"
					devicelistNum++;
				}
			})(i);
		}
		if (devicelistNum!=0) {
			dataTmp = dataTmp.substr(0,dataTmp.length-1);
		}
		dataTmp+=']}';

		ReadDataFromFile('firstsaveToFile='+$('.listHeadLeftContPresetSelect').val()+'&savedata='+dataTmp);
		$('.SelectPresetBox').hide();
	}
	saveToLocalParametersPreset();
}
// checkInWallDacodeData 将id存进数组
function checkInWallDacodeData(num,id) {
	var _h = parseInt($('#ParametersWallSizeH').val());
	var _v = parseInt($('#ParametersWallSizeV').val());
	var _i = parseInt((num-1)/_v);
	var _j = (num-1)%_v;
	var a,b,c,d,dataYUAN,dataHUAN;
	var innull=0;
	for (var i = 0; i < _h; i++) {
		for (var j = 0; j < _v; j++) {
			(function(ii,jj){
				if (PresetDataDetail[ii][jj].length==0&&ii==_i&&jj==_j) {//表示该位置为空
					PresetDataDetail[ii][jj] = id;
					innull+=1;
				}
				else if(PresetDataDetail[ii][jj]!=id&&ii==_i&&jj==_j){//表示该位置为其他设备
					a = ii;
					b = jj;
					dataHUAN = PresetDataDetail[ii][jj];
					innull+=2;
				}
				else if (PresetDataDetail[ii][jj]==id&&(!(ii==_i&&jj==_j))) {//表示该位置为位移前的位置
					c = ii;
					d = jj;
					dataYUAN = PresetDataDetail[ii][jj];
					innull+=1;
				}//((ii==_i&&jj!=_j)||(ii!=_i&&jj==_j)||(ii!=_i&&jj!=_j))
			})(i,j);			
		}
	}
	if (innull==2) {//表示跟空数据位置交换
		PresetDataDetail[c][d] = '';
	}
	else if(innull==3){//表示与该位置进行互换		
		PresetDataDetail[a][b] = dataYUAN;
		PresetDataDetail[c][d] = dataHUAN;
	}
}
// parameter 拖放事件
var checkParameterHasEncode='';
var _parameterWidth,_parameterHeight,_parameterFps;
function dropAll(e) {	
	if (navigator.userAgent.indexOf('Firefox')>0) {
		e.stopPropagation();//阻止冒泡行为 firefox表现为弹出新标签页
		e.preventDefault();//阻止默认行为
	}
	if (dragType!='encode') {
		return;
	}
	var _resolution;
	var _arr = _data.videoswitching_encode_arr.split(',');
	var _num;

	checkParameterHasEncode = _txDeviceMAC;
	for (var i = 0; i <_arr.length-1; i++) {		
		if (_arr[i]==checkParameterHasEncode) {
			_num=i;				
		}		
	}
	_resolution = EncodeVideoDataArr[_num][6]+'x'+EncodeVideoDataArr[_num][7]+'@'+EncodeVideoDataArr[_num][8]+'Hz';
	var _multicastAddr = EncodeVideoDataArr[_num][4];
	$('#WallParameters span:eq(4)').html(_multicastAddr);
	$('#WallParameters span:eq(5)').html(_resolution);
	var _checkInArr='';
	$('.listHeadRightCont li').each(function(e){
		if ($(this).html()!='') {
			_checkInArr += (e+1)+',';
		}
	});
	if (_checkInArr=='') {
		return;
	}
	var presetid = parseInt($('.listHeadLeftContPresetSelect').val())-1;
	var _ResolutionData;
	var _i,_j;//parameterResolution
	var _dataCommand;
	var _h,_v,_i,_j;
	_h = parseInt($('#ParametersWallSizeH').val());
	_v = parseInt($('#ParametersWallSizeV').val());

	_checkInArr = _checkInArr.substr(0,_checkInArr.length-1).split(',');
	for (var i = 0; i < _checkInArr.length; i++) {
		_ResolutionData = returnResolution(_checkInArr[i],checkParameterHasEncode);
		_ResolutionData = _ResolutionData.split(',');
		//console.log(_ResolutionData[0]+':'+_ResolutionData[1]+':'+_ResolutionData[2]+':'+_ResolutionData[3]);
		_i = parseInt((parseInt(_checkInArr[i])-1)/_v);
				_j = (parseInt(_checkInArr[i])-1)%_v;
		_dataCommand = 'join '+checkParameterHasEncode+':HDMI:0 '
					+PresetDataDetail[_i][_j]+':HDMI:0 wall size '+EncodeVideoDataArr[_num][6]+' '+EncodeVideoDataArr[_num][7]+' fps 60 offset '+
					_ResolutionData[0]+' '+_ResolutionData[1]+' keep '+_ResolutionData[2]+' '+_ResolutionData[3]+
					' viewport '+_ResolutionData[4]+' '+_ResolutionData[5]+' '+_ResolutionData[6]+' '+_ResolutionData[7];
		sendCommand('data='+_dataCommand);
		//console.log(_dataCommand);
	}
	dragType = '';
	saveToLocalParametersPreset();
}
// return Resolution
function returnResolution(num,macaddress) {
	var _resolution;
	var _arr = _data.videoswitching_encode_arr.split(',');
	var _num;
	var _viewport = new Array();
	//checkParameterHasEncode = macaddress;
	for (var i = 0; i <_arr.length-1; i++) {		
		if (_arr[i]==macaddress) {
			_num=i;				
		}
	}
	
	_parameterWidth = EncodeVideoDataArr[_num][6];
	_parameterHeight = EncodeVideoDataArr[_num][7];
	_parameterFps = EncodeVideoDataArr[_num][8];
	_viewport[0] = 0;
	_viewport[1] = 0;
	_viewport[2] = _parameterWidth;
	_viewport[3] = _parameterHeight;

	var _h,_v,_i,_j;
	var _x,_y,_width,_height;
	_h = parseInt($('#ParametersWallSizeH').val());
	_v = parseInt($('#ParametersWallSizeV').val());
	//console.log(_checkInArr);
	var _top = parseInt($('#BezelTop').val());
	var _bottom = parseInt($('#BezelBottom').val());
	var _left = parseInt($('#BezelLeft').val());
	var _right = parseInt($('#BezelRight').val());

	var _keepWidth = parseInt(_parameterWidth/_h);
	var _keepHeight = parseInt(_parameterHeight/_v);
	//_checkInArr = _checkInArr.substr(0,_checkInArr.length-1).split(',');

	// 调整像素位置
	_i = parseInt((parseInt(num)-1)/_v);
	_j = (parseInt(num)-1)%_v;
	console.log('_i:'+_i+',_j:'+_j);
	if (_i==0) {//最上边
		if (_j==0) {//最左边	
			_x = 0;
			_y = 0;		
			if (_v>_h) {
				if (_v-_h>1) {
					_viewport[0] = _parameterWidth;
					_viewport[1] = _parameterHeight;
					_viewport[2] = 0;
					_viewport[3] = 0;
					_width = _parameterWidth;
					height = _parameterHeight;
				}
				else{
					_viewport[0] = _parameterWidth/2;
					_viewport[1] = 0;
					_viewport[2] = _parameterWidth/2;
					_viewport[3] = _parameterHeight;
					_width = _parameterWidth/(2*_h)-_right;
					height = _parameterHeight/_h-_bottom;
				}
			}
			else if(_v<_h){
				if (_h-_v>1) {
					_viewport[0] = _parameterWidth;
					_viewport[1] = _parameterHeight;
					_viewport[2] = 0;
					_viewport[3] = 0;
					_width = _parameterWidth;
					height = _parameterHeight;
				}
				else{
					_viewport[0] = 0;
					_viewport[1] = _parameterHeight/2;
					_viewport[2] = _parameterWidth;
					_viewport[3] = _parameterHeight/2;
					_width = _parameterWidth/_v-_right;
					height = _parameterHeight/(2*_v)-_bottom;
				}
			}
			else {							
				_width = _keepWidth - _right;
				height = _keepHeight - _bottom;
			}
		}
		else if((_j+1)<_v){
			_x = _keepWidth*_j + _left;
			_y = 0;
			if (_v>_h) {
				if (_v-_h>1) {
					if (parseInt(_v-2-_h)>1) {
						if (_j==1) {
							_x=0;
							_width = _parameterWidth;
							height = _parameterHeight;	
							_viewport[0] = _parameterWidth;
							_viewport[1] = _parameterHeight;
							_viewport[2] = 0;
							_viewport[3] = 0;
						}
						else if(_j==2){
							_x=0;
							_width = _parameterWidth;
							height = _parameterHeight;	
							_viewport[0] = 0;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight;
						}
						else if(_j==3){
							_x=0;
							_width = _parameterWidth;
							height = _parameterHeight;	
							_viewport[0] = _parameterWidth;
							_viewport[1] = _parameterHeight;
							_viewport[2] = 0;
							_viewport[3] = 0;					
						}
						// _width = _parameterWidth/(2*_h)-_right;
											
					}
					else if(parseInt(_v-2-_h)==1){
						if (_j==1) {
							_x=0;
							_width = parseInt(_parameterWidth/(2*_h))-_right;
							_viewport[0] = _parameterWidth/2;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth/2;
							_viewport[3] = _parameterHeight;
						}
						else if(_j==2){
							_x = parseInt((_j-1)*(_parameterWidth/(2*_h)))+_left;
							if ((_v-_j-1)==1) {
								_width = parseInt(_parameterWidth/(2*_h))-_right;
								_viewport[0] = _parameterWidth/2;
								_viewport[1] = 0;
								_viewport[2] = _parameterWidth/2;
								_viewport[3] = _parameterHeight;
							}
							else {
								_width = parseInt(_parameterWidth/_h)-_left-_right;
							}						
						}
						else if(_j==3){
							_x = parseInt(_j*_parameterWidth/(2*_h))+_left;
							_width = parseInt(_parameterWidth/(2*_h))-_left;	
							_viewport[0] = _parameterWidth/2;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth/2;
							_viewport[3] = _parameterHeight;						
						}
						// _width = _parameterWidth/(2*_h)-_right;
						height = parseInt(_parameterHeight/_h)-_top;
					}
					else if(parseInt(_v-2-_h)<=0){
						if (_j==1) {
							_x=0;
							_width = _parameterWidth/_h-_right;							
						}
						else if(_j==2){
							_x = (_j-1)*(_parameterWidth/_h)+_left;
							if ((_v-_j-1)==1) {
								_width = _parameterWidth/_h-_left;
							}
							else {
								_width = _parameterWidth/_h-_left-_right;
							}						
						}
						else if(_j==3){
							_x = 2*_parameterWidth/_h+_left;
							_width = _parameterWidth/_h-_left;							
						}
						height = _parameterHeight/_h-_bottom;
					}
				}
				else{
					_x = _parameterWidth/(2*_h)+_right+(_j-1)*(_parameterWidth/_h);
					_width = _parameterWidth/_h-_left-_right;
					height = _parameterHeight/_h-_bottom;
				}
			}
			else if(_v<_h){
				if (_h-_v>1) {
					if (parseInt(_h-2-_v)>1) {
						if (_j==1) {
							_x=0;
							_width = _parameterWidth;
							height = _parameterHeight;	
							_viewport[0] = _parameterWidth;
							_viewport[1] = _parameterHeight;
							_viewport[2] = 0;
							_viewport[3] = 0;
						}
						else if(_j==2){
							_x=0;
							_width = _parameterWidth;
							height = _parameterHeight;	
							_viewport[0] = 0;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight;
						}
						else if(_j==3){
							_x=0;
							_width = _parameterWidth;
							height = _parameterHeight;	
							_viewport[0] = _parameterWidth;
							_viewport[1] = _parameterHeight;
							_viewport[2] = 0;
							_viewport[3] = 0;					
						}						
					}
					else if(parseInt(_h-2-_v)==1){
						if (_j==1) {
							_x=0;
							_x=0;
							_width = _parameterWidth;
							height = _parameterHeight;	
							_viewport[0] = _parameterWidth;
							_viewport[1] = _parameterHeight;
							_viewport[2] = 0;
							_viewport[3] = 0;
						}
						else if(_j==2){
							_x=0;
							_width = _parameterWidth;
							height = _parameterHeight;	
							_viewport[0] = _parameterWidth;
							_viewport[1] = _parameterHeight;
							_viewport[2] = 0;
							_viewport[3] = 0;						
						}
						else if(_j==3){
							_x=0;
							_width = _parameterWidth;
							height = _parameterHeight;	
							_viewport[0] = _parameterWidth;
							_viewport[1] = _parameterHeight;
							_viewport[2] = 0;
							_viewport[3] = 0;					
						}
						// _width = _parameterWidth/(2*_h)-_right;
						// height = parseInt(_parameterHeight/_h)-_top;
					}
					else if(parseInt(_h-2-_v)<=0){						
						_x=0;
						_width = _parameterWidth;
						height = _parameterHeight;	
						_viewport[0] = _parameterWidth;
						_viewport[1] = _parameterHeight;
						_viewport[2] = 0;
						_viewport[3] = 0;
					}
				}
				else{
					_x = _parameterWidth/_v+_right+(_j-1)*(_parameterWidth/_v);
					_width = _parameterWidth/_v-_left-_right;
					height = _parameterHeight/(2*_v)-_bottom;
					_viewport[0] = 0;
					_viewport[1] = _parameterHeight/2;
					_viewport[2] = _parameterWidth;
					_viewport[3] = _parameterHeight/2;
				}
			}
			else {				
				_width = _keepWidth - _right - _left;
				height = _keepHeight - _bottom;
			}
		}
		else{//最右边
			_x = _keepWidth*_j + _left;
			_y = 0;
			if (_v>_h) {				
				if (_v-_h>1) {
					_x = 0;
					_viewport[0] = _parameterWidth;
					_viewport[1] = _parameterHeight;
					_viewport[2] = 0;
					_viewport[3] = 0;
					_width = _parameterWidth;
					height = _parameterHeight;
				}
				else{
					_x = _parameterWidth/(2*_h)+_right+(_j-1)*(_parameterWidth/_h);
					_viewport[0] = 0;
					_viewport[1] = 0;
					_viewport[2] = _parameterWidth/2;
					_viewport[3] = _parameterHeight;
					_width = _parameterWidth/(2*_h)-_left;
					height = _parameterHeight/_h-_bottom;
				}
			}
			else if(_v<_h){
				_x = _parameterWidth/_v+_right+(_j-1)*(_parameterWidth/_v);
				if (_h-_v>1) {
					_x=0;
					_width = _parameterWidth;
					height = _parameterHeight;	
					_viewport[0] = _parameterWidth;
					_viewport[1] = _parameterHeight;
					_viewport[2] = 0;
					_viewport[3] = 0;
				}
				else{
					_viewport[0] = 0;
					_viewport[1] = _parameterHeight/2;
					_viewport[2] = _parameterWidth;
					_viewport[3] = _parameterHeight/2;
					_width = _parameterWidth/_v-_right;
					height = _parameterHeight/(2*_v)-_bottom;
				}
			}
			else {				
				_width = _keepWidth - _left;
				height = _keepHeight - _bottom;
			}			
		}
	}
	else if((_i+1)<_h){//中间
		if (_j==0) {//最左边
			_x = 0;
			_y = _keepHeight*_i + _top;
			if (_v>_h) {
				if (_v-_h>1) {
					_x = 0;
					_y = 0;	
					_viewport[0] = _parameterWidth;
					_viewport[1] = _parameterHeight;
					_viewport[2] = 0;
					_viewport[3] = 0;
					_width = _parameterWidth;
					height = _parameterHeight;
				}
				else{
					_y = parseInt(_parameterHeight/_h) + _top;
					_viewport[0] = _parameterWidth/2;
					_viewport[1] = 0;
					_viewport[2] = _parameterWidth/2;
					_viewport[3] = _parameterHeight;
					_width = _parameterWidth/(2*_h)-_right;
					height = _parameterHeight/_h-_top-_bottom;
				}
			}
			else if(_v<_h){
				if (_h-_v>1) {
					_x = 0;					
					if (_h-_v==2) {
						if (_i==1) {
							_y = 0;
							_viewport[0] = 0;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight;
							_width = _parameterWidth/_v-_right;
							height = _parameterHeight/_v-_bottom;
						}
						else if(_i==2){
							_y = (_i-1)*(_parameterHeight/_v)+_top;
							_viewport[0] = 0;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight;
							_width = _parameterWidth/_v-_right;
							height = _parameterHeight/_v-_bottom-_top;
						}
						else if(_i==3){
							_y = (_i-1)*(_parameterHeight/_v)+_top;
							_viewport[0] = 0;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight;
							_width = _parameterWidth/_v-_right;
							height = _parameterHeight/_v-_bottom;
						}						
					}
					else if(_h-_v==3){
						if (_i==1) {
							_y = 0;
							_viewport[0] = 0;
							_viewport[1] = _parameterHeight/2;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight/2;
							_width = _parameterWidth/_v-_right;
							height = _parameterHeight/(2*_v)-_bottom;
						}
						else if (_i==2) {
							_y = parseInt(_parameterHeight/(2*_v))+_top;							
							_width = _parameterWidth/_v-_right;
							if (_v==1) {
								_viewport[0] = 0;
								_viewport[1] = _parameterHeight/2;
								_viewport[2] = _parameterWidth;
								_viewport[3] = _parameterHeight/2;
								height = _parameterHeight/(2*_v)-_top;
							}
							else {
								_viewport[0] = 0;
								_viewport[1] = 0;
								_viewport[2] = _parameterWidth;
								_viewport[3] = _parameterHeight;
								height = _parameterHeight/_v-_bottom-_top;
							}
							
						}
						else if (_i==3) {
							_y = parseInt(_parameterHeight/(2*_v))+_top+(_parameterHeight/_v);
							_viewport[0] = 0;
							_viewport[1] = _parameterHeight/2;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight/2;
							_width = _parameterWidth/_v-_right;
							height = _parameterHeight/(2*_v)-_top;
						}
					}
					else if(_h-_v==4){
						if (_i==1) {
							_y = 0;
							_viewport[0] = _parameterWidth;
							_viewport[1] = _parameterHeight;
							_viewport[2] = 0;
							_viewport[3] = 0;
							_width = _parameterWidth;
							height = _parameterHeight;
						}
						else if (_i==2) {
							_y = 0;
							_viewport[0] = 0;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight;
							_width = _parameterWidth;
							height = _parameterHeight;
						}
						else if (_i==3) {
							_y = 0;
							_viewport[0] = _parameterWidth;
							_viewport[1] = _parameterHeight;
							_viewport[2] = 0;
							_viewport[3] = 0;
							_width = _parameterWidth;
							height = _parameterHeight;
						}
					}
				}
				else{
					_y = parseInt(_parameterHeight/(2*_v))+_top+(_i-1)*(_parameterHeight/_v);
					_viewport[0] = 0;
					_viewport[1] = 0;
					_viewport[2] = _parameterWidth;
					_viewport[3] = _parameterHeight;
					_width = _parameterWidth/_v-_right;
					height = _parameterHeight/_v-_top-_bottom;
				}
			}
			else {
				_width = _keepWidth - _right;
				height = _keepHeight - _bottom - _top;
			}
		}
		else if((_j+1)<_v){				
			_x = _keepWidth*_j + _left;
			_y = _keepHeight*_i + _top;		
			if (_v>_h) {	
				_x = _parameterWidth/(2*_h)+parseInt(_parameterWidth/_h)*(_j-1) + _left;
				_y = parseInt(_parameterHeight/_h)*_i + _top;			
				if (_v-_h>1) {					
					if (parseInt(_v-2-_h)>1) {
						if (_j==1) {
							_x=0;
							_width = _parameterWidth;
							height = _parameterHeight;	
							_viewport[0] = _parameterWidth;
							_viewport[1] = _parameterHeight;
							_viewport[2] = 0;
							_viewport[3] = 0;
						}
						else if(_j==2){
							_x=0;
							_width = _parameterWidth;
							height = _parameterHeight;	
							_viewport[0] = 0;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight;
						}
						else if(_j==3){
							_x=0;
							_width = _parameterWidth;
							height = _parameterHeight;	
							_viewport[0] = _parameterWidth;
							_viewport[1] = _parameterHeight;
							_viewport[2] = 0;
							_viewport[3] = 0;					
						}
						// _width = _parameterWidth/(2*_h)-_right;
											
					}
					else if(parseInt(_v-2-_h)==1){
						if (_j==1) {
							_x=0;
							_width = parseInt(_parameterWidth/(2*_h))-_right;
							_viewport[0] = _parameterWidth/2;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth/2;
							_viewport[3] = _parameterHeight;
						}
						else if(_j==2){
							_x = parseInt((_j-1)*(_parameterWidth/(2*_h)))+_left;
							if ((_v-_j-1)==1) {
								_width = parseInt(_parameterWidth/(2*_h))-_right;
								_viewport[0] = _parameterWidth/2;
								_viewport[1] = 0;
								_viewport[2] = _parameterWidth/2;
								_viewport[3] = _parameterHeight;
							}
							else {
								_width = parseInt(_parameterWidth/_h)-_left-_right;
							}						
						}
						else if(_j==3){
							_x = parseInt(_j*_parameterWidth/(2*_h))+_left;
							_width = parseInt(_parameterWidth/(2*_h))-_left;	
							_viewport[0] = _parameterWidth/2;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth/2;
							_viewport[3] = _parameterHeight;						
						}
						// _width = _parameterWidth/(2*_h)-_right;
						// height = parseInt(_parameterHeight/_h)-_top;
					}
					else if(parseInt(_v-2-_h)<=0){
						if (_j==1) {
							_x=0;
							_width = _parameterWidth/_h-_right;							
						}
						else if(_j==2){
							_x = (_j-1)*(_parameterWidth/_h)+_left;
							if ((_v-_j-1)==1) {
								_width = _parameterWidth/_h-_left;
							}
							else {
								_width = _parameterWidth/_h-_left-_right;
							}						
						}
						else if(_j==3){
							_x = 2*_parameterWidth/_h+_left;
							_width = _parameterWidth/_h-_left;							
						}
						// height = _parameterHeight/_h-_bottom-_top;
					}
				}
				else{
					_width = _parameterWidth/_h-_left-_right;
					// height = _parameterHeight/_h-_top-_bottom;
				}
				height = _parameterHeight/_h-_bottom-_top;
			}
			else if(_v<_h){
				if (_h-_v>1) {
					if (parseInt(_h-2-_v)>1) {
						if (_j==1) {
							_x=0;
							_width = _parameterWidth;
							height = _parameterHeight;	
							_viewport[0] = _parameterWidth;
							_viewport[1] = _parameterHeight;
							_viewport[2] = 0;
							_viewport[3] = 0;
						}
						else if(_j==2){
							_x=0;
							_width = _parameterWidth;
							height = _parameterHeight;	
							_viewport[0] = 0;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight;
						}
						else if(_j==3){
							_x=0;
							_width = _parameterWidth;
							height = _parameterHeight;	
							_viewport[0] = _parameterWidth;
							_viewport[1] = _parameterHeight;
							_viewport[2] = 0;
							_viewport[3] = 0;					
						}						
					}
					else if(parseInt(_h-2-_v)==1){
						if (_i==1) {
							_x=0;
							_width = parseInt(_parameterWidth/(2*_v))-_right;
							_viewport[0] = 0;
							_viewport[1] = _parameterHeight/2;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight/2;
						}
						else if(_i==2){
							_x = parseInt((_j-1)*(_parameterWidth/(2*_h)))+_left;
							if ((_v-_j-1)==1) {
								_width = parseInt(_parameterWidth/(2*_h))-_right;
								_viewport[0] = _parameterWidth/2;
								_viewport[1] = 0;
								_viewport[2] = _parameterWidth/2;
								_viewport[3] = _parameterHeight;
							}
							else {
								_width = parseInt(_parameterWidth/_h)-_left-_right;
							}						
						}
						else if(_i==3){
							_x = parseInt(_j*_parameterWidth/(2*_h))+_left;
							_width = parseInt(_parameterWidth/(2*_h))-_left;	
							_viewport[0] = _parameterWidth/2;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth/2;
							_viewport[3] = _parameterHeight;						
						}
						// _width = _parameterWidth/(2*_h)-_right;
						height = parseInt(_parameterHeight/_h)-_top;
					}
					else if(parseInt(_h-2-_v)<=0){
						if (_i==1) {
							_x = _parameterWidth/_v+_right+(_j-1)*(_parameterWidth/_v);
							_y = 0;
							_viewport[0] = 0;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight;
							_width = _parameterWidth/_v-_right-_left;
							height = _parameterHeight/_v-_bottom;
						}
						else if(_i==2){
							_x = _parameterWidth/_v+_right+(_j-1)*(_parameterWidth/_v);
							_y = (_i-1)*(_parameterHeight/_v)+_top;
							_viewport[0] = 0;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight;
							_width = _parameterWidth/_v-_right-_left;
							height = _parameterHeight/_v-_bottom-_top;
						}
						else if(_i==3){
							_x = _parameterWidth/_v+_right+(_j-1)*(_parameterWidth/_v);
							_y = (_i-1)*(_parameterHeight/_v)+_top;
							_viewport[0] = 0;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight;
							_width = _parameterWidth/_v-_right-_left;
							height = _parameterHeight/_v-_top;
						}						
					}
				}
				else{
					_x = _parameterWidth/_v+_right+(_j-1)*(_parameterWidth/_v);
					_y = parseInt(_parameterHeight/(2*_v))+_top+(_i-1)*(_parameterHeight/_v);
					_width = _parameterWidth/_v-_left-_right;
					height = _parameterHeight/_v-_bottom-_top;
					_viewport[0] = 0;
					_viewport[1] = 0;
					_viewport[2] = _parameterWidth;
					_viewport[3] = _parameterHeight;
				}
			}
			else {				
				_width = _keepWidth - _right - _left;
				height = _keepHeight - _bottom - _top;
			}
		}
		else{//最右边
			_x = _keepWidth*_j + _left;
			_y = _keepHeight*_i + _top;
			if (_v>_h) {
				if (_v-_h>1) {
					_x = 0;
					_y = 0;	
					_viewport[0] = _parameterWidth;
					_viewport[1] = _parameterHeight;
					_viewport[2] = 0;
					_viewport[3] = 0;
					_width = _parameterWidth;
					height = _parameterHeight;
				}
				else{
					_x = _parameterWidth/(2*_h)+parseInt(_parameterWidth/_h)*(_j-1) + _left;
					_y = parseInt(_parameterHeight/_h)*_i + _top;
					_viewport[0] = 0;
					_viewport[1] = 0;
					_viewport[2] = _parameterWidth/2;
					_viewport[3] = _parameterHeight;
					_width = _parameterWidth/(2*_h)-_right;
					height = _parameterHeight/_h-_top-_bottom;
				}
			}
			else if(_v<_h){
				_x = _parameterWidth/_v+_right+(_j-1)*(_parameterWidth/_v);				
				_y = parseInt(_parameterHeight/(2*_v))+_top+(_i-1)*(_parameterHeight/_v);
				if (_h-_v>1) {
					if (_h-_v==2) {
						if (_i==1) {
							_y = 0;
							_viewport[0] = 0;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight;
							_width = _parameterWidth/_v-_left;
							height = _parameterHeight/_v-_bottom;
						}
						else if(_i==2){
							_y = (_i-1)*(_parameterHeight/_v)+_top;
							_viewport[0] = 0;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight;
							_width = _parameterWidth/_v-_left;
							height = _parameterHeight/_v-_bottom-_top;
						}
						else if(_i==3){
							_y = (_i-1)*(_parameterHeight/_v)+_top;
							_viewport[0] = 0;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight;
							_width = _parameterWidth/_v-_left;
							height = _parameterHeight/_v-_bottom;
						}						
					}
					else if(_h-_v==3){
						if (_i==1) {
							_y = 0;
							_viewport[0] = 0;
							_viewport[1] = _parameterHeight/2;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight/2;
							_width = _parameterWidth/_v-_left;
							height = _parameterHeight/(2*_v)-_bottom;
						}
						else if (_i==2) {
							_y = parseInt(_parameterHeight/(2*_v))+_top;							
							_width = _parameterWidth/_v-_left;
							if (_v==1) {
								_viewport[0] = 0;
								_viewport[1] = _parameterHeight/2;
								_viewport[2] = _parameterWidth;
								_viewport[3] = _parameterHeight/2;
								height = _parameterHeight/(2*_v)-_top;
							}
							else {
								_viewport[0] = 0;
								_viewport[1] = 0;
								_viewport[2] = _parameterWidth;
								_viewport[3] = _parameterHeight;
								height = _parameterHeight/_v-_bottom-_top;
							}
							
						}
						else if (_i==3) {
							_y = parseInt(_parameterHeight/(2*_v))+_top+(_parameterHeight/_v);
							_viewport[0] = 0;
							_viewport[1] = _parameterHeight/2;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight/2;
							_width = _parameterWidth/_v-_left;
							height = _parameterHeight/(2*_v)-_top;
						}
					}
					else if(_h-_v==4){
						if (_i==1) {
							_y = 0;
							_viewport[0] = _parameterWidth;
							_viewport[1] = _parameterHeight;
							_viewport[2] = 0;
							_viewport[3] = 0;
							_width = _parameterWidth;
							height = _parameterHeight;
						}
						else if (_i==2) {
							_y = 0;
							_viewport[0] = 0;
							_viewport[1] = 0;
							_viewport[2] = _parameterWidth;
							_viewport[3] = _parameterHeight;
							_width = _parameterWidth;
							height = _parameterHeight;
						}
						else if (_i==3) {
							_y = 0;
							_viewport[0] = _parameterWidth;
							_viewport[1] = _parameterHeight;
							_viewport[2] = 0;
							_viewport[3] = 0;
							_width = _parameterWidth;
							height = _parameterHeight;
						}
					}
				}
				else{
					_viewport[0] = 0;
					_viewport[1] = 0;
					_viewport[2] = _parameterWidth;
					_viewport[3] = _parameterHeight;
					_width = _parameterWidth/_v-_left;
					height = _parameterHeight/_v-_top-_bottom;
				}
			}
			else {				
				_width = _keepWidth - _left;
				height = _keepHeight - _bottom - _top;
			}			
		}
	}
	else{//最下边
		if (_j==0) {//最左边
			_x = 0;
			_y = _keepHeight*_i + _top;
			if (_v>_h) {				
				if (_v-_h>1) {
					_x = 0;
					_y = 0;	
					_viewport[0] = _parameterWidth;
					_viewport[1] = _parameterHeight;
					_viewport[2] = 0;
					_viewport[3] = 0;
					_width = _parameterWidth;
					height = _parameterHeight;
				}
				else{
					_y = _parameterHeight/_h+_bottom+(_h-2)*(_parameterHeight/_h);
					_viewport[0] = _parameterWidth/2;
					_viewport[1] = 0;
					_viewport[2] = _parameterWidth/2;
					_viewport[3] = _parameterHeight;
					_width = _parameterWidth/(2*_h)-_right;
					height = _parameterHeight/_h-_top;
				}
			}
			else if(_v<_h){
				if (_h-_v>1) {
					_x = 0;
					_y = 0;	
					_viewport[0] = _parameterWidth;
					_viewport[1] = _parameterHeight;
					_viewport[2] = 0;
					_viewport[3] = 0;
					_width = _parameterWidth;
					height = _parameterHeight;
				}
				else{
					_y = parseInt(_parameterHeight/(2*_v))+_top+(_i-1)*(_parameterHeight/_v);
					_viewport[0] = 0;
					_viewport[1] = _parameterHeight/2;
					_viewport[2] = _parameterWidth;
					_viewport[3] = _parameterHeight/2;
					_width = _parameterWidth/_v-_right;
					height = parseInt(_parameterHeight/(2*_v))-_bottom;
				}
			}
			else {				
				_width = _keepWidth - _right;
				height = _keepHeight - _top;
			}
				
		}
		else if((_j+1)<_v){
			_x = _keepWidth*_j + _left;
			_y = _keepHeight*_i + _top;
			if (_v>_h) {
				_x = _parameterWidth/(2*_h)+parseInt(_parameterWidth/_h)*(_j-1) + _left;
				_y = parseInt(_parameterHeight/_h)*_i + _top;
				if (_v-_h>1) {
					if (parseInt(_v-2-_h)>1) {
						_width = _parameterWidth;
						height = _parameterHeight;
					}
					else if(parseInt(_v-2-_h)==1){

						_width = _parameterWidth/(2*_h)-_right;
						height = _parameterHeight/_h;
					}
					else if(parseInt(_v-2-_h)<=0){
						if (_j==1) {
							_x=0;
							_width = _parameterWidth/_h-_right;							
						}
						else if(_j==2){
							_x = (_j-1)*(_parameterWidth/_h)+_left;
							if ((_v-_j-1)==1) {
								_width = _parameterWidth/_h-_left;
							}
							else {
								_width = _parameterWidth/_h-_left-_right;
							}						
						}
						else if(_j==3){
							_width = _parameterWidth/_h-_left;							
						}
						height = _parameterHeight/_h-_bottom;
					}					
				}
				else{
					_width = _parameterWidth/_h-_left-_right;
					height = _parameterHeight/_h-_top;
				}
			}
			else if(_v<_h){
				_x = parseInt(_parameterHeight/_v)*_j + _left;
				_y = _parameterWidth/(2*_v)+parseInt(_parameterWidth/_v)*(_i-1) + _top;
				if (_v-_h>1) {
					_x = 0;
					_y = 0;
					_viewport[0] = _parameterWidth;
					_viewport[1] = _parameterHeight;
					_viewport[2] = 0;
					_viewport[3] = 0;
					_width = _parameterWidth;
					height = _parameterHeight;				
				}
				else{
					_x = _parameterWidth/_v+_right+(_j-1)*(_parameterWidth/_v);
					_y = parseInt(_parameterHeight/(2*_v))+_top+(_i-1)*(_parameterHeight/_v);
					_width = _parameterWidth/_v-_left-_right;
					height = _parameterHeight/(2*_v)-_top;
					_viewport[0] = 0;
					_viewport[1] = 0;
					_viewport[2] = _parameterWidth;
					_viewport[3] = _parameterHeight/2;					
				}
			}
			else {				
				_width = _keepWidth -_right - _left;
				height = _keepHeight - _top;
			}			
		}
		else{//最右边
			_x = _keepWidth*_j + _left;
			_y = _keepHeight*_i + _top;
			if (_v>_h) {
				if (_v-_h>1) {
					_x = 0;
					_y = 0;					
					_viewport[0] = _parameterWidth;
					_viewport[1] = _parameterHeight;
					_viewport[2] = 0;
					_viewport[3] = 0;
					_width = _parameterWidth;
					height = _parameterHeight;
				}
				else{
					_x = _parameterWidth/(2*_h)+parseInt(_parameterWidth/_h)*(_j-1) + _left;
					_y = parseInt(_parameterHeight/_h)*_i + _top;
					_viewport[0] = 0;
					_viewport[1] = 0;
					_viewport[2] = _parameterWidth/2;
					_viewport[3] = _parameterHeight;
					_width = _parameterWidth/(2*_h)-_right;
					height = _parameterHeight/_h-_top;
				}
			}
			else if(_v<_h){
				_x = _parameterWidth/_v+_right+(_j-1)*(_parameterWidth/_v);
				_y = parseInt(_parameterHeight/(2*_v))+_top+(_i-1)*(_parameterHeight/_v);
				if (_h-_v>1) {
					_x = 0;
					_y = 0;
					_viewport[0] = _parameterWidth;
					_viewport[1] = _parameterHeight;
					_viewport[2] = 0;
					_viewport[3] = 0;
					_width = _parameterWidth;
					height = _parameterHeight;
				}
				else{
					_viewport[0] = 0;
					_viewport[1] = 0;
					_viewport[2] = _parameterWidth;
					_viewport[3] = _parameterHeight/2;
					_width = _parameterWidth/_v-_left;
					height = _parameterHeight/(2*_v)-_top;
				}
			}
			else {				
				_width = _keepWidth - _left;
				height = _keepHeight - _top;
			}	
				
		}
	}
	// 像素的最终必须是2的倍数
	if ((_x%2)!=0) {
		_x += 1;
	}
	else if ((_y%2)!=0) {
		_y += 1;
	}
	else if ((_width%2)!=0) { 
		_width += 1;
	}
	else if ((height%2)!=0) {
		height -= 1;
	}
	return _x+','+_y+','+_width+','+height+','+_viewport[0]+','+_viewport[1]+','+_viewport[2]+','+_viewport[3];
}
// 当每次调整视频墙位置时，均触发保存为当前场景状态
function saveToLocalParametersPreset(){
	var devicelistNum = 0;
	var presetTx='';

	if ($('#WallParameters span:eq(4)').html()!='&nbsp;'&&$('#WallParameters span:eq(5)').html()!='&nbsp;') {
		presetTx = "'encodeMac':'"+_txDeviceMAC+"','multicast_address':'"+$('#WallParameters span:eq(4)').html()
					+"','resolution':'"+$('#WallParameters span:eq(5)').html()+"'";
	}
	else {
		presetTx = "'encodeMac':'','multicast_address':'','resolution':''";
	}
	var dataTmp = "{"+presetTx+",'value':'"+$('#ParametersWallSizeH').val()+','
								+$('#ParametersWallSizeV').val()+','
								+$('#BezelTop').val()+','
								+$('#BezelBottom').val()+','
								+$('#BezelLeft').val()+','
								+$('#BezelRight').val()+"',"
					+"'device':[";
	var deviceNum = parseInt($('#ParametersWallSizeH').val())*parseInt($('#ParametersWallSizeV').val());
	for (var i = 0; i < deviceNum; i++) {
		(function(ii){
			if ($('#walldecodedrag'+(ii+1)).html()!='') {
				dataTmp+="{'index':"+(ii+1)+','
							+"'hostname':'"+$('#walldecodedrag'+(ii+1)+' span:eq(0)').html()+"',"
							+"'macaddress':'"+$('#walldecodedrag'+(ii+1)+' span:eq(1)').html()+"',"
							+"'ipaddress':'"+$('#walldecodedrag'+(ii+1)+' span:eq(2)').html()+"',"
							+"'version':'"+$('#walldecodedrag'+(ii+1)+' span:eq(3)').html()+"',"
							+"'mode':'"+$('#walldecodedrag'+(ii+1)+' span:eq(4)').html()+"',"
							+"'multicastaddress':'"+$('#walldecodedrag'+(ii+1)+' span:eq(5)').html()+"',"
							+"'status':'"+$('#walldecodedrag'+(ii+1)+' span:eq(6)').html()+"',"
							+"'resolution':'"+$('#walldecodedrag'+(ii+1)+' span:eq(7)').html()+"',"
							+"'colorspace':'"+$('#walldecodedrag'+(ii+1)+' span:eq(8)').html()+"'},"
				devicelistNum++;
			}

		})(i);
	}
	if (devicelistNum!=0) {
		dataTmp = dataTmp.substr(0,dataTmp.length-1);
	}
	dataTmp+=']}';
	ReadDataFromFile('firstsaveToFile=1&savedata='+dataTmp);
}