
function wizardStart(){

	$('#thebody').css({'overflow': 'auto'});
	$('#wizard_ntouch').center();
	if (parseInt($('#wizard_ntouch').css('left')) < 490) $('#wizard_ntouch').css({left: '490px'});
	if (parseInt($('#wizard_ntouch').css('top')) < 77) $('#wizard_ntouch').css({top: '77px'});
	
	window.wizardopen = true;

	window.wizardproject = {};
	window.ntouchbuttonl = window.ntouchbuttonr = false;
	
	wizardproject.panels = [];
	wizardproject.panels.push({
		id: 'ntouch-panel-1', ind: 0,
		buttoncommands: ['','','','','','','',''],
		buttontexts: ['','','','','','','',''],
		buttonstates: ['0','0','0','0','0','0','0','0'],
		buttonsource: 'images/buttons/Buttons-1.png',
		buttonsource2: 'images/buttons/Inactive-Button-2.png',
		bgsource: 'images/ntouch/white.gif',
		font: 'Georgia',
		fontsize: '15px',
		fontcolor: 'black'
	});
	wizardproject.currentpanelindex = 0;
	wizardproject.name = false;
	wizardproject.script = '';
	wizardproject.defaultpanel = 0;
	
	var myColor = new jscolor.color($('#wizard-bgColorSelector')[0]);
	myColor.fromRGB(0,0,0);
	
	$('#wizard-bgColorSelector')[0].onchange = $('#wizard-bgColorSelector')[0].onmouseup = function(){
		for (var j=1; j<9; j++){
			var tele = $('#'+wizardproject.panels[wizardproject.currentpanelindex].id).find('#button'+j)[0];
			$(tele).css({'color': '#'+myColor.toString()+' !important'});
		}
	}
	
	$('#ntouchmeditor').center();
	
	ntouchleditor.on('change', function(){
		switch (ntouchbuttonl){
			case 1:
				var ind = wizardproject.currentpanelindex;
				var tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[0] = ntouchleditor.getValue();
				break;
			case 2:
				var ind = wizardproject.currentpanelindex;
				var tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[1] = ntouchleditor.getValue();
				break;
			case 3:
				var ind = wizardproject.currentpanelindex;
				var tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[2] = ntouchleditor.getValue();
				break;
			case 4:
				var ind = wizardproject.currentpanelindex;
				var tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[3] = ntouchleditor.getValue();
				break;
			default:
				break;
		}
	});
	
	ntouchreditor.on('change', function(){
		switch (ntouchbuttonr){
			case 5:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[4] = ntouchreditor.getValue();
				break;
			case 6:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[5] = ntouchreditor.getValue();
				break;
			case 7:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[6] = ntouchreditor.getValue();
				break;
			case 8:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[7] = ntouchreditor.getValue();
				break;
			default:
				break;
		}
	});
	
	$('#ntouchleditor-textinput').on('input propertychange paste keyup', function(){
		switch (ntouchbuttonl){
			case 1:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttontexts[0] = $('#ntouchleditor-textinput').val();//.substring(0,7);
				$('#'+tstr).find('#button1text').html(wizardproject.panels[ind].buttontexts[0]);
				break;
			case 2:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttontexts[1] = $('#ntouchleditor-textinput').val();//.substring(0,7);
				$('#'+tstr).find('#button2text').html(wizardproject.panels[ind].buttontexts[1]);
				break;
			case 3:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttontexts[2] = $('#ntouchleditor-textinput').val();//.substring(0,7);
				$('#'+tstr).find('#button3text').html(wizardproject.panels[ind].buttontexts[2]);
				break;
			case 4:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttontexts[3] = $('#ntouchleditor-textinput').val();//.substring(0,7);
				$('#'+tstr).find('#button4text').html(wizardproject.panels[ind].buttontexts[3]);
				break;
			default:
				break;
		}
	});
	
	$('#ntouchreditor-textinput').on('input propertychange paste keyup', function(){
		switch (ntouchbuttonr){
			case 5:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttontexts[4] = $('#ntouchreditor-textinput').val();//.substring(0,7);
				$('#'+tstr).find('#button5text').html(wizardproject.panels[ind].buttontexts[4]);
				break;
			case 6:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttontexts[5] = $('#ntouchreditor-textinput').val();//.substring(0,7);
				$('#'+tstr).find('#button6text').html(wizardproject.panels[ind].buttontexts[5]);
				break;
			case 7:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttontexts[6] = $('#ntouchreditor-textinput').val();//.substring(0,7);
				$('#'+tstr).find('#button7text').html(wizardproject.panels[ind].buttontexts[6]);
				break;
			case 8:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttontexts[7] = $('#ntouchreditor-textinput').val();//.substring(0,7);
				$('#'+tstr).find('#button8text').html(wizardproject.panels[ind].buttontexts[7]);
				break;
			default:
				break;
		}
	});
	
	//$('#ntouchreditor-textinput').on('input propertychange paste', function(){
	if (0) ntouchreditor.on('change', function(){
		switch (ntouchbuttonr){
			case 5:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttontexts[4] = $('#ntouchreditor-textinput').val();//.substring(0,7);
				$('#'+tstr).find('#button5text').html(wizardproject.panels[ind].buttontexts[4]);
				break;
			case 6:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttontexts[5] = $('#ntouchreditor-textinput').val();//.substring(0,7);
				$('#'+tstr).find('#button6text').html(wizardproject.panels[ind].buttontexts[5]);
				break;
			case 7:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttontexts[6] = $('#ntouchreditor-textinput').val();//.substring(0,7);
				$('#'+tstr).find('#button7text').html(wizardproject.panels[ind].buttontexts[6]);
				break;
			case 8:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttontexts[7] = $('#ntouchreditor-textinput').val();//.substring(0,7);
				$('#'+tstr).find('#button8text').html(wizardproject.panels[ind].buttontexts[7]);
				break;
			default:
				break;
		}
	});

	$('#wizard_step2').fadeIn();
	
	$('.ntouchbutton').css({cursor: 'pointer'});
	$('.ntouchbutton').hover(function(){
		$(this).stop().animate({opacity: 1.0}, 300);
	},function(){
		$(this).stop().animate({opacity: 0.0}, 300);
	});
	$('#ntouchopen, #ntouchsave, #ntouchtoeditor, #ntouchstyle, #ntouchsetup, #ntouchhelp, #ntouchunits').hover(function(){
		$(this).stop().animate({color: 'black', 'background-color': 'white'}, 300);
	},function(){
		$(this).stop().animate({color: 'white', 'background-color': 'black'}, 300);
	});
	$('#ntouchhelp_wizard, #ntouchhelp_module, #ntouchhelp_command').hover(function(){
		$(this).stop().animate({color: 'black', 'background-color': 'white'}, 300);
	},function(){
		$(this).stop().animate({color: 'white', 'background-color': 'rgb(51,51,51)'}, 300);
	});
	
	$('#ntouchsetup').click(function(){
		$('#ntouchmeditor').show();
		$('#ntouchmeditorbg').show();
		ntouchmeditor.setValue(wizardproject.script, 1);
		$('#ntouchmeditor_code').find('.ace_text-input').focus();
		ntouchmeditor.navigateFileEnd();
	});
	
	$('#ntouchhelp').click(function(){
		if (!$(this).data('open')){
			$('#ntouchhelp_wizard').show();
			$('#ntouchhelp_module').show();
			$('#ntouchhelp_command').show();
			$(this).data('open', true);
		}
		else {
			$('#ntouchhelp_wizard').hide();
			$('#ntouchhelp_module').hide();
			$('#ntouchhelp_command').hide();
			$(this).data('open', false);
		}
	});
	
	$('#ntouchhelp_wizard').click(function(){
		if (standalone){
			event.preventDefault();
			websocket.emit('message', {command: 'dirname'}, function(dir){
				gui.Shell.openItem(dir+'manual/WizardTutorial.pdf');
			});
			return;
		}			
		var win = window.open('manual/WizardTutorial.pdf', '_blank');
		win.focus();
	});
	$('#ntouchhelp_module').click(function(){
		if (standalone) gui.Window.open('modules.php', {
				title: 'SVSi Control Modules', show: true, toolbar : false, frame: true,
				icon: 'icons/icon.png', width: 1330, 
				height: 770, position: 'center'
			});
		else {
			var win = window.open('modules.php', '_blank');
			win.focus();
		}
	});
	$('#ntouchhelp_command').click(function(){
		if (standalone){
			event.preventDefault();
			websocket.emit('message', {command: 'dirname'}, function(dir){
				gui.Shell.openItem(dir+'manual/CombinedNSeriesAPICommandList.pdf');
			});
			return;
		}
		var win = window.open('manual/CombinedNSeriesAPICommandList.pdf', '_blank');
		win.focus();
	});
	
	$('#ntouchmeditor-savebutton').click(function(){
		wizardproject.script = ntouchmeditor.getValue();
		$('#ntouchmeditor').hide();
		$('#ntouchmeditorbg').hide();
	});
	
	$('#ntouchmeditor-cancelbutton').click(function(){
		$('#ntouchmeditor').hide();
		$('#ntouchmeditorbg').hide();
	});
	
	$('#ntouchdefaultlabel').click(function(){
		wizardproject.defaultpanel = wizardproject.currentpanelindex;
		pb.openDialog({
			content: '<div style="margin: 15px;">This panel is now the project\'s default panel.</div>',
			style: {
				height: '120px',
				'border-radius': '15px'
			},
			confirm: false,
			cancel: 'ok'
		});
	});
	
	$('#ntouchopen').click(function(){
		server({
			opts: {
				command: 'read_projectnames'
			}, 
			callback: function(data){
				var wizardsel = '<select id="wizardselect">';
				var list = json_decode(data);
				for (var i=0; i<list.length; i++){
					wizardsel += '<option value="'+list[i].puid+'">'+list[i].name+'</option>';
				}
				wizardsel += '</select>';
				pb.openDialog({
					content: '<div style="margin: 15px;">Select the project to load: '+wizardsel+'</div>',
					style: {'border-radius': '15px', height: '100px'},
					onConfirm: function(){
						nTouchLoad($('#wizardselect').val());
						pb.closeDialog();
					},
					onCancel: function(){
						pb.closeDialog();
					}
				});
			}
		});
	});
	
	$('#ntouchtoeditor').click(function(){
		pb.openDialog({
			content: '<div style="margin: 15px">Once you send the project to the editor it cannot be loaded back into the wizard, continue?</div>',
			style: {'border-radius': '15px', height: '130px'},
			onConfirm: function(){
				pb.closeDialog();
				setTimeout(function(){nTouchToEditor();}, 2000);
			},
			onCancel: function(){
				pb.closeDialog();
			}
		});	
	});
	
	$('#ntouchminus').click(function(){
		if (wizardproject.panels.length > 1){
			pb.openDialog({
				content: 'Are you sure you want to delete this panel?',
				onConfirm: function(){
					deleteWizardPanel();
					pb.closeDialog();
				},
				onCancel: function(){
					pb.closeDialog();
				}
			});
		}
		else {
			pb.openDialog({
				content: 'You must have at least one panel in the project.',
				confirm: false,
				cancel: 'ok'
			});
		}
	});
	
	$('#ntouchplus').click(function(){
		
		switch (ntouchbuttonl){
			case 1:
				var ind = wizardproject.currentpanelindex;
				var tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[0] = ntouchleditor.getValue();
				break;
			case 2:
				var ind = wizardproject.currentpanelindex;
				var tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[1] = ntouchleditor.getValue();
				break;
			case 3:
				var ind = wizardproject.currentpanelindex;
				var tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[2] = ntouchleditor.getValue();
				break;
			case 4:
				var ind = wizardproject.currentpanelindex;
				var tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[3] = ntouchleditor.getValue();
				break;
			default:
				break;
		}
		switch (ntouchbuttonr){
			case 5:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[4] = ntouchreditor.getValue();
				break;
			case 6:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[5] = ntouchreditor.getValue();
				break;
			case 7:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[6] = ntouchreditor.getValue();
				break;
			case 8:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[7] = ntouchreditor.getValue();
				break;
			default:
				break;
		}
		
		$('#ntouchleditor_code').val('');
		$('#ntouchleditor-select').val('tcpclient');
		$('#ntouchleditor-state').val('0');
		$('#ntouchleditor-textinput').val('');
		
		$('#ntouchreditor_code').val('');
		$('#ntouchreditor-select').val('tcpclient');
		$('#ntouchreditor-state').val('0');
		$('#ntouchreditor-textinput').val('');
		
		$('#ntouchleditor').fadeOut();
		$('#ntouchreditor').fadeOut();
		$('.ntouchimgr').fadeOut();
		$('.ntouchimgl').fadeOut();
		
		$('#ntouchleditor-selectnum').hide();
		$('#ntouchreditor-selectnum').hide();
	
		var ind = wizardproject.panels.length;
		wizardproject.panels.push({
			id: 'ntouch-panel-'+(ind+1),
			ind: ind,
			buttoncommands: ['','','','','','','',''],
			buttontexts: ['','','','','','','',''],
			buttonstates: ['0','0','0','0','0','0','0','0'],
			buttonsource: 'images/buttons/Buttons-1.png',
			buttonsource2: 'images/buttons/Inactive-Button-2.png',
			bgsource: 'images/ntouch/white.gif',
			font: 'Georgia',
			fontsize: '15px',
			fontcolor: 'black'
		});
		$('#ntouchlabeltext').fadeOut();
		$('#'+wizardproject.panels[wizardproject.currentpanelindex].id).fadeOut(400, function(){
		
			$('#ntouchlabeltext').html('Panel-'+(ind+1));
			$('#ntouchlabeltext').fadeIn();
		
			wizardproject.currentpanelindex = ind;
			
			$('#ntouch-panels').append('\
				<div id="ntouch-panel-'+(ind+1)+'" class="ntouch-panel" style="width: 240px; height: 320px;\
					background: white; left: 0px; top: 0px; position: absolute; z-index: 40; display: none;">\
					<div id="button1" style="width: 90px; height: 40px; left: 8px;\
						top: 19px; position: absolute; white-space: nowrap;">\
						<img id="button1img" src="images/buttons/Buttons-1.png"\
							style="width: 100%; height: 100%; position: absolute;" />\
						<span id="button1text" style="position: absolute; top: 10px; left: 0px; white-space: nowrap;\
							width: 100%; text-align: center;"></span>\
					</div>\
					<div id="button2" style="width: 90px; height: 40px; left: 8px;\
						top: 102px; position: absolute; white-space: nowrap;">\
						<img id="button2img" src="images/buttons/Buttons-1.png"\
							style="width: 100%; height: 100%; position: absolute;" />\
						<span id="button2text" style="position: absolute; top: 10px; left: 0px; white-space: nowrap;\
							width: 100%; text-align: center;"></span>\
					</div>\
					<div id="button3" style="width: 90px; height: 40px; left: 8px;\
						top: 183px; position: absolute; white-space: nowrap;">\
						<img id="button3img" src="images/buttons/Buttons-1.png"\
							style="width: 100%; height: 100%; position: absolute;" />\
						<span id="button3text" style="position: absolute; top: 10px; left: 0px; white-space: nowrap;\
							width: 100%; text-align: center;"></span>\
					</div>\
					<div id="button4" style="width: 90px; height: 40px; left: 8px;\
						top: 262px; position: absolute; white-space: nowrap;">\
						<img id="button4img" src="images/buttons/Buttons-1.png"\
							style="width: 100%; height: 100%; position: absolute;" />\
						<span id="button4text" style="position: absolute; top: 10px; left: 0px; white-space: nowrap;\
							width: 100%; text-align: center;"></span>\
					</div>\
					<div id="button5" style="width: 90px; height: 40px; left: 142px;\
						top: 19px; position: absolute; white-space: nowrap;">\
						<img id="button5img" src="images/buttons/Buttons-1.png"\
							style="width: 100%; height: 100%; position: absolute;" />\
						<span id="button5text" style="position: absolute; top: 10px; left: 0px; white-space: nowrap;\
							width: 100%; text-align: center;"></span>\
					</div>\
					<div id="button6" style="width: 90px; height: 40px; left: 142px;\
						top: 102px; position: absolute; white-space: nowrap;">\
						<img id="button6img" src="images/buttons/Buttons-1.png"\
							style="width: 100%; height: 100%; position: absolute;" />\
						<span id="button6text" style="position: absolute; top: 10px; left: 0px; white-space: nowrap;\
							width: 100%; text-align: center;"></span>\
					</div>\
					<div id="button7" style="width: 90px; height: 40px; left: 142px;\
						top: 183px; position: absolute; white-space: nowrap;">\
						<img id="button7img" src="images/buttons/Buttons-1.png"\
							style="width: 100%; height: 100%; position: absolute;" />\
						<span id="button7text" style="position: absolute; top: 10px; left: 0px; white-space: nowrap;\
							width: 100%; text-align: center;"></span>\
					</div>\
					<div id="button8" style="width: 90px; height: 40px; left: 142px;\
						top: 262px; position: absolute; white-space: nowrap;">\
						<img id="button8img" src="images/buttons/Buttons-1.png"\
							style="width: 100%; height: 100%; position: absolute;" />\
						<span id="button8text" style="position: absolute; top: 10px; left: 0px; white-space: nowrap;\
							width: 100%; text-align: center;"></span>\
					</div>\
				</div>\
			');
			$('#'+wizardproject.panels[wizardproject.currentpanelindex].id).fadeIn();
		});
	});
	
	$('#ntouchdiv1').click(function(){
		$('.ntouchimgl').fadeOut();
		$('.ntouchimgr').fadeOut();
		$('#ntouchreditor').fadeOut();
		$('#ntouchleditor').fadeIn();
		$('#'+this.id.replace('div','img')).fadeIn();
		ntouchbuttonl = 1;
		var ind = wizardproject.currentpanelindex;		
		$('#ntouchleditor-textinput').val(wizardproject.panels[ind].buttontexts[0]);
		ntouchleditor.setValue(wizardproject.panels[ind].buttoncommands[0], 1);
		$('#ntouchleditor-state').val(wizardproject.panels[ind].buttonstates[0]);
		$('#ntouchleditor-select').val('tcpclient');
		$('#ntouchleditor_code').find('.ace_text-input').focus();
		ntouchleditor.navigateFileEnd();
	});
	$('#ntouchdiv2').click(function(){
		$('.ntouchimgl').fadeOut();
		$('.ntouchimgr').fadeOut();
		$('#ntouchreditor').fadeOut();
		$('#ntouchleditor').fadeIn();
		$('#'+this.id.replace('div','img')).fadeIn();
		ntouchbuttonl = 2;
		var ind = wizardproject.currentpanelindex;		
		$('#ntouchleditor-textinput').val(wizardproject.panels[ind].buttontexts[1]);
		ntouchleditor.setValue(wizardproject.panels[ind].buttoncommands[1], 1);
		$('#ntouchleditor-state').val(wizardproject.panels[ind].buttonstates[1]);
		$('#ntouchleditor-select').val('tcpclient');
		$('#ntouchleditor_code').find('.ace_text-input').focus();
		ntouchleditor.navigateFileEnd();
	});
	$('#ntouchdiv3').click(function(){
		$('.ntouchimgl').fadeOut();
		$('.ntouchimgr').fadeOut();
		$('#ntouchreditor').fadeOut();
		$('#ntouchleditor').fadeIn();
		$('#'+this.id.replace('div','img')).fadeIn();
		ntouchbuttonl = 3;
		var ind = wizardproject.currentpanelindex;		
		$('#ntouchleditor-textinput').val(wizardproject.panels[ind].buttontexts[2]);
		ntouchleditor.setValue(wizardproject.panels[ind].buttoncommands[2], 1);
		$('#ntouchleditor-state').val(wizardproject.panels[ind].buttonstates[2]);
		$('#ntouchleditor-select').val('tcpclient');
		$('#ntouchleditor_code').find('.ace_text-input').focus();
		ntouchleditor.navigateFileEnd();
	});
	$('#ntouchdiv4').click(function(){
		$('.ntouchimgl').fadeOut();
		$('.ntouchimgr').fadeOut();
		$('#ntouchreditor').fadeOut();
		$('#ntouchleditor').fadeIn();
		$('#'+this.id.replace('div','img')).fadeIn();
		ntouchbuttonl = 4;
		var ind = wizardproject.currentpanelindex;		
		$('#ntouchleditor-textinput').val(wizardproject.panels[ind].buttontexts[3]);
		ntouchleditor.setValue(wizardproject.panels[ind].buttoncommands[3], 1);
		$('#ntouchleditor-state').val(wizardproject.panels[ind].buttonstates[3]);
		$('#ntouchleditor-select').val('tcpclient');
		$('#ntouchleditor_code').find('.ace_text-input').focus();
		ntouchleditor.navigateFileEnd();
	});
	$('#ntouchdiv5').click(function(){
		$('.ntouchimgr').fadeOut();
		$('.ntouchimgl').fadeOut();
		$('#ntouchleditor').fadeOut();
		$('#ntouchreditor').fadeIn();
		$('#'+this.id.replace('div','img')).fadeIn();
		ntouchbuttonr = 5;
		var ind = wizardproject.currentpanelindex;		
		$('#ntouchreditor-textinput').val(wizardproject.panels[ind].buttontexts[4]);
		ntouchreditor.setValue(wizardproject.panels[ind].buttoncommands[4], 1);
		$('#ntouchreditor-state').val(wizardproject.panels[ind].buttonstates[4]);
		$('#ntouchreditor-select').val('tcpclient');
		$('#ntouchreditor_code').find('.ace_text-input').focus();
		ntouchreditor.navigateFileEnd();
	});
	$('#ntouchdiv6').click(function(){
		$('.ntouchimgr').fadeOut();
		$('.ntouchimgl').fadeOut();
		$('#ntouchleditor').fadeOut();
		$('#ntouchreditor').fadeIn();
		$('#'+this.id.replace('div','img')).fadeIn();
		ntouchbuttonr = 6;
		var ind = wizardproject.currentpanelindex;		
		$('#ntouchreditor-textinput').val(wizardproject.panels[ind].buttontexts[5]);
		ntouchreditor.setValue(wizardproject.panels[ind].buttoncommands[5], 1);
		$('#ntouchreditor-state').val(wizardproject.panels[ind].buttonstates[5]);
		$('#ntouchreditor-select').val('tcpclient');
		$('#ntouchreditor_code').find('.ace_text-input').focus();
		ntouchreditor.navigateFileEnd();
	});
	$('#ntouchdiv7').click(function(){
		$('.ntouchimgr').fadeOut();
		$('.ntouchimgl').fadeOut();
		$('#ntouchleditor').fadeOut();
		$('#ntouchreditor').fadeIn();
		$('#'+this.id.replace('div','img')).fadeIn();
		ntouchbuttonr = 7;
		var ind = wizardproject.currentpanelindex;		
		$('#ntouchreditor-textinput').val(wizardproject.panels[ind].buttontexts[6]);
		ntouchreditor.setValue(wizardproject.panels[ind].buttoncommands[6], 1);
		$('#ntouchreditor-state').val(wizardproject.panels[ind].buttonstates[6]);
		$('#ntouchreditor-select').val('tcpclient');
		$('#ntouchreditor_code').find('.ace_text-input').focus();
		ntouchreditor.navigateFileEnd();
	});
	$('#ntouchdiv8').click(function(){
		$('.ntouchimgr').fadeOut();
		$('.ntouchimgl').fadeOut();
		$('#ntouchleditor').fadeOut();
		$('#ntouchreditor').fadeIn();
		$('#'+this.id.replace('div','img')).fadeIn();
		ntouchbuttonr = 8;
		var ind = wizardproject.currentpanelindex;		
		$('#ntouchreditor-textinput').val(wizardproject.panels[ind].buttontexts[7]);
		ntouchreditor.setValue(wizardproject.panels[ind].buttoncommands[7], 1);
		$('#ntouchreditor-state').val(wizardproject.panels[ind].buttonstates[7]);
		$('#ntouchreditor-select').val('tcpclient');
		$('#ntouchreditor_code').find('.ace_text-input').focus();
		ntouchreditor.navigateFileEnd();
	});
	
	$('#ntouchleditor-select').change(function(){
		if ($(this).val() == '!goto'){
			$('#ntouchleditor-selectnum').show();
			$('#ntouchleditor-selectnum').html('');
			for (var i=0; i<wizardproject.panels.length; i++){
				$('#ntouchleditor-selectnum').append('<option value="'+(i+1)+'">'+(i+1)+'</option>');
			}
		}
		else $('#ntouchleditor-selectnum').hide();
	});
	$('#ntouchreditor-select').change(function(){
		if ($(this).val() == '!goto'){
			$('#ntouchreditor-selectnum').show();
			$('#ntouchreditor-selectnum').html('');
			for (var i=0; i<wizardproject.panels.length; i++){
				$('#ntouchreditor-selectnum').append('<option value="'+(i+1)+'">'+(i+1)+'</option>');
			}
		}
		else $('#ntouchreditor-selectnum').hide();
	});
	$('#ntouchmeditor-select').change(function(){
		if ($(this).val() == '!goto'){
			$('#ntouchmeditor-selectnum').show();
			$('#ntouchmeditor-selectnum').html('');
			for (var i=0; i<wizardproject.panels.length; i++){
				$('#ntouchmeditor-selectnum').append('<option value="'+(i+1)+'">'+(i+1)+'</option>');
			}
		}
		else $('#ntouchmeditor-selectnum').hide();
	});
	
	$('#ntouchleditor-addbutton').click(function(){
	
		var ind = wizardproject.currentpanelindex;		
		var tobj = wizardproject.panels[ind];

		if ($('#ntouchleditor-select').val() == 'goto')
			$('#ntouchleditor_code').val($('#ntouchleditor_code').val()+'\n//Goto Panel '+$('#ntouchleditor-selectnum').val()+'\n\
gotopanel('+$('#ntouchleditor-selectnum').val()+');');

		switch (ntouchbuttonl){
			case 1:
				tobj.buttoncommands[0] = ntouchleditor.getValue();
				break;
			case 2:
				tobj.buttoncommands[1] = ntouchleditor.getValue();
				break;
			case 3:
				tobj.buttoncommands[2] = ntouchleditor.getValue();
				break;
			case 4:
				tobj.buttoncommands[3] = ntouchleditor.getValue();
				break;
			default:
				break;
		}

	});
	$('#ntouchreditor-addbutton').click(function(){
	
		var ind = wizardproject.currentpanelindex;		
		var tobj = wizardproject.panels[ind];

		if ($('#ntouchreditor-select').val() == 'goto')
			$('#ntouchreditor_code').val($('#ntouchreditor_code').val()+'\n//Goto Panel '+$('#ntouchreditor-selectnum').val()+'\n\
gotopanel('+$('#ntouchreditor-selectnum').val()+');');
		
		switch (ntouchbuttonr){
			case 5:
				tobj.buttoncommands[4] = ntouchreditor.getValue();
				break;
			case 6:
				tobj.buttoncommands[5] = ntouchreditor.getValue();
				break;
			case 7:
				tobj.buttoncommands[6] = ntouchreditor.getValue();
				break;
			case 8:
				tobj.buttoncommands[7] = ntouchreditor.getValue();
				break;
			default:
				break;
		}

	});
	$('#ntouchmeditor-addbutton').click(function(){
	
		var ind = wizardproject.currentpanelindex;		
		var tobj = wizardproject.panels[ind];
	
		if ($('#ntouchmeditor-select').val() == 'tcpclient')
			$('#ntouchmeditor_code').val($('#ntouchmeditor_code').val()+'\n//Send raw hex TCP data to a device\n\
//tcpclient(ip, port, messagedata);\n\
tcpclient("192.168.1.12", 50002, asciiToHex("getStatus"));');

		if ($('#ntouchmeditor-select').val() == 'udpclient')
			$('#ntouchmeditor_code').val($('#ntouchmeditor_code').val()+'\n//Send raw hex UDP data to a device\n\
//udpclient(ip, port, messagedata);\n\
udpclient("192.168.1.12", 50002, asciiToHex("getStatus"));');

		if ($('#ntouchmeditor-select').val() == 'goto')
			$('#ntouchmeditor_code').val($('#ntouchmeditor_code').val()+'\n//Goto Panel '+$('#ntouchleditor-selectnum').val()+'\n\
gotopanel('+$('#ntouchmeditor-selectnum').val()+');');

		switch (ntouchbuttonl){
			case 1:
				tobj.buttoncommands[0] = $('#ntouchmeditor_code').val();
				break;
			case 2:
				tobj.buttoncommands[1] = $('#ntouchmeditor_code').val();
				break;
			case 3:
				tobj.buttoncommands[2] = $('#ntouchmeditor_code').val();
				break;
			case 4:
				tobj.buttoncommands[3] = $('#ntouchmeditor_code').val();
				break;
			default:
				break;
		}

	});
	
	$('#ntouchleditor-state').change(function(){
	
		var ind = wizardproject.currentpanelindex;		
		var tobj = wizardproject.panels[ind];
		var tstr = wizardproject.panels[ind].id;
		
		if ($('#ntouchleditor-state').val() === '0'){
			switch (ntouchbuttonl){
				case 1:
					$('#'+tstr).find('#button1img').show();
					$('#'+tstr).find('#button1img').attr('src', tobj.buttonsource);
					wizardproject.panels[ind].buttonstates[0] = '0';
					break;
				case 2:
					$('#'+tstr).find('#button2img').show();
					$('#'+tstr).find('#button2img').attr('src', tobj.buttonsource);
					wizardproject.panels[ind].buttonstates[1] = '0';
					break;
				case 3:
					$('#'+tstr).find('#button3img').show();
					$('#'+tstr).find('#button3img').attr('src', tobj.buttonsource);
					wizardproject.panels[ind].buttonstates[2] = '0';
					break;
				case 4:
					$('#'+tstr).find('#button4img').show();
					$('#'+tstr).find('#button4img').attr('src', tobj.buttonsource);
					wizardproject.panels[ind].buttonstates[3] = '0';
					break;
				default:
					break;
			}
		}	
		if ($('#ntouchleditor-state').val() == '1'){
			switch (ntouchbuttonl) {
				case 1:
					$('#'+tstr).find('#button1img').show();
					$('#'+tstr).find('#button1img').attr('src', tobj.buttonsource2);
					wizardproject.panels[ind].buttonstates[0] = '1';
					break;
				case 2:
					$('#'+tstr).find('#button2img').show();
					$('#'+tstr).find('#button2img').attr('src', tobj.buttonsource2);
					wizardproject.panels[ind].buttonstates[1] = '1';
					break;
				case 3:
					$('#'+tstr).find('#button3img').show();
					$('#'+tstr).find('#button3img').attr('src', tobj.buttonsource2);
					wizardproject.panels[ind].buttonstates[2] = '1';
					break;
				case 4:
					$('#'+tstr).find('#button4img').show();
					$('#'+tstr).find('#button4img').attr('src', tobj.buttonsource2);
					wizardproject.panels[ind].buttonstates[3] = '1';
					break;
				default:
					break;
			}
		}	
		if ($('#ntouchleditor-state').val() == '-1'){
			switch (ntouchbuttonl) {
				case 1:
					$('#'+tstr).find('#button1img').attr('src', 'images/ntouch/blank.gif');
					wizardproject.panels[ind].buttonstates[0] = '-1';
					break;
				case 2:
					$('#'+tstr).find('#button2img').attr('src', 'images/ntouch/blank.gif');
					wizardproject.panels[ind].buttonstates[1] = '-1';
					break;
				case 3:
					$('#'+tstr).find('#button3img').attr('src', 'images/ntouch/blank.gif');
					wizardproject.panels[ind].buttonstates[2] = '-1';
					break;
				case 4:
					$('#'+tstr).find('#button4img').attr('src', 'images/ntouch/blank.gif');
					wizardproject.panels[ind].buttonstates[3] = '-1';
					break;
				default:
					break;
			}
		}	
		
	});
	
	$('#ntouchreditor-state').change(function(){
	
		var ind = wizardproject.currentpanelindex;		
		var tobj = wizardproject.panels[ind];
		var tstr = wizardproject.panels[ind].id;
		
		if ($('#ntouchreditor-state').val() == '0'){
			switch (ntouchbuttonr) {
				case 5:
					$('#'+tstr).find('#button5img').show();
					$('#'+tstr).find('#button5img').attr('src', tobj.buttonsource);
					wizardproject.panels[ind].buttonstates[4] = '0';
					break;
				case 6:
					$('#'+tstr).find('#button6img').show();
					$('#'+tstr).find('#button6img').attr('src', tobj.buttonsource);
					wizardproject.panels[ind].buttonstates[5] = '0';
					break;
				case 7:
					$('#'+tstr).find('#button7img').show();
					$('#'+tstr).find('#button7img').attr('src', tobj.buttonsource);
					wizardproject.panels[ind].buttonstates[6] = '0';
					break;
				case 8:
					$('#'+tstr).find('#button8img').show();
					$('#'+tstr).find('#button8img').attr('src', tobj.buttonsource);
					wizardproject.panels[ind].buttonstates[7] = '0';
					break;
				default:
					break;
			}
		}	
		if ($('#ntouchreditor-state').val() == '1'){
			switch (ntouchbuttonr) {
				case 5:
					$('#'+tstr).find('#button5img').show();
					$('#'+tstr).find('#button5img').attr('src', tobj.buttonsource2);
					wizardproject.panels[ind].buttonstates[4] = '1';
					break;
				case 6:
					$('#'+tstr).find('#button6img').show();
					$('#'+tstr).find('#button6img').attr('src', tobj.buttonsource2);
					wizardproject.panels[ind].buttonstates[5] = '1';
					break;
				case 7:
					$('#'+tstr).find('#button7img').show();
					$('#'+tstr).find('#button7img').attr('src', tobj.buttonsource2);
					wizardproject.panels[ind].buttonstates[6] = '1';
					break;
				case 8:
					$('#'+tstr).find('#button8img').show();
					$('#'+tstr).find('#button8img').attr('src', tobj.buttonsource2);
					wizardproject.panels[ind].buttonstates[7] = '1';
					break;
				default:
					break;
			}
		}	
		if ($('#ntouchreditor-state').val() == '-1'){
			switch (ntouchbuttonr) {
				case 5:
					$('#'+tstr).find('#button5img').attr('src', 'images/ntouch/blank.gif');
					wizardproject.panels[ind].buttonstates[4] = '-1';
					break;
				case 6:
					$('#'+tstr).find('#button6img').attr('src', 'images/ntouch/blank.gif');
					wizardproject.panels[ind].buttonstates[5] = '-1';
					break;
				case 7:
					$('#'+tstr).find('#button7img').attr('src', 'images/ntouch/blank.gif');
					wizardproject.panels[ind].buttonstates[6] = '-1';
					break;
				case 8:
					$('#'+tstr).find('#button8img').attr('src', 'images/ntouch/blank.gif');
					wizardproject.panels[ind].buttonstates[7] = '-1';
					break;
				default:
					break;
			}
		}	
		
	});
	
	$('#ntouchlarrow').click(function(){ntouchswitch('left');});
	$('#ntouchrarrow').click(function(){ntouchswitch('right');});
	$('#ntouchsave').click(function(){nTouchSave();});
	$('#ntouchstyle').click(function(){$('#ntouchstyler').animate({bottom: '0px'});});
	$('#ntouchstyleok').click(function(){$('#ntouchstyler').animate({bottom: '-200px'});});
	$('#ntouchstylereset').click(function(){
		$('#'+wizardproject.panels[wizardproject.currentpanelindex].id)[0].style.background = 'url(images/ntouch/white.gif)'; 
		$('#'+wizardproject.panels[wizardproject.currentpanelindex].id)[0].style.backgroundSize = '100% 100%';
		wizardproject.panels[wizardproject.currentpanelindex].bgsource = 'images/ntouch/white.gif';
		for (var j=1; j<9; j++){
			if (wizardproject.panels[wizardproject.currentpanelindex].buttonstates[j-1] == '0')
			$('#'+wizardproject.panels[wizardproject.currentpanelindex].id).find('#button'+j+'img')[0].src =
				'images/buttons/Buttons-1.png';
		}
		wizardproject.panels[wizardproject.currentpanelindex].buttonsource = 'images/buttons/Buttons-1.png';
		for (var j=1; j<9; j++){
			if (wizardproject.panels[wizardproject.currentpanelindex].buttonstates[j-1] == '1')
			$('#'+wizardproject.panels[wizardproject.currentpanelindex].id).find('#button'+j+'img')[0].src =
				'images/buttons/Inactive-Button-2.png';
		}
		wizardproject.panels[wizardproject.currentpanelindex].buttonsource2 = 'images/buttons/Inactive-Button-2.png';
	});
	
	$('#ntouchpanelbg')[0].addEventListener("click", function(e){
		this.value = null;
	});
	
	$('#ntouchpanelbg')[0].addEventListener("change", function(e){
		var file = this.files[0]; if (!file) return;
		sendFile(file, {command: 'image', puid: wizardproject.puid}, function(opts){
			wizardproject.puid = opts.puid;
			$('#'+wizardproject.panels[wizardproject.currentpanelindex].id)[0].style.background = 'url('+opts.filename+')'; 
			$('#'+wizardproject.panels[wizardproject.currentpanelindex].id)[0].style.backgroundSize = '100% 100%';
			wizardproject.panels[wizardproject.currentpanelindex].bgsource = opts.filename;
		});
	});
	
	$('#ntouchbuttonstate1bg')[0].addEventListener('click', function(e){
		this.value = null;
	});

	$('#ntouchbuttonstate1bg')[0].addEventListener('change', function(e){
		var file = this.files[0]; if (!file) return;
		sendFile(file, {command: 'image', puid: wizardproject.puid}, function(opts){
			wizardproject.puid = opts.puid;
			for (var j=1; j<9; j++){
				if (wizardproject.panels[wizardproject.currentpanelindex].buttonstates[j-1] == '0')
				$('#'+wizardproject.panels[wizardproject.currentpanelindex].id).find('#button'+j+'img')[0].src = opts.filename;
			}
			wizardproject.panels[wizardproject.currentpanelindex].buttonsource = opts.filename;
		});
	});
	
	$('#ntouchfontsize').change(function(){
		var ind = wizardproject.currentpanelindex;
		var tpanels = wizardproject.panels;
		var buttons = [];
		for (var j=1; j<9; j++){
			var tele = $('#'+tpanels[ind].id).find('#button'+j)[0];
			$(tele).css({'font-size': $('#ntouchfontsize').val() + 'px'});
		}
	});
	
	$('#ntouchfonttype').change(function(){
		var ind = wizardproject.currentpanelindex;
		var tpanels = wizardproject.panels;
		var buttons = [];
		for (var j=1; j<9; j++){
			var tele = $('#'+tpanels[ind].id).find('#button'+j)[0];
			$(tele).css({'font-family': $('#ntouchfonttype').val()});
		}
	});

}

var __ntouchswitching = false;
function ntouchswitch(dir){

	if (__ntouchswitching) return;
	var ind = wizardproject.currentpanelindex;
	var tobj = wizardproject.panels[ind];
	
		switch (ntouchbuttonl){
			case 1:
				var ind = wizardproject.currentpanelindex;
				var tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[0] = ntouchleditor.getValue();
				break;
			case 2:
				var ind = wizardproject.currentpanelindex;
				var tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[1] = ntouchleditor.getValue();
				break;
			case 3:
				var ind = wizardproject.currentpanelindex;
				var tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[2] = ntouchleditor.getValue();
				break;
			case 4:
				var ind = wizardproject.currentpanelindex;
				var tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[3] = ntouchleditor.getValue();
				break;
			default:
				break;
		}
		switch (ntouchbuttonr){
			case 5:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[4] = ntouchreditor.getValue();
				break;
			case 6:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[5] = ntouchreditor.getValue();
				break;
			case 7:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[6] = ntouchreditor.getValue();
				break;
			case 8:
				var ind = wizardproject.currentpanelindex, tstr = wizardproject.panels[ind].id;
				wizardproject.panels[ind].buttoncommands[7] = ntouchreditor.getValue();
				break;
			default:
				break;
		}

	if (dir == 'left'){
		
		if (wizardproject.currentpanelindex == 0) return;
		
		$('#ntouchlpane').fadeOut();
		$('#ntouchrpane').fadeOut();
		$('.ntouchimgr').fadeOut();
		$('.ntouchimgl').fadeOut();
		
		$('#ntouchlabeltext').fadeOut();
		$('#'+tobj.id).fadeOut(400, function(){
		
			$('#ntouchlabeltext').html('Panel-'+(wizardproject.currentpanelindex));
			$('#ntouchlabeltext').fadeIn();
		
			wizardproject.currentpanelindex -= 1;
			
			$('#'+wizardproject.panels[wizardproject.currentpanelindex].id).fadeIn();
			__ntouchswitching = false;
			$('#ntouchdiv1').click();
		});
		
	}
	if (dir == 'right'){
		
		if (wizardproject.currentpanelindex == wizardproject.panels.length - 1) return;
		
		$('#ntouchlpane').fadeOut();
		$('#ntouchrpane').fadeOut();
		$('.ntouchimgr').fadeOut();
		$('.ntouchimgl').fadeOut();
		
		$('#ntouchlabeltext').fadeOut();
		$('#'+tobj.id).fadeOut(400, function(){
		
			$('#ntouchlabeltext').html('Panel-'+(wizardproject.currentpanelindex+2));
			$('#ntouchlabeltext').fadeIn();
		
			wizardproject.currentpanelindex += 1;
			
			$('#'+wizardproject.panels[wizardproject.currentpanelindex].id).fadeIn();
			__ntouchswitching = false;
			$('#ntouchdiv1').click();
		});
		
	}
	__ntouchswitching = true;
}

function deleteWizardPanel(){
	var ind = wizardproject.currentpanelindex;
	var tobj = wizardproject.panels[ind];
	
	$('#ntouchleditor').fadeOut();
	$('#ntouchreditor').fadeOut();
	$('.ntouchimgr').fadeOut();
	$('.ntouchimgl').fadeOut();
	
	$('#ntouchlabeltext').fadeOut();
	$('#'+tobj.id).fadeOut(400, function(){
		removeFromArray(wizardproject.panels, wizardproject.panels[wizardproject.currentpanelindex]);
		wizardproject.currentpanelindex = 0;
		$('#ntouchlabeltext').html('Panel-'+(wizardproject.currentpanelindex+1));
		$('#ntouchlabeltext').fadeIn();
		$('#'+wizardproject.panels[wizardproject.currentpanelindex].id).fadeIn();
	});
}

function nTouchToEditor(){
	if (!wizardproject.name) pb.openDialog({
		content: '<div style="margin: 15px;">Please enter a name for your project:'+
			'<input id="dialog_textinput" type="text" value=""></input></div>',
		style: {'border-radius': '10px', height: '120px'},
		confirm: 'ok',
		onConfirm: function(){
			server({opts: {
				command: 'check_projectname', 
				name: $('#dialog_textinput').val()}, 
					callback: function(data){
					console.log('check', data);
					if (1 || data.trim() == '0'){
						
						wizardproject.name = $('#dialog_textinput').val();
						
						var ind = wizardproject.currentpanelindex;
						var tpanels = wizardproject.panels;
						
						var thisobj = {};
						thisobj.name = $('#dialog_textinput').val();
						thisobj.buttonindex = (tpanels.length * 8)+1;
						thisobj.widgetindex = 1;
						thisobj.widgetpartindex = 1;
						thisobj.panelindex = tpanels.length+1;
						thisobj.panels = [];
						thisobj.core = {name: wizardproject.name};
						thisobj.core.scripts = [wizardproject.script,''];
						thisobj.wizardproject = wizardproject;
						
						for (var i=0; i<tpanels.length; i++){
							var buttons = [];
							for (var j=1; j<9; j++){
								var tele = $('#'+tpanels[i].id).find('#button'+j)[0];
								var tele2 = $('#'+tpanels[i].id).find('#button'+j+'img')[0];
								console.log(tele.style.cssText);
								var tbut = new Button({
									style: tele.style.cssText+'; width: 90px; height: 40px;',
									textnodeHTML: tpanels[i].buttontexts[j-1],
									touchpanelkey: j, actualwidth: 90, actualheight: 40
								});
								tbut.element.className += ' draggable';
								tbut.core.scripts[0] = tpanels[i].buttoncommands[j-1];
								tbut.core.actualwidth = 90; tbut.core.actualheight = 40;
								tbut.core.states = [
									{style: '', bgimage: tele2.src, images: [], script: tpanels[i].buttoncommands[j-1]}, 
									{style: '', bgimage: 'images/buttons/Inactive-Button-2.png', images: [], script: ''}
								];
								buttons.push(tbut);
							}
							var tpan = new Panel({style: 
									'width: 240px; height: 320px; background: url('+tpanels[i].bgsource+');'});
							tpan.buttons = buttons;
							thisobj.panels.push(pb.panelToJSON(tpan));
						}
						
						var opts = {command: 'write_project', name: $('#dialog_textinput').val()};
						
						if (wizardproject.puid) opts.puid = wizardproject.puid;
						
						for (var i=0; i<thisobj.panels.length; i++) thisobj.panels[i].core.defaultpanel = false;
						
						thisobj.panels[wizardproject.defaultpanel].core.defaultpanel = true;
						
						server({opts: opts,
							data: thisobj, 
							callback: function(data){
								console.log('data', data);
								$('#wizard').fadeOut();
								wizardopen = false;
								$('#thebody').css({'background': "url('images/ui/furley_bg3.png')", 'background-repeat': 'repeat'});
								pb_ready = true;
								pb.loadProject({fromDB: false, puid: data.puid, name: wizardproject.name});
								$('#thebody').css({'overflow': 'hidden'});
								
							}
						});
						
						pb.closeDialog();
						
					}
					else if (data.trim() == '1'){
						alert('That name is already taken, please try another.');
					}
				}
			});
		
		},
		cancel: false
	});
	else {		
		var ind = wizardproject.currentpanelindex;
		var tpanels = wizardproject.panels;
		var thisobj = {};
		thisobj.name = wizardproject.name;
		thisobj.buttonindex = (tpanels.length * 8)+1;
		thisobj.widgetindex = 1;
		thisobj.widgetpartindex = 1;
		thisobj.panelindex = tpanels.length+1;
		thisobj.panels = [];
		thisobj.core = {name: wizardproject.name};
		thisobj.core.scripts = [wizardproject.script,''];
		
		thisobj.wizardproject = wizardproject;
		
		for (var i=0; i<tpanels.length; i++){
			var buttons = [];
			for (var j=1; j<9; j++){
				var tele = $('#'+tpanels[i].id).find('#button'+j)[0];
				var tele2 = $('#'+tpanels[i].id).find('#button'+j+'img')[0];
				var tbut = new Button({
					style: tele.style.cssText+'; width: 90px; height: 40px;',
					textnodeHTML: tpanels[i].buttontexts[j-1],
					touchpanelkey: j, actualwidth: 90, actualheight: 40
				});
				var t = 1.0;
				tbut.element.style.width = (parseFloat(tbut.element.style.width)*t)+'px';
				tbut.element.style.height = (parseFloat(tbut.element.style.height)*t)+'px';
				tbut.element.style.top = (parseFloat(tbut.element.style.top)*t)+'px';
				tbut.element.className += ' draggable';
				tbut.core.scripts[0] = tpanels[i].buttoncommands[j-1];
				tbut.core.actualwidth = 90; tbut.core.actualheight = 40;
				tbut.core.states = [
					{style: '', bgimage: tele2.src, images: [], script: tpanels[i].buttoncommands[j-1]}, 
					{style: '', bgimage: 'images/buttons/Inactive-Button-2.png', images: [], script: ''}
				];
				buttons.push(tbut);
			}
			var tpan = new Panel({style: 
					'width: 240px; height: 320px; background: url('+tpanels[i].bgsource+');'});
			tpan.buttons = buttons;
			thisobj.panels.push(pb.panelToJSON(tpan));
		}
		
		var opts = {command: 'write_project', name: wizardproject.name};
		
		if (wizardproject.puid) opts.puid = wizardproject.puid;
		
		for (var i=0; i<thisobj.panels.length; i++) thisobj.panels[i].core.defaultpanel = false;
		
		thisobj.panels[wizardproject.defaultpanel].core.defaultpanel = true;
		
		server({opts: opts, data: thisobj, 
			callback: function(data){
				console.log('server callback', data);
				$('#wizard').fadeOut();
				wizardopen = false;
				$('#thebody').css({'background': "url('images/ui/furley_bg3.png')", 'background-repeat': 'repeat'});
				pb.ready = true;
				pb.loadProject({fromDB: false, puid: data.puid, name: wizardproject.name});
				$('#thebody').css({'overflow': 'hidden'});			
			}
		});
	}
	window.wizardopen = false;
}

function nTouchSave(){
	
	if (!wizardproject.name) pb.openDialog({
	
		content: '<div style="margin: 14px;">Please enter a name for your project:'+
			'<input id="dialog_textinput" type="text" value=""></input></div>',
		confirm: 'ok',
		style: {
			height: '170px',
			'border-radius': '17px'
		},
		onConfirm: function(){
						
			pb.panelindex = 0;
			
			wizardproject.name = $('#dialog_textinput').val();
			
			var ind = wizardproject.currentpanelindex;
			var tpanels = wizardproject.panels;
			
			var thisobj = {};
			thisobj.name = $('#dialog_textinput').val();
			thisobj.buttonindex = (tpanels.length * 8)+1;
			thisobj.widgetindex = 1;
			thisobj.widgetpartindex = 1;
			thisobj.panelindex = tpanels.length+1;
			thisobj.panels = [];
			thisobj.core = {name: wizardproject.name};
			thisobj.core.scripts = [wizardproject.script,''];
			
			thisobj.wizardproject = wizardproject;
			
			for (var i=0; i<tpanels.length; i++){
				var buttons = [];
				for (var j=1; j<9; j++){
					var tele = $('#'+tpanels[i].id).find('#button'+j)[0];
					var tele2 = $('#'+tpanels[i].id).find('#button'+j+'img')[0];
					var tbut = new Button({
						style: tele.style.cssText+'; width: 90px; height: 40px;',
						textnodeHTML: tpanels[i].buttontexts[j-1],
						touchpanelkey: j
					});
					var t = 1.0;
					tbut.element.style.width = (parseFloat(tbut.element.style.width)*t)+'px';
					tbut.element.style.height = (parseFloat(tbut.element.style.height)*t)+'px';
					tbut.element.style.top = (parseFloat(tbut.element.style.top)*t)+'px';
					tbut.core.scripts[0] = tpanels[i].buttoncommands[j-1];
					tbut.core.actualwidth = 90; tbut.core.actualheight = 40;
					tbut.core.states = [
						{style: '', bgimage: tele2.src, images: [], script: tpanels[i].buttoncommands[j-1]}, 
						{style: '', bgimage: 'images/buttons/Inactive-Button-2.png', images: [], script: ''}
					];
					buttons.push(tbut);
				}
				var tpan = new Panel({style: 
						'width: 240px; height: 320px; background: url('+tpanels[i].bgsource+');'});
				tpan.buttons = buttons;
				thisobj.panels.push(pb.panelToJSON(tpan));
			}
			
			var opts = {command: 'write_project', name: $('#dialog_textinput').val()};
			
			if (wizardproject.puid) opts.puid = wizardproject.puid;
			
			for (var i=0; i<thisobj.panels.length; i++) thisobj.panels[i].core.defaultpanel = false;
			thisobj.panels[wizardproject.defaultpanel].core.defaultpanel = true;
			
			server({opts: opts, data: thisobj, 
				callback: function(data){
					wizardproject.puid = data.puid;
					setTimeout(function(){
						var str = '';
						if (wallcon){
							str = '<center><br>Project Saved to Server <br><br><span id="project-link"\
								style="cursor: pointer; text-decoration: underline;">Here\'s a link to your project.</span>\
								<br><br><span id="settowallcon" style="cursor: pointer;">Set to Wall Controller</span></center>';
						}
						if (standalone || ncommand){
							str = '<center><br>Project Saved to Server <br><br>'+
								'<span id="project-link" style="cursor: pointer; text-decoration: underline;"'+
								'>Here\'s a link to your project.</span></center>';
						}
						pb.openDialog({
							content: str,
							style: {
								height: '170px',
								'border-radius': '17px'
							},
							cancel: false,
							onConfirm: function(){pb.closeDialog();},
							init: function(){
								$('#settowallcon').click(function(){
									var fd = new FormData();

									fd.append('command', 'setSettings');
									fd.append('homepage', 'http://127.0.0.1/panels/?project='+wizardproject.puid);
									pb.closeDialog();
									server2({formdata: fd, callback: function(data){
												pb.openDialog({
													content: '<div style="margin: 15px;">This project is now the Wall Controller\'s panel.',
													confirm: 'ok',
													cancel: false,
													style: {
														height: '120px',
														'border-radius': '15px'
													},
													onConfirm: function(){pb.closeDialog();}
												});
											}
										}, '../index.php');
								});
								if (standalone) $('#project-link')[0].onclick = function(){
									var panelwin = gui.Window.open('../panels/index.php?project='+wizardproject.puid, {
										title: 'SVSi Control Panels', show: true, toolbar : false, frame: true,
										icon: 'icons/icon.png', width: 240, 
										height: 320, position: 'center'
									});
								}
								else $('#project-link')[0].onclick = function(){
									window.open('../panels/index.php?project='+wizardproject.puid, '_blank');
								}
							}
						});
					}, 1200);
				}
			});
			
			pb.closeDialog();

		},
		cancel: false
	});
	else {
					
		pb.panelindex = 0;			
					
		var ind = wizardproject.currentpanelindex;
		var tpanels = wizardproject.panels;
		
		var thisobj = {};
		thisobj.name = wizardproject.name;
		thisobj.buttonindex = (tpanels.length * 8)+1;
		thisobj.widgetindex = 1;
		thisobj.widgetpartindex = 1;
		thisobj.panelindex = tpanels.length+1;
		thisobj.panels = [];
		thisobj.id = wizardproject.id;
		thisobj.core = {name: wizardproject.name};
		thisobj.core.scripts = [wizardproject.script,''];
		
		thisobj.wizardproject = wizardproject;
		
		for (var i=0; i<tpanels.length; i++){
			var buttons = [];
			for (var j=1; j<9; j++){
				var tele = $('#'+tpanels[i].id).find('#button'+j)[0];
				var tele2 = $('#'+tpanels[i].id).find('#button'+j+'img')[0];
				var tbut = new Button({
					style: tele.style.cssText+'; width: 90px; height: 40px;',
					textnodeHTML: tpanels[i].buttontexts[j-1],
					touchpanelkey: j
				});
				var t = 1.0;
				tbut.element.style.width = (parseFloat(tbut.element.style.width)*t)+'px';
				tbut.element.style.height = (parseFloat(tbut.element.style.height)*t)+'px';
				tbut.element.style.top = (parseFloat(tbut.element.style.top)*t)+'px';
				tbut.core.scripts[0] = tpanels[i].buttoncommands[j-1];
				tbut.core.actualwidth = 90; tbut.core.actualheight = 40;
				tbut.core.states = [
					{style: '', bgimage: tele2.src, images: [], script: tpanels[i].buttoncommands[j-1]}, 
					{style: '', bgimage: 'images/buttons/Inactive-Button-2.png', images: [], script: ''}
				];
				buttons.push(tbut);
			}
			var tpan = new Panel({style: 
					'width: 240px; height: 320px; background: url('+tpanels[i].bgsource+');'});
			tpan.buttons = buttons;
			thisobj.panels.push(pb.panelToJSON(tpan));
		}
		
		var opts = {command: 'write_project', name: wizardproject.name};
		if (wizardproject.puid) opts.puid = wizardproject.puid;
		
		for (var i=0; i<thisobj.panels.length; i++) thisobj.panels[i].core.defaultpanel = false;
		thisobj.panels[wizardproject.defaultpanel].core.defaultpanel = true;
		
		server({opts: opts, data: thisobj, 
			callback: function(data){
				wizardproject.puid = data.puid;
				setTimeout(function(){
					var str = '';
					if (wallcon){
						str = '<center><br>Project Saved to Server <br><br><span id="project-link"\
							style="cursor: pointer; text-decoration: underline;">Here\'s a link to your project.</span>\
							<br><br><span id="settowallcon" style="cursor: pointer;">Set to Wall Controller</span></center>';
					}
					if (standalone || ncommand){
						str = '<center><br>Project Saved to Server <br><br>'+
							'<span id="project-link" style="cursor: pointer; text-decoration: underline;"'+
							'>Here\'s a link to your project.</span></center>';
					}
					pb.openDialog({
						content: str,
						style: {
							height: '170px',
							'border-radius': '17px'
						},
						cancel: false,
						onConfirm: function(){pb.closeDialog();},
						init: function(){
							$('#settowallcon').click(function(){
								var fd = new FormData();
								fd.append('command', 'setSettings');
								fd.append('homepage', 'http://127.0.0.1/panels/?project='+wizardproject.puid);
								server2({formdata: fd,
									callback: function(data){
										pb.openDialog({
											content: '<div style="margin: 15px;">This project is now the Wall Controller\'s panel.',
											confirm: 'ok', cancel: false,
											style: {
												height: '120px',
												'border-radius': '15px'
											},
											onConfirm: function(){pb.closeDialog();}
										});
									}
								}, '../index.php');
								pb.closeDialog();
							});
							if (standalone) $('#project-link')[0].onclick = function(){
								var panelwin = gui.Window.open('../panels/index.php?project='+wizardproject.puid, {
									title: 'SVSi Control Panels', show: true, toolbar : false, frame: true,
									icon: 'icons/icon.png', width: 240, 
									height: 320, position: 'center'
								});
							}
							else $('#project-link')[0].onclick = function(){
								window.open('../panels/index.php?project='+wizardproject.puid, '_blank');
							}
						}
					});
				}, 1200);
			}
		});
	
	}
	
}

function nTouchLoad(puid){

	server({opts: {command: 'read_project', puid: puid}, callback: function(data){
		var project = json_decode(data);
		if (!project.wizardproject){
			pb.openDialog({
				content: '<div style="margin: 15px;">The selected project is not a Wizard Project.</div>',
				confirm: false,
				style: {
					height: '120px'
				},
				cancel: 'ok',
				onCancel: function(){
					pb.closeDialog();
				}
			});
			return;
		}
		wizardproject.panels = [];
		$('#ntouch-panels').html('');
		wizardproject = project.wizardproject;	
		
		for (var i=0; i<project.panels.length; i++){
			var tstr1 = project.panels[i].buttons[0].core.style.substring(
				project.panels[i].buttons[0].core.style.indexOf('(')+1,
				project.panels[i].buttons[0].core.style.indexOf(')')
			).replace(/"/g, '');
			var tstr2 = project.panels[i].buttons[1].core.style.substring(
				project.panels[i].buttons[1].core.style.indexOf('(')+1,
				project.panels[i].buttons[1].core.style.indexOf(')')
			).replace(/"/g, '');
			var tstr3 = project.panels[i].buttons[2].core.style.substring(
				project.panels[i].buttons[2].core.style.indexOf('(')+1,
				project.panels[i].buttons[2].core.style.indexOf(')')
			).replace(/"/g, '');
			var tstr4 = project.panels[i].buttons[3].core.style.substring(
				project.panels[i].buttons[3].core.style.indexOf('(')+1,
				project.panels[i].buttons[3].core.style.indexOf(')')
			).replace(/"/g, '');
			var tstr5 = project.panels[i].buttons[4].core.style.substring(
				project.panels[i].buttons[4].core.style.indexOf('(')+1,
				project.panels[i].buttons[4].core.style.indexOf(')')
			).replace(/"/g, '');
			var tstr6 = project.panels[i].buttons[5].core.style.substring(
				project.panels[i].buttons[5].core.style.indexOf('(')+1,
				project.panels[i].buttons[5].core.style.indexOf(')')
			).replace(/"/g, '');
			var tstr7 = project.panels[i].buttons[6].core.style.substring(
				project.panels[i].buttons[6].core.style.indexOf('(')+1,
				project.panels[i].buttons[6].core.style.indexOf(')')
			).replace(/"/g, '');
			var tstr8 = project.panels[i].buttons[7].core.style.substring(
				project.panels[i].buttons[7].core.style.indexOf('(')+1,
				project.panels[i].buttons[7].core.style.indexOf(')')
			).replace(/"/g, '');
			var tstr9 = project.panels[i].core.style.substring(
				project.panels[i].core.style.indexOf('(')+1,
				project.panels[i].core.style.indexOf(')')
			).replace(/"/g, '');
			//var pt1 = wizardproject.panels[i].buttonsource.substring(wizardproject.panels[i].buttonsource.indexOf('projects/')+9,
			//	wizardproject.panels[i].buttonsource.indexOf('/images'));
			//wizardproject.panels[i].buttonsource = wizardproject.panels[i].buttonsource.replace(pt1, wizardproject.puid);
			tstr1 = tstr2 = tstr3 = tstr4 = tstr5 = tstr6 = tstr7 = tstr8 = wizardproject.panels[i].buttonsource;
			if (wizardproject.panels[i].buttonstates[0] === "-1") tstr1 = "images/ntouch/blank.gif";
			if (wizardproject.panels[i].buttonstates[1] === "-1") tstr2 = "images/ntouch/blank.gif";
			if (wizardproject.panels[i].buttonstates[2] === "-1") tstr3 = "images/ntouch/blank.gif";
			if (wizardproject.panels[i].buttonstates[3] === "-1") tstr4 = "images/ntouch/blank.gif";
			if (wizardproject.panels[i].buttonstates[4] === "-1") tstr5 = "images/ntouch/blank.gif";
			if (wizardproject.panels[i].buttonstates[5] === "-1") tstr6 = "images/ntouch/blank.gif";
			if (wizardproject.panels[i].buttonstates[6] === "-1") tstr7 = "images/ntouch/blank.gif";
			if (wizardproject.panels[i].buttonstates[7] === "-1") tstr8 = "images/ntouch/blank.gif";
		
			$('#ntouch-panels').append('\
				<div id="ntouch-panel-'+(i+1)+'" class="ntouch-panel" style="display: none; width: 240px; height: 320px;\
					background: url(\''+tstr9+'\'); left: 0px; top: 0px; position: absolute; z-index: 40; background-size: 100% 100%;">\
					<div id="button1" style="width: 90px; height: 40px; left: 8px;\
						top: 19px; position: absolute; white-space: nowrap;">\
						<img id="button1img" src="'+tstr1+'"\
							style="width: 100%; height: 100%;" />\
						<span id="button1text" style="position: absolute; top: 0px; left: 0px; white-space: nowrap;\
							width: 100%; text-align: center; line-height: 40px;">'+project.panels[i].buttons[0].core.textnodeHTML+'</span>\
					</div>\
					<div id="button2" style="width: 90px; height: 40px; left: 8px;\
						top: 102px; position: absolute; white-space: nowrap;">\
						<img id="button2img" src="'+tstr2+'"\
							style="width: 100%; height: 100%;" />\
						<span id="button2text" style="position: absolute; top: 0px; left: 0px; white-space: nowrap;\
							width: 100%; text-align: center; line-height: 40px;">'+project.panels[i].buttons[1].core.textnodeHTML+'</span>\
					</div>\
					<div id="button3" style="width: 90px; height: 40px; left: 8px;\
						top: 183px; position: absolute; white-space: nowrap;">\
						<img id="button3img" src="'+tstr3+'"\
							style="width: 100%; height: 100%;" />\
						<span id="button3text" style="position: absolute; top: 0px; left: 0px; white-space: nowrap;\
							width: 100%; text-align: center; line-height: 40px;">'+project.panels[i].buttons[2].core.textnodeHTML+'</span>\
					</div>\
					<div id="button4" style="width: 90px; height: 40px; left: 8px;\
						top: 262px; position: absolute; white-space: nowrap;">\
						<img id="button4img" src="'+tstr4+'"\
							style="width: 100%; height: 100%;" />\
						<span id="button4text" style="position: absolute; top: 0px; left: 0px; white-space: nowrap;\
							width: 100%; text-align: center; line-height: 40px;">'+project.panels[i].buttons[3].core.textnodeHTML+'</span>\
					</div>\
					<div id="button5" style="width: 90px; height: 40px; left: 142px;\
						top: 19px; position: absolute; white-space: nowrap;">\
						<img id="button5img" src="'+tstr5+'"\
							style="width: 100%; height: 100%;" />\
						<span id="button5text" style="position: absolute; top: 0px; left: 0px; white-space: nowrap;\
							width: 100%; text-align: center; line-height: 40px;">'+project.panels[i].buttons[4].core.textnodeHTML+'</span>\
					</div>\
					<div id="button6" style="width: 90px; height: 40px; left: 142px;\
						top: 102px; position: absolute; white-space: nowrap;">\
						<img id="button6img" src="'+tstr6+'"\
							style="width: 100%; height: 100%;" />\
						<span id="button6text" style="position: absolute; top: 0px; left: 0px; white-space: nowrap;\
							width: 100%; text-align: center; line-height: 40px;">'+project.panels[i].buttons[5].core.textnodeHTML+'</span>\
					</div>\
					<div id="button7" style="width: 90px; height: 40px; left: 142px;\
						top: 183px; position: absolute; white-space: nowrap;">\
						<img id="button7img" src="'+tstr7+'"\
							style="width: 100%; height: 100%;" />\
						<span id="button7text" style="position: absolute; top: 0px; left: 0px; white-space: nowrap;\
							width: 100%; text-align: center; line-height: 40px;">'+project.panels[i].buttons[6].core.textnodeHTML+'</span>\
					</div>\
					<div id="button8" style="width: 90px; height: 40px; left: 142px;\
						top: 262px; position: absolute; white-space: nowrap;">\
						<img id="button8img" src="'+tstr8+'"\
							style="width: 100%; height: 100%;" />\
						<span id="button8text" style="position: absolute; top: 0px; left: 0px; white-space: nowrap;\
							width: 100%; text-align: center; line-height: 40px;">'+project.panels[i].buttons[7].core.textnodeHTML+'</span>\
					</div>\
				</div>\
				'
			);

		}
		
		wizardproject = project.wizardproject;
		wizardproject.puid = puid;
		wizardproject.currentpanelindex = 0;
		console.log('project', project);
		if (project.core && project.core.scripts) 
			wizardproject.script = project.core.scripts[0];
		else wizardproject.script = '';
		wizardproject.defaultpanel = 0;
		for (var i=0; i<project.panels.length; i++){
			if (project.panels[i].core.defaultpanel) wizardproject.defaultpanel = i;
			for (var j=1; j<9; j++){
				var tele = $('#'+wizardproject.panels[i].id).find('#button'+j)[0];
				console.log(tele);
				tele.style.cssText += project.panels[i].buttons[j-1].core.style;
				tele.style.cssText += "; width: 90px; height: 40px;";
				tele.style.background = 'none';
			}
		}
		
		$('#ntouch-panel-1').fadeIn();
		$('#ntouchlabeltext').html('panel-1');
		
	}});

}
