

function decoderselect(){	
	
	var twidget = new Widget({
		style: 'margin: 4px; float: left; width: 80px; height: 80px; position: relative; display: block; z-index: 1000000;', 
		className: 'draggable cloneable',
		set: 'decoderselect',
		id: 'Default-Widget-15',
		init: function(){
		
			if (activepanel) this.ontopanel();
		
			if (ncommand){
				$.get("/php_includes/EchoAllUnitsRX.php", {},
					function(data){
						if (data == 'false') this.rxunitlist = [];
						else this.rxunitlist = objToArray(json_decode(data));
					}.bind(this));		
				$.get("/php_includes/EchoAllUnitsWP.php", {},
					function(data){
						if (data == 'false') this.wpunitlist = [];
						else this.wpunitlist = objToArray(json_decode(data));
					}.bind(this));
			}
			else {
				this.txunitlist = [];
				this.rxunitlist = [];
				this.dvrunitlist = [];
				this.wpunitlist = [];
				for (var k=0; k<unitlist.length; k++){
					unitlist[k].id = unitlist[k].name;
					unitlist[k].nickname = unitlist[k].name;
					unitlist[k].ipaddr = unitlist[k].ip;
					if (unitlist[k].type == 'TX') this.txunitlist.push(unitlist[k]);
					if (unitlist[k].type == 'RX') this.rxunitlist.push(unitlist[k]);
					if (unitlist[k].type == 'DVR') this.dvrunitlist.push(unitlist[k]);
					if (unitlist[k].type == 'WP') this.wpunitlist.push(unitlist[k]);
				
				}
			}
			
		},
		ontopanel: function(){
			if (standalone){
				this.txunitlist = [];
				this.rxunitlist = [];
				this.dvrunitlist = [];
				this.wpunitlist = [];
				for (var k=0; k<unitlist.length; k++){
					unitlist[k].id = unitlist[k].name;
					unitlist[k].nickname = unitlist[k].name;
					unitlist[k].ipaddr = unitlist[k].ip;
					if (unitlist[k].type == 'TX') this.txunitlist.push(unitlist[k]);
					if (unitlist[k].type == 'RX') this.rxunitlist.push(unitlist[k]);
					if (unitlist[k].type == 'DVR') this.dvrunitlist.push(unitlist[k]);
					if (unitlist[k].type == 'WP') this.wpunitlist.push(unitlist[k]);
				
				}
			}
			if (activepanel){
				this.parts[0].ontopanel();
			}
			else {
				//console.log('setupwidget', this);
				this.doubleclick = function(){
					
					pb.openDialog({
						title: '<span style="font-size: 21px;">Decoder Unit List:</span>',
						content: '\
							Units:<br>\
							<div id="widget-unit-list" style="width: 645px; height: 250px; background: grey; overflow-y: scroll;"></div>\
							<input value="Check All" type="button" id="check-all-unit-list" style="position: absolute; left: 14px; top: 330px;" />\
							<input value="Uncheck All" type="button" id="uncheck-all-unit-list" style="position: absolute; left: 90px; top: 330px;" />',
						style: {
							width: '700px', 
							height: '420px', 
							background: 'black',
							color: 'rgb(238, 238, 238)',
							margin: '15px',
							'border-radius': '15px',
							padding: '10px'
						},
						init: function(){
						
							//console.log('flist', this.core);
						
							var ulist = this.rxunitlist;
							var str = '';
							
							for (var i=0; i<ulist.length; i++){
								if (0&&ulist[i].CommExpired !== '0' && !standalone) var tsr = ' color: red !important;';
								else tsr = '';
								
								str += '<div style="display: inline-block;'+tsr+'"><input type="checkbox" class="unitlist-checkbox" id="0-'+ulist[i].ipaddr+'_checkbox" />&nbsp;'+
									ulist[i].nickname+'</div><br>';
							}
							
							var ulist = this.wpunitlist;
							
							for (var i=0; i<ulist.length; i++){
								for (var k=1; k<5; k++){
									if (0&&ulist[i].CommExpired !== '0' && !standalone) var tsr = ' color: red !important;';
									else tsr = '';
									
									str += '<div style="display: inline-block;'+tsr+'"><input type="checkbox" class="unitlist-checkbox" id="'+k+'-'+ulist[i].ipaddr+'_checkbox" />&nbsp;'+
										k+' '+ulist[i].nickname+'</div><br>';
								}
							}
							$('#widget-unit-list')[0].innerHTML = str;
							for (var i=0; i<this.core.finalunitlist.length; i++){
								for (var j=0; j<$('.unitlist-checkbox').length; j++){
									if (('0-'+this.core.finalunitlist[i].ipaddr+'_checkbox') == $('.unitlist-checkbox')[j].id){
										$($('.unitlist-checkbox')[j]).prop('checked', true);
									}
									else if (this.core.finalunitlist[i].iswp !== 0 &&
										(this.core.finalunitlist[i].iswp+'-'+this.core.finalunitlist[i].ipaddr+'_checkbox') == $('.unitlist-checkbox')[j].id){
										$($('.unitlist-checkbox')[j]).prop('checked', true);
									}
								}
							}
							
							$('#check-all-unit-list').click(function(){
								$('.unitlist-checkbox').prop('checked', true);
							});
							
							$('#uncheck-all-unit-list').click(function(){
								$('.unitlist-checkbox').prop('checked', false);
							});

						}.bind(this),
						onCancel: function(){pb.closeDialog();},
						onConfirm: function(){
							
							var tulist = [];
							var ulist = this.rxunitlist;
						
							for (var pk=0; pk<ulist.length; pk++){
								var nobj = deepCopy(ulist[pk]);
								nobj.iswp = 0;
								if ($(document.getElementById('0-'+ulist[pk].ipaddr+'_checkbox')).prop('checked')) tulist.push(nobj);
							}
							
							var ulist = this.wpunitlist;
							
							for (var pk=0; pk<ulist.length; pk++){
								for (var jk=1; jk<5; jk++){
									var nobj = deepCopy(ulist[pk]);
									nobj.iswp = jk;
									if ($(document.getElementById(jk+'-'+ulist[pk].ipaddr+'_checkbox')).prop('checked')) tulist.push(nobj);
								}
							}

							this.core.finalunitlist = tulist;
							
							pb.closeDialog();
							
						}.bind(this)
					
					});
				};
				console.log('after', this);
			}
			
		}
	});
	
	twidget.core.finalunitlist = [];
	
	var tpart = new WidgetPart({
		widget: twidget,
		style: 'position: absolute; z-index: 1000000;',
		className: 'item_child movable',
		ontopanel: function(){
	
			this.element.innerHTML = '<div style="overflow-y: hidden; overflow-x: scroll; height: calc(100% + 16px);"></div>';
			if (this.element.children && this.element.children[0]) this.element.children[0].style.height = ($(this.widget.element).outerHeight + 16) + 'px';
			
			this.element.style.height = '100%';
			this.element.style.top = '0%';
			this.widget.parts[0].element.style.display = 'block';
			this.widget.parts[0].element.style.overflowY = 'hidden';
			
			this.widget.unitlist = this.widget.core.finalunitlist;
			
			var eletable = document.createElement('table');
			eletable.style.height = '100%';
			eletable.style.cursor = 'pointer';
			var elerow = document.createElement('tr');
			$(eletable).append(elerow);
			
			for (var unitind=0; unitind<this.widget.unitlist.length; unitind++){
			
				var eletd = document.createElement('td');
				
				eletd.style.minWidth = '200px';
				eletd.style.height = '100%';
				eletd.style.verticalAlign = 'middle';
				eletd.style.textAlign = 'center';
				eletd.style.color = 'white';
				eletd.style.fontFamily = 'time';
				eletd.style.fontSize = '1.5em';
				eletd.id = this.widget.unitlist[unitind].ipaddr;
				eletd.className = this.widget.id+'-unit-list-item unselectable';
			
				var ip = this.widget.unitlist[unitind].ipaddr;
				var wp = '';
				
				if (this.widget.unitlist[unitind].iswp !== 0){
					var command = 'wpswitch';
					var wp = ' '+this.widget.unitlist[unitind].iswp;
					var sp = '-';
					eletd.id = this.widget.unitlist[unitind].iswp+'-'+this.widget.unitlist[unitind].ipaddr;
				}
				else {
					var command = 'switch';
					var wp = '';
					var sp = '';
					eletd.id = '0-'+this.widget.unitlist[unitind].ipaddr;
				}
				
				$(eletd).append(wp+sp+this.widget.unitlist[unitind].nickname);
				
				var ulist = this.widget.unitlist;
				
				eletd.onclick = function(eletd, ip, wp, command){

					if (Math.abs(pb.mouse.vectortodown.x) > 3 || Math.abs(pb.mouse.vectortodown.y) > 3){
						return;
					}
				
					if (eletd.className.indexOf('decoder-selected') == -1){
					
						$(eletd).addClass('decoder-selected');
						
						if ($('.encoder-selected').exists()){
							if ($('.encoder-selected')[0].id != ip){
								if (wallcon || true){
									//if (tip != ip){
										var stream = false;
										for (var i=0;i<unitlist.length;i++){
											if ($('.encoder-selected')[0].id === unitlist[i].ip) stream = unitlist[i].stream;
										}
										//console.log('stream', stream);
										if (stream !== false) tcpclient(ip, 50001, asciiToHex('set:'+stream));
										else console.log('no stream found');//nCmd('tcpclient '+ip+' '+50001+' '+asciiToHex('set:'+stream), true);
									//}
								}
								else //sendScript(tstr);
								sendScript(command+' '+ip+wp+' '+$('.encoder-selected')[0].id);
							}
							runTimeout(function(){
								$('.encoder-selected').removeClass('encoder-selected');
								$('.decoder-selected').removeClass('decoder-selected');
							}, 400);
						}
						
					}
					else {
						$(eletd).removeClass('decoder-selected');
					}
					
				}.bind(this, eletd, ip, wp, command);
				
				$(elerow).append(eletd);
				
			}
			
			$(this.element.children[0]).append(eletable);
			$(eletable).draggable({
				axis: 'x',
				drag: function(divele) {
					var n = Number($(this).css('left').replace('px',''));
					var w = Number($(this).css('width').replace('px',''));
					var wb = Number($(divele).css('width').replace('px',''));
					if (n > 0){
						this.style.left = '0px';
						return false;
					}
					if (n < -w + wb){
						this.style.left = (-w + wb) + 'px';
						return false;
					}
				}.bind(eletable, this.element),
			});
				
		}
	});
	tpart.downoffset = 0;
	tpart.element.innerHTML = '<div class="item_child" style="cursor: default; font-size: 18px; width: 100%; height: 100%; font-family: time; color: white; text-align: center;">'+
		'<br>Decoder<br>Select</div>';
	tpart.responsive = true;
	tpart.element.style.cssText += 'overflow-y: auto; position: absolute; left: 0%; top: 0%; height: 100%; width: 100%; background: rgba(0,0,0,0.5);';

	twidget.addPart(tpart);

	return twidget;
}