function preparestream


%this prepare stream gets things ready for the simple verison,  T1+1 and
%T2+1 blank conditions

%stream pattern
global ITEMPRESENT ITEMOFF RANDOMBGCOLOR ONTIMINGVARIATION OFFTIMINGVARIATION ITEMS SIZES LOCATIONSX LOCATIONSY COLORS BACKCOLORS FRAMECOLOR ITEMSETS LOCATIONSXVAR LOCATIONSYVAR LOCKP1MASKLOCATION LOCKM1MASKLOCATION ORTHOPAIRS
%stream structure
global PREITEMS POSTITEMS MINRSVPLENGTH P1BLANKPERCENTAGE M1BLANKPERCENTAGE  TARGETDIST  SKELETAL STARTVARIANCE LAGS  TARGETREPETITION DRECYCLE INFORMUSER DTNOREP ITEMSETPOOL


%frame parameters

global Axeshandle ITEMPools Setsize 


%thisstream

global Targpos Targlag Streamlength Stream Streamnum Stream2 Streamnum2 Targnum Events Dpos Dnum Numtargs Targset Eventscopy Condition priorcondition Thiscondition


%screens

global window woff wblank


global condcopy

Thiscondition = ceil(rand*size(Condition,2));


condcopy = Condition(:,Thiscondition);

Numtargs = 2;

current = 6;
Targpos = 0;
Targlag = 0;

%set the lags for the targets
%and pick the T's and D's

thislag = floor(STARTVARIANCE*rand) + PREITEMS;
Stream = 0;
Streamnum = 0;

Streamnum2 = 0;
Stream2 = 0;

Targnum = 0;
Targset = 0;
if(size(ORTHOPAIRS) > 0)
    orthoflag = 1;
end

% Factor 1: Lag    80/240 msec
% Factor 2:  T1 stream (up or down)    0 is up, 1 is down
% Factor 3:  Dot stream (up or down)
% Factor 4:  dot time  (T1 or T2)
% Factor 5:  T2 stream (up or down)
%Factor 6:  Dot or No Dot  (25%)
for(t = 1:Numtargs)   
    
    Targpos(t) = thislag;     %position in RSVP
    thisset = ITEMSETS(t);
    Targset(t) = thisset;       
    
    %pick one
    num = ceil(rand*Setsize(thisset));
	%do REPETITION CHECKING
	
    if(TARGETREPETITION == 0)
        while(sum(Targnum== num) >0)
			num = ceil(rand*Setsize(thisset)); 'FOUND A TARG REPEAT';
		end
	end
    Targnum(t) = num;
	
	
	Streamnum(thislag) = Targnum(t);
	Stream(thislag) = t;  
	
    [x c] = min(LAGS(thisset,:));  %find the zero
    lagsize = c - 1;
	
    lag = Condition(1,Thiscondition);
    if (lag == 0)
        lag = LAGS(thisset,ceil(lagsize*rand));          
	end

    if(Numtargs > t)
        Targlag(t) = lag;
	end
    thislag = thislag + lag;
end

%Targnum(2) = min(Targnum(1) +1,18);

%now determine the length of the stream,

Streamlength = max(MINRSVPLENGTH, Targpos(Numtargs)+POSTITEMS);
s = length(Stream);
dif = Streamlength - s;
if(dif > 0)
    Stream(s+1:s+dif) = 0;
    Streamnum(s+1:s+dif) = 0;
end

s = length(Stream2);
dif = Streamlength - s;

if(dif > 0)
	
	Stream2(s+1:s+dif) = 0;
    Streamnum2(s+1:s+dif) = 0;
end

%now fill in the distractors, as appropriate
if(SKELETAL)
    Stream(1) = -1;  %if skeletal, put -1's in for all the blanks
    for(i = 2:Streamlength)
        if(Stream(i) == 0 & Stream(i-1) <=.9)
            Stream(i) = -1;
		end
	end
end
%now fill in the actual distractors
thisset = ITEMSETS(max(TARGETDIST)+1);
Numd = 0;
for(i = 1:Streamlength)   %1-n = target #     -1 = blank      0 = D
	
	Numd = Numd + 1;
	Dpos(Numd) = i;
	num = pickd(thisset);
	num2 = pickd(thisset);
	
	%do REPETITION CHECKING
	Dnum(1:2,Numd) = 0;
	
	if(DRECYCLE > 0)
		checkback = min(Numd-1,DRECYCLE);
		while(num == num2  | sum(sum(Dnum(:,(Numd-checkback):Numd)== num)) >0 | sum(sum(Dnum(:,(Numd-checkback):Numd)== num2)) >0)
			
			
			num = pickd(thisset);
			
		end
	end
	Dnum(1,Numd) = num;  %do repeitition checking
	  %do repeitition checking
	
	if(Stream(i) ==0)
		Streamnum(i) = Dnum(1,Numd);
		
	end

end


%now set up the schedule

currenttime = 0;
evcount = 0;

%Events    1 is time, 2 is set,  3 is number   4 is on/off  5 is xpos  

%6 is ypos   7 is fore color   8 is back color

%note that randomized variation in color and position is nonfunctional due
%to the psychtoolbox migration


Events = 0;
Events(evcount+1,1) = 0;

dotnow = 0;


blankgap = .0665;
offset = .026;

for(i = 1: Streamlength)
	
    thisset = 0;
 	if(Stream(i) == 0)
        thisset = ITEMSETS(max(TARGETDIST)+1);%distractor
	elseif(Stream(i) > 0)
        thisset = Targset(Stream(i));  %item        
	end

	evad = 0;	
    if(i == 1)
        Events(evcount+1,1) = 0;
		Events(evcount+2,1) = .066;
		eventbasetime = 0;		
	else
		if(Stream(i-1)<2)   %we are after a T2
		
			eventbasetime = Events(evcount-3,1);
		else
			eventbasetime = Events(evcount-5,1);
		end
		%blanks
		Events(evcount+2,1) = eventbasetime + ITEMPRESENT+.066; 
		Events(evcount+2,2) = -1;
		
		Events(evcount+1,1) = eventbasetime + ITEMPRESENT; 	
		Events(evcount+1,7) = Stream(i);	
		Events(evcount+1,2) = thisset;

		Events(evcount+1,3) = Streamnum(i);
		
		%Events(evcount+1:evcount+2,7) = COLORS(thisset);
		Events(evcount+1,8) = BACKCOLORS(thisset);
		
		%use 5 to indicate a dotting
		
		
		
		evcount = evcount +s;
		
end




%final OFF Event
% Events(evcount+1:evcount+2,1) = Events(evcount,1) + ITEMPRESENT+ITEMOFF;        
% Events(evcount+1:evcount+2,2) = thisset;
% Events(evcount+1:evcount+2,3) = Streamnum(i);
% Events(evcount+1:evcount+2,4) = 0;

%evcount = evcount + 2;    

Events = sortrows(Events);
Eventscopy = Events;
priorcondition = Condition;
cs = size(Condition,2);
if(cs > 1);
	for(i = Thiscondition+1:cs) 
		Condition(1:6,i-1) =  Condition(1:6,i);
	end
end
Condition  = Condition(1:6,1:(cs-1));




function retval = pickd(thisset)

global DTNOREP Setsize Targnum

if(DTNOREP == 0)
	retval = ceil(rand*Setsize(thisset));
else
	flag = 1;
	while(flag > 0)
		retval = ceil(rand*Setsize(thisset));
		flag = 0;
		for(i = 1:length(Targnum))
			if(retval == Targnum(i))
				flag = 1;
			end
		end
	end
end

