
function encoderselect(){	
	
	var twidget = new Widget({
		style: 'margin: 4px; float: left; width: 80px; height: 80px; position: relative; display: block; z-index: 1000000;', 
		className: 'draggable cloneable',
		set: 'encoderselect',
		id: 'Default-Widget-12',
		init: function(){
		
			if (activepanel) this.ontopanel();
			
			if (ncommand){
				$.get("/php_includes/EchoAllUnitsTX.php", {},
					function(data){
						if (data == 'false') this.txunitlist = [];
						else this.txunitlist = objToArray(json_decode(data));
					}.bind(this));
				$.get("/php_includes/EchoAllUnitsDVR.php", {},
					function(data){
						if (data == 'false') this.dvrunitlist = [];
						else this.dvrunitlist = 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 {
				this.doubleclick = function(){
					pb.openDialog({
						title: '<span style="font-size: 21px;">Widget Properties:</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(){
						
							var ulist = this.txunitlist;
							var str = '';
							
							for (var i=0; i<ulist.length; i++){
								if (0&&ulist[i].CommExpired !== '0') var tsr = ' color: red !important;';
								else tsr = '';
								
								str += '<div style="display: inline-block;'+tsr+'"><input type="checkbox" class="unitlist-checkbox" id="'+ulist[i].ipaddr+'_checkbox" />&nbsp;'+
									ulist[i].nickname+'</div><br>';
							}
							
							var ulist = this.dvrunitlist;
							
							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="'+ulist[i].ipaddr+'_checkbox" />&nbsp;'+
									ulist[i].nickname+'</div><br>';
								str += '<div style="display: inline-block;'+tsr+'"><input type="checkbox" class="unitlist-checkbox" id="'+ulist[i].ipaddr+'_2_checkbox" />&nbsp;'+
									ulist[i].nickname+' 2nd Stream</div><br>';
							}
							
							var ulist = this.wpunitlist;
							
							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="'+ulist[i].ipaddr+'_checkbox" />&nbsp;'+
									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 ((this.core.finalunitlist[i].ipaddr+'_checkbox') == $('.unitlist-checkbox')[j].id){
										$($('.unitlist-checkbox')[j]).prop('checked', true);
									}
									if ((this.core.finalunitlist[i].ipaddr+'_2_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 ulist = deepCopy(this.txunitlist);
							ulist.push.apply(ulist, this.dvrunitlist);
							ulist.push.apply(ulist, this.wpunitlist);
							
							var tulist = [];
							for (var pk=0; pk<ulist.length; pk++){
								if ($(document.getElementById(ulist[pk].ipaddr+'_checkbox')).prop('checked')) tulist.push(deepCopy(ulist[pk]));
								if ($(document.getElementById(ulist[pk].ipaddr+'_2_checkbox')).prop('checked')){
									var pitem = deepCopy(ulist[pk]);
									pitem.dvrb = '1';
									tulist.push(pitem);
								}
							}

							this.core.finalunitlist = tulist;
							
							pb.closeDialog();
							
						}.bind(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.element).removeClass('item_child');
			
			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;
				if (this.widget.unitlist[unitind].dvrb === '1') eletd.id = eletd.id+'_2';
				eletd.className = this.widget.id+'-unit-list-item unselectable';
			
				var ip = this.widget.unitlist[unitind].ipaddr;
				if (this.widget.unitlist[unitind].dvrb == '1'){
					ip = ip+'_2';
					this.widget.unitlist[unitind].nickname += ' 2nd Stream';
				}
				
				$(eletd).append(this.widget.unitlist[unitind].nickname);
				
				eletd.onclick = function(eletd, ip){
				
					if (Math.abs(pb.mouse.vectortodown.x) > 3 || Math.abs(pb.mouse.vectortodown.y) > 3){
						return;
					}
				
					if (eletd.className.indexOf('encoder-selected') == -1){
					
						$('.encoder-selected').removeClass('encoder-selected');
						$(eletd).addClass('encoder-selected');
						
						if ($('.decoder-selected').exists()){
						
							var tstr = ''; var tip = false;
							
							for (var ok=0; ok<$('.decoder-selected').length; ok++){
							
								if ($('.decoder-selected')[ok].id.indexOf('0-') !== -1){
									tip = $('.decoder-selected')[ok].id.replace('0-', '');
									var command = 'switch';
									var wp = '';
								}
								else {
									tip = $('.decoder-selected')[ok].id.substring(2,$('.decoder-selected')[ok].id.length);
									var command = 'wpswitch';
									var wp = ' '+$('.decoder-selected')[ok].id.substring(0,1);
								}
								
								if (tip && tip != ip) tstr += command+' '+tip+wp+' '+ip+'\n';
								
							}

							if (wallcon || true){
								if (tip && tip != ip){
									var stream = getStreamFromUnit(ip);
									//console.log(stream);
									if (1) tcpclient(tip, 50001, asciiToHex('set:'+stream));
									else nCmd('tcpclient '+tip+' '+50001+' '+asciiToHex('set:'+stream), true);
								}
							}
							else sendScript(tstr);
							
							runTimeout(function(){
								$('.encoder-selected').removeClass('encoder-selected');
								$('.decoder-selected').removeClass('decoder-selected');
							}, 400);
						}
						
					}
					else {
						$(eletd).removeClass('encoder-selected');
					}
					
				}.bind(this, eletd, ip);

				$(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>Encoder<br>Select</div>';
	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;
	
}