function browser_resizefcn(par, data)

% browser_resizefcn - resize function for sound browser
% -----------------------------------------------------
%
% browser_resizefcn(par)
%
% Input:
% ------
%  par - browser handle

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Date: 2005-08-25 10:08:52 -0400 (Thu, 25 Aug 2005) $
% $Revision: 1676 $
%--------------------------------

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

%--
% check browser handle input
%--

if (nargin < 1)
	error('Browser handle input is required.');
end

if ~is_browser(par)
	error('Browser handle input is not browser handle.');
end

%--
% get parent state
%--

if (nargin < 2) || isempty(data)
	data = get_browser(par);
end

%--
% set resize function callback
%--

resizefcn = get(par, 'resizefcn');

if isempty(resizefcn) && ~isa(resizefcn, 'function_handle')
	
	set(par, 'resizefcn', @browser_resize_callback);

end

%-----------------------------------
% GET RELEVANT HANDLES
%-----------------------------------

%--
% get channel and support axes handles
%--

% NOTE: this handles problems while deleting figures

try
	ax = data.browser.axes;
catch
	return;
end

ax_supp = findobj(par, 'type', 'axes', 'tag', 'support');

%--
% get slider, colorbar, and statusbar handles
%--


%-----------------------------------%-----------------------------------%--
%---------------------------------

% FIXME: we cannot resize browser when the navigate palette is open

% NOTE: this proposed solution does not work

ax_slider = findobj(par, 'tag', 'BROWSER_TIME_SLIDER');

% ax_slider = get_time_slider(par); ax_slider = ax_slider.handle(1);

%-----------------------------------%-----------------------------------%--
%---------------------------------


% NOTE: colorbar tag is provided by MATLAB and could change

ax_color = findobj(par, 'type', 'axes', 'tag', 'Colorbar');

ax_status = findobj(par, 'type', 'axes', 'tag', 'Status', 'visible', 'on');

%-----------------------------------
% HANDLE PIXEL SIZE DETAILS
%-----------------------------------

%--
% get figure size in pixel units
%--

units = get(par, 'units');

if ~strcmp(units, 'pixels')
	set(par, 'units', 'pixels');
end

pos = get(par, 'position');

set(par, 'units', 'pixels');

%--
% compute values needed for updating positions
%--

statusbar = ~isempty(ax_status);

color = ~isempty(ax_color);

slider = strcmpi(get(ax_slider, 'visible'), 'on');

[layout, tile] = browser_tile_layout(statusbar, color, slider);

%--
% compute tile width and height as fractions of figure
%--

scale = 1.2;

hh = (scale * tile) / pos(4); 

ww = (scale * tile) / pos(3);

%--
% convert layout to fractions of width/height
%--

layout.top = layout.top * hh;

layout.bottom = layout.bottom * hh;

layout.yspace = layout.yspace * hh;

layout.left = layout.left * ww;

layout.right = layout.right * ww;

%--
% conditionally get status text handles
%--

if statusbar
	
	text_left = findobj(ax_status, 'tag', 'Status_Text_Left');
	
	text_right = findobj(ax_status, 'tag', 'Status_Text_Right');
	
end

%--
% compute axes positions using axes array
%--

n = length(ax);

pos = axes_array(n, 1, layout);

if isempty(pos)
	return;
end

%-----------------------------------
% UPDATE OBJECT POSTIONS
%-----------------------------------

%--
% resize when no colorbar
%--

if isempty(ax_color)

	%--
	% update positions of channel axes
	%--
	
	% check axes position vectors
	
	for k = 1:n
		if (any(pos{k} <= 0))
			return;
		end
	end
	
	% set axes positions
	
	for k = 1:n
		set(ax(k), 'position', pos{k});
	end
	
	%--
	% update position of support axes
	%--

	set(ax_supp, ...
		'position', [layout.left, layout.bottom, 1 - (layout.left + layout.right), 1 - (layout.top + layout.bottom)] ...
	);
	
	%--
	% update position of slider and status bar if available
	%--

	if isempty(ax_status)
		
		set(ax_slider, ...
			'position', [layout.left, hh, pos{1}(3), hh] ...
		);
		
	else	
		
		set(ax_slider, ...
			'position',[layout.left, 2.25*hh, pos{1}(3), hh] ...
		);
		
		set(ax_status,'position',[0, 0, 1, 1.25*hh]);

		tmp = get(text_left,'position'); tmp(1) = 4.5*ww/tile;
		
		set(text_left,'position',tmp);
		
		tmp = get(text_right,'position'); tmp2 = get(text_right,'extent'); tmp(1) = (1 - 3*ww/tile) - tmp2(3);
		
		set(text_right,'position',tmp);

	end
	
%--
% resize when colorbar present
%--

else

	%--
	% update positions of channel axes
	%--
	
	% compute positions of channel axes, return when figure is too small
	
	for k = 1:n
		tmp{k} = [pos{k}(1), pos{k}(2), 1 - (layout.left + 4*ww), pos{k}(4)];
		if (any(tmp{k} <= 0))
			return;
		end
	end
	
	% set positions of channel axes
	
	for k = 1:n
		set(ax(k),'position',tmp{k});
	end
	
	%--
	% update position of support axes and colorbar
	%--

	posk = [layout.left, layout.bottom, 1 - (layout.left + 8*ww), 1 - (layout.top + layout.bottom)];
	set(ax_supp,'position',posk);

	posk = [1 - 3.5*ww, layout.bottom, 1.5*ww, 1 - (layout.top + layout.bottom)];
	set(ax_color,'position',posk);

	%--
	% update position of slider and status bar if available
	%--

	if (isempty(ax_status))
		
		posk = [layout.left, hh, 1 - (layout.left + 4*ww), hh];
		
		set(ax_slider,'position',posk);
		
	else 
		
		posk = [layout.left, 2.25*hh, 1 - (layout.left + 4*ww), hh];
		set(ax_slider,'position',posk);
		set(ax_status,'position',[0, 0, 1, 1.25*hh]);

		tmp = get(text_left,'position');
		tmp(1) = 4.5*ww/tile;
		set(text_left,'position',tmp);
		
		tmp = get(text_right,'position');
		tmp2 = get(text_right,'extent');
		
		tmp(1) = (1 - 3*ww/tile) - tmp2(3);
		set(text_right,'position',tmp);

	end
	
end

%--
% refresh figure
%--

refresh(gcf);

%-----------------------------------
% BROWSER_RESIZE_CALLBACK
%-----------------------------------

% NOTE: this is a trivial function handle callback wrapper for the resize

function browser_resize_callback(obj, eventdata)

browser_resizefcn(obj);
