function dragdrop(action)
%drag and drop variables to the plotit or analyzeit variable boxes.
global xfile xfnew xf xhand whichfile xnum whichnew whereat


bcolor=get(xhand.varlist,'backgroundcolor');
if any(bcolor-1)
	varname=xf.varlist{get(xhand.varlist,'value')};
	if length(xfile{varname}(:))<2
		errordlg('A scalar has been selected!','Error');
		return;
	end;
	newwindow=0;
else
	bcolor=get(xhand.newvarlist,'backgroundcolor');
	if any(bcolor-1)
		varnum=get(xhand.newvarlist,'value');
		varname=xf.newvarlist{varnum};
		eval(['temp=length(xfnew(' num2str(varnum) ').data);']);
		if temp<2
			errordlg('A scalar has been selected!','Error');
			return;
		end;
		newwindow=1;
	else
		return;
	end;
end;

switch action

case 'graphit'
% 	temp=get(gco,'string');
% 	pan=str2num(temp(10));
	pan=find(xhand.panpush(1:4)==gco);
	temp=get(gco,'position');
	oldname=get(xhand.varpanedit(pan),'string');
	i=0;
	if ~isempty(oldname)
		s=oldname;
		while ~isempty(s)
			i=i+1;
			[t,s]=strtok(s,' ,');
		end;
		oldname=[oldname ', '];
	end;
	if newwindow
		whichnew.graph(pan,i+1)=xnum.newvarlist;
		whichfile(pan,i+1)=10+whereat(whichnew.graph(pan,i+1));
		
		filemapping(varname,pan,i);

	else
		whichfile(pan,i+1)=xnum.file;
		whichnew.graph(pan,i+1)=0;
	end;
	set(xhand.varpanedit(pan),'string', [oldname varname]);
	fillvoid(pan,'mouse');
	
case 'analyzeit'
	pan=find(xhand.anapush(1:5)==gco);
	oldname=get(xhand.ana(pan),'string');
	i=0;
	if ~isempty(oldname),
		s=oldname;
		while ~isempty(s)
			i=i+1;
			[t,s]=strtok(s,' ,');
		end;
		oldname=[oldname ', '];
	end;
	if newwindow
		whichnew.ana(pan,i+1)=xnum.newvarlist;
		whichfile(pan,i+1)=10+whereat(whichnew.ana(pan,i+1));
% 		pointer=whichnew.ana(pan,i+1);
		filemapping(varname,pan,i);
	else
		whichfile(pan,i+1)=xnum.file;
		whichnew.ana(pan,i+1)=0;
	end;
	set(xhand.ana(pan),'string', [oldname varname]);

end; %switch
return;

function filemapping(varname,pan,i);
% dealing with mapping files with different analyses
global whichfile whichnew whereat xnum tflag

suffixstr=[];

% for ijk=1:length(tflag)
% 	dum=length(tflag{ijk})-1;
% 	if length(varname)>dum
% 		if strcmpi(varname(end-dum : end),tflag{ijk})
% 			suffixstr=tflag{ijk};
% 			break;
% 		end;
% 	end;
% end;

dum=findstr('_',fliplr(varname));
if ~isempty(dum);
	thisindex=find(strcmpi(varname(end-dum+1 : end),tflag)==1);
	if ~isempty(thisindex)
		suffixstr=tflag{thisindex};
	end;
end;
	
if strcmpi(suffixstr,'_avg')
	whichfile(pan,i+1)=20+whereat(xnum.newvarlist);
elseif strcmpi(suffixstr,'_sub')
	whichfile(pan,i+1)=30+whereat(xnum.newvarlist);
elseif strcmpi(suffixstr,'_merg')
	whichfile(pan,i+1)=40+whereat(xnum.newvarlist);
elseif strcmpi(suffixstr,'_f33')
	whichfile(pan,i+1)=50+whereat(xnum.newvarlist);
elseif strcmpi(suffixstr,'_filt')
	whichfile(pan,i+1)=60+whereat(xnum.newvarlist);
elseif strcmpi(suffixstr,'_f2f')
	whichfile(pan,i+1)=70+whereat(xnum.newvarlist);
elseif strcmpi(suffixstr,'_fh')
	whichfile(pan,i+1)=80+whereat(xnum.newvarlist);
elseif strcmpi(suffixstr,'_brdg')
	whichfile(pan,i+1)=90+whereat(xnum.newvarlist);
end;

return;

