/******************************************************************************
@owner: SVSi
@author: Arthur Johnson
@functions: Script Editor object and functions
@textEditor
*******************************************************************************/
var textEditor = {};

textEditor.index = 0;
textEditor.scripts = [];
textEditor.is_open = false; 

textEditor.open = function(opts){
	
	if (undef(opts)) opts = {};
	if (textEditor.is_open) return;
	
	var ids = '';
	
	textEditor.scripts = [];
	textEditor.index = !undef(opts.index) ? opts.index : 0;
	
	$('#maineditor-itemid').html('');
	$('#maineditor-command').change();
	
	if (opts.isProject){
		ids = 'Project Setup Script';
		$('#maineditor-selector, #maineditor-topoptions').hide();
		textEditor.scripts.push({name: 'setup', script: pb.project.core.scripts[0]});
	}
	else {
		$('#maineditor-selector, #maineditor-topoptions').show();
		$('.outlined').each(function(){
			var item = pb.getItem(this.id);
			if (item instanceof Button) for (var i=0; i<item.core.states.length; i++){
				var found = false;
				for (var j=0; j<textEditor.scripts.length; j++){
					if (item.core.states[i].name == textEditor.scripts[j].name) found = true;
				}
				if (!found) textEditor.scripts.push({name: item.core.states[i].name, script: item.core.states[i].script});
			}
			if (item.core && item.core.id){
				if (ids === '') ids += item.core.id;
				else ids += ', '+item.core.id;
			}
		});
		for (var i=0; i<$('.outlined').length; i++){
			var item = pb.getItem($('.outlined')[i].id);
			if (item instanceof Button || item instanceof Widget || item instanceof SubPanel){
				textEditor.scripts.push({name: 'conditional', script: item.core.conditional});
				$('#maineditor-topoptions').show();
				break;
			}
			else {
				$('#maineditor-topoptions').hide();
			}
		}
	}
	
	//select dropdowns---------------------------------------//
	var tstr = '';
	for (var k=0; k<unitlist.length; k++){
		if (unitlist[k].type != 'RX') var tsr = 'ENC';
		else var tsr = 'DEC';
		tstr += '<option value="'+unitlist[k].ip+'">'+tsr+' - '+unitlist[k].name+'</option>';
	}
	$($('#selectSVSiUnit')[0]).append(tstr);
	var tstr = '';
	for (var i=0; i<manufacturerlist.length; i++){
		tstr += '<option value="'+manufacturerlist[i]+'">'+manufacturerlist[i]+'</option>';
	}
	$('#selectManufacturer').html(tstr);
	server({
		opts: {command: 'read_allmodules'}, 
			callback: function(data){
				modulelist = json_decode(data);
			}
	});
	server({
		opts: {command: 'read_allmanufacturers'}, 
			callback: function(data){
				manufacturerlist = json_decode(data);
				var tstr = '';
				for (var i=0; i<manufacturerlist.length; i++){
					tstr += '<option value="'+manufacturerlist[i]+'">'+manufacturerlist[i]+'</option>';
				}
				$('#selectManufacturer').html(tstr);
			}
	});
	$('#selectGotoPanel').html('');
	var tstr = '';
	for (var i=0; i<pb.user.panels.length; i++){
		tstr = tstr + '<option value="'+(i+1)+'">'+pb.user.panels[i].core.name+'</option>';
	}
	$('#selectGotoPanel').append(tstr);
	$('#maineditor-help-button')[0].onclick = function(){
		if ($(this).data('show')){
			$('#maineditor-help-menu').hide();
			$('#maineditor-help-button').data('show', false);
		}
		else {
			$('#maineditor-help-menu').show();
			$('#maineditor-help-button').data('show', true);
		}
	}
	$('#maineditor-scripthelp')[0].onclick = function(){
		if (standalone){
			websocket.emit('message', {command: 'dirname'}, function(dir){
				gui.Shell.openItem(dir+'manual/PanelBuilderScriptsGuide.pdf');
			});
		}
		else window.open('manual/PanelBuilderScriptsGuide.pdf','_blank');
	}
	$('#maineditor-modulehelp')[0].onclick = 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 window.open('modules.php', '_blank');
	}
	$('#maineditor-commandhelp')[0].onclick = function(){
		if (standalone){
			websocket.emit('message', {command: 'dirname'}, function(dir){
				gui.Shell.openItem(dir+'manual/CombinedNSeriesAPICommandList.pdf');
			});
		}
		else window.open('manual/CombinedNSeriesAPICommandList.pdf','_blank');
	}
	//select dropdowns----------------------------------------//
		
	$('#maineditor-save-button')[0].onclick = function(){
		if (opts.isProject){
			pb.project.core.scripts[0] = maineditor.getValue();
			textEditor.close();
		}
		else {
			for (var i=0; i<textEditor.scripts.length; i++){
				if (textEditor.scripts[i].name == textEditor.index) textEditor.scripts[i].script = maineditor.getValue();
			}
			textEditor.close(true);
		}
	};
	
	$('#maineditor-script-selector-select')[0].onchange = function(){
		for (var i=0; i<textEditor.scripts.length; i++){
			if (textEditor.scripts[i].name == textEditor.index) textEditor.scripts[i].script = maineditor.getValue();
		}
		textEditor.index = $(this).val();
		for (var i=0; i<textEditor.scripts.length; i++){
			if (textEditor.scripts[i].name == textEditor.index) maineditor.setValue(textEditor.scripts[i].script);
		}
	};
	
	ng.maineditor_scripts = textEditor.scripts;
			
	if (ids.length > 25) ids = ids.substring(0, 25)+'...';
	$('#maineditor-itemid').html(ids);
	$('#maineditor').stop().fadeIn(function(){$('#maineditor-script-selector-select').val(textEditor.index);});
	
	if (textEditor.scripts.length){
		if (textEditor.index === 0)
			textEditor.index = textEditor.scripts[0].name;
		maineditor.setValue(textEditor.scripts[0].script, -1);
	}
	else maineditor.setValue('', -1);
	
	$('#maineditor').find('.ace_text-input').focus();
	maineditor.navigateFileEnd();
	
	textEditor.is_open = true;
	
}

textEditor.close = function(save){

	if (!textEditor.is_open) return;
	
	if (save){
		for (var i=0; i<textEditor.scripts.length; i++){
			if (textEditor.scripts[i].name == 'conditional') $('.outlined').each(function(){
				var item = pb.getItem(this.id);
				if (item instanceof Button || item instanceof Widget || item instanceof SubPanel) item.core.conditional = textEditor.scripts[i].script;
				//if (item instanceof Widget) item.core.conditional = textEditor.scripts[i].script;
			});
			else $('.outlined').each(function(){
				var item = pb.getItem(this.id);
				if (item instanceof Button) for (var j=0; j<item.core.states.length; j++){
					if (textEditor.scripts[i].name == item.core.states[j].name)
						item.core.states[j].script = textEditor.scripts[i].script;
				}
			});
		}
	}

	if (maineditor.completer) maineditor.completer.detach();
	$('#maineditor').stop().fadeOut();
	
	textEditor.is_open = false;
	
}