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  bardur barorient


%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);

lag = 0;

Numtargs  = 4;

IDD = condcopy(1)

%Factor 1,  3 conditions
%TTTT
%TDTDTDT
%TTDDDTT



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

Soa = 1;
%Soa of 2 used to switch us back to 50 ms SOA


thislag = (floor(STARTVARIANCE*rand) + PREITEMS) *Soa;

Stream = 0;
Streamnum = 0;


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


for(t = 1:Numtargs)   
    Targpos(t) = thislag;     %position in RSVP
    thisset = ITEMSETS(1);
    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;  
	
	if(Stream(thislag) ==1)
		t1pos = thislag;
    end

%Factor 1,  3 conditions
%TTTT
%TDTDTDT
%TTDDDTT



    
	switch(IDD)
        
        case(1)
            thislag = thislag + 1;
            
        case(2)
            thislag = thislag + 2;
    end
        
end

%now determine the length of the stream,


Targpos

Streamlength =  (Targpos(Numtargs)+POSTITEMS*Soa);

	
	
s = length(Stream);
dif = Streamlength - s;
if(dif > 0)
    Stream(s+1:s+dif) = 0;
    Streamnum(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);
	
	
	%do REPETITION CHECKING
	Dnum(1:2,Numd) = 0;
	
	if(DRECYCLE > 0)
		checkback = min(Numd-1,DRECYCLE);
		while(sum(sum(Dnum(:,(Numd-checkback):Numd)== num)) >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 = 1;

%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(1,1:7) = 0;



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
	eventbasetime = Events(evcount,1);   %previous item time
	
	Events(evcount+1,1) = eventbasetime + 8*.013333;   
	
	Events(evcount+1,7) = Stream(i);	
	Events(evcount+1,2) = thisset;
	Events(evcount+1,3) = Streamnum(i);	
	evcount = evcount +1;
end		




Events(evcount+1,1) = Events(evcount-1,1)+ITEMPRESENT;
Events(evcount+1,2) = 99;
evcount = evcount + 1;



%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;    

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

