function showstream

%stream pattern
global ITEMPRESENT ITEMOFF RANDOMBGCOLOR ONTIMINGVARIATION OFFTIMINGVARIATION ITEMS SIZES LOCATIONSX LOCATIONSY COLORS BACKCOLORS FRAMECOLOR ITEMSETS 
%stream structure
global PREITEMS POSTITEMS MINRSVPLENGTH P1BLANKPERCENTAGE M1BLANKPERCENTAGE MAXTARGETS MINTARGETS SKELETAL STARTVARIANCE LAGS  TARGETREPETITION DRECYCLE INFORMUSER

%frame parameters

global Axeshandle ITEMPools Setsize  Fixcross 

%thisstream

global Targpos Targlag Streamlength Stream Streamnum Targnum Events Dpos Dnum Time Numtargs Targset Trialnum

%user data

global In1 In2 Intime Miscflag

%screens

global window woff wblank centerx centery wsize wfix wlines

%Misc
global waits xes plannedtime actualtime Scrnsize p1p2  finaltime keyspressed keycount dotsize condcopy


%Fixation for 1500 msec

%Screen('CopyWindow',wfix,window,[50-wsize,50-wsize,50+wsize,50+wsize],[centerx-wsize,centery-wsize,centerx+wsize,centery+wsize]);
Screen('CopyWindow',wfix,window,[1,1,wsize*2+1,wsize*2+1],[centerx-wsize,centery-wsize,centerx+wsize,centery+wsize]);

%a = getChar;
%while(a~=13)
%	a = getChar;
%end	
pause(1)

Screen('CopyWindow',wblank,window,[1,1,wsize*2+1,wsize*2+1],[centerx-wsize,centery-wsize,centerx+wsize,centery+wsize]);
%create the actual display screen

nowtime = GetSecs;

stoptime = nowtime + Streamlength *(ITEMPRESENT+ITEMOFF) + 1.5


waitscount = 0;  %  testing variables
xcount  = 0;
waits = 0;
%do fixcross!!!

numevents = size(Events,1);

Time = 0;



priority(0);   %set to 2 for production
plannedtime = 0;
actualtime = 0;

keyspressed = zeros(50,2);
keycount = 0;
keydown = 0;
dotup = 0;

nowtime = GetSecs;
starttime = nowtime;
firsttime = nowtime;
Events(:,1) = Events(:,1) + starttime;

dottrial = 0;

[keyd secs keyc] = KbCheck;
if(keyd)
	keydown = 1;
end


while(nowtime < stoptime & Events(numevents,1) < 9999999)   
	%check for key presses, get RT
	
	[keyd secs keyc] = KbCheck;
	if(keyd)
		if(keydown ==0)
			keycount = keycount + 1;
			keyspressed(keycount,1) = GetSecs-starttime;
			
			[x i] = max(keyc);
			keyspressed(keycount,2) =i;
			keydown = 1;
			FlushEvents('keyDown');
        end
	
	else
		keydown = 0;
	end

    nowtime = GetSecs;    
    % T1
    xcount = xcount+1;
    [x i] = min(Events(:,1)-nowtime); %includes minus refresh rate
    
    xes(xcount) = x;    
	dotsize = 5;
	sep = 6;
    if(x < 0)   % an event time has happened
        thisset = Events(i,2);
        num = Events(i,3);
		Screen(window,'WaitBlanking');   %wait one frame
		missedby = Events(i,1)-nowtime;
		
		
        if (Events(i,2)==99)
			
			%end the stream!
			stoptime = nowtime;
			actualtime(i) = GetSecs-starttime;  %time the previous event was actually displayed
			
			Events((i+1):numevents,1)= Events((i+1):numevents,1) -missedby;
			Time(i) =  actualtime(i);
			Events(i,1)  = Events(i,1) + 9999999-firsttime;  %make the current event invalid
			
			
		elseif (Events(i,2)==3)
	
			%Display a bar
			
			Screen('CopyWindow',wlines(num),window,[1,1,wsize*2+1,wsize*2+1],[centerx-wsize,centery-wsize,centerx+wsize,centery+wsize]);
			
			
			actualtime(i) = GetSecs-starttime;  %time the previous event was actually displayed
			
			Events((i+1):numevents,1)= Events((i+1):numevents,1) -missedby;
			Time(i) =  actualtime(i);
			Events(i,1)  = Events(i,1) + 9999999-firsttime;  %make the current event invalid
			
			
			
		elseif (Events(i,2)<3&Events(i,2)>0)
	
			%Screen('CopyWindow',wlines(ceil(rand*3)+9),window,[1,1,wsize*2+1,wsize*2+1],[centerx-wsize,centery-wsize,centerx+wsize,centery+wsize]);
			Screen('CopyWindow',woff(thisset,num),window,[1,1,wsize*2+1,wsize*2+1],[centerx-wsize,centery-wsize,centerx+wsize,centery+wsize]);
%        	Screen('CopyWindow',woff(thisset,num),window,[1,1,wsize*2+1,wsize*2+1]);
        
            actualtime(i) = GetSecs-starttime;  %time the previous event was actually displayed	
			Events((i+1):numevents,1)= Events((i+1):numevents,1) -missedby;
			Time(i) =  actualtime(i);
			Events(i,1)  = Events(i,1) + 9999999-firsttime;
			
			
		elseif( Events(i,2)<0)  %blank
	
	
	
			Screen('CopyWindow',wblank,window,[1,1,wsize*2+1,wsize*2+1],[centerx-wsize,centery-wsize,centerx+wsize,centery+wsize]);
			
			actualtime(i) = GetSecs-starttime;  %time the previous event was actually displayed
			
			Events((i+1):numevents,1)= Events((i+1):numevents,1) -missedby;
			Time(i) =  actualtime(i);
			Events(i,1)  = Events(i,1) + 9999999-firsttime;
			
			
			
		else   %unknown event!!1  skip it
	
			actualtime(i) = GetSecs-starttime;  %time the previous event was actually displayed
			
			Events((i+1):numevents,1)= Events((i+1):numevents,1) -missedby;
			Time(i) =  actualtime(i);
			Events(i,1)  = Events(i,1) + 9999999-firsttime;
			
		end	
	
	
	end

end
	FlushEvents('keyDown');

	priority(0);
Screen(window,'WaitBlanking',0); 
Screen('CopyWindow',wblank,window);



%     	priorityLevel=MaxPriority(window,'WaitBlanking');
%Rush(loop,priorityLevel);


'Estimated duration';
stoptime-starttime;

'actual duration';
totaltime = nowtime -starttime;

priority(0);



%obtain user response in arrays In1 and Intime
Screen('CopyWindow',wblank,window);

disableinput = 0;

if(disableinput==0)
	
	s = sprintf('____________________');
	starty = centery*2 - 550;
	SCREEN(window,'DrawText',s,10,starty+70,[10,10,10]);
	finaltime = GetSecs-starttime;
	starttime = GetSecs;
	In1 = 0;
	numinput = 0;
	
%	In1 = 13;  %COMMMMENT THIS!!!
	
	
	
	while (In1(max(1,numinput)) ~=13)
		if(In1(max(1,numinput)) == 8)  %backspace
			numinput = max(numinput -1,1);
		else
			numinput = numinput + 1;
		end
		In1(numinput)  = GetChar;
		Intime(numinput) = GetSecs-starttime;
		
		if(In1(max(1,numinput)) == 8)  %backspace
			s = sprintf('%c',' ');
%			SCREEN(window,'DrawText',s,10+(numinput-1)*18,starty+50,[10,10,10]);
			SCREEN(window,'FillRect',[150,150,150],[8+(numinput-1)*22,starty+20,10+(numinput)*22,starty+70]);
			
		else
		
				s = sprintf('%c',In1(numinput));
				SCREEN(window,'DrawText',s,10+numinput*22,starty+50,[10,10,10]);
		
		end	
	end
end



%if(INFORMUSER)
if(1)
	outp  = 0;
	starty = starty + 50
		s = sprintf('The letters were');
		
		
		TextWidth = SCREEN (window, 'TextWidth', s);
		sw =     SCREEN(window,'DrawText',s,10,starty+100,[10,10,10]);
		for(i = 1: Numtargs)
			s = sprintf('%c',ITEMS(Targset(i),Targnum(i)));
			%        outp(i) = ITEMS(Targset(i),Targnum(i));
			SCREEN(window,'DrawText',s,10+sw+(i-1)*50,starty+170,COLORS(Targset(i),1:3));        
		end
	
end



if(disableinput ==0)
	s = sprintf('Press Enter To Continue')
	SCREEN(window,'DrawText',s,10,starty+250,[10,10,10]);
	a = GetChar; % UNCOMMMENT ME JESUS

end



Screen('CopyWindow',wblank,window);

Miscflag = p1p2;
