function load_session_dialog(user)

% load_session_dialog - allow user to select from available session to load
% -------------------------------------------------------------------------
%
% load_session_dialog(user)
%
% Input:
% ------
%  user - user (def: active user)

%------------------------
% HANDLE INPUT
%------------------------

%--
% set default user
%--

if nargin < 1
	user = get_active_user;
end

%--
% get sessions
%--

names = get_sessions('', user);

% NOTE: there is nothing to load return

if isempty(names)
	return; 
end

%------------------------
% CREATE DIALOG
%------------------------

%--
% create controls
%--

control = empty(control_create);

control(end + 1) = control_create( ...
	'style', 'separator', ...
	'type', 'header', ...
	'min', 1, ...
	'string', ['Session  (', user.name, ')'] ...
);

control(end + 1) = control_create( ...
	'name', 'name', ... 
	'style', 'popup', ...
	'string', get_sessions, ...
	'value', 1 ...
);

control(end + 1) = control_create( ...
	'name', 'info', ...
	'style', 'listbox', ...
	'lines', 6 ...
);

%--
% configure dialog
%--

opt = dialog_group; 

opt.text_menu = 1;

opt.header_color = get_extension_color('root');

%--
% present dialog
%--

out = dialog_group('Load ...', control, opt, @load_session_callback);

% NOTE: if dialog was cancelled or aborted return

values = out.values;

if isempty(values)
	return;
end

%--
% open session
%--

% NOTE: the name value is packed in a cell

sessions = load_session(values.name{1}, user)


%------------------------------------
% LOAD_SESSION_CALLBACK
%------------------------------------

function load_session_callback(obj, eventdata, user)

%--
% get callback context
%--

callback = get_callback_context(obj, 'pack')

pal = callback.pal; control = callback.control;

%--
% get session name and info handles
%--

name = get_control(pal.handle, 'name', 'value')

handles = get_control(pal.handle, 'info', 'handles')

return;

%--
% display session info
%--

session = load_session(name{1}, user);

% TODO: we really want a session info string function

set(handles.obj, ...
	'string', session.content ...
);

