function display_events(par, time, duration, data, light)

% display_events - display events in time range
% ---------------------------------------------
%
% display_events(par, time, duration, data, light)
%
% Input:
% ------
%  par - parent browser handle
%  time - page beginning
%  duration - page duration
%  data - parent browser state
%  light - lightweight display flag
%
% Output:
% -------
%  (none)

%--------------------------------
% Author: Matt Robbins
%--------------------------------
% $Revision: 5009 $
% $Date: 2006-05-16 13:10:15 -0400 (Tue, 16 May 2006) $
%--------------------------------

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

%--
% set light display 
%--

if nargin < 5 || isempty(light)
	light = 0;
end

%--
% get browser state
%--

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

%---------------------------
% SETUP
%---------------------------

%--
% convenience variables
%--

sound = data.browser.sound;

ch = get_channels(get_browser(par, 'channels', data));

nch = length(ch);

%--
% get axes 
%--

ax = data.browser.axes;

%--
% delete current selection objects
%--

tmp = data.browser.selection.handle;

delete(tmp(find(ishandle(tmp)))); % this selection may not be needed

%--
% remove currently displayed events
%--

type = {'line', 'patch', 'text'};

for k = 1:length(type)	

	h1 = findall(ax, 'type', type{k});

	n1 = length(h1);

	%--
	% get patch children
	%--

	if strcmp(type{k}, 'patch') && n1

		%--
		% get context menus and menu children
		%--

		if (n1 > 1)
			tmp1 = cell2mat(get(h1, 'uicontextmenu'));
		else
			tmp1 = get(h1, 'uicontextmenu');
		end

		tmp2 = findobj(tmp1, 'type', 'uimenu');

		%--
		% delete patch menus
		%--

		delete(tmp2);
		
		delete(tmp1);

	end

	% TODO: there is a problem with invalid handles in the findall during active scrolling

	h2 = findall(h1, 'flat', 'tag', 'file_boundary');

	if ~isempty(h2)
		h1 = setdiff(h1, h2);
	end

	h2 = findall(h1, 'flat', 'tag', '');

	if ~isempty(h2)
		h1 = setdiff(h1, h2);
	end

	delete(h1);

end

%--
% disable selection options
%--

if isfield(data.browser, 'sound_menu')

	tmp = data.browser.sound_menu.play;

	set(get_menu(tmp, 'Selection'), 'enable', 'off');
	
end

%--
% display active detection selected events
%--

% NOTE: the idea of temporary logs can be extended to have multiple
% active detectors, somehow they must be distinguished from each
% other

if ~isempty(data.browser.detector.active)

	tmp = data.browser.active_detection_log.length;

	if tmp
		event_view('sound', par, '__ACTIVE_DETECTION__', 1:tmp, ax, data, light);
	end

end

%--
% display page-visible events from logs
%--

t1 = time; t2 = time + duration;

for k = 1:length(data.browser.log)

	%--
	% check log visibility
	%--

	if data.browser.log(k).visible && data.browser.log(k).length

		%--
		% time selection
		%--

		if (data.browser.log(k).length > 1)
			time = struct_field(data.browser.log(k).event, 'time');
		else
			time = struct_field(data.browser.log(k).event(1), 'time');
		end

		%--
		% convert time to session time if view is expanded
		%--		

		time = get_sound_time(sound, time, 1);		

		% NOTE: time selection considers the following possibilities: the
		% start time is within the page, the end time is within the
		% page, and the event stradles the page. all the conditions
		% are strict inequalities, we should not be so unlucky as
		% to have things appearing and disappearing due to this.

		ix = find( ...
			((time(:,1) > t1) & (time(:,1) < t2)) | ...
			((time(:,2) > t1) & (time(:,2) < t2)) | ...
			((time(:,1) < t1) & (time(:,2) > t2)) ...
		);

		%--
		% joint channel selection
		%--

		if ~isempty(ix)

			% NOTE: this uses an integer look up table

			%--
			% convert channel indices to integers
			%--

			c = struct_field(data.browser.log(k).event(ix), 'channel');
			
			c = uint8(c);

			%--
			% consider zero offset
			%--

			T = zeros(1,nch + 1); T(ch + 1) = 1;

			%--
			% select indices using look up table
			%--

			ix2 = find(lut_apply(c, T));

			%--
			% dereference indices
			%--

			if ~isempty(ix2)
				ix = ix(ix2);
			else
				ix = [];
			end

		end

		%--
		% display events from log
		%--

		if ~isempty(ix)		
			
			event_view('sound', par, k, ix, ax, [], light);
				
		end

	end

end

%--
% get out for light display
%--

if light
	return;
end

%--
% update event palette for events in page display
%--

% NOTE: we try to skip over code as fast as possible

%--
% check for open event palette
%--

pal = get_palette(par, 'Event', data);

if ~isempty(pal)

	%--
	% check page events only control
	%--

	[ignore, value] = control_update([], pal, 'page_events_only', [], data);

	%--
	% update event palette display
	%--

	% TODO: consider we know displayed events for efficiency

	if value
		update_find_events(par, [], data);			
	end

end
