function [p] = gui_erp_open(p,command,parent)

% gui_erp_open - Load averaged ERP data into matlab workspace
% 
% Usage: [p] = gui_erp_open(p,[command],[parent])
% 
% p is a structure, generated by 'eeg_toolbox_defaults'
% command is either 'init' or 'load'
% parent is a handle to the gui that calls this gui, useful
% for updating the UserData field of the parent from this gui.
% The p structure is returned to the parent when the parent 
% handle is given.
% 

% $Revision: 1.1 $ $Date: 2009-04-28 22:13:56 $

% Licence:  GNU GPL, no express or implied warranties
% History:  01/2002, Darren.Weber_at_radiology.ucsf.edu
%           08/2002, Darren.Weber_at_radiology.ucsf.edu
%                    added EMSE erp handling
%                    added interpolation of zero point option
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


if ~exist('p','var'),
 [p] = eeg_toolbox_defaults;
elseif isempty(p),
 [p] = eeg_toolbox_defaults;
end

if ~exist('command','var'),
  command = 'init';
elseif isempty(command),
  command = 'init';
end

command = lower(command);

switch command,
  case 'init',
    if exist('parent','var'),
      ERPOpen = INIT(p,parent);
    else
      ERPOpen = INIT(p,'');
    end
  otherwise,
    ERPOpen = get(gcbf,'Userdata');
    set(ERPOpen.gui,'Pointer','watch');
end


switch command,
  
  case 'plot',
    
    ERPOpen.p = erp_open(ERPOpen.p,ERPOpen.gui);
    
    plotfig = figure('Name',ERPOpen.p.volt.file,...
      'NumberTitle','off',...
      'UserData',ERPOpen.p);
    movegui(plotfig,'center');
    
    plot(ERPOpen.p.volt.timeArray,ERPOpen.p.volt.data); axis tight;
    eeg_plot_metric;
    
    if isequal(get(ERPOpen.handles.Bhold,'Value'),1),
      parent = ERPOpen.gui;
    else
      if isfield(ERPOpen,'parent'),
        parent = ERPOpen.parent.gui;
      else
        parent = [];
      end
    end
    
    if isempty(parent), [Xpoint,Ypoint] = eeg_crosshair('init',ERPOpen.p);
    else                [Xpoint,Ypoint] = eeg_crosshair('init',ERPOpen.p,parent);
    end
    
  case 'return',
    
    ERPOpen.p = erp_open(ERPOpen.p);
    
  case 'save',
    
    fprintf('\nGUI_ERP_OPEN: Save As not implemented yet.\n');
    
  case 'cancel',
    
    GUI.parent = ERPOpen.parent;
    gui_updateparent(GUI);
    close gcbf;
    
  otherwise,
    
end



switch command,
  case {'init','cancel'},
  otherwise,
    set(ERPOpen.gui,'Pointer','arrow');
    set(ERPOpen.gui,'Userdata',ERPOpen);
    if isequal(get(ERPOpen.handles.Bhold,'Value'),1),
     [p] = gui_updateparent(ERPOpen,0);
    else
     [p] = gui_updateparent(ERPOpen);
      close gcbf;
    end
end



return



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ERPOpen] = INIT(p,parent)
% GUI General Parameters

GUIwidth  = 500;
GUIheight = 120;

version = '$Revision: 1.1 $';
name = sprintf('ERP File Open [v %s]\n',version(11:15));

GUI = figure('Name',name,'Tag','ERP_OPEN',...
  'NumberTitle','off',...
  'MenuBar','none','Position',[1 1 GUIwidth GUIheight]);
movegui(GUI,'center');

Font.FontName   = 'Helvetica';
Font.FontUnits  = 'Pixels';
Font.FontSize   = 12;
Font.FontWeight = 'normal';
Font.FontAngle  = 'normal';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Voltage Data Selection and Parameters

G.Title_data = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
  'Position',[.01 .75 .17 .2],...
  'String','Data Type:','HorizontalAlignment','left');

switch lower(p.volt.type),
  case 'ascii',   voltType = 1;
  case 'emse',    voltType = 2;
  case 'scan4x',  voltType = 3;
  case 'scan3x',  voltType = 4;
  case 'matlab',  voltType = 5;
  otherwise,      voltType = 1;
end
G.PvoltType = uicontrol('Tag','PvoltType','Parent',GUI,'Style','popupmenu',...
  'Units','Normalized',Font,  ...
  'Position',[.20 .75 .2 .2],...
  'String',{'ASCII' 'EMSE' 'Scan4x' 'Scan3x' 'Matlab'},'Value',voltType,...
  'Callback',strcat('ERPOpen = get(gcbf,''Userdata'');',...
  'ERPOpen.p.volt.type = popupstr(ERPOpen.handles.PvoltType);',...
  'set(gcbf,''Userdata'',ERPOpen); clear ERPOpen;'));

% ERP Parameters
G.Bascii = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
  'Position',[.45 .75 .2 .2],...
  'String','ERP Parameters','BusyAction','queue',...
  'BackgroundColor',[0.0 0.0 0.75],...
  'ForegroundColor',[1 1 1], 'HorizontalAlignment', 'center',...
  'Callback','tempgui = gui_eeg_ascii_parameters(gcbf); clear tempgui;');

% Interpolate Zero
G.BinterpZero = uicontrol('Parent',GUI,'Style','checkbox','Units','Normalized', Font, ...
  'Position',[.80 .75 .18 .2],'String','Interp 0','BusyAction','queue',...
  'TooltipString','Interpolate zero, mainly for ascii, scan3x, scan4x files.',...
  'Value',p.volt.interpZero,'HorizontalAlignment', 'center',...
  'Callback',strcat('ERPOpen = get(gcbf,''Userdata'');',...
  'ERPOpen.p.volt.interpZero = get(ERPOpen.handles.BinterpZero,''Value''); ',...
  'set(gcbf,''Userdata'',ERPOpen); clear ERPOpen;'));

G.Title_path = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
  'Position',[.01 .50 .17 .2],...
  'String','Path','HorizontalAlignment','left');
G.EvoltPath = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
  'Position',[.20 .50 .58 .2], 'String',p.volt.path,...
  'Callback',strcat('ERPOpen = get(gcbf,''Userdata'');',...
  'ERPOpen.p.volt.path = get(ERPOpen.handles.EvoltPath,''String'');',...
  'set(gcbf,''Userdata'',ERPOpen); clear ERPOpen;'));

G.Title_file = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
  'Position',[.01 .25 .17 .2],...
  'String','File','HorizontalAlignment','left');
G.EvoltFile = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
  'Position',[.20 .25 .58 .2], 'String',p.volt.file,...
  'Callback',strcat('ERPOpen = get(gcbf,''Userdata'');',...
  'ERPOpen.p.volt.file = get(ERPOpen.handles.EvoltFile,''String'');',...
  'set(gcbf,''Userdata'',ERPOpen); clear ERPOpen;'));

Font.FontWeight = 'bold';

% BROWSE: Look for the data
browsecommand = strcat('ERPOpen = get(gcbf,''Userdata'');',...
  'cd(ERPOpen.p.volt.path);',...
  '[file, path] = uigetfile(',...
  '{''*.dat;*.asc;*.txt'', ''ASCII Data Files (*.dat,*.asc,*.txt)'';', ...
  ' ''*.erp'', ''NeuroScan Average (*.erp)'';', ...
  ' ''*.erp'', ''EMSE Average (*.erp)'';', ...
  ' ''*.mat'', ''MATLAB Data Files (*.mat)'';', ...
  ' ''*.*'',   ''All Files (*.*)''},', ...
  '''Select ERP File'');',...
  'if ~isequal(path,0), ERPOpen.p.volt.path = path; end;',...
  'if ~isequal(file,0), ERPOpen.p.volt.file = file; end;',...
  'set(ERPOpen.handles.EvoltPath,''String'',ERPOpen.p.volt.path);',...
  'set(ERPOpen.handles.EvoltFile,''String'',ERPOpen.p.volt.file);',...
  'if ~isempty(findstr(file,''.dat'')) | ~isempty(findstr(file,''.txt'')) | ~isempty(findstr(file,''.asc'')), ',...
  '  set(ERPOpen.handles.PvoltType,''Value'',1); ',...
  '  ERPOpen.p.volt.type = popupstr(ERPOpen.handles.PvoltType);',...
  'elseif ~isempty(findstr(file,''.erp'')), ',...
  '  set(ERPOpen.handles.PvoltType,''Value'',3); ',...
  '  ERPOpen.p.volt.type = popupstr(ERPOpen.handles.PvoltType);',...
  'else, ',...
  '  set(ERPOpen.handles.PvoltType,''Value'',5); ',...
  '  ERPOpen.p.volt.type = popupstr(ERPOpen.handles.PvoltType);',...
  'end;',...
  'set(gcbf,''Userdata'',ERPOpen); clear ERPOpen file path;');
G.BvoltFile = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized',Font, ...
  'Position',[.01 .01 .17 .2], 'String','BROWSE',...
  'BackgroundColor',[0.8 0.8 0.0],...
  'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
  'Callback', browsecommand );

% PLOT: Load & plot the data!
G.Bplot = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
  'Position',[.20 .01 .18 .2],...
  'String','PLOT','BusyAction','queue',...
  'TooltipString','Plot the ERP data and return p struct.',...
  'BackgroundColor',[0.0 0.5 0.0],...
  'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
  'Callback',strcat('ERPOpen = get(gcbf,''Userdata'');',...
  'p = gui_erp_open(ERPOpen.p,''plot'');',...
  'clear ERPOpen;'));

% Save As
G.Bsave = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
  'Position',[.40 .01 .18 .2],'HorizontalAlignment', 'center',...
  'String','SAVE AS','TooltipString','ERP File Conversion Tool (not implemented yet)',...
  'BusyAction','queue',...
  'Visible','on',...
  'BackgroundColor',[0.0 0.0 0.75],...
  'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
  'Callback',strcat('ERPOpen = get(gcbf,''Userdata'');',...
  'p = gui_erp_open(ERPOpen.p,''save'');',...
  'clear ERPOpen;'));

% Quit, return file parameters
G.Breturn = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
  'Position',[.60 .01 .18 .2],...
  'String','RETURN','BusyAction','queue',...
  'TooltipString','Return p struct to workspace and parent GUI.',...
  'BackgroundColor',[0.75 0.0 0.0],...
  'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
  'Callback',strcat('ERPOpen = get(gcbf,''Userdata'');',...
  'p = gui_erp_open(ERPOpen.p,''return'');',...
  'clear ERPOpen;'));

% Cancel
G.Bcancel = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
  'Position',[.80 .01 .18 .2],...
  'String','CANCEL','BusyAction','queue',...
  'TooltipString','Close, do not return parameters.',...
  'BackgroundColor',[0.75 0.0 0.0],...
  'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
  'Callback',strcat('ERPOpen = get(gcbf,''Userdata'');',...
  'p = gui_erp_open(ERPOpen.p,''cancel'');',...
  'clear ERPOpen;'));

% Help
G.Bhelp = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
  'Position',[.80 .25 .18 .2],'String','Help','BusyAction','queue',...
  'BackgroundColor',[1 1 0],...
  'ForegroundColor',[0 0 0], 'HorizontalAlignment', 'center',...
  'Callback','doc erp_open;');

G.Bhold = uicontrol('Parent',GUI,'Style','checkbox','Units','Normalized', Font, ...
  'Position',[.80 .50 .18 .2],'String','Hold GUI','BusyAction','queue',...
  'TooltipString','ERP File Load GUI remains open after ''Plot'' or ''Return'' commands.',...
  'Value',p.hold,'HorizontalAlignment', 'center');


% Store userdata
if exist('parent','var'), ERPOpen.parent.gui = parent; end
ERPOpen.gui = GUI;          
ERPOpen.handles = G;
ERPOpen.p = p;
set(GUI,'Userdata',ERPOpen);
set(GUI,'HandleVisibility','callback');

return
