function [out work]= plotSelectGui(sigmaData, out, work ,user, conditions, FH, plotVars)

clearvars X Y S
 
%  Create and then hide the GUI as it is being constructed.
   
   f = figure('Visible','off','Position',[5,5,800,800]);
   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%       Set up push buttons         %%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   hselect      = uicontrol('Style', 'pushbutton', 'String', 'Select',...
            'Position', [5,350,70,24],...
            'Callback', {@selectCallback});
    
   hzoom        = uicontrol('Style', 'pushbutton', 'String', 'Zoom +',...
            'Position', [5,290,70,24],...
            'Callback', {@zoomCallback});
  
   hzoomOut     = uicontrol('Style', 'pushbutton', 'String', 'Zoom -',...
            'Position', [5,270,70,24],...
            'Callback', {@zoomOutCallback});
    
   hjindxYes    = uicontrol('Style', 'pushbutton', 'String', 'Show Indx Value',...
            'Position', [5,210,100,24],...
            'Callback', {@jindxYesCallback});
    
   hjindxNo     = uicontrol('Style', 'pushbutton', 'String', 'No Indx Value',...
            'Position', [5,190,100,24],...
            'Callback', {@jindxNoCallback});
        
   htext        = uicontrol('Style', 'edit', 'BackgroundColor', 'w' ,'String', ' ',...
            'Position', [5,330,80,24],...
            'Callback', {@htextCallback});
   
   if ~isempty(FH)     
        hmolOn       = uicontrol('Style', 'checkbox', 'String', 'Mol ON',...
            'Position', [5,480,80,24],...
            'Callback', {@molOnCallback});
        
        hmolReset    = uicontrol('Style', 'pushbutton', 'String', 'Molview Reset',...
            'Position', [5,450,100,24],...
            'Callback', {@molviewResetCallback});
        
        %ha = axes('Units','Pixels','Position',[50,60,300,300]);
   
        align([hselect,hzoom,hzoomOut,hjindxYes,hjindxNo,htext,hmolOn,hmolReset],'Left','None');
   
        % Initialize the GUI. Change units to normalized so components resize automatically.
   
        set([f,hselect,hzoom,hzoomOut,hjindxYes,hjindxNo,htext,hmolOn,hmolReset],...
        'Units','normalized');
   
   elseif isempty(FH)
       
       %ha = axes('Units','Pixels','Position',[50,60,300,300]);
   
        align([hselect,hzoom,hzoomOut,hjindxYes,hjindxNo,htext],'Left','None');
   
        % Initialize the GUI. Change units to normalized so components resize automatically.
   
        set([f,hselect,hzoom,hzoomOut,hjindxYes,hjindxNo,htext],...
        'Units','normalized');
   end
       

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%           Plot function           %%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


funplot = plotVars.currenPlotFun;

[X Y out work] = funplot(sigmaData, out, work, user, conditions);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%         End plot function         %%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   % Assign the GUI a name to appear in the window title.
   set(f,'Name', 'Correlation Plot')
   % Move the GUI to the center of the screen.
   movegui(f,'center')
   % Make the GUI visible.
   set(f,'Visible','on');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%       Push button callbacks       %%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   function molOnCallback(hmolOn, eventdata)
        
        if (get(hmolOn,'Value') == get(hmolOn,'Max'))
            
        	plotVars.molviewON = 1;
        
        else
            
            plotVars.molviewON = 0;
        
        end

   end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   function selectCallback(source,eventdata) 
    
        [x y] = ginput(1);
        [k indx] = min(abs(X-x));
        [k indy] = min(abs(Y-y));
  
    if indx == indy

        fprintf(['\n\nsigmaData(', num2str(indx), ')\n\n'])
        
        sigmaData(indx)
        
        if ~isempty(FH) && plotVars.molviewON == 1
            
            bonds = ['background black; select atomno = ', num2str(sigmaData(indx).numcode.ATOMi_pdbSerNo), ',atomno = ' , num2str(sigmaData(indx).numcode.ATOMj_pdbSerNo), '; connect (selected); wireframe 0.05; color bonds red; SelectionHalos ON'];
            evalrasmolscript(FH, bonds)
            
            plotVars.previousIndx(plotVars.count) = indx;
            plotVars.count = plotVars.count + 1;
            
       end
        
    elseif indx ~= indy
        disp('AMBIGOUS: ZOOM IN AND PLACE CURSOR MORE PRECISELY')
    end
   
   end
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
   function htextCallback(source,eventdata) 
       
       indx = str2double(get(htext,'string'));
        
       if isnan(indx)
            errordlg('You must enter a numeric value','Bad Input','modal')
            return
       end
       
       if indx > 0 && indx <= length(sigmaData)
       
            fprintf(['\n\nsigmaData(', num2str(indx), ')\n\n'])
       
            sigmaData(indx)
       
            if ~isempty(FH) && plotVars.molviewON == 1
            %if ishandle(FH) && plotVars.molviewON == 1

                bonds = ['background black; select atomno = ', num2str(sigmaData(indx).numcode.ATOMi_pdbSerNo), ',atomno = ' , num2str(sigmaData(indx).numcode.ATOMj_pdbSerNo), '; connect (selected); wireframe 0.05; color bonds red; SelectionHalos ON'];
                evalrasmolscript(FH, bonds)
                
                plotVars.previousIndx(plotVars.count) = indx;
                plotVars.count = plotVars.count + 1;
            
            end
            
       %elseif indx <= 0 || indx > length(sigmaData)
           
           %fprintf(['\n\nINDEX NOT WITHIN RANGE OF EXISTING SIGMADATA ENTRIES, TRY AGAIN\n\n'])
          
           
       elseif indx <= 0 || indx > length(sigmaData)
            errordlg('INDEX NOT WITHIN RANGE OF EXISTING SIGMADATA ENTRIES','Bad Input','modal')
            return
       end
   end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   function molviewResetCallback(source,eventdata)
       
       if ~isempty(FH) && plotVars.molviewON == 1
            for j = plotVars.previousIndx
                if sigmaData(j).unique == 1 || sigmaData(j).unique == 2  
                    if ~isempty(sigmaData(j).numcode.ATOMi_pdbSerNo) && ~isempty(sigmaData(j).numcode.ATOMj_pdbSerNo)
        
                    bonds = ['background black; select atomno = ', num2str(sigmaData(j).numcode.ATOMi_pdbSerNo), ',atomno = ' , num2str(sigmaData(j).numcode.ATOMj_pdbSerNo), '; connect (selected); wireframe 0.05; color bonds limegreen;'];
        
                    evalrasmolscript(FH, bonds)
                    
                    jlast = j;
                    
                    end
                end
            end
            
            halloOff = ['background black; select atomno = ', num2str(sigmaData(jlast).numcode.ATOMi_pdbSerNo), ',atomno = ' , num2str(sigmaData(jlast).numcode.ATOMj_pdbSerNo), '; connect (selected); wireframe 0.05; color bonds limegreen; SelectionHalos OFF;'];
            evalrasmolscript(FH, halloOff)
            
            plotVars.previousIndx = [1];
            plotVars.count = 1;
            
       end
       
   end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   function zoomCallback(source,eventdata) 
        
       zoom
       
   end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   
   function zoomOutCallback(source,eventdata) 
        
       zoom out
       
   end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   
   function jindxYesCallback(source,eventdata) 
       
   cla
   hold(gca)
   
   user.jindx = 'y';

    if plotVars.molviewON == 1
    
        set(hmolOn,'Value',1)
   
    end
    
    [X Y out work] = funplot(sigmaData, out, work, user, conditions);
    
    user.jindx = 'n';
   
   end
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   function jindxNoCallback(source,eventdata) 
   
   cla
   hold(gca)
   
   user.jindx = 'n';
    
    if plotVars.molviewON == 1
    
        set(hmolOn,'Value',1)
   
    end
    
   [X Y out work] = funplot(sigmaData, out, work, user, conditions);
   
   end 
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

end