
$(function() {

var file = null;
var changed = false;

$('button').button();
$('button.editactive').disable();
$('#editor').tabsupport().disable();

function loadfile(f)
{
	if (!f)
		f = file;

	$('button.editactive').disable();
	$('#editor').disable().val('');
	$('#msg').text('Loading ' + f);
	$.get('get.jim?file=' + encodeURIComponent(f), function(data) {
		if (data.match('^>>>'))
		{
			$('#msg').text(data);
			file = null;
			changed = false;
		}
		else
		{
			$('#editor').val(data).enable();
			$('button.editactive').enable();
			$('#msg').html('Editing <i>' + f + '</i>');
			file = f;
			changed = false;
		}
	});
}

function result(msg)
{
	$('#result')
	    .html(msg)
	    .slideDown('slow')
	    .delay(5000)
	    .slideUp('slow');
}

$('#chooser').fileTree({
	root: '/',
	script: 'files.jim',
	multiFolder: false
}, function(file) {
	$('#chooserd').dialog('close');
	loadfile(file);
});

$('#chooserd').dialog({
	title: "Choose File to Edit",
	modal: true, autoOpen: false,
	height: '500', width: '600',
	draggable: true, resizable: true,
	closeOnEscape: true,
	buttons: {
		"Cancel" : function() {$(this).dialog('close');}
	}
});

$('#open').click(function() {
	$('#chooserd').dialog('open');
});

$('#editor').change(function() {
	changed = true;
});

$('#save').click(function() {
	if (!changed)
	{
		result('No changes to save.');
		return;
	}

	if (!confirm('Save file?'))
		return;
	$.post('put.jim', {
	    'file': file,
	    'data': $('#editor').val()
	}, function(data) {
		result(data);
	});
});

$('#revert').click(function() {
	if (!changed)
	{
		result('No changes to revert.');
		return;
	}
	if (!confirm('Discard changes and re-load file?'))
		return;
	loadfile();
});

$('#back').click(function() {
		window.location = '/diag/diag.jim';
	});

function createf_submit()
{
	var f = $('#createf_name').val();
	console.log('Creating: ' + f);

	$('#createf').dialog('close');
	$.get('create.jim?file=' + encodeURIComponent(f), function(data) {
		if (data.match('^>>>'))
		{
			$('#msg').text(data);
			file = null;
			changed = false;
		}
		else
		{
			$('#editor').val('').enable();
			$('button.editactive').enable();
			$('#msg').html('Editing new file <i>' + f + '</i>');
			file = f;
			changed = false;
		}
	});
}

$('#createf').dialog({
	autoOpen: false,
	height: 'auto', width: 'auto',
	modal: true,
	buttons: {
		"Create File": createf_submit,
		"Cancel": function() {
			$(this).dialog('close');
		}
	}
});

$('#create').click(function() {
	console.log('opening dialog');
	$('#createf').dialog('open');
});

$('a.qfile').on('click', function() {
	loadfile($(this).text());
});

});
