function varargout = plot_buttons (argument)
% plot_buttons: adds buttons to the track line input plot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if ((isstr(argument)==1)&(strcmp(argument,'L1_slips')==1))
   [bhandle,L1_linehandle,L2_linehandle,ahandle]=get_buttons_handles;
   
   L1_handle=[];
   if (strcmpi(get(L1_linehandle,'string'),'L1 Slips: Off')==1)
      set(L1_linehandle,'String','L1 Slips: On');
      
      % plot L1 cycle slips
      slips=get(bhandle,'userdata');
      if (isempty(slips{1})==0)
         set(gcf,'Pointer','watch');
         for (i=1:size(slips{1},1))
            if (isempty(slips{1}(i,1))==0)
               axes(ahandle);
               L1_handle(i)=line([slips{1}(i,1)-0.01;slips{1}(i,1)-0.01],[-1e7;1e7]);
               color='g';
               set(L1_handle,'color',color);
               set(L1_handle,'LineStyle','-');
            end;
         end;
         set(gcf,'Pointer','arrow');
      end;
   elseif (strcmpi(get(L1_linehandle,'string'),'L1 Slips: On')==1)
      set(L1_linehandle,'String','L1 Slips: Off');
      L1_handle=get(L1_linehandle,'UserData');
      delete(L1_handle);
   else
      err_text='Cannot Determine the current line status!';
      fault(err_text);
      return;
   end;
   
   % save line to userdata
   set(L1_linehandle,'UserData',L1_handle);
elseif ((isstr(argument)==1)&(strcmp(argument,'L2_slips')==1))
   [bhandle,L1_linehandle,L2_linehandle,ahandle]=get_buttons_handles;
   
   L2_handle=[];
   if (strcmpi(get(L2_linehandle,'string'),'L2 Slips: Off')==1)
      set(L2_linehandle,'String','L2 Slips: On');
      
      % plot L2 cycle slips
      slips=get(bhandle,'userdata');
      if (isempty(slips{2})==0)
         set(gcf,'Pointer','watch');
         for (i=1:size(slips{2},1))
            if (isempty(slips{2}(i,1))==0)
               axes(ahandle);
               L2_handle(i)=line([slips{2}(i,1)+0.01;slips{2}(i,1)+0.01],[-1e7;1e7]);
               color='r';
               set(L2_handle,'color',color);
               set(L2_handle,'LineStyle','-');
            end;
         end;
         set(gcf,'Pointer','arrow');
      end;
   elseif (strcmpi(get(L2_linehandle,'string'),'L2 Slips: On')==1)
      set(L2_linehandle,'String','L2 Slips: Off');
      L2_handle=get(L2_linehandle,'UserData');
      delete(L2_handle);
   else
      err_text='Cannot Determine the current line status!';
      fault(err_text);
      return;
   end;
   
   % save line to userdata
   set(L2_linehandle,'UserData',L2_handle);
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('Plot Functions:'), ...
      'HorizontalAlignment','left', ...
      'Style','text', ...
      'Tag','statictext_handle_holder');
   
   han{2}=uicontrol('Parent',fig, ...
      'Units','characters', ...
      'Position',[0  0  15  1.5], ...
      'String','L1 Slips: off', ...
      'Style','pushbutton', ...
      'Tag','L1_button', ...
      'Callback','plot_buttons(''L1_slips'');');
   
   han{3}=uicontrol('Parent',fig, ...
      'Units','characters', ...
      'Position',[0  0  15  1.5], ...
      'String','L2 Slips: off', ...
      'Style','pushbutton', ...
      'Tag','L2_button', ...
      'Callback','plot_buttons(''L2_slips'');');
   
   % 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;
   
   % output handle for start/finish button
   if (nargout>0)
      varargout{1}=han{1};
   end
else
   err_text='Unable to determine the "plot_buttons" argument!';
   fault(err_text);
   return;
end;

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

function [bhandle,L1_linehandle,L2_linehandle,ahandle] = get_buttons_handles
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bhandle=[];
slips=[];
L1_linehandle=[];
L2_linehandle=[];
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);
         set_line=set_line+1;
      elseif (strcmpi(get(kids(i),'tag'),'L1_button')==1)
         L1_linehandle=kids(i);
         set_line=set_line+1;
      elseif (strcmpi(get(kids(i),'tag'),'L2_button')==1)
         L2_linehandle=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;
   end;
end;

if (set_line~=4)
   err_text='Unable to retrieve handles!';
   fault(err_text);
   return;
end;

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