function abort = instruct(instrSet,varargin)
%Present instructions
%instrSet determines instruction set to display

global window blockLength errorThreshold W H white bground instrSize postInstrTime
global stimuli stimrect respBox data brown

%Set the instructions to be displayed
switch instrSet %determine instruction set
case 1 %initial instructions and training
    keys='CEB'; %continue key for each page
    lines = {{... %cell array of pages, each cell an array of lines
            'WELCOME', ...
            ' ', ...
            'Today you are participating in a very important experiment.  Your effort here will help',...
            'us in our research to better understand neurological diseases and cognitive processes.  ',...
            'We thank you and ask you to please put forth an honest effort during the experiment. ',...
            '',...
            ['Press the ' keys(1) ' key to continue.']},...
        {...
            'INSTRUCTIONS (1 of 2):', ...
            ' ', ...
            'In this experiment you will learn about nine cartoon lizards.'...
            ' ',...
            'On each trial, you will see a lizard in the middle of nine lizard-holes, as shown below.',...
            'Your job is to click on the hole you think that lizard lives in.'...
            ' ',...
            'The lizards are all the same except for their color, so you only need to pay attention to color.',...
            ' ',...
            ['Hit the ' keys(2) ' key to continue.']},...
        {...
            'INSTRUCTIONS (2 of 2):', ...
            ' ',...
            'To start off, we''ll show you the right answers for all nine lizards.',...
            'After that, we''ll test your memory and give you more chances to learn the right answers.',...
            ' ',...
            'Your performance in this experiment is very important to our research,',...
            'so please try to do the best you can.',...
            ' ',...
            ['Press the ' keys(3) ' key to begin the experiment.']...
    }};
case 2 %main task
    keys = 'S'; %key for continuing
    lines={{...
            'MEMORY TEST',...
            ' ',...
            'Now we''ll test your memory.  Click on the lizard-hole for each lizard you see.',...
            'After you respond, the right answer will turn green.  This will help you learn.',...
            ' ',...
            'Things will go very quickly, so do your best to keep up.',...
            ' ',...
            'The experiment will end once you learn to make all the right answers.',...
            ' ',...
            ['Hit ' keys ' to start.']...
    }};
case 3  %update at end of block
    errors = varargin{1}; %number of errors on most recent block
    keys = 'P'; %key for continuing
    lines={{...
            'PROGRESS:', ...
            ' ', ...
            ['You got ' num2str(blockLength-errors) ' correct out of the last ' num2str(blockLength) '.'],...
            ['You need to get ' num2str(blockLength-errorThreshold) ' out of ' num2str(blockLength) ' to finish the experiment.'],...
            ' ', ...
            ['Hit ' keys ' when you''re ready to continue.']...
    }};
case 4  %end of experiment
    errors = varargin{1}; %number of errors on most recent block
    lines={{...
            'GREAT WORK!',...
            ' ',...
            ['Out of the last ' num2str(blockLength) ', you got ' num2str(blockLength-errors) ' correct.'],...
            ' ',...
            'This is the end of the experiment. Please contact your experimenter.'...
    }};
end

%Text coordinate parameters
startX = instrSize*3; %position of left margin
startY = instrSize*3; %vertical position of first line
incrY = instrSize*1.5; %line height
Screen(window, 'TextSize', instrSize); %set font size

%Present instructions:
abort = 0; %default output: experiment was not aborted
for page = 1:length(lines) %loop through pages
    Screen(window,'FillRect',bground); %clear screen
    y = startY; %start at top line
    for i = 1:length(lines{page}) %write this page of instructions line by line
       Screen(window, 'DrawText', lines{page}{i}, startX, y, white); %write this line
       y = y + incrY; %move to position of next line
    end
    if instrSet==1 && page==2 %show example tableau
        shift = H/5; %downward shift to make room for instructions
        compress = .75; %compression factor (vertical and horizontal) to take up less room
        boxes = respBox*compress; %reduce size of each response region
        boxes(:,[1 3]) = boxes(:,[1 3]) + W/2*(1-compress); %recenter horizontally
        boxes(:,[2 4]) = boxes(:,[2 4]) + H/2*(1-compress) + shift; %recenter vertically, then move down by shift
        for r=1:9 %loop responses
            Screen('FillOval',window,brown,boxes(r,:)); %draw this response region
        end
        Screen(window,'PutImage',stimuli{1},stimrect*compress+[1 0 1 0]*(1-compress)*W/2+[0 1 0 1]*((1-compress)*H/2+shift)); %show arbitrary stimulus, compressed and shifted as with response regions
    end
    Screen('Flip',window); %update display
    if instrSet<4 %wait for keypress to move on, except at end of experiment
        x = getKeyResp(keys(page), keys(page), 0); %wait for appropriate key
        if x(1)==-1 %escape sequence activated
            abort=-1;
            break %break page loop
        end
        if instrSet==1 && page==1 %this is the first time subject presses a key (experimenter might have set up program earlier)
            data.dateTime = clock; %timestamp for start of experiment
        end
    end
end
if instrSet<4 && abort==0
    Screen(window,'FillRect',bground); %clear screen
    Screen('Flip',window); %update display
    WaitSecs(postInstrTime); %pause before moving on
end