function ihbSPM = ihb_GetSPM
%----------------------------------------------------------------------------------
% FORMAT ihbSPM = ihb_GetSPM
% Return information from SPM.mat file for both SPM99 and SPM2
% ihbSPM - struct with information (see ihb_InitStructureSPM.m)
%----------------------------------------------------------------------------------
%   07.07.03    Sergey Pakhomov
%----------------------------------------------------------------------------------
saveDir = pwd;
%----------------------------------------------------------------------------------
% Check existance of figures used by spm_getSPM
%----------------------------------------------------------------------------------
ihbdfl_spm_fig_Interactive = 'Interactive';
ihbdfl_spm_fig_SelFileWin = 'SelFileWin';
ihbdfl_spm_fig_ConMan = 'ConMan';
hFigInteractive = findobj('Type', 'figure', 'Tag', ihbdfl_spm_fig_Interactive);
hFigSelFileWin = findobj('Type', 'figure', 'Tag', ihbdfl_spm_fig_SelFileWin);
hFigConMan = findobj('Type', 'figure', 'Tag', ihbdfl_spm_fig_ConMan);
%----------------------------------------------------------------------------------
ihbSPM = ihb_InitStructureSPM;
swd    = spm_str_manip(spm_get(1,'SPM.mat','Select SPM.mat'),'H');
if length(whos('-file', fullfile(swd,'SPM.mat'), 'SPM'))==0
    
    v = spm('Ver');
    if ~strcmp(v, 'SPM99')
        cd(saveDir);
        ihbSPM = [];
        str = 'You specify SPM.mat created by SPM99 but running SPM2';
        title = 'Wrong SPM version';
        h = msgbox(str, title, 'error', 'modal');
        waitfor(h);
        return;
    end
    [SPM,VOL,xX,xCon,xSDM] = ihb_spm_getSPM99(swd); % SPM99 SPM.mat file
    ihbSPM.title = SPM.title;
    ihbSPM.Z = SPM.Z;
    ihbSPM.n = SPM.n;
    ihbSPM.STAT = SPM.STAT;
    ihbSPM.df = SPM.df;
    ihbSPM.u = SPM.u;
    ihbSPM.k = SPM.k;
    ihbSPM.XYZ = SPM.XYZ;
    ihbSPM.XYZmm = SPM.XYZmm;
    ihbSPM.R = VOL.R;
    ihbSPM.FWHM = VOL.FWHM;
    ihbSPM.VOX = VOL.VOX;
else
    v = spm('Ver');
    if ~strcmp(v, 'SPM2')
        cd(saveDir);
        ihbSPM = [];
        str = 'You specify SPM.mat created by SPM2 but running SPM99';
        title = 'Wrong SPM version';
        h = msgbox(str, title, 'error', 'modal');
        waitfor(h);
        return;
    end
    [SPM,xSPM] = ihb_spm_getSPM2(swd);              % SPM2 SPM.mat file
    ihbSPM.title = xSPM.title;
    ihbSPM.Z = xSPM.Z;
    ihbSPM.n = xSPM.n;
    ihbSPM.STAT = xSPM.STAT;
    ihbSPM.df = xSPM.df;
    ihbSPM.u = xSPM.u;
    ihbSPM.k = xSPM.k;
    ihbSPM.XYZ = xSPM.XYZ;
    ihbSPM.XYZmm = xSPM.XYZmm;
    ihbSPM.R = xSPM.R;
    ihbSPM.FWHM = xSPM.FWHM;
    ihbSPM.VOX = xSPM.VOX;
end
%----------------------------------------------------------------------------------
% Delete figures used by spm_getSPM if they were created during above call
%----------------------------------------------------------------------------------
if isempty(hFigInteractive) 
    delete(findobj('Type', 'figure', 'Tag', ihbdfl_spm_fig_Interactive)); 
end;
if isempty(hFigSelFileWin) 
    delete(findobj('Type', 'figure', 'Tag', ihbdfl_spm_fig_SelFileWin)); 
end;
if isempty(hFigConMan) 
    delete(findobj('Type', 'figure', 'Tag', ihbdfl_spm_fig_ConMan)); 
end;
%----------------------------------------------------------------------------------
cd(saveDir);