function Fmenu = ma_menu(varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% this function sets up and configures the amat menu bar
%% input is current state, i.e. the name of one of the major buttons
%% or null which is equivalent to startup

if(nargin>0)
    state = varargin{1};
else
    state = '';
end

%% define bar locations
bx = 5;
bw = 110;
by = [500:-40:10];
bh = 30;
buttonpos = repmat([5 500 110 030],length(by),1);
buttonpos(:,2) = by';

%% define titles
amenu(1).ttl = 'Search';
amenu(1).tip = 'Search the database';
amenu(1).callback = 'ma_menu(''Search'');';
amenu(1).pos = buttonpos(1,:);
amenu(1).opt(1).ttl = 'Papers';
amenu(1).opt(1).tip = 'Search by author, year and journal';
amenu(1).opt(1).callback = 'ma_search(''papers_gui'');';
amenu(1).opt(1).pos = buttonpos(5,:);
amenu(1).opt(2).ttl = 'Neighbours';
amenu(1).opt(2).tip = 'Search for neighbours of a given point';
amenu(1).opt(2).callback = 'ma_coords(''neighbours_gui'');';
amenu(1).opt(2).pos = buttonpos(6,:);
%amenu(1).opt(3).ttl = 'Brain area';
%amenu(1).opt(3).tip = 'Search brain region';
%amenu(1).opt(3).callback = 'disp(''not implemented yet'')';
%amenu(1).opt(3).pos = buttonpos(7,:);

amenu(2).ttl = 'Utilities';
amenu(2).tip = 'Database Utilities';
amenu(2).callback = 'ma_menu(''Utilities'');';
amenu(2).pos = buttonpos(2,:);
amenu(2).opt(1).ttl = 'Rebuild Database';
amenu(2).opt(1).tip = 'rebuild references database from pubmed';
amenu(2).opt(1).callback = 'ma_database(''Rebuild'');';
amenu(2).opt(1).pos = buttonpos(5,:);
amenu(2).opt(2).ttl = 'Update Database';
amenu(2).opt(2).tip = 'update references database from pubmed';
amenu(2).opt(2).callback = 'ma_database(''Update'');';
amenu(2).opt(2).pos = buttonpos(6,:);
amenu(2).opt(3).ttl = 'Database Stats';
amenu(2).opt(3).tip = 'Show database Statistics';
amenu(2).opt(3).callback = 'ma_database(''stats'');';
amenu(2).opt(3).pos = buttonpos(7,:);
amenu(2).opt(4).ttl = 'Reload database';
amenu(2).opt(4).tip = 'reload local and global coordinates';
amenu(2).opt(4).callback = 'ma_database(''read_brain_areas''); ma_database(''stats'');';
amenu(2).opt(4).pos = buttonpos(8,:);

amenu(3).ttl = 'Exit';
amenu(3).tip = 'Quit the program';
amenu(3).callback = 'amat(''exit'')';
amenu(3).pos = buttonpos(end,:);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% do setup every time
%% start up menu window

%% check if menu window exists
Fmenu = findobj(get(0,'Children'),'Flat','Tag','Fmenu');
if length(Fmenu) > 1
    close(Fmenu(2:end));
    Fmenu = Fmenu(1);
elseif (length(Fmenu)<1)  %% if no menu window
    Fmenu = figure('IntegerHandle','off',...
        'Name','AMAT',...
        'NumberTitle','off',...
        'Tag','Fmenu');
end

S  = get(0,'ScreenSize');
PF = spm_platform('fonts');

%% set up Fmenu nicely and clear it
set(Fmenu,'Position',[10   160   120   580],...
    'Resize','off',...
    'Pointer','Watch',...
    'Color',[0.3 0.3 0.6],...
    'MenuBar','none',...
    'DefaultUicontrolFontName',PF.helvetica,...
    'DefaultUicontrolFontSize',12,...
    'DefaultUicontrolInterruptible','on');
fc = get(Fmenu,'children');
delete(fc);

%-Add nice header
%-----------------------------------------------------------------------
hA = axes('Parent',Fmenu,'Position',[0 0 1 1],'Visible','Off',...
    'XLim',[0,120],'YLim',[0,580]);

text(60,570,'MetaAnalysis',...
    'Parent',hA,...
    'FontName',PF.times,'FontSize',12,'FontWeight','Bold',...
    'VerticalAlignment','Middle','HorizontalAlignment','Center',...
    'Color',[0 0.9 0.2]);
text(60,550,'Toolbox',...
    'Parent',hA,...
    'FontName',PF.times,'FontSize',12,...
    'FontWeight','Bold',...
    'VerticalAlignment','Middle','HorizontalAlignment','Center',...
    'Color',[0 0.9 0.2]);    
text(60,380,state,'Parent',hA,...
    'FontName',PF.times,'FontSize',12,'FontWeight','Bold',...
    'VerticalAlignment','Middle','HorizontalAlignment','Center',...
    'Color',[0 0.9 0.5]);

%% add major buttons
for i=1:size(amenu,2)
    uicontrol(Fmenu,'String',amenu(i).ttl, ...
        'TooltipString',amenu(i).tip, ...
        'Position',amenu(i).pos,...
        'CallBack',amenu(i).callback,...
        'ForegroundColor',[0 0 1]);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% then add buttons as required

tt = strvcat(amenu.ttl);
minors = strmatch(state,tt,'exact');  %% minor buttons
        
if(length(minors>0))

    for i=1:size(amenu(minors).opt,2)
        uicontrol(Fmenu,'String',amenu(minors).opt(i).ttl, ...
            'Position',amenu(minors).opt(i).pos,...
            'TooltipString',amenu(minors).opt(i).tip, ...
            'CallBack',amenu(minors).opt(i).callback,...
            'ForegroundColor',[0 0.5 0.5]);
    end
end

%% then make the whole thing visible
set(Fmenu,'Visible','on',...
        'Pointer','Arrow');
    
