function [G,p] = gui_eeg_contours(p,parent)

% gui_eeg_contours - GUI controls for topographic mapping of scalp potentials
%
% Paint Graphical user interface (GUI) for eeg_contour_engine.m
%

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

% Licence:  GNU GPL, no express or implied warranties
% History:  09/99 Chris Harvey, initial development
%                 diagnostics, optional plots, interactive timepoint selection
%           07/01 Darren.Weber_at_radiology.ucsf.edu
%                 completely revised. Significant improvement to GUI painting
%                 and general parameter handling and callback functions. Added
%                 various functionality to take advantage of better engines.
% 
% Depends:        eeg_contours.m
%                 eeg_colormap.m, ColorMapsShow.m, ColorMapsMake.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% initial values.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if ~exist('p','var'),
	% Default parameters
   [p] = eeg_toolbox_defaults('create');
elseif isempty(p),
   [p] = eeg_toolbox_defaults('create');
end
EEGCONTOURS.p = p;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Paint the GUI
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


    % GUI General Parameters
    
    version = ' [alpha 1.0]';

    GUIwidth  = 550;
    GUIheight = 500;
    
    GUI = figure('Name',strcat('EEG Contours ',version),'Tag','EEG_Contours',...
                'NumberTitle','off','HandleVisibility','callback',...
                'MenuBar','none');
    set(GUI,'Position',[1 1 GUIwidth GUIheight]);  % Activate GUI Figure
    movegui(GUI,'center');
    
    filewidth       = .70;          %Normalized GUI units
    frameindent     = .01;
    frameheight     = .06;
    framewidth      = .98;
    
    titlewidth      = .20;
    textheight      = .04;  
    textwidth       = .10;
    editheight      = textheight;  
    editwidth       = textwidth;   
    boxheight       = .05;
    boxwidth        = textwidth + .02;
    radioheight     = textheight;
    radiowidth      = .22;
    popupheight     = textheight;
    popupwidth      = .25;
    sliderheight    = textheight;
    sliderwidth     = .35;
    
    Font.FontName   = 'Helvetica';
    Font.FontUnits  = 'Pixels';
    Font.FontSize   = 12;
    Font.FontWeight = 'normal';
    Font.FontAngle  = 'normal';
    
    
    
    
    indent      = [frameindent .25 .50 .75 .85];
    L           = [.01 .06 .11 .16 .21 .26 .31 .36 .41];
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Electrode File Selection
% Cartesian or Spherical Coordinates? Units of measurement: cm or m?
    height = .99 - frameheight*4;
    
    G.Frame_elec = uicontrol('Tag','Frame_elec','Parent',GUI,'Style','frame',...
        'Units','Normalized','Position',[frameindent height framewidth frameheight*4]);
    
    G.Title_elec = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized',Font, ...
        'Position',[frameindent*2 height+L(4) titlewidth textheight],...
        'String','Electrode File','HorizontalAlignment','left',...
        'BackgroundColor',[0.0 0.0 0.75],'ForegroundColor', [1 1 1],...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''UserData'');',...
                          'gui_elec_open(EEGCONTOURS.p,''init'',EEGCONTOURS.gui);',...
                          'clear EEGCONTOURS;'));
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Surface Interpolation

    G.Title_gridMethod = uicontrol('Parent',GUI,'Style','text','Units','Normalized', Font, ...
        'Position',[frameindent*2 height+L(2) titlewidth textheight*2],...
        'String','Surface Interpolation','HorizontalAlignment','left');
    
    switch p.elec.shape
    case 'sphere',    shape = 1;    % spherical fitting
    case 'ellipse',   shape = 2;    % elliptical fitting
    case 'realistic', shape = 3;    % no fitting, use real locations
    otherwise,        shape = 3;
    end
   	G.PelecShape = uicontrol('Tag','PelecShape','Parent',GUI,'Style','popupmenu',...
        'Units','Normalized',Font,...
        'TooltipString','Fit ''sphere'' or ''ellipse'' to ''realistic'' electrode positions.',...
        'Position',[indent(2) height+L(3)+.005 popupwidth popupheight],...
        'String',{'sphere' 'ellipse' 'realistic'},'Value',shape,...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
                          'EEGCONTOURS.p.elec.shape = popupstr(EEGCONTOURS.handles.PelecShape);',...
                          'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
   
    switch p.interpMethod
    case 'linear',        interp = 1;
    case 'cubic',         interp = 2;
    case 'nearest',       interp = 3;
    case 'v4 or invdist', interp = 4;
    end
    G.PinterpMethod = uicontrol('Tag','PinterpMethod','Parent',GUI,'Style','popupmenu',...
        'Units','Normalized',Font,...
        'TooltipString','Matlab ''griddata'' interpolation methods.',...
        'Position',[indent(3) height+L(3)+.005 popupwidth popupheight],...
        'String',{'linear' 'cubic' 'nearest' 'v4' 'invdist'},'Value',interp,...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
                          'EEGCONTOURS.p.interpMethod = popupstr(EEGCONTOURS.handles.PinterpMethod);',...
                          'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
   
    switch p.grid.method
        case 1,     gridM = [1 0]; gridEnable = {'on', 'off'};
        otherwise,  gridM = [0 1]; gridEnable = {'off','on' };
    end
    
	G.BgridMethod1 = uicontrol('Parent',GUI,'Style','Radiobutton','Units','Normalized', Font, ...
		'Position',[indent(2) height+L(2)+.002 .2 textheight], 'String','Grid Size','Value',gridM(1));
    G.SgridSize = uicontrol('Parent',GUI,'Style','slider','Units','Normalized', ...
    	'Position',[indent(3) height+L(2)+.002 sliderwidth boxheight],'Enable',char(gridEnable(1)),...
        'Min', p.grid.sizeMin, 'Max', p.grid.sizeMax, 'Value', p.grid.size, 'SliderStep',[0.005 0.05]);
    G.EgridSize = uicontrol('Parent',GUI,'Style','edit','Units','Normalized', Font, ...
    	'Position',[indent(5) height+L(2)+.002 .1 boxheight],'Enable',char(gridEnable(1)),...
        'Min', p.grid.sizeMin, 'Max', p.grid.sizeMax, 'Value', p.grid.size, 'String', num2str(p.grid.size));
    
	G.BgridMethod2 = uicontrol('Parent',GUI,'Style','Radiobutton','Units','Normalized', Font, ...
		'Position',[indent(2) height+L(1) .2 textheight], 'String','Grid Resolution','Value',gridM(2));
    G.SgridRes = uicontrol('Parent',GUI,'Style','slider','Units','Normalized', ...
    	'Position',[indent(3) height+L(1) sliderwidth boxheight],'Enable',char(gridEnable(2)),...
        'Min', p.grid.resMin, 'Max', p.grid.resMax, 'Value', p.grid.res, 'SliderStep',[0.005 0.05]);
    G.EgridRes = uicontrol('Parent',GUI,'Style','edit','Units','Normalized', Font, ...
    	'Position',[indent(5) height+L(1) .1 boxheight],'Enable',char(gridEnable(2)),...
        'Min', p.grid.resMin, 'Max', p.grid.resMax, 'Value', p.grid.res, 'String', num2str(p.grid.res));
    
    % Set grid size parameters
    set(G.BgridMethod1,'Callback', strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'set(EEGCONTOURS.handles.BgridMethod1,''Value'',1);',...
        'set(EEGCONTOURS.handles.BgridMethod2,''Value'',0);',...
        'set(EEGCONTOURS.handles.SgridSize,''Enable'',''on'');',...
        'set(EEGCONTOURS.handles.EgridSize,''Enable'',''on'');',...
        'set(EEGCONTOURS.handles.SgridRes,''Enable'',''off'');',...
        'set(EEGCONTOURS.handles.EgridRes,''Enable'',''off''); ',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    set(G.SgridSize,'Callback',...
        strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'EEGCONTOURS.p.grid.size = round( get(EEGCONTOURS.handles.SgridSize,''Value'') ); ',...
        'set(EEGCONTOURS.handles.EgridSize,''String'', num2str(EEGCONTOURS.p.grid.size),''Value'', EEGCONTOURS.p.grid.size ); ',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    set(G.EgridSize,'Callback',...
        strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'EEGCONTOURS.p.grid.size = round( str2num( get(EEGCONTOURS.handles.EgridSize,''String'') ) ); ',...
        'set(EEGCONTOURS.handles.EgridSize,''String'', num2str(EEGCONTOURS.p.grid.size),''Value'', EEGCONTOURS.p.grid.size ); ',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    % Set grid resolution parameters
    set(G.BgridMethod2,'Callback', strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'set(EEGCONTOURS.handles.BgridMethod1,''Value'',0);',...
        'set(EEGCONTOURS.handles.BgridMethod2,''Value'',1);',...
        'set(EEGCONTOURS.handles.SgridSize,''Enable'',''off'');',...
        'set(EEGCONTOURS.handles.EgridSize,''Enable'',''off'');',...
        'set(EEGCONTOURS.handles.SgridRes,''Enable'',''on'');',...
        'set(EEGCONTOURS.handles.EgridRes,''Enable'',''on''); ',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    set(G.SgridRes,'Callback',...
        strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'EEGCONTOURS.p.grid.res = get(EEGCONTOURS.handles.SgridRes,''Value''); ',...
        'set(EEGCONTOURS.handles.EgridRes,''String'', num2str(EEGCONTOURS.p.grid.res),''Value'', EEGCONTOURS.p.grid.res ); ',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    set(G.EgridRes, 'Callback',...
        strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'EEGCONTOURS.p.grid.res = str2num( get(EEGCONTOURS.handles.EgridRes,''String'') ); ',...
        'set(EEGCONTOURS.handles.EgridRes,''String'', num2str(EEGCONTOURS.p.grid.res),''Value'', EEGCONTOURS.p.grid.res ); ',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));

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

    height = (height - .01) - frameheight*5;
    
    G.Frame_data = uicontrol('Parent',GUI,'Style','frame','Units','Normalized',...
        'Position',[frameindent height framewidth frameheight*5]);
    
    G.Title_data = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized',Font, ...
        'Position',[frameindent*2 height+L(5) titlewidth textheight],...
        'String','Voltage File','HorizontalAlignment','left',...
        'BackgroundColor',[0.0 0.0 0.75],'ForegroundColor', [1 1 1],...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''UserData'');',...
                          'gui_eeg_open(EEGCONTOURS.p,''init'',EEGCONTOURS.gui);',...
                          'clear EEGCONTOURS;'));
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Range of Data to Visualise
    
    G.Title_range = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
        'Position',[frameindent*2 height+L(4) titlewidth textheight],...
        'String','Voltage Range','HorizontalAlignment','left');

    switch p.rangeMethod
        case 'minmaxall', range = [1 0 0 0]; rangeEnable = 'off'; % Min/Max,all points
        case 'minmaxone', range = [0 1 0 0]; rangeEnable = 'off'; % Min/Max, single point
        case 'minmaxabs', range = [0 0 1 0]; rangeEnable = 'off'; % Min/Max, Absolute
        otherwise,        range = [0 0 0 1]; rangeEnable = 'on' ; % Specified range
    end
    % Min/Max across whole window
    G.BrangeMethod1 = uicontrol('Parent',GUI,'Style','Radiobutton',...
        'Units','Normalized',Font, ...
        'Position',[indent(2) height+L(3) radiowidth radioheight], 'String','Min/Max all points','Value',range(1),...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
            'EEGCONTOURS.p.rangeMethod = ''minmaxall'';',...
            'set(EEGCONTOURS.handles.BrangeMethod1,''Value'',1);',...
            'set(EEGCONTOURS.handles.BrangeMethod2,''Value'',0);',...
            'set(EEGCONTOURS.handles.BrangeMethod3,''Value'',0);',...
            'set(EEGCONTOURS.handles.BrangeMethod4,''Value'',0);',...
            'set(EEGCONTOURS.handles.EminimumIntensity,''Enable'',''off'');',...
            'set(EEGCONTOURS.handles.EmaximumIntensity,''Enable'',''off'');',...
            'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    % Min/Max across time point only
    G.BrangeMethod2 = uicontrol('Parent',GUI,'Style','Radiobutton',...
        'Units','Normalized',Font,  ...
        'Position',[indent(2) height+L(4) radiowidth radioheight],...
        'String','Min/Max','Value',range(2),...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
            'EEGCONTOURS.p.rangeMethod = ''minmaxone'';',...
            'set(EEGCONTOURS.handles.BrangeMethod1,''Value'',0);',...
            'set(EEGCONTOURS.handles.BrangeMethod2,''Value'',1);',...
            'set(EEGCONTOURS.handles.BrangeMethod3,''Value'',0);',...
            'set(EEGCONTOURS.handles.BrangeMethod4,''Value'',0);',...
            'set(EEGCONTOURS.handles.EminimumIntensity,''Enable'',''off'');',...
            'set(EEGCONTOURS.handles.EmaximumIntensity,''Enable'',''off'');',...
            'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    % Abs Min/Max across time point only
    G.BrangeMethod3 = uicontrol('Parent',GUI,'Style','Radiobutton',...
        'Units','Normalized',Font,  ...
        'Position',[indent(3) height+L(4) radiowidth radioheight],...
        'String','Abs Min/Max','Value',range(3),...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
            'EEGCONTOURS.p.rangeMethod = ''minmaxabs'';',...
            'set(EEGCONTOURS.handles.BrangeMethod1,''Value'',0);',...
            'set(EEGCONTOURS.handles.BrangeMethod2,''Value'',0);',...
            'set(EEGCONTOURS.handles.BrangeMethod3,''Value'',1);',...
            'set(EEGCONTOURS.handles.BrangeMethod4,''Value'',0);',...
            'set(EEGCONTOURS.handles.EminimumIntensity,''Enable'',''off'');',...
            'set(EEGCONTOURS.handles.EmaximumIntensity,''Enable'',''off'');',...
            'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    % scale by specified range
    G.BrangeMethod4 = uicontrol('Parent',GUI,'Style','Radiobutton',...
        'Units','Normalized',Font,  ...
        'Position',[indent(3) height+L(3) radiowidth radioheight], 'String','Specified Range', 'Value',range(4),...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
            'EEGCONTOURS.p.rangeMethod = ''minmaxgiv'';',...
            'set(EEGCONTOURS.handles.BrangeMethod1,''Value'',0);',...
            'set(EEGCONTOURS.handles.BrangeMethod2,''Value'',0);',...
            'set(EEGCONTOURS.handles.BrangeMethod3,''Value'',0);',...
            'set(EEGCONTOURS.handles.BrangeMethod4,''Value'',1);',...
            'min = EEGCONTOURS.p.minimumIntensity;',...
            'max = EEGCONTOURS.p.maximumIntensity;',...
            'set(EEGCONTOURS.handles.EminimumIntensity,''Enable'',''on'',''Value'',min,''String'',num2str(min) );',...
            'set(EEGCONTOURS.handles.EmaximumIntensity,''Enable'',''on'',''Value'',max,''String'',num2str(max) );',...
            'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    G.Title_min = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
        'Position',[indent(5)           height+L(4) textwidth textheight],'String','Min');
    G.EminimumIntensity = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
        'Position',[indent(5)-editwidth height+L(4) editwidth editheight],'Enable',rangeEnable,...
        'String',num2str(p.minimumIntensity),'Value',p.minimumIntensity,...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
            'min = str2num( get(EEGCONTOURS.handles.EminimumIntensity,''String'') );',...
            'set(EEGCONTOURS.handles.EminimumIntensity,''Value'',min,''String'',num2str(min) );',...
            'EEGCONTOURS.p.minimumIntensity = min; ',...
            'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
        
    G.Title_max = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
        'Position',[indent(5)           height+L(3) textwidth textheight],'String','Max');
    G.EmaximumIntensity = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
        'Position',[indent(5)-editwidth height+L(3) editwidth  editheight],'Enable',rangeEnable,...
        'String',num2str(p.maximumIntensity),'Value',p.maximumIntensity,...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
            'max = str2num( get(EEGCONTOURS.handles.EmaximumIntensity,''String'') );',...
            'set(EEGCONTOURS.handles.EmaximumIntensity,''Value'',max,''String'',num2str(max) );',...
            'EEGCONTOURS.p.maximumIntensity = max; ',...
            'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Range of TimePoints
    G.Title_range = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
        'Position',[frameindent*2 height+L(2) titlewidth textheight],...
        'String','Time Range','HorizontalAlignment','left');

    switch p.timeMethod
        case 1, time = [1 0]; endEnable = 'off'; singleEnable = 'on';  % Single Timepoint
        case 2, time = [0 1]; endEnable = 'on';  singleEnable = 'off'; % Range Timepoints
    end
    G.BclickTimePoint = uicontrol('Parent',GUI,'Style','Checkbox','Units','Normalized',Font,  ...
        'Position',[indent(1)+.05 height+L(1) radiowidth radioheight],...
        'String','Click Plot',...
        'TooltipString','Interactive data timepoint selection.',...
        'Value',p.clickTimePoint,...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
            'EEGCONTOURS.p.clickTimePoint = get(EEGCONTOURS.handles.BclickTimePoint,''Value'');',...
            'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
        
    G.Title_sample1   = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
        'Position',[indent(2)-.04           height+L(2) radiowidth textheight],...
        'String','Sample Rate',...
        'TooltipString','Disabled until functionality developed.');
    G.EtimeSample     = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
        'Position',[indent(2)               height+L(1) editwidth  editheight],...
        'Min', 1, 'Max', 50000, ...
        'Value', p.volt.sampleMsec, 'String', num2str(p.volt.sampleMsec),...
        'TooltipString','Specify EEG/ERP sample rate in milliseconds.');
    
    G.BtimeMethod1    = uicontrol('Parent',GUI,'Style','Radiobutton',...
        'Units','Normalized',Font, ...
        'Position',[indent(3) height+L(2) radiowidth radioheight],...
        'String','Single Point','Value',time(1));
    G.EtimePoint      = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font, ...
        'Position',[indent(4) height+L(2) editwidth  editheight], 'Min', 1, 'Max', 50000, ...
        'Value', p.volt.samplePoint, 'String', num2str(p.volt.samplePoint));
    G.Title_start     = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
        'Position',[indent(5) height+L(2) textwidth textheight],'String','Start');
    
    G.BtimeMethod2 = uicontrol('Parent',GUI,'Style','Radiobutton',...
        'Units','Normalized',Font,  ...
        'Position',[indent(3)           height+L(1) radiowidth radioheight],...
        'String','Range','Value',time(2),...
        ...
        ...
        'TooltipString','Disabled until functionality developed.',...
        'Enable','off');
    
    G.EendTime  = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
        'Position',[indent(4) height+L(1) editwidth  editheight],'Enable',endEnable,...
        'String',num2str(p.endTime),'Value',p.endTime);
    G.Title_end = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
        'Position',[indent(5)           height+L(1) textwidth  textheight],'String','End');

    % Single Timepoint
    set(G.BtimeMethod1,'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'set(EEGCONTOURS.handles.BtimeMethod1,''Value'',1);',...
        'set(EEGCONTOURS.handles.BtimeMethod2,''Value'',0);',...
        'set(EEGCONTOURS.handles.EendTime,''Enable'',''off'');',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    % Specify a time point range
    set(G.BtimeMethod2,'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'set(EEGCONTOURS.handles.BtimeMethod1,''Value'',0);',...
        'set(EEGCONTOURS.handles.BtimeMethod2,''Value'',1);',...
        'set(EEGCONTOURS.handles.EendTime,''Enable'',''on'');',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    set(G.EtimeSample,'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'EEGCONTOURS.p.timeSample = str2num( get(EEGCONTOURS.handles.EtimeSample,''String'') );',...
        'set(EEGCONTOURS.handles.EtimeSample,''Value'',EEGCONTOURS.p.timeSample,''String'',num2str(EEGCONTOURS.p.timeSample) );',...
        'set(EEGCONTOURS.handles.EtimePoint,''Value'',EEGCONTOURS.p.volt.samplePoint,''String'',num2str(EEGCONTOURS.p.volt.samplePoint) );',...
        'set(EEGCONTOURS.handles.EendTime,  ''Value'',EEGCONTOURS.p.endTime,  ''String'',num2str(EEGCONTOURS.p.endTime) );',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    set(G.EtimePoint,'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'EEGCONTOURS.p.volt.samplePoint = str2num( get(EEGCONTOURS.handles.EtimePoint,''String'') );',...
        'set(EEGCONTOURS.handles.EtimePoint,''Value'',EEGCONTOURS.p.volt.samplePoint,''String'',num2str(EEGCONTOURS.p.volt.samplePoint) );',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    set(G.EendTime,'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'EEGCONTOURS.p.endTime = str2num( get(EEGCONTOURS.handles.EendTime,''String'') );',...
        'set(EEGCONTOURS.handles.EendTime,  ''Value'',EEGCONTOURS.p.endTime,  ''String'',num2str(EEGCONTOURS.p.endTime) );',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Contour Size
    height = (height - .01) - frameheight;
    
    G.Frame_step = uicontrol('Parent',GUI,'Style','frame','Units','Normalized',...
        'Position',[frameindent   height framewidth frameheight]);
    
    G.Bcontourstep = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
        'Position',[frameindent*2 height+L(1) titlewidth textheight],...
        'String','Contour Steps','HorizontalAlignment','left',...
        'BackgroundColor',[0.0 0.0 0.75],'ForegroundColor', [1 1 1],...
        'Callback','gui_eeg_contour_steps(gcbf);');
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Contour ColorMap
    height = (height - .01) - frameheight;
    
    G.Frame_step = uicontrol('Parent',GUI,'Style','frame','Units','Normalized',...
        'Position',[frameindent   height framewidth frameheight]);
    
    G.Title_step = uicontrol('Parent',GUI,'Style','text','Units','Normalized', Font, ...
        'Position',[frameindent*2 height+L(1) titlewidth textheight],...
        'String','Color Maps','HorizontalAlignment','left');

    G.BEEGcolorMap = uicontrol('Parent',GUI,'Style','pushbutton',...
        'Units','Normalized', Font, ...
        'Position',[indent(2)+.01 height+L(1) radiowidth radioheight], 'String','EEG ColorMaps',...
        'BusyAction','queue','HorizontalAlignment', 'center',...
        'BackgroundColor',[0.0 0.0 0.75],'ForegroundColor', [1 1 1],...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
            'tmp = gui_eeg_colormap(EEGCONTOURS.gui);',...
            'clear tmp EEGCONTOURS;'));
    
    G.BcolorMap = uicontrol('Parent',GUI,'Style','pushbutton',...
        'Units','Normalized', Font, ...
        'Position',[indent(3)+.01 height+L(1) radiowidth radioheight], 'String','MATLAB ColorMaps',...
        'BusyAction','queue','HorizontalAlignment', 'center',...
        'BackgroundColor',[0.0 0.0 0.75],'ForegroundColor', [1 1 1],...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
            'EEGCONTOURS.p.colorMap.map = ColorMapsShow;',...
            'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    G.BcreateCMap = uicontrol('Parent',GUI,'Style','pushbutton',...
        'Units','Normalized', Font, ...
        'Position',[indent(4) height+L(1) radiowidth radioheight], 'String','Create ColorMap',...
        'BusyAction','queue','HorizontalAlignment', 'center',...
        'BackgroundColor',[0.0 0.0 0.75],'ForegroundColor', [1 1 1],...
        'Tooltipstring','Not yet integrated fully with EEG toolbox - can export to main workspace.',...
        'Callback','ColorMapsMake;');

    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Plot Controls
    height = (height - .01) - frameheight*3;
    
    G.Frame_plots = uicontrol('Parent',GUI,'Style','frame','Units','Normalized', ...
        'Position',[frameindent   height  framewidth frameheight*3]);
    
    G.Title_plots = uicontrol('Parent',GUI,'Style','text','Units','Normalized', Font, ...
        'Position',[frameindent*2 height+L(3) titlewidth  textheight],...
        'String','Plot Control','HorizontalAlignment','left');
    
    G.BplotElec = uicontrol('Parent',GUI,'Style','Checkbox','Units','Normalized', Font, ...
        'Position',[indent(2) height+L(3) radiowidth radioheight],...
        'String','Electrodes','Value',p.elec.plot,...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'EEGCONTOURS.p.elec.plot = get(EEGCONTOURS.handles.BplotElec, ''Value'');',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    G.BplotESurface    = uicontrol('Parent',GUI,'Style','Checkbox',...
        'Units','Normalized', Font, ...
        'Position',[indent(2) height+L(2) radiowidth radioheight],...
        'String','Elec Surface','Value',p.elec.plotSurf,...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'EEGCONTOURS.p.elec.plotSurf = get(EEGCONTOURS.handles.BplotESurface, ''Value'');',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));

    G.BplotMSurface    = uicontrol('Parent',GUI,'Style','Checkbox',...
        'Units','Normalized', Font, ...
        'Position',[indent(2) height+L(1) radiowidth radioheight],...
        'String','Mesh Surface','Value',p.mesh.plotSurf,...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'EEGCONTOURS.p.mesh.plotSurf = get(EEGCONTOURS.handles.BplotMSurface, ''Value'');',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    G.BplotRaw2D      = uicontrol('Parent',GUI,'Style','Checkbox',...
        'Units','Normalized', Font, ...
        'Position',[indent(3) height+L(3) radiowidth radioheight],...
        'String','2D Contour','Value',p.contour.raw2D,...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'EEGCONTOURS.p.contour.raw2D =      get(EEGCONTOURS.handles.BplotRaw2D, ''Value'');',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));

    G.Bplot2D = uicontrol('Parent',GUI,'Style','Checkbox','Units','Normalized', Font, ...
        'Position',[indent(3) height+L(2) radiowidth radioheight],...
        'String','2D Projected','Value',p.contour.plot2D,...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'EEGCONTOURS.p.contour.plot2D =     get(EEGCONTOURS.handles.Bplot2D, ''Value'');',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    G.Bplot3D = uicontrol('Parent',GUI,'Style','Checkbox','Units','Normalized', Font, ...
        'Position',[indent(3) height+L(1) radiowidth radioheight],...
        'String','3D contour','Value',p.contour.plot3D,...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'EEGCONTOURS.p.contour.plot3D =     get(EEGCONTOURS.handles.Bplot3D, ''Value'');',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));

    switch p.saveGraphics
    case 'Save Plots', graphics = 1;
    case 'eps',  graphics = 2;
    case 'jpg',  graphics = 3;
    case 'tif',  graphics = 4;
    case 'png',  graphics = 5;
    otherwise,   graphics = 1;
    end
    G.BsaveGraphics = uicontrol('Tag','PinterpMethod','Parent',GUI,'Style','popupmenu',...
        'Units','Normalized',Font,...
        'TooltipString','Automatically save plot(s) to specified graphics file format.',...
        'Position',[indent(4) height+L(3) radiowidth popupheight],...
        'String',{'Save Plots' 'eps' 'jpg' 'tif' 'png'},...
        'Value',graphics,...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'EEGCONTOURS.p.saveGraphics = popupstr(EEGCONTOURS.handles.BsaveGraphics);',...
        'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); clear EEGCONTOURS;'));
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % GUI Controls
    height = (height - .01) - frameheight;
    boxwidth = 0.2;
    
    Font.FontWeight = 'bold';
    
    G.Bquit = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
        'Position',[indent(1) height+L(1) boxwidth boxheight],...
        'String','QUIT','BusyAction','queue',...
        'BackgroundColor',[0.75 0.0 0.0],...
        'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
                          'p = EEGCONTOURS.p;',...
                          'if isfield(EEGCONTOURS,''parent''), ',...
                              'parent = get(EEGCONTOURS.parent.gui,''Userdata''); ',...
                              'parent.p = EEGCONTOURS.p; ',...
                              'set(EEGCONTOURS.parent.gui,''Userdata'',parent); ',...
                              'clear parent; ',...
                          'end; ',...
                          'clear EEGCONTOURS; close gcbf;'));
    
    % SAVE GUI PARAMETERS
    G.Bsave = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
        'Position',[indent(2) height+L(1) boxwidth boxheight],...
        'String','SAVE','BusyAction','queue',...
        'TooltipString','Save current parameters as defaults for current working directory.',...
        'BackgroundColor',[0.0 0.0 0.75],...
        'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
                          'eeg_toolbox_defaults(''write'',EEGCONTOURS.p);',...
                          'clear EEGCONTOURS;'));
    

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % NEXT: Run the engine!
    
    G.Bplot = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
        'Position',[indent(3) height+L(1) boxwidth boxheight],...
        'String','PLOT','BusyAction','queue',...
        'TooltipString','Create plots for given parameters.',...
        'BackgroundColor',[0.0 0.5 0.0],...
        'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
        'Callback',strcat('EEGCONTOURS = get(gcbf,''Userdata'');',...
        'set(EEGCONTOURS.gui,''Pointer'',''watch'');',...
        'p = eeg_contours_engine(EEGCONTOURS.p);',...
        'set(EEGCONTOURS.handles.EminimumIntensity,''String'', num2str(p.minimumIntensity), ''Value'', p.minimumIntensity ); ',...
        'set(EEGCONTOURS.handles.EmaximumIntensity,''String'', num2str(p.maximumIntensity), ''Value'', p.maximumIntensity ); ',...
        'set(EEGCONTOURS.handles.EtimePoint,''String'', num2str(p.volt.samplePoint),''Value'', p.volt.samplePoint ); ',...
        'set(EEGCONTOURS.handles.EgridSize,''String'', num2str(p.grid.size),''Value'', p.grid.size ); ',...
        'set(EEGCONTOURS.handles.EgridRes,''String'', num2str(p.grid.res),''Value'', p.grid.res ); ',...
        'EEGCONTOURS.p = p; ',...
        'if isequal(get(EEGCONTOURS.handles.Bhold,''Value''),0),',...
            'p = gui_updateparent(EEGCONTOURS,0);',...
            'set(EEGCONTOURS.gui,''Pointer'',''arrow'');',...
            'close gcbf;',...
        'else, ',...
            'set(EEGCONTOURS.gui,''Userdata'',EEGCONTOURS); ',...
            'set(EEGCONTOURS.gui,''Pointer'',''arrow'');',...
        'end; clear EEGCONTOURS;'));

    % Hold GUI Open checkbox
	G.Bhold = uicontrol('Parent',GUI,'Style','checkbox','Units','Normalized', Font, ...
        'Position',[indent(4) height+L(1) boxwidth boxheight],...
        'String','Hold GUI','BusyAction','queue',...
        'TooltipString','EEG Contours remains open after ''Plot'' command.',...
        'Value',p.hold,'HorizontalAlignment', 'center');
    
    EEGCONTOURS.gui = GUI;
    EEGCONTOURS.handles = G;
    if exist('parent','var'), EEGCONTOURS.parent.gui = parent; end
    set(EEGCONTOURS.gui,'UserData',EEGCONTOURS);
    
