function [h,g] = axes_array(m,n,layout,par)

% axes_array - create an array of axes
% ------------------------------------
%
%       [h,g] = axes_array(m,n,layout,par)
%
%  [pos,size] = axes_array(m,n,layout)
%
%      layout = axes_array
%
% Input:
% ------
%  m - rows 
%  n - columns
%  layout - layout options
%  par - parent handle
%
% Output:
% -------
%  h - array axes handle
%  g - support axes handle
%  pos - array axes positions
%  layout - default layout options
%
% NOTE: positions are all in normalized units

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 4856 $
% $Date: 2006-04-27 19:30:34 -0400 (Thu, 27 Apr 2006) $
%--------------------------------

% TODO: make use of the parent figure input argument when creating axes

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

%--
% set default parent
%--

if ((nargin < 4) || isempty(par))
	par = gcf;
end

%--
% set default layout parameters
%--

if ((nargin < 3) || isempty(layout))
	
	%--
	% default layout parameters
	%--

	% NOTE: these are normalized units
	
	layout.top = 0.05;
	
	layout.bottom = 0.075;

	layout.left = 0.075;
	
	layout.right = 0.075;

	layout.xspace = 0.025;
	
	layout.yspace = 0.025;

	%--
	% possibly return layout options
	%--

	if (~nargin)
		h = layout; return;
	end

end

%--
% compute size of axes in array
%--

x = (1 - ((n - 1) * layout.xspace) - (layout.left + layout.right)) / n;

y = (1 - ((m - 1) * layout.yspace) - (layout.top + layout.bottom)) / m;

%---------------------------------------
% CREATE AXES ARRAY
%---------------------------------------

if (nargin > 3)
	
	%--
	% create invisible support axes
	%--
	
	pos = [ ...
		layout.left, ...
		layout.bottom, ...
		1 - (layout.left + layout.right), ...
		1 - (layout.top + layout.bottom) ...
	];
	
	g = axes( ...
		'parent',par, ...
		'position',pos ...
	);

	% NOTE: set support axes properties, including a 'support' tag
	
	set(g, ...
		'visible','off', ...
		'hittest','off', ...
		'xtick',[], ...
		'ytick',[], ...
		'tag','support' ...
	);
	
	%--
	% build state information to store in support axes
	%--
	
	data = get(g,'userdata');
	
	data.row = m; 
	
	data.col = n; 
	
	data.pos = layout;
		
	data.colorbar = findobj(par,'type','axes','tag','Colorbar');
	
	%--
	% create visible axes array
	%--
	
	h = zeros(m,n);

	for j = 1:n
		for i = 1:m

			pos = [ ...
				((j - 1) * (x + layout.xspace)) + layout.left, ...
				1 - (i * (y + layout.yspace)) + layout.yspace - layout.top, ...
				x, ...
				y ...
			];

			h(i,j) = axes( ...
				'parent',par, ...
				'position', pos, ...
				'userdata',[i,j] ...
			);

		end
	end

	%--
	% add children handles to state and store state
	%--
	
	data.child = h;
	
	set(g,'userdata',data);
	
%---------------------------------------
% COMPUTE AXES POSITIONS
%---------------------------------------

else

	%--
	% compute array axes positions
	%--
	
	h = cell(m,n);

	for j = 1:n
		for i = 1:m

			h{i,j} = [ ...
				((j - 1) * (x + layout.xspace)) + layout.left, ...
				1 - (i * (y + layout.yspace)) + layout.yspace - layout.top, ...
				x, ...
				y ...
			];

		end
	end

	g = [x, y];

end