LoadToResetArgument();
// -----------------------------------------------------
$('.CommonListNav li:eq(7)').on('mouseover',function(){
	if ($('.CommonListNav li:eq(7) span').html()=='Remove from Group') {
		return;
	}
	
	$('.GroupsList').css('top','126px');
	if ((parseInt($('#CommonList').css('top'))+parseInt($('.GroupsList').css('top'))+$('.GroupsList').height())>$(window).height()) {
		var _pageY = -(parseInt($('#CommonList').css('top'))+$('.GroupsList').height()-$(window).height()+10);
		$('.GroupsList').css('top',_pageY+'px');
	}
	if ($('.GroupsList li').length>0) {
		$('.GroupsList').show();
	}
});
$('.CommonListNav li:eq(7)').on('mouseleave',function(){
	if ($('.CommonListNav li:eq(7) span').html()=='Remove from Group') {
		return;
	}
	$('.GroupsList').hide(); 
	
});
$('.GroupsList').on('mouseover',function(){
	
	$('.CommonListNav li:eq(7)').addClass('listHover');
	$(this).show();
});
$('.GroupsList').on('mouseleave',function(){
	$(this).hide();
	$('.CommonListNav li:eq(7)').removeClass('listHover');
});
$('.CommonListNav li:eq(7)').on('click',function(){
	if (!$('.CommonListNav li:eq(7) span').html()=='Remove from Group') {
		return;
	}
	var groupID;	
	$('#encodersGroup li').each(function(e){
		if ($(this).hasClass('listHover')) {
			groupID = parseInt($('#encodersGroup li:eq('+e+') span').html());
		}
	});
	var grouplist = EncodeGroupDataListArr;
	var  newgrouplist='';
	var _num;
	$('.listCont:eq(1) .listContList li').each(function(e){
		if ($(this).hasClass('listContListLiHover')) {
			_num = e;
		}
	});
	var GroupDataArr = '';
	grouplist[_num]='';    
    for (var i = 0; i < grouplist.length; i++) {//重新拼合数据         
        GroupDataArr+= grouplist[i]+',';
    }
    GroupDataArr = GroupDataArr.substr(0,GroupDataArr.length-1);
    var data = 'type=9&id='+groupID+'&deviceMAC='+GroupDataArr+'&devicetype=tx';
	//console.log(data);
	PageAddGroup(data);
	$('.listCont:eq(1) li').each(function(e){
		if ($(this).hasClass('listContListLiHover')) {
			$(this).remove();
		}
	});
	if (document.getElementById('CommonList')) {
		document.getElementById('CommonList').parentNode.removeChild(document.getElementById('CommonList'));
		$('.listContList:eq(0) li').removeClass('listContListLiHover');
		$('.listContList:eq(1) li').removeClass('listContListLiHover');
	}
});
$('#ResetFactory').on('click',function(){	
	if (document.getElementById('CommonList')) {
		document.getElementById('CommonList').parentNode.removeChild(document.getElementById('CommonList'));
		$('.listContList:eq(0) li').removeClass('listContListLiHover');
		$('.listContList:eq(1) li').removeClass('listContListLiHover');
	}
	if ($('.connectionToServerBtn div').html()=='Connect') {
	 	return;
	}	
	if ($('#encodersGroup li:eq(0)').hasClass('listHover')) {
		var _arr = _data.videoswitching_encode_arr.split(',');
		var _id = _arr[deviceSelectID];	
	}
	else {
		var _arr = EncodeGroupDataListArr[deviceSelectID].split(':');
		var _id = _arr[0];
	}
	sendCommand('data=factory '+_id);//Get ALL settings //event 0
	//sendCommand('data=1');
	// console.log(ajaxCommandAck);
});

// Group 增加成员事件
$('.GroupsListNav li').each(function(e){
    $(this).on('click',function(){
        var data = 'type=6&id='+$('#encodersGroup li:eq('+(e+1)+') span').html();
        //console.log(data);
        PageAddGroup(data);
        setTimeout(function(){
        	data = ajaxDataStream;
	        EncodeGroupDataListArr = data.split(',');
	        if (data!='null') {
	            var _arr = _data.videoswitching_encode_arr.split(',');
	            var _id = _arr[deviceSelectID];
	            
	            for (var i = 0; i < EncodeGroupDataListArr.length; i++) {
	            	var _EncodeGroupDataListArr = EncodeGroupDataListArr[i].split(':');
	                if (_id == _EncodeGroupDataListArr[0]) {
	                    alert('Device existed!');
	                    return;
	                }
	            }
	        }

	        var _id = $('.GroupsListNav li:eq('+e+') span').html();
	        var _dataTmp = 'type=9&devicetype=tx&id='+_id;
			var _temp='',_deviceIDNum;
			$('.listContList:eq(1) li').each(function(a){
				if($(this).hasClass('listContListLiHover')){				
					_deviceIDNum=a;
				}
			});	
			if (data=='null') {
				_temp = $('.listContList:eq(1) li:eq('+_deviceIDNum+') span:eq(1)').html()+':N';		
			}
			else{
				var _checkType=0;
				for (var i = 0; i < EncodeGroupDataListArr.length; i++) {
					if (EncodeGroupDataListArr[i] == '') {
						EncodeGroupDataListArr[i] = $('.listContList:eq(1) li:eq('+_deviceIDNum+') span:eq(1)').html()+':N';
						_checkType=1;
						break;
					}
				}
				if (_checkType==0) {
					EncodeGroupDataListArr[EncodeGroupDataListArr.length] = $('.listContList:eq(1) li:eq('+_deviceIDNum+') span:eq(1)').html()+':N';
				}
				for (var i = 0; i < EncodeGroupDataListArr.length; i++) {
					_temp += EncodeGroupDataListArr[i] + ',';
				}
				_temp = _temp.substr(0,_temp.length-1);
			}		
			PageAddGroup(_dataTmp+'&deviceMAC='+_temp);
	        if (document.getElementById('CommonList')) {
	            document.getElementById('CommonList').parentNode.removeChild(document.getElementById('CommonList'));
	            $('.listContList:eq(0) li').removeClass('listContListLiHover');
	            $('.listContList:eq(1) li').removeClass('listContListLiHover');
	        }
	        if (document.getElementById('GroupSettingBox')) {
	            document.getElementById('GroupSettingBox').parentNode.removeChild(document.getElementById('GroupSettingBox'));
	            //$('.listContList:eq(0) li').removeClass('listContListLiHover');
	        }
        },100);
	        
    });
});
// click 点击发送命令事件 ========================================================================
$('#CommonListSetting').on('click',function(){
	if (document.getElementById('CommonList')) {
		document.getElementById('CommonList').parentNode.removeChild(document.getElementById('CommonList'));
		// $('.listContList:eq(0) li').removeClass('listContListLiHover');
		// $('.listContList:eq(1) li').removeClass('listContListLiHover');
	}
	listPage("template/video_switching/listSettingBox.php","decoders=5");

	setTimeout(function(){
		$('.cont_commom').append(ajaxDataStream);
	},100);
});
// Send/Receive RS232
$('#SendReceiveRS232').on('click',function(){
	if (document.getElementById('CommonList')) {
		document.getElementById('CommonList').parentNode.removeChild(document.getElementById('CommonList'));
		// $('.listContList:eq(0) li').removeClass('listContListLiHover');
		// $('.listContList:eq(1) li').removeClass('listContListLiHover');
	}
	listPage("template/video_switching/listSettingBox.php","decoders=6");

	setTimeout(function(){
		$('.cont_commom').append(ajaxDataStream);
	},100);
});
// Send/Receive IR
$('#SendReceiveIR').on('click',function(){
	if (document.getElementById('CommonList')) {
		document.getElementById('CommonList').parentNode.removeChild(document.getElementById('CommonList'));
		// $('.listContList:eq(0) li').removeClass('listContListLiHover');
		// $('.listContList:eq(1) li').removeClass('listContListLiHover');
	}
	listPage("template/video_switching/listSettingBox.php","decoders=7");

	setTimeout(function(){
		$('.cont_commom').append(ajaxDataStream);
	},100);
});
// Reboot
$('#Reboot').on('click',function(){
	if (document.getElementById('CommonList')) {
		document.getElementById('CommonList').parentNode.removeChild(document.getElementById('CommonList'));
		// $('.listContList:eq(0) li').removeClass('listContListLiHover');
		// $('.listContList:eq(1) li').removeClass('listContListLiHover');
	}
	listPage("template/video_switching/listSettingBox.php","decoders=8");

	setTimeout(function(){
		$('.cont_commom').append(ajaxDataStream);
	},100);
});

$('#StartAnalogAudio').on('click',function(){
    if (document.getElementById('CommonList')) {
        document.getElementById('CommonList').parentNode.removeChild(document.getElementById('CommonList'));
        $('.listContList:eq(0) li').removeClass('listContListLiHover');
        $('.listContList:eq(1) li').removeClass('listContListLiHover');
    }
    if ($('.connectionToServerBtn div').html()=='Connect') {
		 return;
	}
    if ($('#encodersGroup li:eq(0)').hasClass('listHover')) {
		var _arr = _data.videoswitching_encode_arr.split(',');
		var _id = _arr[deviceSelectID];	
	}
	else {
		var _arr = EncodeGroupDataListArr[deviceSelectID].split(':');
		var _id = _arr[0];
	}
    sendCommand('data=start '+_id+':AUDIO:0');//Get ALL settings //event 0
    //sendCommand('data=1');
    // console.log(ajaxCommandAck);
});

$('#StopAnalogAudio').on('click',function(){
    if (document.getElementById('CommonList')) {
        document.getElementById('CommonList').parentNode.removeChild(document.getElementById('CommonList'));
        $('.listContList:eq(0) li').removeClass('listContListLiHover');
        $('.listContList:eq(1) li').removeClass('listContListLiHover');
    }
    if ($('.connectionToServerBtn div').html()=='Connect') {
		 return;
	}
    if ($('#encodersGroup li:eq(0)').hasClass('listHover')) {
		var _arr = _data.videoswitching_encode_arr.split(',');
		var _id = _arr[deviceSelectID];	
	}
	else {
		var _arr = EncodeGroupDataListArr[deviceSelectID].split(':');
		var _id = _arr[0];
	}
    sendCommand('data=stop '+_id+':AUDIO:0');//Get ALL settings //event 0
    $('.listCont:eq(0) li').each(function(e){
    	if ($('.listCont:eq(0) li:eq('+e+') .rs232DisplayType').hasClass('rs232DisplayStream')) {
    		$('.listCont:eq(0) li:eq('+e+') .rs232DisplayType').removeClass('rs232DisplayStream');
    	}
    });
    $('.listCont:eq(1) li').each(function(e){
    	if ($('.listCont:eq(1) li:eq('+e+') .rs232DisplayType').hasClass('rs232DisplayStream')) {
    		$('.listCont:eq(1) li:eq('+e+') .rs232DisplayType').removeClass('rs232DisplayStream');
    	}
    });
    //sendCommand('data=1');
    // console.log(ajaxCommandAck);
});
// pin to this position
$('#PinToThisPosition').on('click',function(){
	if (!$('#PingToPosition').hasClass('PingToThisPosition')) {
		$('.listCont:eq(1) li').each(function(e){
			if ($(this).hasClass('listContListLiHover')) {
			    _deviceNum = e;
			}
		});
		if ($('#encodersGroup li:eq(0)').hasClass('listHover')){
			
			$('#DeviceEncodePositionID'+_deviceNum+' .PositionPIN').remove();
			var _macAddress = $('#DeviceEncodePositionID'+_deviceNum+' span:eq(1)').html();
			var _decodePinPosition  = decodePinPosition.split(',');
			var _resetData = '';
			for (var i = 0; i < _decodePinPosition.length; i++) {
				var _decodePinPositionArr = _decodePinPosition[i].split(':');
				if (_macAddress==_decodePinPositionArr[0]) {
					_decodePinPosition[i] = '';
				}
			}
			for (var i = 0; i < _decodePinPosition.length; i++) {			
				_resetData+=_decodePinPosition[i]+',';
								
			}
			_resetData = _resetData.substr(0,_resetData.length-1);
			ReadDataFromFile('PositionInfoSetFile=6&PositionInfoData='+_resetData);
			
		}
		else {
			for (var i = 0; i < EncodeGroupDataListArr.length; i++) {
		    	if (_deviceNum==i) {
		    		var _DecodeGroupDataListArrTmp = EncodeGroupDataListArr[i].split(':');
		    		for (var j = 0; j < EncodeGroupDataListArr.length; j++) {
		    			var _DecodeGroupDataListArrTmp2 = EncodeGroupDataListArr[j].split(':');
		    			if (_DecodeGroupDataListArrTmp2[0]==_DecodeGroupDataListArrTmp[0]) {
		    				EncodeGroupDataListArr[j] = _DecodeGroupDataListArrTmp[0]+':N';
		    				break;
		    			}
		    			if (EncodeGroupDataListArr[j]=='') {
		    				EncodeGroupDataListArr[j] = _DecodeGroupDataListArrTmp[0]+':N';
		    				EncodeGroupDataListArr[i] = '';
		    				break;
		    			}
		    		}
		    		
		    	}
		    }
		    var _temp='';
		    for (var i = 0; i < EncodeGroupDataListArr.length; i++) {
		    	_temp+=EncodeGroupDataListArr[i]+',';
		    }
		    _temp = _temp.substr(0,_temp.length-1);
		    PageAddGroup('type=9&id='+$('#encodersGroup li:eq('+_GroupNumber+') span').html()+'&deviceMAC='+_temp);
		    $('.listCont:eq(1) li:eq('+_deviceNum+') .PositionPIN').remove();
		    console.log(EncodeGroupDataListArr);
		}
		return;
	}
	var _deviceNum;
	var _jsonPositionData;
	$('.listCont:eq(1) li').each(function(e){
	    if ($(this).hasClass('listContListLiHover')) {
	    	_deviceNum = e;
	    }
	});
	$.ajax({
	    type: 'POST',
	    url: 'common/readfile.php',
	    data: 'PositionInfoFromFile=6',
	    datatype: 'json',
	    async: true,
	    success: function(rst) {
	    	if ($('#encodersGroup li:eq(0)').hasClass('listHover')) {
	    		var _arr = _data.videoswitching_encode_arr.split(',');
		        _jsonPositionData = rst;        	
		        var _jsonPositionDataArr = _jsonPositionData.split(',');
		        if (_jsonPositionData!='') {        		
		        	var _jsonPositionDataTmp='';
		        	if (_jsonPositionData.indexOf(_arr[_deviceNum])>=0) {
		        		for (var i = 0; i < _jsonPositionDataArr.length; i++) {
			        		var _jsonPositionDataArrTmp = _jsonPositionDataArr[i].split(':');
			        		if (_jsonPositionDataArrTmp[0]==_arr[_deviceNum]) {
			        			_jsonPositionDataArrTmp[1] = _deviceNum;
			        			_jsonPositionDataArr[i] = _jsonPositionDataArrTmp[0]+':'+_jsonPositionDataArrTmp[1];	        				
			        		}
			        	}
			        	_jsonPositionData='';
			        	for (var i = 0; i < _jsonPositionDataArr.length; i++) {
				        	_jsonPositionData += _jsonPositionDataArr[i] + ',';
				        }
				        _jsonPositionData = _jsonPositionData.substr(0,_jsonPositionData.length-1);
		        	}else{
		        		_jsonPositionData += ',' + _arr[_deviceNum]+':'+_deviceNum;
		        	}
		        }else{
		        	_jsonPositionData = _arr[_deviceNum]+':'+_deviceNum;
		        }
		        console.log(_jsonPositionData);
		        ReadDataFromFile('PositionInfoSetFile=6&PositionInfoData='+_jsonPositionData);
	    	}
		    else {
	    		for (var i = 0; i < EncodeGroupDataListArr.length; i++) {
		    		if (_deviceNum==i) {
		    			var _DecodeGroupDataListArrTmp = EncodeGroupDataListArr[i].split(':');
		    			_DecodeGroupDataListArrTmp[1] = _deviceNum;
		    			EncodeGroupDataListArr[i] = _DecodeGroupDataListArrTmp[0]+':'+_DecodeGroupDataListArrTmp[1];
		    		}
		    	}
		    	var _temp='';
		    	for (var i = 0; i < EncodeGroupDataListArr.length; i++) {
		    		_temp+=EncodeGroupDataListArr[i]+',';
		    	}
		    	_temp = _temp.substr(0,_temp.length-1);
		    	PageAddGroup('type=9&id='+$('#encodersGroup li:eq('+_GroupNumber+') span').html()+'&deviceMAC='+_temp);
		    	console.log(EncodeGroupDataListArr);
	    	}
	    }
	});
	$('#DeviceEncodePositionID'+_deviceNum).append("<div class='PositionPIN'></div>");
});
function LoadToResetArgument(){
	var _deviceNum;
	var _jsonPositionData;
	if (!$('#encodersGroup li:eq(0)').hasClass('listHover')) {
		$('.CommonListNav li:eq(7) span').html('Remove from Group')
	}
	$('.listCont:eq(1) li').each(function(e){
	    if ($(this).hasClass('listContListLiHover')) {
	        _deviceNum = e;
	    }
	});
	if ($('.CommonListNav li:eq(7) span').html()=='Remove from Group') {
		$('.selectIco').hide();
	}
	else{
		$('.selectIco').show();
	}
	$.ajax({
	    type: 'POST',
	    url: 'common/readfile.php',
	    data: 'PositionInfoFromFile=6',
	    datatype: 'json',
	    async: true,
	    success: function(rst) {
	        if ($('#encodersGroup li:eq(0)').hasClass('listHover')) {
	    		var _arr = _data.videoswitching_encode_arr.split(',');
		        // _jsonPositionData = _arr[_deviceNum-1]+':'+_deviceNum; 
		        console.log(rst.indexOf(_arr[_deviceNum]));
		        if (rst.indexOf(_arr[_deviceNum])>=0) {
		        	$('#PingToPosition').removeClass('PingToThisPosition');
		        }else{
		        	$('#PingToPosition').addClass('PingToThisPosition');
		        }
	    	}
		    else {
		    	var _EncodeGroupDataListArr = EncodeGroupDataListArr[_deviceNum].split(':');
		    	if (parseInt(_EncodeGroupDataListArr[1])>=0) {
		    		$('#PingToPosition').removeClass('PingToThisPosition');
		    	}
		    	else {
		    		$('#PingToPosition').addClass('PingToThisPosition');
		    	}
		    }
	    }
	});
}