%function movePoint(tag,geoUnits)
%  Uses global mission from editor.m
%  Uses global origin from plotmission.m
function movePoint(tag,geoUnits)
	global mission

str = 'How do you want to edit the mission?';
btn1 = 'Move Point(s)';
btn2 = 'Move Block';
btn3 = 'Cancel';
button = questdlg(str,'Edit Mission:',btn1,btn2,btn3,btn1);

if strcmp(button,btn1),
	movePoints(tag,geoUnits)
elseif strcmp(button,btn2),
	moveRelative(tag,geoUnits)
elseif strcmp(button,btn3),
	return
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function movePoints(tag,geoUnits)
h = [];
jj = [];
lx = [];
ly = [];
while 1,
	[ix,iy,button] = ginput(1);
	if button == 1,
		if isempty(h), [h,jj] = whichPointsAbsolute(ix,iy,tag);
		else, button = 3; end
		break;
	elseif button == 3,
		[tmph,tmpjj] = whichPointsAbsolute(ix,iy,tag);
		if isempty(tmph), break, end
		h = [h; tmph];
		jj = [jj; tmpjj];
		lx = ix;
		ly = iy;
	end
end

if ~isempty(h),	
	if button == 1,
		[x,y] = ginput(1);
		updatePlotAbsolute(h,jj,x,y)
		updateMissionAbsolute(h,jj,x,y,geoUnits)
	else,
		dx = ix-lx;
		dy = iy-ly;
		updatePlotRelative(h,jj,dx,dy)
		updateMissionRelative(h,jj,dx,dy,geoUnits)
	end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function moveRelative(tag,geoUnits)
h = [];
[ix1,iy1] = ginput(1);
[ix2,iy2] = ginput(1);
ix = [ix1,ix2];
iy = [iy1,iy2];
[h,jj] = whichPointsRelative(ix,iy,tag);
if ~isempty(h),
	[x,y] = getRelativeMove(geoUnits);
	if isempty(x) | isempty(y),
		[x,y] = ginput(1);
	end
	dx = x-mean(ix);
	dy = y-mean(iy);
	updatePlotRelative(h,jj,dx,dy)
	updateMissionRelative(h,jj,dx,dy,geoUnits)
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [X,Y] = getRelativeMove(whichmap)
X = [];
Y = [];
if strcmp(whichmap,'utm'),
	prompt{1} = 'Enter destination Easting:'; 
	prompt{2} = 'Enter destination Northing:';
	reference = 1;
else
	prompt{1} = 'Enter destination Longitude:'; 
	prompt{2} = 'Enter destination Latitude:';
	reference = 2;
end

try,
	title = 'Enter Destination or Hit Cancel to Use Mouse';
	answer = inputdlg(prompt,title,1);
	if length(answer) == 0,
		[x,y] = ginput(1);
		answer{1} = x(1);
		answer{2} = y(1);
	else
		for ii=1:length(answer),
			answer{ii} = str2num(answer{ii});
		end
	end
	X = answer{1};
	Y = answer{2};
catch, errorbox('Bad inputs'), end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% update the plot
function updatePlotAbsolute(h,jj,x,y)
for ii=1:length(h),
	if strcmp(get(h(ii),'Type'),'text')
		hpos = get(h(ii),'Position');
		hpos = [x y];
		set(h(ii),'Position',hpos)
	else
		hx = get(h(ii),'XData');
		hy = get(h(ii),'YData');
		hx(jj(ii)) = x;
		hy(jj(ii)) = y;
		set(h(ii),'XData',hx,'YData',hy)	
		set(h(ii),'MarkerSize',max(get(h(ii),'MarkerSize')/2,6))
	end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% update the plot
function updatePlotRelative(h,jj,dx,dy)
for ii=1:length(h),
	if strcmp(get(h(ii),'Type'),'text')
		hpos = get(h(ii),'Position');
		hpos = [hpos+dx hpos+dy];
		set(h(ii),'Position',hpos)
	else
		hx = get(h(ii),'XData');
		hy = get(h(ii),'YData');
		if jj(ii) > 0,
			hx(jj(ii)) = hx(jj(ii))+dx;
			hy(jj(ii)) = hy(jj(ii))+dy;
		else
			hx(:) = hx(:)+dx;
			hy(:) = hy(:)+dy;
		end
		set(h(ii),'XData',hx,'YData',hy)	
		set(h(ii),'MarkerSize',max(get(h(ii),'MarkerSize')/2,6))
	end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% move the point in the mission
function updateMissionAbsolute(h,jj,x,y,geoUnits)
	global mission
for ii=1:length(h),
	index = get(h(ii),'UserData');
	if ~isempty(index) & index >= 1,
		if length(get(h(ii),'XData')) == 2 & jj(ii) == 1,
			mission{index} = setiPoint(mission{index},x,y,geoUnits);
		else,
			mission{index} = setPoint(mission{index},x,y,geoUnits);
		end
	end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% move the point in the mission
function updateMissionRelative(h,jj,dx,dy,geoUnits)
	global mission
for ii=1:length(h),
	index = get(h(ii),'UserData');
	if ~isempty(index) & index >= 1,
		if length(get(h(ii),'XData')) == 2 & jj(ii) == 1,
			mission{index} = dsetiPoint(mission{index},dx,dy,geoUnits);
		elseif length(get(h(ii),'XData')) == 2 & jj(ii) == -1
			mission{index} = dsetiPoint(mission{index},dx,dy,geoUnits);
			mission{index} = dsetPoint(mission{index},dx,dy,geoUnits);
		else
			mission{index} = dsetPoint(mission{index},dx,dy,geoUnits);
		end
	end
end


%%%%%%%%%%%%%%%%%%%%%
% global mission is declared in editor.m
% global origin is declared in plotmission.m
function bstruct = setPoint(bstruct,x,y,geoUnits)
	global mission, global origin
if isfield(bstruct,'northing') & isfield(bstruct,'easting'),
	if strcmp(geoUnits,'geo'),
		[x,y] = myutmcalc(x,y,'forward',selectGeoid);
	elseif strcmp(geoUnits,'utm'),
		x = x + origin(1);
		y = y + origin(2);
	end
	bstruct = setfield(bstruct,'northing',y);
	bstruct = setfield(bstruct,'easting',x);
elseif isfield(bstruct,'latitude') & isfield(bstruct,'longitude')
	if strcmp(geoUnits,'utm'),
		x = x + origin(1);
		y = y + origin(2);
		[x,y] = myutmcalc(x,y,'inverse',selectGeoid,getUtmStruct);
	end
	bstruct = setfield(bstruct,'longitude',x);
	bstruct = setfield(bstruct,'latitude',y);
end


%%%%%%%%%%%%%%%%%%%%%
% global mission is declared in editor.m
% global origin is declared in plotmission.m
function bstruct = dsetPoint(bstruct,dx,dy,geoUnits)
	global mission, global origin
if isfield(bstruct,'northing') & isfield(bstruct,'easting'),
	x = getfield(bstruct,'easting');	
	y = getfield(bstruct,'northing');
	if strcmp(geoUnits,'geo'),
		[rlat,rlon] = radii(origin(2));
		x = x + dx*rlon;
		y = y + dy*rlat;
	elseif strcmp(geoUnits,'utm'),
		x = x + dx;
		y = y + dy;
	end
	bstruct = setfield(bstruct,'northing',y);
	bstruct = setfield(bstruct,'easting',x);
elseif isfield(bstruct,'latitude') & isfield(bstruct,'longitude')
	x = getfield(bstruct,'longitude');	
	y = getfield(bstruct,'latitude');
	if strcmp(geoUnits,'utm'),
		x = x + dx/rlon;
		y = y + dy/rlat;
	elseif strcmp(geoUnits,'geo'),
		x = x + dx;
		y = y + dy;
	end
	bstruct = setfield(bstruct,'longitude',x);
	bstruct = setfield(bstruct,'latitude',y);
end


%%%%%%%%%%%%%%%%%%%%%
% global mission is declared in editor.m
% global origin is declared in plotmission.m
function bstruct = setiPoint(bstruct,x,y,geoUnits)
	global mission, global origin
if isfield(bstruct,'initialn') & isfield(bstruct,'initiale'),
	if strcmp(geoUnits,'geo'),
		[x,y] = myutmcalc(x,y,'forward',selectGeoid);
	elseif strcmp(geoUnits,'utm'),
		x = x + origin(1);
		y = y + origin(2);
	end
	bstruct = setfield(bstruct,'initialn',y);
	bstruct = setfield(bstruct,'initiale',x);
elseif isfield(bstruct,'initiallat') & isfield(bstruct,'initiallong')
	if strcmp(geoUnits,'utm'),
		x = x + origin(1);
		y = y + origin(2);
		[x,y] = myutmcalc(x,y,'inverse',selectGeoid,getUtmStruct);
	end
	bstruct = setfield(bstruct,'initiallong',x);
	bstruct = setfield(bstruct,'initiallat',y);
end


%%%%%%%%%%%%%%%%%%%%%
% global mission is declared in editor.m
% global origin is declared in plotmission.m
function bstruct = dsetiPoint(bstruct,dx,dy,geoUnits)
	global mission, global origin
if isfield(bstruct,'initialn') & isfield(bstruct,'initiale'),
	x = getfield(bstruct,'initiale');	
	y = getfield(bstruct,'initialn');
	if strcmp(geoUnits,'geo'),
		[rlat,rlon] = radii(origin(2));
		x = x + dx*rlon;
		y = y + dy*rlat;
	elseif strcmp(geoUnits,'utm'),
		x = x + dx;
		y = y + dy;
	end
	bstruct = setfield(bstruct,'initialn',y);
	bstruct = setfield(bstruct,'initiale',x);
elseif isfield(bstruct,'initiallat') & isfield(bstruct,'initiallong')
	x = getfield(bstruct,'initiallong');	
	y = getfield(bstruct,'initiallat');
	if strcmp(geoUnits,'utm'),
		x = x + dx/rlon;
		y = y + dy/rlat;
	elseif strcmp(geoUnits,'geo'),
		x = x + dx;
		y = y + dy;
	end
	bstruct = setfield(bstruct,'initiallong',x);
	bstruct = setfield(bstruct,'initiallat',y);
end


%%%%%%%%%%%%%%%%%%%%%
function utmStruct = getUtmStruct
	global mission
if isfield(mission{1},'utmzone'),
	utmStruct = myutmzone(mission{1}.utmzone);
else
	utmStruct = myutmzone;
end
mission{1}.utmzone = [int2str(utmStruct.number) utmStruct.letter];


%%%%%%%%%%%%%%%%%%%%%
function [h,jj] = whichPointsAbsolute(x,y,tag)
jj = [];
h = [];
htags = findobj(get(gcf, 'Children'), 'Tag',tag);
minDist = getMinDist;
for ii=1:length(htags),
	if strcmp(get(htags(ii),'Type'),'text')
		hpos = get(htags(ii),'Position');
		d = sqrt((hpos(1)-x).^2+(hpos(2)-y).^2);
		if d < minDist,
			h = [h; htags(ii)];
			jj = [jj; 0];
		end
	else
		hx = get(htags(ii),'XData');
		hy = get(htags(ii),'YData');
		d = sqrt((hx-x).^2+(hy-y).^2);
		indices = find(d < minDist);
		if length(indices) > 0,
			h = [h; htags(ii)];
			jj = [jj; min(indices)];
			set(htags(ii),'MarkerSize',min(get(htags(ii),'MarkerSize')*2,15))
		end
	end
end


%%%%%%%%%%%%%%%%%%%%%
function [h,jj] = whichPointsRelative(x,y,tag)
jj = [];
h = [];
htags = findobj(get(gcf, 'Children'), 'Tag',tag);
for ii=1:length(htags),
	if strcmp(get(htags(ii),'Type'),'text')
		hpos = get(htags(ii),'Position');
		if hpos(1) > min(x) & hpos(1)< max(x) & hpos(2) > min(y) & hpos(2) < max(y),
			h = [h; htags(ii)];
			jj = [jj; 0];
		end
	else
		hx = get(htags(ii),'XData');
		hy = get(htags(ii),'YData');
		indices = find(hx > min(x) & hx < max(x) & hy > min(y) & hy < max(y));
		if length(indices) > 0,
			h = [h; htags(ii)];
			if length(indices) == 1,
				jj = [jj; indices];
			else
				jj = [jj; -1];
			end
			set(htags(ii),'MarkerSize',min(get(htags(ii),'MarkerSize')*2,15))
		end
	end
end


%%%%%%%%%%%%%%%%%%%%%
function d = getMinDist
xlims = diff(get(gca,'XLim'));
ylims = diff(get(gca,'YLim'));
d = 0.01 * sqrt(xlims^2 + ylims^2);