function [trueindex] = checkBehaviorIndex(action,handles,index)
% function [trueindex] = checkBehaviorIndex(index)
try,
persistent headerindex, global mission, global behaviorGroup, global numGroupBehaviors

trueindex = index;

% dont do anything if display not grouped
if ~get(handles.group_checkbox,'Value'), 
	return, 
end

% use only first index
if ~isempty(index) & ~strcmp(action,'getmissionindex'),
	if (length(index)>1), index = index(1); end
end

if isempty(numGroupBehaviors),
	numGroupBehaviors = 8;
end

switch action,
	case {'clear'},
		headerindex = [];
		trueindex = headerindex;
	case {'save'},
		headerindex = [headerindex,index];
		trueindex = [];
	case {'getmissionindex'},
		% return the mission index corresponding to the selected behavior. returns 0 if header
		for ind=1:length(index),
			currindex = index(ind);
			if ~isempty(headerindex),
				legindex = find(headerindex == currindex);
				if ~isempty(legindex),
					trueindex(ind) = 0;
					return
				end
				numshift = length(find(headerindex < currindex));
				trueindex(ind) = currindex - numshift;
				if get(handles.group_checkbox,'UserData'),
					% collapsed
					numgroup = 0;
					numbehaviors = numGroupBehaviors;
					if ~get(handles.sequence_radio,'Value'),
						numbehaviors = fliplr(numGroupBehaviors);
					end
					for ii=1:numshift,
						numgroup = numgroup + numbehaviors(ii);
					end
					trueindex(ind) = trueindex(ind) + numgroup;
				end
			end
		end
		if (max(trueindex) == 0), trueindex = []; end
	case {'getdisplayindex'},
		% return the display index corresponding to the mission behavior. accounted for seq already.
		% 
		if ~isempty(headerindex),
			trueindex = index;
			numshift = 0;
			ii = 1;
			if ~get(handles.group_checkbox,'UserData'), % expanded
				while (length(find(headerindex < trueindex)) > sum(numshift)),
					numshift(ii) = length(find(headerindex < trueindex)) - sum(numshift);
					trueindex = trueindex + numshift(ii);
					ii = ii + 1;
				end
			else
				headerindex2 = headerindex;
				
				if get(handles.sequence_radio,'Value'),
					numbehaviors = numGroupBehaviors;
				else
					numbehaviors = fliplr(numGroupBehaviors);
				end
				% collapsed - need to find headerindex as if expanded
				for jj=1:length(headerindex),
					headerindex2(jj) = headerindex2(jj) + sum(numbehaviors(1:jj-1));
				end
				
				while (length(find(headerindex2 < trueindex)) > sum(numshift)),
					numshift(ii) = length(find(headerindex2 < trueindex)) - sum(numshift);
					trueindex = trueindex + numshift(ii);
					ii = ii + 1;
				end
				
				totnumshift = length(find(headerindex2 < trueindex));
				numgroups = sum(numbehaviors(1:totnumshift));		%+ number of tracklines(eg., 6) -> subtract groups
				displength = (length(mission) + sum(numshift)) - (sum(numbehaviors)); % should equal only 4 or so behaviors 				
				trueindex = trueindex - numgroups;
				if (~isempty(find(headerindex==trueindex)) | (trueindex < 1) | (trueindex > displength)),
					trueindex = 1;
				end
			end
		end
	case {'headers'},
		% return all display header indices.
		trueindex = headerindex;
end
catch, errorbox('checkBehaviorIndex failed'), end
