

function dvr(){	
	
	var twidget = new Widget({
		style: 'margin: 4px; float: left; width: 80px; height: 80px; position: relative;\
			display: block; z-index: 1000000; background: url(images/widgets/dvrbg.png); background-size: 100% 100%;', 
		className: 'draggable cloneable',
		set: 'dvr',
		type: 'default',
		id: 'Default-Widget-9',
		init: function(){
		
			if (activepanel) this.ontopanel();
			
			this.core.dvrmac = 'all';
			
			if (useDB_read){
				$.get("/php_includes/EchoAllUnitsTX.php", {},
					function(data){
						if (data == 'false') this.txunitlist = [];
						else this.txunitlist = objToArray(json_decode(data));
					}.bind(this));
				$.get("/php_includes/EchoAllUnitsRX.php", {},
					function(data){
						if (data == 'false') this.rxunitlist = [];
						else this.rxunitlist = 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]);
				
				}
				//console.log('initwidget', this);
			}
				
		},
		ontopanel: function(){
			
			this.element.style.backgroundSize = "100% 100%";
			
			if (activepanel){
			
				this.element.children[0].innerHTML = '';
				
				this.parts[7].element.style.display = 'block';
				
				$.get('scripts/index.php', {dvrplaylistjson: this.core.dvrip},
					function(data){
						
						this.parts[7].element.style.display = 'block';
						this.parts[7].element.style.background = 'none';
						this.parts[7].element.style.color = 'white';
						$(this.parts[7].element).css({position: 'absolute', top: '3%', left: '7%',
							width: '40%', height: '8%', 'font-weight': 'bold'});
						if (this.core.dvrname){
							this.parts[7].element.innerHTML = truncateString(this.core.dvrname, 13);
						}
						else{
							this.parts[7].element.innerHTML = 'No NVR';
							return;
						}
						
						this.core.dvrplaylist = json_decode(data);
						
						this.core.dvrplaylist = $.map(this.core.dvrplaylist, function(value, index) {
							return [value];
						});
						
						this.parts[0].element.innerHTML = '<div style="cursor: default;"></div>';
						this.parts[0].element.style.height = '66%';
						this.parts[0].element.style.width = '82%';
						this.parts[0].element.style.top = '12%';
						this.parts[0].element.style.left = '9%';
						this.parts[0].element.style.border = '2px solid grey';
						$(this.parts[0].element).css({'overflow-x': 'hidden'});
						
						for (var dvrlind=0; dvrlind<this.core.dvrplaylist.length-1; dvrlind++){
							var lele = document.createElement('div');
							var lmacele = document.createElement('div');
							lmacele.style.display = 'none';
							lmacele.innerHTML = this.core.dvrplaylist[dvrlind].fileName;
							lele.style.color = 'white';
							lele.style.minHeight = '2.6em';
							lele.innerHTML = '[' + this.core.dvrplaylist[dvrlind].recordDate + ']\n' +
												this.core.dvrplaylist[dvrlind].description;
							lele.className = ''+this.core.id+'-dvr-playlist-item';
							lele.style.border = '1px solid darkgrey';
							lele.onclick = function(){
								$('.'+pb.getItemById(this.parentNode.parentNode.id).widget.id+'-dvr-playlist-item').css({backgroundColor: 'rgba(0,0,0,0.0)', color: 'white'});
								$('.'+pb.getItemById(this.parentNode.parentNode.id).widget.id+'-dvr-playlist-item').removeClass(''+pb.getItemById(this.parentNode.parentNode.id).widget.id+'-dvr-selected');
								this.style.backgroundColor = 'white';
								this.style.color = 'black';
								$(this).addClass(''+pb.getItemById(this.parentNode.parentNode.id).widget.id+'-dvr-selected');
							}.bind(lele);
							$(this.parts[0].element.children[0]).append(lele);
							$(lele).append(lmacele);
						}
						
						this.parts[6].element.innerHTML = '<img src="images/widgets/goback.png" class="item_child"\
							style="cursor: pointer; position: absolute; left: 0%; top: 0%; height: 100%; min-height: 50px;" />';
						this.parts[6].element.style.width = '10%';
						this.parts[1].element.style.display = 'none';
						this.parts[6].element.className = '';
						this.parts[6].click = function(){	
							this.parts[7].element.style.display = 'block';
							$.get("/php_includes/EchoAllUnitsDVR.php", {},
								function(data){
								
									this.parts[7].element.style.display = 'none';
									
									this.core.dvrlist = json_decode(data);
									
									this.element.children[0].innerHTML = '';
									this.parts[1].element.style.display = "block";
									this.parts[2].element.style.display = "block";
									this.parts[3].element.style.display = "none";
									this.parts[4].element.style.display = "none";
									this.parts[5].element.style.display = "none";
									this.parts[6].element.style.display = "none";
									
									for (var dvrind=0; dvrind<this.core.dvrlist.length; dvrind++){
									
										var ele = document.createElement('div');
										var macele = document.createElement('div');
										macele.style.display = 'none';
										macele.innerHTML = this.core.dvrlist[dvrind].mac;
										ele.style.color = 'white';
										ele.style.minHeight = '2.6em';
										ele.innerHTML = this.core.dvrlist[dvrind].nickname;
										
										ele.className = ''+this.core.id+'-dvr-playlist-item';
										ele.style.border = '1px solid darkgrey';
										ele.onclick = function(){
											$('.'+pb.getItemById(this.parentNode.parentNode.id).widget.id+'-dvr-playlist-item').css({backgroundColor: 'rgba(0,0,0,0.0)', color: 'white'});
											$('.'+pb.getItemById(this.parentNode.parentNode.id).widget.id+'-dvr-playlist-item').removeClass(''+pb.getItemById(this.parentNode.parentNode.id).widget.id+'-dvr-selected');
											this.style.backgroundColor = 'white';
											this.style.color = 'black';
											$(this).addClass(''+pb.getItemById(this.parentNode.parentNode.id).widget.id+'-dvr-selected');
										}.bind(ele);
							
										$(this.element.children[0]).append(ele);
										$(ele).append(macele);
										
									}
								
								}.bind(this.parts[0]));
						}

						this.parts[3].element.style.width = '14%';
						this.parts[3].element.className = '';
						this.parts[3].click = function(){
						
							if (!$('.'+this.core.id+'-dvr-selected').exists()){
								pb.openDialog({
									title: 'Playback:',
									content: 'No playlist tracks selected for playback.'
								});
								return;
							}
							
							pb.openDialog({
								title: 'Playback: ',
								content: 'Loop: <input type="checkbox" id="loop-playback" /><br><br>\
											Start Offset: <select id="start-offset-select">\
												<option value="0" selected="selected">None</option>\
												<option value="300">5 Minutes</option>\
												<option value="900">15 Minutes</option>\
												<option value="1800">30 Minutes</option>\
												<option value="2700">45 Minutes</option>\
												<option value="3600">1 Hour</option>\
												<option value="5400">1.5 Hours</option>\
												<option value="7200">2 Hours</option>\
												<option value="10800">3 Hours</option>\
												<option value="NaN">Max</option>\
											</select><br><br>\
											Stop Offset: <select id="stop-offset-select">\
												<option value="0" selected="selected">None</option>\
												<option value="300">5 Minutes</option>\
												<option value="900">15 Minutes</option>\
												<option value="1800">30 Minutes</option>\
												<option value="2700">45 Minutes</option>\
												<option value="3600">1 Hour</option>\
												<option value="5400">1.5 Hours</option>\
												<option value="7200">2 Hours</option>\
												<option value="10800">3 Hours</option>\
												<option value="NaN">Max</option>\
											</select><br><br>',
								onConfirm: function(){
								
									var dvrfilename = $('.'+this.core.id+'-dvr-selected')[0].children[0].innerHTML;
								
									var tscript = '';
									tscript += 'play ';
									tscript += this.core.dvrip;
									tscript += ' ' + dvrfilename;
									
									if ($('#loop-playback').prop('checked')) tscript += ' loop';
									else tscript += ' noloop';
									
									tscript += ' ' + $('#start-offset-select').val();
									tscript += ' ' + $('#stop-offset-select').val();
									 
									$.post("/scripts/index.php", {executescript: "yes", script: tscript});	

									pb.closeDialog();
										
								}.bind(this)
							});
								
						}
						
						this.parts[4].element.style.width = '14%';
						this.parts[4].element.className = '';
						this.parts[4].click = function(){
							
							var tscript = '';
							tscript += 'stop ';
							tscript += this.core.dvrip;
							 
							$.post("/scripts/index.php", {executescript: "yes", script: tscript});
							
							console.log('sending: '+tscript);	

							var tscript = '';
							tscript += 'stoprecord ';
							tscript += this.core.dvrip;
							 
							$.post("/scripts/index.php", {executescript: "yes", script: tscript});
								
						}
						
						this.parts[5].element.style.width = '14%';
						
						this.parts[5].element.className = '';
						this.parts[5].click = function(){
								
							if (this.core.finalunitlist.length == 0){
								pb.openDialog({
									title: 'Record:',
									content: 'No input streams selected for this NVR.'
								});
								return;
							}
						
							var tarr = this.core.finalunitlist;
							var tstr = '';
							
							for (var k=0; k<tarr.length; k++){
								tstr += '<option value="'+tarr[k].ipaddr+'">'+tarr[k].nickname+'</option>';
							}
							
							pb.openDialog({
								title: 'Record:',
								content: 'Input Stream:<br /><select id="selectSource">'+tstr+'</select>'+
									'<br />Time to Record:<br />'+
									'<input type="number" id="dvrplayerwidgetrecordlength" value="45" size="4" maxlength="4"></input>&nbsp;Minute(s)'+
									'<br />Recording Description:<br />'+
									'<input type="text" id="dvrplayerwidgetrecordname" value=""></input>',
								onConfirm: function(){
									
									var unitip = $("#selectSource").val();
									var rectime = $("#dvrplayerwidgetrecordlength").val();
									var recname = $("#dvrplayerwidgetrecordname").val();
									
									var tscript = '';
									tscript += 'record ';
									tscript += this.core.dvrip;
									tscript += ' ' + unitip;
									tscript += ' ' + rectime;
									tscript += ' ' + recname;
									
									$.post("/scripts/index.php", {executescript: "yes", script: tscript});	
											
									pb.closeDialog();
									
								}.bind(this)
							});
					
						}
					
					}.bind(this));
					
			}
			else {
				
				this.setSize(270,300);
			
				this.parts[1].element.innerHTML = '';
			
				this.parts[3].element.innerHTML = '<img src="images/widgets/play.png" class="item_child"\
					style="cursor: pointer; width: 100%; height: 100%; min-height: 30px;" />';
				this.parts[4].element.innerHTML = '<img src="images/widgets/stop2.png" class="item_child"\
					style="cursor: pointer; width: 100%; height: 100%; min-height: 30px;" />';
				this.parts[5].element.innerHTML = '<img src="images/widgets/record.png" class="item_child"\
					style="cursor: pointer; width: 100%; height: 100%; min-height: 30px;" />';
					
				this.doubleclick = function(){
				
					pb.openDialog({
						title: '<span style="font-size: 21px;">Widget Properties:</span>',
						content: '<br>Select an NVR:<br>\
									<div id="'+this.core.id+'-main-dvr" class="scrollerpane" style="width: 645px; height: 150px;\
										background: grey; overflow-y: scroll;">\
									</div><br>\
									Associated Encoders:<br>\
									Series:  <select id="encoder-series-select">\
										<option value="N1000">N1000</option>\
										<option value="N2000">N2000</option>\
										<option value="N3000">N3000</option>\
									</select><br><br>\
									<div id="'+this.core.id+'-dvr-encoder-list" class="scrollerpane" style="width: 645px;\
										height: 150px; background: grey; overflow-y: scroll;"></div>',
						style: {
							width: '700px', 
							height: '500px', 
							background: 'black',
							color: 'rgb(238, 238, 238)',
							margin: '15px',
							'border-radius': '15px',
							padding: '20px'
						},
						init: function(){
						
							var str = '';
							
							for (var i=0; i<this.dvrunitlist.length; i++){
							
								if (this.dvrunitlist[i].CommExpired !== '0') var tsr = ' color: red !important;';
										else tsr = '';
								
								str += '<div style="display: inline-block; '+tsr+'"><input class="dvr-selection" type="checkbox" id="'+this.dvrunitlist[i].ipaddr+'_checkbox" />&nbsp;'+
									this.dvrunitlist[i].nickname+'</div><br>';
									
								$(document.getElementById(this.dvrunitlist[i].ipaddr+'_checkbox')).prop('checked', true);
								
							}
							
							$('#'+this.core.id+'-main-dvr')[0].innerHTML = str;
							
							if (this.core.dvrip) $(document.getElementById(this.core.dvrip+'_checkbox')).prop('checked', true);
							
							var t1 = 'N1';
							var t2 = 'VR';
							
							if (this.series == 'N2000'){
								t1 = 'N2';
							}
							if (this.series == 'N3000'){
								t1 = 'N3';
							}
							
							str = '';
							
							for (var i=0; i<this.txunitlist.length; i++){
								
								if (this.txunitlist[i].CommExpired !== '0') var tsr = ' color: red !important;';
								else tsr = '';
								
								if (this.txunitlist[i].serialnumber.indexOf(t1) !== -1 || 
										this.txunitlist[i].serialnumber.indexOf('VR') !== -1)
								str += '<div style="display: inline-block; '+tsr+'"><input type="checkbox" class="dvr-encoder-unit-checkbox" id="'+this.txunitlist[i].ipaddr+'_checkbox" />&nbsp;'+
									this.txunitlist[i].nickname+'</div><br>';
									
							}
							
							for (var i=0; i<this.wpunitlist.length; i++){
								
								if (this.wpunitlist[i].CommExpired !== '0') var tsr = ' color: red !important;';
								else tsr = '';
								
								if (this.wpunitlist[i].sn.indexOf(t1) !== -1 || 
										this.wpunitlist[i].sn.indexOf('VR') !== -1)
								str += '<div style="display: inline-block; '+tsr+'"><input type="checkbox" class="dvr-encoder-unit-checkbox" id="'+this.wpunitlist[i].ipaddr+'_checkbox" />&nbsp;'+
									this.wpunitlist[i].nickname+'</div><br>';
									
							}
							
							$('#'+this.core.id+'-dvr-encoder-list')[0].innerHTML = str;
							
							$('.dvr-selection').click(function(){
								$('.dvr-selection').prop('checked', false);
								$(this).prop('checked', true);
							});
							
							for (var i=0; i<this.core.finalunitlist.length; i++){
								
								$(document.getElementById(this.core.finalunitlist[i].ipaddr+'_checkbox')).prop('checked', true);
								
							}
							
							$('#encoder-series-select').change(function(){
							
								$('.dvr-encoder-unit-checkbox').prop('checked', false);
							
								this.series = $('#encoder-series-select').val();
								
								var t1 = 'N1';
								var t2 = 'VR';
								
								if (this.series == 'N2000'){
									t1 = 'N2';
								}
								if (this.series == 'N3000'){
									t1 = 'N3';
								}
								
								str = '';
								
								for (var i=0; i<this.txunitlist.length; i++){
									
									if (this.txunitlist[i].CommExpired !== '0') var tsr = ' color: red !important;';
									else tsr = '';
									
									if (this.txunitlist[i].serialnumber.indexOf(t1) !== -1 || 
										this.txunitlist[i].serialnumber.indexOf('VR') !== -1)
											str += '<div style="display: inline-block; '+tsr+'"><input type="checkbox" class="dvr-encoder-unit-checkbox" id="'+this.txunitlist[i].ipaddr+'_checkbox" />&nbsp;'+
												this.txunitlist[i].nickname+'</div><br>';
										
								}
								
								for (var i=0; i<this.wpunitlist.length; i++){
									
									if (this.wpunitlist[i].CommExpired !== '0') var tsr = ' color: red !important;';
									else tsr = '';
									
									if (this.wpunitlist[i].sn.indexOf(t1) !== -1 || 
										this.wpunitlist[i].sn.indexOf('VR') !== -1)
											str += '<div style="display: inline-block; '+tsr+'"><input type="checkbox" class="dvr-encoder-unit-checkbox" id="'+this.wpunitlist[i].ipaddr+'_checkbox" />&nbsp;'+
												this.wpunitlist[i].nickname+'</div><br>';
										
								}
								
								$('#'+this.core.id+'-dvr-encoder-list')[0].innerHTML = str;
								
								for (var i=0; i<this.core.finalunitlist.length; i++){
									
									$(document.getElementById(this.core.finalunitlist[i].ipaddr+'_checkbox')).prop('checked', true);
									
								}
								
							}.bind(this));
							
							$('#encoder-series-select').val(this.core.series);
							
						}.bind(this),
						onCancel: function(){pb.closeDialog();},
						onConfirm: function(){
							
							var dvrip = 'all';
							var dvrname = '';
							for (var pk=0; pk<this.dvrunitlist.length; pk++){
							
								if ($(document.getElementById(this.dvrunitlist[pk].ipaddr+'_checkbox')).prop('checked')) dvrip = this.dvrunitlist[pk].ipaddr;
								if ($(document.getElementById(this.dvrunitlist[pk].ipaddr+'_checkbox')).prop('checked')) dvrname = this.dvrunitlist[pk].nickname;
								
							}
							
							var tulist = [];
							for (var pk=0; pk<this.txunitlist.length; pk++){
							
								if ($(document.getElementById(this.txunitlist[pk].ipaddr+'_checkbox')).prop('checked')) tulist.push(this.txunitlist[pk]);
								
							}
							for (var pk=0; pk<this.wpunitlist.length; pk++){
							
								if ($(document.getElementById(this.wpunitlist[pk].ipaddr+'_checkbox')).prop('checked')) tulist.push(this.wpunitlist[pk]);
								
							}

							this.core.series = this.series;
							this.core.dvrip = dvrip;
							this.core.dvrname = dvrname;
							this.core.finalunitlist = tulist;
							console.log('closing...');
							pb.closeDialog();
							
						}.bind(this)
					
					});

				};
				
			}
		}
	});
	twidget.core.dvrlist = [];
	twidget.core.dvrplaylist = [];
	twidget.core.dvrip = false;
	twidget.core.dvrname = false;
	twidget.core.series = 'N1000';
	twidget.core.finalunitlist = [];
	twidget.core.downoffset = 0;

	twidget.core.statusUpdate = function(statusobj){
		
		if (statusobj.isplay === '0000000000'){
			this.parts[3].element.innerHTML = '<img src="images/widgets/play.png" class="item_child"\
					style="cursor: pointer; width: 100%; height: 100%; min-height: 30px;" />';
		}
		else {
			this.parts[3].element.innerHTML = '<img src="images/widgets/play2.png" class="item_child"\
					style="cursor: pointer; width: 100%; height: 100%; min-height: 30px;" />';
		}
		
		if (statusobj.isrec === '0000000000'){
			this.parts[5].element.innerHTML = '<img src="images/widgets/record.png" class="item_child"\
					style="cursor: pointer; width: 100%; height: 100%; min-height: 30px;" />';
		}
		else {
			this.parts[5].element.innerHTML = '<img src="images/widgets/record2.png" class="item_child"\
					style="cursor: pointer; width: 100%; height: 100%; min-height: 30px;" />';
		}
		
		if (statusobj.isplay !== '0000000000' || statusobj.isrec !== '0000000000'){
			this.parts[4].element.innerHTML = '<img src="images/widgets/stop.png" class="item_child"\
					style="cursor: pointer; width: 100%; height: 100%; min-height: 30px;" />';
		}
		else {
			this.parts[4].element.innerHTML = '<img src="images/widgets/stop2.png" class="item_child"\
					style="cursor: pointer; width: 100%; height: 100%; min-height: 30px;" />';
		}
		
		
	}
	
	var tpart = new WidgetPart({
		widget: twidget,
		style: 'position: absolute; z-index: 1000000;overflow-y: auto; left: 0%; top: 0%;\
			height: 100%; width: 100%;',
		className: 'item_child ',
		elementHTML: '<div class="item_child" style="cursor: default; font-size: 18px;\
			width: 100%; height: 100%; font-family: time; color: white; text-align: center;">\
			<br>NVR<br>Widget</div>' 
	});
	tpart.core.downoffset = 0;
	twidget.addPart(tpart);
	
	var tpart = new WidgetPart({
		widget: twidget, 
		style: 'color: white; position: absolute; left: 0%; top: 0%;\
			height: 10%; width: 82%; z-index: 999999; font-size: 100%;', 
		className: 'item_child'
	});
	twidget.addPart(tpart);
	
	var tpart = new WidgetPart({
		widget: twidget, 
		style: 'display: none; position: absolute; left: 80%; top: 100%;\
			height: 10%; width: 10%; z-index: 999999; font-size: 100%;', 
		className: 'moveable',
		elementHTML: '<img src="images/widgets/goforward.png" class="item_child"\
			style="cursor: pointer; position: absolute; left: 0%;\
			top: 0%; height: 100%; min-height: 50px;" />'
	});
	twidget.addPart(tpart);
	
	var tpart = new WidgetPart({
		widget: twidget, 
		style: 'display: block; height: 14%; position: absolute; left: 49%; top: 83%; width: 14%;\
				z-index: 999999; font-size: 100%;', 
		className: 'item_child'
	});
	twidget.addPart(tpart);
	
	var tpart = new WidgetPart({
		widget: twidget, 
		style: 'display: block; height: 14%; position: absolute; left: 63%; top: 83%; width: 14%;\
				z-index: 999999; font-size: 100%;', 
		className: 'item_child '
	});
	twidget.addPart(tpart);
	
	var tpart = new WidgetPart({
		widget: twidget, 
		style: 'display: block; height: 14%; position: absolute; left: 77%; top: 83%; width: 14%;\
				z-index: 999999; font-size: 100%;', 
		className: 'item_child'
	});
	twidget.addPart(tpart);
	
	var tpart = new WidgetPart({
		widget: twidget, 
		style: 'display: none; position: absolute; left: 0%; top: 82%; height: 10%;\
				z-index: 999999; font-size: 100%;', 
		className: 'item_child'
	});
	twidget.addPart(tpart);
	
	var tpart = new WidgetPart({
		widget: twidget, 
		style: 'display: none; position: absolute; left: 0%; top: 0%; width: 100%; height: 100%;\
				overflow: hidden; z-index: 9999999; font-size: 100%; background: rgba(255,255,255,0.3);', 
		className: 'item_child',
		elementHTML: '<img src="images/widgets/preload.GIF" style="width: 64px; height: 64px;\
			position: absolute; left: calc(50% - 32px); top: calc(50% - 32px);" \>'
	});
	twidget.addPart(tpart);
	
	return twidget;
}