function varargout = sync_buttons (argument)
% sync_buttons: adds buttons for syncronization operations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if ((isstr(argument)==1)&(strcmp(argument,'sync_lines')==1))
   [bhandle,sync_pt,sync_pt_handle,linehandle,pthandle,ahandle]=get_buttons_handles;
   
   if (strcmpi(get(linehandle,'string'),'Sync Lines: Off')==1)
      set(linehandle,'String','sync lines: on');
      
      % plot integer second lines
      axlim=get(ahandle,'xlim');
      sync_lines=[ceil(axlim(1)):1:floor(axlim(end))];
      set(gcf,'Pointer','watch');
      for (i=1:size(sync_lines,2))
         if (isempty(sync_lines(i))==0)
            axes(ahandle);
            line_handle(i)=line([sync_lines(i);sync_lines(i)],[-1e7;1e7]);
            color='r';
            set(line_handle,'color',color);
            set(line_handle,'LineStyle','-');
         end;
      end;
      set(gcf,'Pointer','arrow');
   elseif (strcmpi(get(linehandle,'string'),'Sync Lines: On')==1)
      set(linehandle,'String','sync lines: off');
      line_handle=get(linehandle,'UserData');
      delete(line_handle);
   else
      err_text='Cannot Determine the current line status!';
      fault(err_text);
      return;
   end;
   
   % save line to userdata
   set(linehandle,'UserData',line_handle);
elseif ((isstr(argument)==1)&(strcmp(argument,'sync_pt')==1))
   [bhandle,sync_pt,sync_pt_handle,linehandle,pthandle,ahandle]=get_buttons_handles;
   
   % if exist, delete old sync point
   if (isempty(sync_pt_handle)==0)
      delete(sync_pt_handle);
   end;
   
   % user input to select point & location to move it to
   [x1,y1]=ginput(1);
   if (strcmpi(get(gcf,'name'),'set GPS time-of-sync')==1)
      x1=round(x1);
   end;
   sync_pt_handle=plot(x1,y1,'xr');
   set(sync_pt_handle,'tag','sync_point');
   sync_pt=x1;
   
   % save line to userdata
   set(bhandle,'UserData',sync_pt);
elseif (isempty(intersect(get(0,'children'),argument))==0)
   fig=argument;
   % add a start line button
   han{1}=uicontrol('Parent',fig, ...
      'Units','characters', ...
      'Position',[0  -0.2  25  1.5], ...
      'BackgroundColor',get(0,'DefaultUicontrolBackgroundColor'), ...
      'String',sprintf('Sync Functions:'), ...
      'HorizontalAlignment','left', ...
      'Style','text', ...
      'Tag','statictext_handle_holder');
   
   if (strcmpi(get(gcf,'name'),'set GPS time-of-sync')==1)
      han{2}=uicontrol('Parent',fig, ...
         'Units','characters', ...
         'Position',[0  0  15  1.5], ...
         'String','sync lines: off', ...
         'Style','pushbutton', ...
         'Tag','line_button', ...
         'Callback','sync_buttons(''sync_lines'');');
      
      han{3}=uicontrol('Parent',fig, ...
         'Units','characters', ...
         'Position',[0  0  15  1.5], ...
         'String','sync pt.', ...
         'Style','pushbutton', ...
         'Tag','pt_button', ...
         'Callback','sync_buttons(''sync_pt'');');
   else
      han{2}=uicontrol('Parent',fig, ...
         'Units','characters', ...
         'Position',[0  0  15  1.5], ...
         'String','sync pt.', ...
         'Style','pushbutton', ...
         'Tag','pt_button', ...
         'Callback','sync_buttons(''sync_pt'');');
   end;
   
   % move buttons
   fighan=get(fig);
   for (i=1:size(han,2))
      set(han{i},'Units',fighan.Units);
      cur_han=get(han{i});
      set(han{i},'Position',cur_han.Position+[0 (fighan.Position(4)-(i)*cur_han.Position(4)) 0 0]);
   end;
   
   % change the "close & save" operation to a uiresume
   kids=get(gcf,'Children');
   for (i=1:size(kids,1))
      if (strcmpi(get(kids(i),'type'),'uicontrol')==1)
         if (strcmpi(get(kids(i),'string'),'close')==1)
            set(kids(i),'String','Save (Close)');
            set(kids(i),'Callback','uiresume(gcf)');
            clocation=get(kids(i),'position');
         end;
      end;
   end;
   
   % add a "close & cancel" button
   han{5}=uicontrol('Parent',fig, ...
      'Units','characters', ...
      'Position',clocation+[clocation(3) 0 0 0], ...
      'String','Cancel (Close)', ...
      'Style','pushbutton', ...
      'Tag','pushbutton1', ...
      'Callback','closereq;');
   
   % output handle for start/finish button
   if (nargout>0)
      varargout{1}=han{1};
   end
else
   err_text='Unable to determine the "sync_buttons" argument!';
   fault(err_text);
   return;
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;

function [bhandle,sync_pt,sync_pt_handle,linehandle,pthandle,ahandle] = get_buttons_handles
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bhandle=[];
sync_pt=[];
sync_pt_handle=[];
linehandle=[];
pthandle=[];
ahandle=[];
set_line=0;

% get line data from handle
kids=get(gcf,'Children');
for (i=1:size(kids,1))
   if (strcmpi(get(kids(i),'type'),'uicontrol')==1)
      if (strcmpi(get(kids(i),'tag'),'statictext_handle_holder')==1)
         bhandle=kids(i);
         sync_pt=get(bhandle,'UserData');
         set_line=set_line+1;
      elseif (strcmpi(get(kids(i),'tag'),'line_button')==1)
         linehandle=kids(i);
         set_line=set_line+1;
      elseif (strcmpi(get(kids(i),'tag'),'pt_button')==1)
         pthandle=kids(i);
         set_line=set_line+1;
      end;
   elseif ((strcmpi(get(kids(i),'type'),'axes')==1)&(strcmpi(get(kids(i),'tag'),'bottom_axes')==1))
      ahandle=kids(i);
      set_line=set_line+1;
      
      akids=get(kids(i),'Children');
      for (j=1:size(akids,1))
         if (strcmpi(get(akids(j),'tag'),'sync_point')==1)
            sync_pt_handle=akids(j);
         end;
      end;
      
   end;
end;

if (strcmpi(get(gcf,'name'),'set GPS time-of-sync')==1)
   if (set_line~=4)
      err_text='Unable to retrieve handles!';
      fault(err_text);
      return;
   end;
elseif (set_line~=3)
   err_text='Unable to retrieve handles!';
   fault(err_text);
   return;
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;
