function abort = demo
%Initial training block for identification experiment
%Show each stimulus and have subject click on correct response

global data window demoIti FBtime white green red brown bground wrongTime
global W H respBox stimuli stimrect responseSize

abort = 0; %default output indicating experiment was not aborted during this function

highlight = .05; %fraction of hole diameter for highlighting
highlightMatrix = [1+highlight 0 -highlight 0;0 1+highlight 0 -highlight;-highlight 0 1+highlight 0;0 -highlight 0 1+highlight]; %used for drawing ring around response region

Screen('TextSize',window,responseSize); %set font size
prompt = 'This lizard lives in the highlighted hole. Click on his hole.'; %instruction on each trial
[promptWidth,~]=Screen(window,'DrawText',prompt,0,0); %draw text in black and return new pen location; a trick to get display width

z=randperm(9); %random stimulus order
for i=z %loop through stimuli
    
    %Present stimulus etc.
    Screen('FillRect',window,bground); %clear screen
    Screen('DrawText',window,prompt,W/2-promptWidth/2,responseSize*1.5,white); %present prompt
    Screen('PutImage', window, stimuli{i}, stimrect); %draw stimulus
    Screen('FillOval',window,green,respBox(data.map(i),:)*highlightMatrix); %draw correct response region oversized in green (center gets covered in brown)
    for r=1:9 %loop response regions
        Screen('FillOval',window,brown,respBox(r,:)); %draw response region
    end
    SetMouse(W/2,H/2,window); %put mouse in center of screen
    ShowCursor; %unhide mouse
    Screen('Flip',window); %update display
    
    %Wait for click on correct response region
    ok = 0; %no correct response yet
    while ~ok
        x = getResp; %waits for mouse click on a response; returns chosen location and response time
        resp = x(1); %chosen location, in 1:9
        if resp == -1 %escape sequence activated
            abort = -1;
            break %break while(~ok) loop
        end
        if resp == data.map(i) %correct response; show feedback and move on to next trial
            ok = 1; %will exit loop
            Screen('FillRect',window,bground); %clear screen
            Screen('DrawText',window,prompt,W/2-promptWidth/2,responseSize*1.5,white); %redraw prompt
            Screen(window, 'PutImage', stimuli{i}, stimrect); %redraw stimulus
            for r = 1:9 %loop responses
                Screen('FillOval',window,brown,respBox(r,:)); %redraw response region
            end
            Screen('FillOval',window,green,respBox(resp,:)); %draw correct region in solid green
            HideCursor %hide mouse
            Screen('Flip',window); %update display
        elseif resp>0 %incorrect response (conditioning on resp>0 to ignore -1 outputs from getResp)
            Screen('FillRect',window,bground); %clear screen
            Screen('DrawText',window,prompt,W/2-promptWidth/2,responseSize*1.5,white); %redraw prompt
            Screen(window, 'PutImage', stimuli{i}, stimrect); %redraw stimulus
            Screen('FillOval',window,green,respBox(data.map(i),:)*highlightMatrix); %redraw green circle for border
            for r = 1:9 %loop responses
                Screen('FillOval',window,brown,respBox(r,:)); %redraw response region
            end
            Screen('FillOval',window,red,respBox(resp,:)); %draw chosen region in solid red
            Screen('Flip',window); %update display
            WaitSecs(wrongTime); %pause
            Screen('FillRect',window,bground); %clear screen
            Screen('DrawText',window,prompt,W/2-promptWidth/2,responseSize*1.5,white); %redraw prompt
            Screen(window, 'PutImage', stimuli{i}, stimrect); %redraw stimulus
            Screen('FillOval',window,green,respBox(data.map(i),:)*highlightMatrix); %redraw green circle for border
            for r = 1:9 %loop responses
                Screen('FillOval',window,brown,respBox(r,:)); %redraw response region
            end
            Screen('Flip',window); %update display
        end
    end
    if abort==-1
        break %break for(i) loop
    end
    WaitSecs(FBtime); %pause for feedback
    Screen('FillRect',window,bground); %clear screen
    if i~=z(9) %keep circles on screen while clearing everything else, except on last trial
        for r=1:9 %loop responses
            Screen('FillOval',window,brown,respBox(r,:)); %draw response region
        end
    end
    Screen('Flip',window); %update display
    WaitSecs(demoIti); %pause between trials
end
