var checkIsCustom = 0;
$(document).ready(function(){
	addOfflineModeEvent(0);
	addOfflineModeEvent(1);
	addOfflineModeEvent(2);
	addOfflineModeEvent(3);
	addOfflineModeEvent(4);
	addOfflineModeEvent(5);
	addOfflineModeEvent(6);
	addOfflineModeEvent(7);
	addOfflineModeEvent(8);
	addOfflineModeEvent(9);
	addOfflineModeEvent(10);
	addOfflineModeEvent(11);
	$('.offlinemodeNdwChooseCont a').each(function(){
		$(this).on('click',function(){
			$('.offlinemodeNdwChooseCont a').removeClass('offlinemodeNdwChoosed');
			$(this).addClass('offlinemodeNdwChoosed');
		});
	});
	$('#ipmode').on('change',function(){
		if ($(this).val()=='0') {
			$('#ipaddress').attr('disabled',true);			
			$('#mask').attr('disabled',true);
			$('#gateway').attr('disabled',true);
			$('#ipaddress').css('background-color','#949494');
			$('#mask').css('background-color','#949494');
			$('#gateway').css('background-color','#949494');
		}
		else if($(this).val()=='1'){
			$('#ipaddress').removeAttr('disabled');
			$('#mask').removeAttr('disabled');
			$('#gateway').removeAttr('disabled');
			$('#ipaddress').css('background-color','#fcfcfc');
			$('#mask').css('background-color','#fcfcfc');
			$('#gateway').css('background-color','#fcfcfc');
		}
	});
	$('#SettingBoxClose').on('click',function(){
		$('#NdwSettingBox').hide();
	});
	$('#DecodeSettingBoxClose').on('click',function(){
		$('#decodeSettingBox').hide();
	});
	$('#EncodeSettingBoxClose').on('click',function(){
		$('#encodesettingBox').hide();
	});
	$('.offlinemodeNdwNextBtn').on('click',function(){
		$('#NdwSettingBox').hide();
		if (clickType==0) {
			if ($('.offlinemodeNdwChooseCont a:eq(0)').hasClass('offlinemodeNdwChoosed')) {
				$('.finishBox').show();
			}
			else if($('.offlinemodeNdwChooseCont a:eq(1)').hasClass('offlinemodeNdwChoosed')){
				$('#decodeSettingBox').show();
			}
		}
		else if(clickType==1){
			if ($('.offlinemodeNdwChooseCont a:eq(0)').hasClass('offlinemodeNdwChoosed')) {
				$('.finishBox').show();
			}
			else if($('.offlinemodeNdwChooseCont a:eq(1)').hasClass('offlinemodeNdwChoosed')){
				$('#encodesettingBox').show();
			}
		}			
	});
	$('#finishBoxClose').on('click',function(){
		$('.finishBox').hide();
	});
	// finish
	$('#FinishBoxBtn').on('click',function(){		
		var macaddress = $('#macaddress').val();

		if (clickType==0) {
			var _numDecode = decodeDeviceSetting.length;
			if (checkIsCustom==1) {
				decodeDeviceSetting[_numDecode-1][0] = macaddress;
			}else{
				decodeDeviceSetting[_numDecode]=[];
				decodeDeviceSetting[_numDecode][0] = macaddress;
				decodeDeviceSetting[_numDecode][1] = '2';//analog audio output
				decodeDeviceSetting[_numDecode][2] = '2';//hdmi audio output
				decodeDeviceSetting[_numDecode][3] = macaddress.toLocaleUpperCase();//network setting hostname
				decodeDeviceSetting[_numDecode][4] = 'DHCP';//network setting ipmode
				decodeDeviceSetting[_numDecode][5] = '';//ip address
				decodeDeviceSetting[_numDecode][6] = '';//ip mask
				decodeDeviceSetting[_numDecode][7] = '';//ip gateway
				decodeDeviceSetting[_numDecode][8] = '57600';//rs232 baudrate
				decodeDeviceSetting[_numDecode][9] = '7';//rs232 databit
				decodeDeviceSetting[_numDecode][10] = '1';//rs232 stopbit
				decodeDeviceSetting[_numDecode][11] = 'NONE';//rs232 parity
			}			
				
			$('#decodeSettingBox').hide();
			$('.finishBox').hide();
			
			var _num=decodeDeviceSetting.length;
			document.getElementById('offlinemodeDecodeAddBtn').parentNode.removeChild(document.getElementById('offlinemodeDecodeAddBtn'));
			document.getElementById('offlinemodeVideoWallDecodeAddBtn').parentNode.removeChild(document.getElementById('offlinemodeVideoWallDecodeAddBtn'));
			document.getElementById('offlinemodeHdmiAudioDecodeAddBtn').parentNode.removeChild(document.getElementById('offlinemodeHdmiAudioDecodeAddBtn'));
			document.getElementById('offlinemodeAnalogAudioDecodeAddBtn').parentNode.removeChild(document.getElementById('offlinemodeAnalogAudioDecodeAddBtn'));
			document.getElementById('offlinemodeRs232DecodeAddBtn').parentNode.removeChild(document.getElementById('offlinemodeRs232DecodeAddBtn'));
			document.getElementById('offlinemodeInfraredDecodeAddBtn').parentNode.removeChild(document.getElementById('offlinemodeInfraredDecodeAddBtn'));
			
			$('.listCont:eq(0) ul').append("<li class='decodeencodeexist'>\
												<span>"+decodeDeviceSetting[_num-1][3]+"</span>\
												<span>"+decodeDeviceSetting[_num-1][4]+"</span>\
												<span></span>\
												<span></span>\
												<span></span>\
												<span></span>\
												<span></span>\
												<span></span>\
												<div class='decodeDisplayType '></div>\
												<div class='DragToDropBtn'></div>\
											</li>");
			$('.listContVideoWall:eq(0) ul').append("<li class='decodeencodeexist'>\
														<span>"+decodeDeviceSetting[_num-1][3]+"</span>\
														<span>"+decodeDeviceSetting[_num-1][4]+"</span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<div class='decodeDisplayType '></div>\
														<div class='DragToDropBtn'></div>\
													</li>");
			$('.listContHdmiAudio:eq(0) ul').append("<li class='decodeencodeexist'>\
														<span>"+decodeDeviceSetting[_num-1][3]+"</span>\
														<span>"+decodeDeviceSetting[_num-1][4]+"</span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<div class='AudioDisplayType '></div>\
														<div class='DragToDropBtn'></div>\
													</li>");
			$('.listContAnalogAudio:eq(0) ul').append("<li class='decodeencodeexist'>\
														<span>"+decodeDeviceSetting[_num-1][3]+"</span>\
														<span>"+decodeDeviceSetting[_num-1][4]+"</span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<div class='AudioDisplayType '></div>\
														<div class='DragToDropBtn'></div>\
													</li>");
			// ------------------------------------------------------------------------
			$('.listContRs232:eq(0) ul').append("<li class='decodeencodeexist'>\
													<span>"+decodeDeviceSetting[_num-1][3]+"</span>\
													<span>"+decodeDeviceSetting[_num-1][4]+"</span>\
													<span></span>\
													<span></span>\
													<span></span>\
													<span></span>\
													<span></span>\
													<span></span>\
													<div class='rs232DisplayType'></div>\
													<div class='DragToDropBtn'></div>\
												</li>");
			$('.listContInfrared:eq(0) ul').append("<li class='decodeencodeexist'>\
														<span>"+decodeDeviceSetting[_num-1][3]+"</span>\
														<span>"+decodeDeviceSetting[_num-1][4]+"</span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<div class='infraredAllRx '></div>\
														<div class='DragToDropBtn'></div>\
													</li>");
			// ------------------------------------------------------------------------
			$('.listContVideoWall:eq(0) ul').append("<li id='offlinemodeVideoWallDecodeAddBtn'>\
														<a class='offlinemodeAddBtn'></a>\
													</li>");
			$('.listCont:eq(0) ul').append("<li id='offlinemodeDecodeAddBtn'>\
												<a class='offlinemodeAddBtn'></a>\
											</li>");
			$('.listContHdmiAudio:eq(0) ul').append("<li id='offlinemodeHdmiAudioDecodeAddBtn'>\
														<a class='offlinemodeAddBtn'></a>\
													</li>");
			$('.listContAnalogAudio:eq(0) ul').append("<li id='offlinemodeAnalogAudioDecodeAddBtn'>\
														<a class='offlinemodeAddBtn'></a>\
													</li>");
			// ----------------------------------------------------------------------------
			$('.listContRs232:eq(0) ul').append("<li id='offlinemodeRs232DecodeAddBtn'>\
													<a class='offlinemodeAddBtn'></a>\
												</li>");
			$('.listContInfrared:eq(0) ul').append("<li id='offlinemodeInfraredDecodeAddBtn'>\
													<a class='offlinemodeAddBtn'></a>\
												</li>");
			// ----------------------------------------------------------------------------

			addOfflineModeEvent(0);
			addOfflineModeEvent(2);
			addOfflineModeEvent(4);
			addOfflineModeEvent(6);
			addOfflineModeEvent(8);
			addOfflineModeEvent(10);
		}
		else if (clickType==1) {
			var _numEncode = encodeDeviceSetting.length;
			encodeDeviceSetting[_numEncode]=[];
			encodeDeviceSetting[_numEncode][0] = macaddress;
			encodeDeviceSetting[_numEncode][1] = true;//HDCP support
			encodeDeviceSetting[_numEncode][2] = true;//HDCP2.0 support
			encodeDeviceSetting[_numEncode][3] = '';//edid
			encodeDeviceSetting[_numEncode][4] = macaddress.toLocaleUpperCase();//network setting hostname
			encodeDeviceSetting[_numEncode][5] = 'DHCP';//network setting ipmode
			encodeDeviceSetting[_numEncode][6] = '';//ip address
			encodeDeviceSetting[_numEncode][7] = '';//ip mask
			encodeDeviceSetting[_numEncode][8] = '';//ip gateway
			encodeDeviceSetting[_numEncode][9] = '57600';//rs232 baudrate
			encodeDeviceSetting[_numEncode][10] = '7';//rs232 databit
			encodeDeviceSetting[_numEncode][11] = '1';//rs232 stopbit
			encodeDeviceSetting[_numEncode][12] = 'NONE';//rs232 parity
			$('#encodesettingBox').hide();
			$('.finishBox').hide();
			
			var _num=encodeDeviceSetting.length;
			document.getElementById('offlinemodeEncodeAddBtn').parentNode.removeChild(document.getElementById('offlinemodeEncodeAddBtn'));
			document.getElementById('offlinemodeVideoWallEncodeAddBtn').parentNode.removeChild(document.getElementById('offlinemodeVideoWallEncodeAddBtn'));
			document.getElementById('offlinemodeHdmiAudioEncodeAddBtn').parentNode.removeChild(document.getElementById('offlinemodeHdmiAudioEncodeAddBtn'));
			document.getElementById('offlinemodeAnalogAudioEncodeAddBtn').parentNode.removeChild(document.getElementById('offlinemodeAnalogAudioEncodeAddBtn'));
			document.getElementById('offlinemodeRs232EncodeAddBtn').parentNode.removeChild(document.getElementById('offlinemodeRs232EncodeAddBtn'));
			document.getElementById('offlinemodeInfraredEncodeAddBtn').parentNode.removeChild(document.getElementById('offlinemodeInfraredEncodeAddBtn'));
			
			$('.listCont:eq(1) ul').append("<li class='decodeencodeexist'>\
												<span>"+encodeDeviceSetting[_num-1][3]+"</span>\
												<span>"+encodeDeviceSetting[_num-1][4]+"</span>\
												<span></span>\
												<span></span>\
												<span></span>\
												<span></span>\
												<span></span>\
												<span></span>\
												<div class='encodeDisplayType '></div>\
												<div class='DragToDropBtn'></div>\
											</li>");
			$('.listContVideoWall:eq(1) ul').append("<li class='decodeencodeexist'>\
														<span>"+encodeDeviceSetting[_num-1][3]+"</span>\
														<span>"+encodeDeviceSetting[_num-1][4]+"</span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<div class='encodeDisplayType '></div>\
														<div class='DragToDropBtn'></div>\
													</li>");
			$('.listContHdmiAudio:eq(1) ul').append("<li class='decodeencodeexist'>\
														<span>"+encodeDeviceSetting[_num-1][3]+"</span>\
														<span>"+encodeDeviceSetting[_num-1][4]+"</span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<div class='AudioDisplayType '></div>\
														<div class='DragToDropBtn'></div>\
													</li>");
			$('.listContAnalogAudio:eq(1) ul').append("<li class='decodeencodeexist'>\
														<span>"+encodeDeviceSetting[_num-1][3]+"</span>\
														<span>"+encodeDeviceSetting[_num-1][4]+"</span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<span></span>\
														<div class='AudioDisplayType '></div>\
														<div class='DragToDropBtn'></div>\
													</li>");
			// --------------------------------------------------------------------------
			$('.listContRs232:eq(1) ul').append("<li class='decodeencodeexist'>\
													<span>"+encodeDeviceSetting[_num-1][3]+"</span>\
													<span>"+encodeDeviceSetting[_num-1][4]+"</span>\
													<span></span>\
													<span></span>\
													<span></span>\
													<span></span>\
													<span></span>\
													<span></span>\
													<div class='rs232DisplayType '></div>\
													<div class='DragToDropBtn'></div>\
												</li>");
			$('.listContInfrared:eq(1) ul').append("<li class='decodeencodeexist'>\
													<span>"+encodeDeviceSetting[_num-1][3]+"</span>\
													<span>"+encodeDeviceSetting[_num-1][4]+"</span>\
													<span></span>\
													<span></span>\
													<span></span>\
													<span></span>\
													<span></span>\
													<span></span>\
													<div class='infraredAllRx '></div>\
													<div class='DragToDropBtn'></div>\
												</li>");
			// --------------------------------------------------------------------------
			$('.listCont:eq(1) ul').append("<li id='offlinemodeEncodeAddBtn'>\
												<a class='offlinemodeAddBtn'></a>\
											</li>");
			$('.listContVideoWall:eq(1) ul').append("<li id='offlinemodeVideoWallEncodeAddBtn'>\
														<a class='offlinemodeAddBtn'></a>\
													</li>");
			$('.listContHdmiAudio:eq(1) ul').append("<li id='offlinemodeHdmiAudioEncodeAddBtn'>\
														<a class='offlinemodeAddBtn'></a>\
													</li>");
			$('.listContAnalogAudio:eq(1) ul').append("<li id='offlinemodeAnalogAudioEncodeAddBtn'>\
															<a class='offlinemodeAddBtn'></a>\
														</li>");
			// --------------------------------------------------------------------------
			$('.listContRs232:eq(1) ul').append("<li id='offlinemodeRs232EncodeAddBtn'>\
															<a class='offlinemodeAddBtn'></a>\
														</li>");
			$('.listContInfrared:eq(1) ul').append("<li id='offlinemodeInfraredEncodeAddBtn'>\
															<a class='offlinemodeAddBtn'></a>\
														</li>");
			// --------------------------------------------------------------------------
			addOfflineModeEvent(1);
			addOfflineModeEvent(3);
			addOfflineModeEvent(5);
			addOfflineModeEvent(7);
			addOfflineModeEvent(9);
			addOfflineModeEvent(11);
		}
	});
	// custom setting
	$('.settingBoxContBottomUL li').each(function(e){
		$(this).on('click',function(){
			$('.settingBoxContBottomUL li').removeClass('settingBoxBtnHover');
			$(this).addClass('settingBoxBtnHover');

			$('.settingBoxContBottomContList .settingBoxContBottomCont').removeClass('settingBoxContBottomContShow');
			$('.settingBoxContBottomContList .settingBoxContBottomCont:eq('+e+')').addClass('settingBoxContBottomContShow');
		});
	});
	// setting manaul
	$('#AnalogAudioOutput .settingBoxLine a').each(function(e){
		$(this).on('click',function(){			
			$('#AnalogAudioOutput .settingBoxLine a').removeClass('clickRadiuBtn');
			$(this).addClass('clickRadiuBtn');			
		});
	});
	// HDMI Audio Output
	$('#HDMIAudioOutput .settingBoxLine a').each(function(e){
		$(this).on('click',function(){			
			$('#HDMIAudioOutput .settingBoxLine a').removeClass('clickRadiuBtn');
			$(this).addClass('clickRadiuBtn');			
		});
	});
	// decode setting save
	$('#decondeSettingSaveBtn').on('click',function(){
		var _num=decodeDeviceSetting.length;
		decodeDeviceSetting[_num] = [];
		if ($('.settingBoxAudioSource a:eq(0)').hasClass('clickRadiuBtn')) {
			decodeDeviceSetting[_num][1] = '2';
		}
		else if($('.settingBoxAudioSource a:eq(1)').hasClass('clickRadiuBtn')){
			decodeDeviceSetting[_num][1] = '3';
		}
		if ($('.settingBoxHDMIAudioSource a:eq(0)').hasClass('clickRadiuBtn')) {
			decodeDeviceSetting[_num][2] = '2';
		}
		else if($('.settingBoxHDMIAudioSource a:eq(1)').hasClass('clickRadiuBtn')){
			decodeDeviceSetting[_num][2] = '3';
		}
		else if($('.settingBoxHDMIAudioSource a:eq(2)').hasClass('clickRadiuBtn')){
			decodeDeviceSetting[_num][2] = '6';
		}
		else if($('.settingBoxHDMIAudioSource a:eq(3)').hasClass('clickRadiuBtn')){
			decodeDeviceSetting[_num][2] = '7';
		}
		decodeDeviceSetting[_num][3] = $('#hostname').val();
		if ($('#ipmode').val()=='0') {
			decodeDeviceSetting[_num][4] = 'DHCP';
			decodeDeviceSetting[_num][5] = '';//ip address
			decodeDeviceSetting[_num][6] = '';//ip mask
			decodeDeviceSetting[_num][7] = '';//ip gateway
		}
		else if($('#ipmode').val()=='1'){
			decodeDeviceSetting[_num][4] = 'STATIC';
			decodeDeviceSetting[_num][5] = $('#ipaddress').val();//ip address
			decodeDeviceSetting[_num][6] = $('#mask').val();//ip mask
			decodeDeviceSetting[_num][7] = $('#gateway').val();//ip gateway
		}
		decodeDeviceSetting[_num][8] = $('#rs232baudrate').val();//rs232 baudrate
		decodeDeviceSetting[_num][9] = $('#databit').val();;//rs232 databit
		decodeDeviceSetting[_num][10] = $('#stopbit').val();;//rs232 stopbit
		decodeDeviceSetting[_num][11] = $('#parity').val();;//rs232 parity
		$('#decodeSettingBox').hide();
		$('.finishBox').show();
		checkIsCustom = 1;
	});
	// decode setting save
	$('#encondeSettingSaveBtn').on('click',function(){
		var _num=encodeDeviceSetting.length;
		encodeDeviceSetting[_num] = [];
		if ($('#HDCPSupport').val()=='0') {
			encodeDeviceSetting[_num][1] = true;
		}
		else if($('#HDCPSupport').val()=='1'){
			encodeDeviceSetting[_num][1] = false;
		}
		if ($('#HDCP22Support').val()=='0') {
			encodeDeviceSetting[_num][2] = true;
		}
		else if($('#HDCP22Support').val()=='1'){
			encodeDeviceSetting[_num][2] = false;
		}
		encodeDeviceSetting[_num][3] = $('#DisplayEdidData').val();//EDID Data
		encodeDeviceSetting[_num][4] = $('#encodehostname').val();//hostname
		if ($('#encodeipmode').val()=='0') {
			encodeDeviceSetting[_num][5] = 'DHCP';
			encodeDeviceSetting[_num][6] = '';//ip address
			encodeDeviceSetting[_num][7] = '';//ip mask
			encodeDeviceSetting[_num][8] = '';//ip gateway
		}
		else if($('#encodeipmode').val()=='1'){
			encodeDeviceSetting[_num][5] = 'STATIC';
			encodeDeviceSetting[_num][6] = $('#encodeipaddress').val();//ip address
			encodeDeviceSetting[_num][7] = $('#encodemask').val();//ip mask
			encodeDeviceSetting[_num][8] = $('#encodegateway').val();//ip gateway
		}
		encodeDeviceSetting[_num][9] = $('#encoders232baudrate').val();//rs232 baudrate
		encodeDeviceSetting[_num][10] = $('#encodedatabit').val();;//rs232 databit
		encodeDeviceSetting[_num][11] = $('#encodestopbit').val();;//rs232 stopbit
		encodeDeviceSetting[_num][12] = $('#encodeparity').val();;//rs232 parity
		$('#encodeSettingBox').hide();
		$('.finishBox').show();		
	});
});
	
// 左侧菜单点击事件	
function MenuNavShow(num){
	$('.OfflineModeCont .OfflineModeContList').each(function(){
		$(this).hide();
	});
	switch(num){
	case 0:
		$('.OfflineModeVideo').show();
		break;
	case 1:
		$('.OfflineModeVideoWall').show();
		break;
	case 2:
		$('.OfflineModeHdmiAudio').show();
		break;
	case 3:
		$('.OfflineModeAnalogAudio').show();
		break;
	case 4:
		$('.OfflineModeRs232').show();
		break;
	case 5:
		$('.OfflineModeInfrared').show();
		break;
	case 6:
		$('.OfflineModeFirmwareUpdate').show();
		break;
	case 7:
		$('.OfflineModeBackup').show();
		break;
	case 8:
		$('.OfflineModeSystemLog').show();
		break;
	case 9:
		$('.OfflineModeVideoRouting').show();
		break;
	case 10:
		$('.OfflineModeHdmiAudioRouting').show();
		break;
	case 11:
		$('.OfflineModeAnalogAudioRouting').show();
		break;
	case 12:
		$('.OfflineModeRs232Routing').show();
		break;
	case 13:
		$('.OfflineModeInfraredRouting').show();
		break;
	}
}
// 加载事件函数
function addOfflineModeEvent(num){
	switch(num){
	case 0:
		$('#offlinemodeDecodeAddBtn').on('click',function(){
			clickType=0;
			$('#NdwSettingBox').show();
		});
		break;
	case 1:
		$('#offlinemodeEncodeAddBtn').on('click',function(){
			clickType=1;
			$('#NdwSettingBox').show();
		});
		break;
	case 2://
		$('#offlinemodeVideoWallDecodeAddBtn').on('click',function(){
			clickType=0;
			$('#NdwSettingBox').show();
		});
		break;
	case 3://
		$('#offlinemodeVideoWallEncodeAddBtn').on('click',function(){
			clickType=1;
			$('#NdwSettingBox').show();
		});
		break;
	case 4://
		$('#offlinemodeHdmiAudioDecodeAddBtn').on('click',function(){
			clickType=0;
			$('#NdwSettingBox').show();
		});
		break;
	case 5://
		$('#offlinemodeHdmiAudioEncodeAddBtn').on('click',function(){
			clickType=1;
			$('#NdwSettingBox').show();
		});
		break;
	case 6://
		$('#offlinemodeAnalogAudioDecodeAddBtn').on('click',function(){
			clickType=0;
			$('#NdwSettingBox').show();
		});
		break;
	case 7://
		$('#offlinemodeAnalogAudioEncodeAddBtn').on('click',function(){
			clickType=1;
			$('#NdwSettingBox').show();
		});
		break;
	case 8://
		$('#offlinemodeRs232DecodeAddBtn').on('click',function(){
			clickType=0;
			$('#NdwSettingBox').show();
		});
		break;
	case 9://
		$('#offlinemodeRs232EncodeAddBtn').on('click',function(){
			clickType=1;
			$('#NdwSettingBox').show();
		});
		break;
	case 10://
		$('#offlinemodeInfraredDecodeAddBtn').on('click',function(){
			clickType=0;
			$('#NdwSettingBox').show();
		});
		break;
	case 11://
		$('#offlinemodeInfraredEncodeAddBtn').on('click',function(){
			clickType=1;
			$('#NdwSettingBox').show();
		});
		break;
	}
}