function varargout = m_blocks(varargin)


if nargin > 0,
	
	varargin{1} = [ 'm_blocks_'  varargin{1} ];
	if nargout == 0,
		feval(varargin{:});
	else
		[ varargout{1:nargout} ] = feval(varargin{:});
	end
	
end

return;

%--------------------------------------------------------------------------
% Initialize window

function m_blocks_win(map_script)

fh = findobj('tag', 'm_blocks');
if ~isempty(fh),
	return;
end

ud.select = -1;

%fh = figure;
eval(map_script);
%m_grid;
fh = gcf;
set(fh, 'doublebuffer', 'on');
set(fh, 'userdata', ud);
set(fh, 'tag', 'm_blocks');
set(fh, 'keypressfcn', 'm_blocks(''move'')');

children = get(fh, 'children');
ah = findobj(children, 'type', 'axes');
set(ah, 'tag', 'map-axes');

return;

%--------------------------------------------------------------------------
% Load data

function [ prf, blk ] = m_blocks_load(profiles, blocks, yb)

prf = read_asc(profiles);
prf = tonan(prf);

i   = 1;
blk = [];
fid = fopen(blocks, 'r');
while ~feof(fid),
	
	% Read next line
	line = fgetl(fid);
	if ~ischar(line), break, end;
	
	% Skip if not empty line
	if length(line), continue, end;
	
	% Read block number
	bnum = fscanf(fid, '%d', 1);
	trng = fscanf(fid, '%d/%d/%d %d:%d:%f'); 
	tbeg = to_day(yb, trng(1:6)');
	tend = to_day(yb, trng(7:12)');

	blk(i,:) = [ bnum, tbeg, tend ];
	i = i + 1;

end
fclose(fid);
	
return;

%--------------------------------------------------------------------------
% Initialize plot

function m_blocks_init_plot(prf, blk, yb)

% Get figure
fh = findobj('tag', 'm_blocks');
figure(fh);
children = get(fh, 'children');
ah = findobj(children, 'tag', 'map-axes');
axes(ah);

ud = get(fh, 'userdata');
ud.blocks.handles = {};
ud.blocks.data    = {};
ud.year_base = yb;

% Add each block
for i=1:size(blk,1),
	
	ii = find(prf(:,1) > blk(i,2) & (prf(:,1) < blk(i,3)));
	hh = m_line(prf(ii,2), prf(ii,3), 'color', 'black', 'linewidth', 0.5);
	ud.blocks.handles{i} = hh;
	
	state = round(0.5 * mean(prf(ii,4))) * 2;
	ud.blocks.data{i} = [ blk(i,:), state ];
	
end
set(fh, 'userdata', ud);

% Select first block
m_blocks_select(1, 'absolute');

return;
	
%--------------------------------------------------------------------------
% Select next block

function m_blocks_select(b, method)


% Get figure
fh = findobj('tag', 'm_blocks');
figure(fh);
ud = get(fh, 'userdata');
children = get(fh, 'children');
ah = findobj(children, 'tag', 'map-axes');
axes(ah);

% Get current selection
now = ud.select;

% Change color of old block and adjacent ones
if now > 0,
	set(ud.blocks.handles{now}, 'color', 'black', 'linewidth', 0.5);
	if now > 1,
		set(ud.blocks.handles{now-1}, 'color', 'black', 'linewidth', 0.5);
	end
	if now < length(ud.blocks.handles),
		set(ud.blocks.handles{now+1}, 'color', 'black', 'linewidth', 0.5);
	end
else
	now = 1;
end

% Select new block
if strcmp(method, 'relative'),
	new = now + b;
else
	new = b;
end
new = min(new, length(ud.blocks.handles));
new = max(new, 1);
ud.select = new;

% Change color of new blocks
set(ud.blocks.handles{new}, 'color', 'red', 'linewidth', 2);
if new > 1,
	set(ud.blocks.handles{new-1}, 'color', 'blue', 'linewidth', 2);
end
if new < length(ud.blocks.handles),
	set(ud.blocks.handles{new+1}, 'color', 'green', 'linewidth', 2);
end

set(fh, 'userdata', ud);

% Plot title
b = ud.blocks.data{new};
switch b(4),
 case 1,
	str = 'none';
 case 2,
	str = 'accel';
 case 4,
	str = 'turn';
 case 8,
	str = 'on station';
 case 16,
	str = 'underway';
 otherwise,
	str = '*** UNKNOWN ***';
end
vh = findobj(children, 'tag', 'title-axis');
if isempty(vh),
	vh = axes('position', [0 0 1 1]);
	set(vh, 'visible', 'off', 'tag', 'title-axis');
end
axes(vh);
th = get(vh, 'children');
if ~isempty(th),
	delete(th);
end
text(0.5, 0.98, ...
		 [ 'BLOCK: ' sprintf('%03d', b(1)) '  ' ...
			 to_date(ud.year_base, b(2)) ' to ' to_date(ud.year_base, b(3)) ...
			 '  STATE: ' str ], ...
		 'FontSize', 12, 'FontWeight', 'bold', 'FontAngle', 'italic', ...
		 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle');
axes(ah);

return;

%--------------------------------------------------------------------------
% Move on plot

function m_blocks_move()

fh = findobj('tag', 'm_blocks');
c  = get(fh, 'currentcharacter');
ud = get(fh, 'userdata');

switch lower(c),
	
 case 'n',
	m_blocks_select(1, 'relative');
 case 'p',
	m_blocks_select(-1, 'relative');
 case 'f',
	m_blocks_select(1, 'absolute');
 case 'l',
	m_blocks_select(length(ud.blocks.handles), 'absolute');
 otherwise
	%nothing
end

return;

	

	