function pairComp(sesDat)
% a pair comparison test with various possible quesions (preference e.g.) and comments
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% copy useful variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

UIControl_FontSize_bak = get(0, 'DefaultUIControlFontSize'); 
set(0, 'DefaultUIControlFontSize', 28);

reREAD=sesDat.reREAD;
noTst=sesDat.noTst;
nbTst=sesDat.nbTst;
if reREAD
   rsl=sesDat.tstDat{noTst}.rsl;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% read the test file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
test=readPairComp(sesDat.tstDat{noTst}.tstFile);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set the test screen
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


hf=figure;		
set(hf,'units','normalized');
%set(hf,'position',[0 .5 1 .4]);
set(hf,'position',[0 .05 .55 .75]);
set(hf,'closereq','');     %%%
set(hf,'numbertitle','off');
set(hf,'menubar','none');
set(hf,'name',sprintf('pair #1/%d',test.nbComb));
set(gca,'position',[0 0 1 1]);
axis([0 1 0 1]);
set(gca,'yticklabel','');
set(gca,'xticklabel','');
grid off;
hp=gca;

butCol=[.6 .9 .6]; 				% by default pale green
set(hp,'color',[.7 .7 .7]);     % gray background

% Figure-event properties

set(gcf,'KeyPressFcn','pairKey')

% Create the 3 frames

% set the upper frame

y1=.76;
y2=.75;
y3=.31;
y4=.3;
x1=0;
x2=.8;
x3=.81;

hold on

% sound frame
fill([x1 x1 1 1],[y1 1 1 y1],[.8 .8 1]);
% questions frames
fill([x1 x1 1 1],[y3 y2 y2 y3],[.7 .7 1]);
% comments and "finished" button frame
fill([x1 x1 1 1],[0 y4 y4 0],[.9 .9 .1]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PUSH BUTTONS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

y=(1-y1);
yy=y/20;
h=y-2*yy;

x=(1-x1);
xx=(1-x1)/8;

wab=3*xx;
wstop=2*xx;
xa=xx/2;
xb=9/2*xx;
xstop=3*xx;

posA=[xa y1+yy wab h];
posB=[xb y1+yy wab h];
posStop=[xstop y1+yy wstop h];

ha=uicontrol('style','pushbutton','string','A','units','normalized','position',posA, 'FontSize', 56);
set(ha,'backgroundcolor',butCol);
set(ha,'callback',sprintf('PCpush(%d,1)',hf.Number));

if strcmp(computer,'PCWIN')
   hStop=uicontrol('style','pushbutton','string','STOP','units','normalized','position',posStop, 'FontSize', 56);
   set(hStop,'callback',sprintf('PCpush(%d,2)',hf.Number));
else
   hStop='';
end

hb=uicontrol('style','pushbutton','string','B','units','normalized','position',posB, 'FontSize', 56);
set(hb,'backgroundcolor',butCol);
set(hb,'callback',sprintf('PCpush(%d,3)',hf.Number));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% questions and scales
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~isfield(test,'nbScale')
   test.nbScale=0;
   test.scale='';
end
if ~isfield(test,'nbQuest')
   test.nbQuest=0;
   test.quest='';
end

test.hScale='';
test.hQuest='';

nbGUI=2*test.nbScale+test.nbQuest;

y=(y2-y3);
h=y/nbGUI;
hh=h/20;

x=(1-x1);
xx=(1-x1)/8;

noGUI=1;
% SCALES
for noScale=1:test.nbScale
   s=test.scale(noScale);  
   Y1=y2-((2*noGUI-1)*h-hh);
   %display text
   ho='horizontalAlignment';
   ve='verticalAlignment';
   text((1-x1)/2,Y1,s.name,ho,'center',ve,'bottom');	% Scale name
   % Level names
   text(x1+xx,Y1,s.level{1},ho,'left',ve,'bottom');	
   text(1-xx,Y1,s.level{3},ho,'right',ve,'bottom');
   
   Y2=Y1-h+hh;
   posG=[xx Y2 x-2*xx h-2*hh];   
   hScale(noScale)=uicontrol('style','slider','units','normalized','position',posG,'min',s.minn,'max',s.maxx,'value',s.minn);
   if reREAD
      set(hScale(noScale),'value',rsl.scale(1,noScale));
   end
   noGUI=noGUI+1;
end

% QUESTIONS check boxes
for noQuest=1:test.nbQuest
   noGUI=2*test.nbScale+noQuest;
   posG=[xx y2-(noGUI*h-hh) x-2*xx h];
   hQuest(noQuest)=uicontrol('style','checkbox','string',test.quest{noQuest},'units','normalized','position',posG,'min',0,'max',1);
   set(hQuest(noQuest),'callback',sprintf('PCcheck(%d,%d)',hf.Number,noQuest));   
   if reREAD
      set(hQuest(noQuest),'value',rsl.quest(1,noQuest));
   end
   noGUI=noGUI+1;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% "Comments" field
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if test.COM
   if reREAD
      stri=rsl.com{1};
   else
      stri='Write comments here.';
   end
   posCom=[x1 0 x2-x1 y4];
   hCom=uicontrol('style','edit','max',2,'string',stri,'units','normalized','position',posCom);
   dat.com=stri;
else
   hCom='';
   dat.com='';
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% "Submit" button
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
posFin=[x3 y4/3 1-x3 y4/3];
hFin=uicontrol('style','pushbutton','string','Submit','units','normalized','position',posFin);
set(hFin,'callback',sprintf('endPairComp(%d)',hf.Number));


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Test timer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dat.t0=clock;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Send useful datas
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% sounds and combination
dat.noComb=1;
dat.nbComb=test.nbComb;
dat.comb=test.comb;
dat.quest=test.quest;
dat.nbQuest=test.nbQuest;
dat.scale=test.scale;
dat.nbScale=test.nbScale;
dat.stri=stri;
dat.COM=test.COM;

% handles
dat.hf=hf;
dat.butCol=butCol;

dat.ha=ha;
dat.hb=hb;
dat.hStop=hStop;
dat.hQuest=hQuest;
%dat.hScale=hScale; % errors when uncommented
dat.hFin=hFin;
dat.hCom=hCom;

% global datas
dat.test=test;
dat.sesDat=sesDat;
if reREAD
   dat.rsl=rsl;
else
	dat.rsl='';
end

set(hf,'userdata',dat);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% clean up
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;

