function [vars] = groupBehaviors(mission,handles)
% function [vars] = groupBehaviors(mission,handles)
try,
global behaviorGroup
global numGroupBehaviors

if isempty(behaviorGroup),
	initBehaviorGroup(mission);
end

numGroupBehaviors = [];

pointind = 1;
tindex = 1;
checkBehaviorIndex('clear',handles,0);
vars=mission{1}.name;


namegroups = fieldnames(behaviorGroup);
numgroups = length(namegroups);

	if get(handles.sequence_radio,'Value'), % go forwards, else default go backwards
		
		ii = length(mission);
		while ii>=2;
pointind = [2 ii];		
			nextname = lower(mission{ii}.name);
			for jj=1:numgroups,
				if strcmp(nextname,char(namegroups(jj))), % compare to 'trackline'
					numbehaviors = getNumBehaviors(behaviorGroup,char(namegroups(jj)),tindex);
					%getfield(behaviorGroup,char(namegroups(jj)),'numbehaviors');
					numback = getfield(behaviorGroup,char(namegroups(jj)),'numback');
					hname = getfield(behaviorGroup,char(namegroups(jj)),'headername');
					% if UserData = 1, collapsed, if UserData = 0, expanded view
					if get(handles.group_checkbox,'UserData'),
						% only show trackline header
						nextname = strcat(hname,'.',num2str(tindex));
						vars=strvcat(vars(1:size(vars,1)-numback,:),nextname);
						% save this index
						checkBehaviorIndex('save',handles,size(vars,1));
					else 
						% going backwards through mission file:
						% add the header
						nextname = strcat(hname,'.',num2str(tindex));
						vars=strvcat(vars(1:size(vars,1)-numback,:),nextname);
						checkBehaviorIndex('save',handles,size(vars,1));
						% add displaced behaviors
						for kk=numback:-1:1,
							vars=strvcat(vars,strcat('.....',mission{ii+kk}.name));
						end
						vars=strvcat(vars,strcat('.....',mission{ii}.name));
						numforward = numbehaviors-numback-1;
						% add the rest of the behaviors in this group
						for kk=1:numforward,
							vars=strvcat(vars,strcat('.....',mission{ii-kk}.name));
						end
					end
					ii = ii - (numbehaviors - numback);
					tindex = tindex + 1;
				else
					vars=strvcat(vars,nextname);
					ii = ii - 1;
				end
			end
		end
	else
		ii = 2;
		tindex = sum(getLegs('get',0));
		
		while ii<=length(mission),		
pointind = [3 ii];
			nextname = lower(mission{ii}.name);
			for jj=1:numgroups,
				if strcmp(nextname,char(namegroups(jj))), % compare to 'trackline'
					numbehaviors = getNumBehaviors(behaviorGroup,char(namegroups(jj)),tindex);
					%numbehaviors = getfield(behaviorGroup,char(namegroups(jj)),'numbehaviors');
					numback = getfield(behaviorGroup,char(namegroups(jj)),'numback');
					hname = getfield(behaviorGroup,char(namegroups(jj)),'headername');
					% translate number back for 'Layers' - flip + count
					nameorder = fliplr(behaviorGroup.trackline.names);
					gpslegs = getGpsLegs('get',0);
					if gpslegs(tindex) == 0,
						nameorder = nameorder(3:length(nameorder));
					end
					for kk=1:numbehaviors,
						if strcmp(nextname,char(nameorder(kk))),
							numback = kk - 1;
							break;
						end
					end
					% if UserData = 1, collapsed, if UserData = 0, expanded view
					if get(handles.group_checkbox,'UserData'),
						nextname = strcat(hname,'.',num2str(tindex));
						vars=strvcat(vars(1:size(vars,1)-numback,:),nextname);
						checkBehaviorIndex('save',handles,size(vars,1));				
					else
						nextname = strcat(hname,'.',num2str(tindex));
						vars=strvcat(vars(1:size(vars,1)-numback,:),nextname);
						checkBehaviorIndex('save',handles,size(vars,1));
						for kk=numback:-1:1,
							vars=strvcat(vars,strcat('.....',mission{ii-kk}.name));
						end
						vars=strvcat(vars,strcat('.....',mission{ii}.name));
						numforward = numbehaviors-numback-1;
						for kk=1:numforward,
							vars=strvcat(vars,strcat('.....',mission{ii+kk}.name));
						end
					end
					ii = ii + (numbehaviors - numback);
					tindex = tindex - 1;
				else
					vars=strvcat(vars,nextname);
					ii = ii + 1;
				end
			end
		end
	end
	pointind = 4;
	% take out ...., replace with blanks
	for ii=1:size(vars,1),
		vars(ii,:) = strrep(vars(ii,:),'.....','     ');
	end
	
	vec = checkBehaviorIndex('headers',handles,0);
	for ii=vec, vars(ii,:) = strrep(vars(ii,:),'.',' '); end
	
catch, errorbox(num2str(pointind)), end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function numbehaviors = getNumBehaviors(behaviorGroup,name,legnum)
global numGroupBehaviors

addgps = 1;
numbehaviors = getfield(behaviorGroup,name,'numbehaviors');
if strcmp(name,'trackline'),
	gpslegs = getGpsLegs('get',0);
	if (legnum <= length(gpslegs)),
		addgps = gpslegs(legnum);
	end
	
	if addgps == 0,
		numbehaviors = numbehaviors - 2;
	else
		numbehaviors = numbehaviors*addgps;
	end
end
numGroupBehaviors(legnum) = numbehaviors;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function initBehaviorGroup(mission)
global behaviorGroup
global sonarType
try,
behaviorGroup.trackline.numback = 1;		% once group identified (come upon 'trackline' in mission, 
						% num to backtrack in mission index to get to beginning of group
						% - order using Sequence, *not* Layers
behaviorGroup.trackline.headername = 'surveyleg';
if isfield(mission{1},'missiontype'),

	name = mission{1}.missiontype;
	if strcmp(name,'surveyareaoneway') | strcmp(name,'surveyarearaw'),
		if ~strcmp(sonarType,'none'),
			behaviorGroup.trackline.names = {'waypoint','trackline',sonarType,'trackline',sonarType,'trackline','waypoint','waypoint','ascend','getgps'};
		else	
			behaviorGroup.trackline.names = {'waypoint','trackline','trackline','trackline','waypoint','waypoint','ascend','getgps'}; 
		end
		
	elseif strcmp(name,'compassstar'),
		if ~strcmp(sonarType,'none'),
			behaviorGroup.trackline.names = {sonarType,'trackline',sonarType,'ascend','getgps'};
		else	
			behaviorGroup.trackline.names = {'trackline','ascend','getgps'}; 
		end
	else
		if ~strcmp(sonarType,'none'),
			behaviorGroup.trackline.names = {'waypoint','trackline',sonarType,'trackline',sonarType,'trackline','ascend','getgps'};
		else	
			behaviorGroup.trackline.names = {'waypoint','trackline','trackline','trackline','ascend','getgps'}; 
		end	
	end
	

else
	if ~strcmp(sonarType,'none'),
		behaviorGroup.trackline.names = {'waypoint','trackline',sonarType,'trackline',sonarType,'trackline','ascend','getgps'};
	else	
		behaviorGroup.trackline.names = {'waypoint','trackline','trackline','trackline','ascend','getgps'}; 
	end
end

behaviorGroup.trackline.numbehaviors = length(behaviorGroup.trackline.names);

catch, errorbox, end
