function pal = waitbar_group(name,control,par,pos,opt)

% waitbar_group - create a waitbar group
% --------------------------------------
%
% pal = waitbar_group(name,control,par,pos,opt)
%
% opt = waitbar_group
%
% Input:
% ------
%  name - waitbar name
%  control - waitbar controls
%  par - waitbar parent (def: [])
%  pos - position with respect to parent
%  opt - waitbar options
%
% Output:
% -------
%  pal - waitbar handle
%  opt - default waitbar options

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 3168 $
% $Date: 2006-01-11 16:54:20 -0500 (Wed, 11 Jan 2006) $
%--------------------------------

% TODO: create a simple PROGRESS waitbar when control array is empty

% TODO: attach update callback, so that we may simply call 'waitbar_update'

%--
% waitbar example code
%--

if (~nargin && ~nargout)
	pal = three_waitbars; return;
end

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

%--
% set and possibly output options
%--

if ((nargin < 5) || isempty(opt))
	
	%--
	% create and set default control group options for waitbar
	%--
	
	opt = control_group;

	opt.width = 18;

	opt.top = 0;

	opt.bottom = 1.75;

	opt.handle_to_callback = 1;

	%--
	% waitbar specific options
	%--

	opt.update_rate = 0.05;

	opt.show_after = 0;
	
	%--
	% possibly return options
	%--
	
	if (~nargin)
		pal = opt; return;
	end
	
end

%--
% set default position
%--

if ((nargin < 4) || isempty(pos))
	pos = 'center';
end

%--
% set parent
%--

if (nargin < 3)
	par = [];
end

%--------------------------------------------------------------
% ADD PROGRESS HEADER
%--------------------------------------------------------------

tmp = control_create( ...
	'style','separator', ...
	'type','header', ...
	'string','Progress' ...
);

if (isempty(control))
	
	control = control_create( ...
		'name','PROGRESS', ...
		'alias','Overall progress bar', ...
		'style','waitbar', ...
		'confirm',1, ...
		'update_rate',[], ...
		'lines',1.1, ...
		'space',2 ...
	);

end

tmp = [tmp, control];

control = tmp;

%--------------------------------------------------------------
% CREATE WAITBAR
%--------------------------------------------------------------

% TODO: decide on how to create the bottom margin for the waitbar figure

%--
% create control group in new figure
%--

pal = control_group(par,'',name,control,opt);

%--
% set palette figure tag
%--

set(pal, ...
	'visible','off', ...
	'tag',['XBAT_WAITBAR::' name] ...
);

%--
% set close request function
%--

%--
% tag progress header
%--

g = findobj(pal,'tag','HEADER','string','Progress');

set(g,'tag','PROGRESS_HEADER');

%--------------------------------------------------------------
% INITIALIZE DISPLAY
%--------------------------------------------------------------

%--
% initialize all waitbars to zero
%--

for k = 1:length(control)
	
	if (strcmp(control(k).style,'waitbar'))
		waitbar_update(pal,control(k).name,'value',0);
	end
	
end

%--
% position waitbar
%--

if (~isempty(par))	
	position_palette(pal,par,pos);
end

%--
% display waitbar
%--

if (opt.show_after > 0)
	set(pal,'visible','off'); return;
end

set(pal,'visible','on'); drawnow;
	