program jef_program_home;

uses 'jef_program_variables.pas';

//показ домашней страницы
function GetHome: string;
var
  sl: TStringList;
  PersonCode, i, kol, kol2, code: integer;    // 20090828-JeF
  all, description, genre, year, director, writer, actor, bio, character, movie, LastAdd: string;
  FileName, site, url, postfix, s, s2, rate: string;    // 20090828-JeF
  jLstFace, jListWidth, RecordCount, rating, LastCode: integer;    // 20090828-JeF
  Actions, ThmPage, links, txtpos, tag, info: string;								// 20090703-JeF
  MovieLastAdd, MovieLastUpd, PersonLastAdd, PersonLastUpd, Sitelinks, jExtraBar: string;		// 20090703-JeF
  MovieDbCount, MovieInColCount, PersonDbCount, PersonInColCount, j, k: integer;	// 20090703-JeF
  
begin
	result:='';
	jHomePageCode:='0';
	
	if jInitialize then begin
		Initialize;
		if (length(jHomePageCode)=18) then begin
			result:=ExtraFeature(StrToInt(copy(jHomePageCode,1,4)),copy(jHomePageCode,5,14));
			exit;
		end;
	end;


	sl:=TStringList.Create;

	all:=format('<h1><img src="%s" align=right>%s</h1>', [GetRscPath('icon.gif'), GetLangStr(91)]);

    all:=all+'<table width=100% border=0><tr><td valign=top>';

		with sl do begin
			Clear;
      Add('select count(code) as amount from movies');
		end;
		RecordCount:=ExecuteSQL(sl.text);
    kol:=GetIntegerField('amount');
	MovieDbCount:=kol;	// 20090703-JeF

		with sl do begin
			Clear;
      Add('select count(code) as amount from movies where in_collection=1');
		end;
		RecordCount:=ExecuteSQL(sl.text);
    kol2:=GetIntegerField('amount');
	MovieInColCount:=kol2;	// 20090703-JeF

    //all:=all+format('Movies in collection: %d, in database: %d<br>', [kol2, kol]);
    all:=all+format(GetLangStr(92), [kol2, kol]);

	with sl do begin
		Clear;
		Add('select count(code) as amount from persons');
	end;
	RecordCount:=ExecuteSQL(sl.text);
    kol:=GetIntegerField('amount');
	PersonDbCount:=kol;	// 20090703-JeF

	with sl do begin
		Clear;
		Add('select count(code) as amount from persons where in_collection=1');
	end;
	RecordCount:=ExecuteSQL(sl.text);
    kol2:=GetIntegerField('amount');
	PersonInColCount:=kol2;	// 20090703-JeF

    all:=all+format(GetLangStr(93), [kol2, kol]);

    //'Tasks:<ul><li><a href="add_movie">Add movie to collection</a><li><a href="add_person">Add person to collection</a><li><a href="search">Search movie or person</a></ul>';
	s:=GetStrOption('HomePageCode');
	if (length(s)=18) then s:='1' else s:='0';
	BuildMainNavBar(1,s+'11111111100');
	jExtraBar:=jMainNavBar;
	jExtraBar:=ReplaceStr(jExtraBar,'"></a>','" width="18"></a>');
    Actions:='<ul>';	// 20090703-JeF
    Actions:=Actions+'<li><a href="add_movie">'+GetLangStr(20)+'</a>';	// 20090703-JeF

	//20101224-JeF
	Actions:=Actions+format('&nbsp;<a href="custom900200000:2"><img src="%s" width="16" align="absmiddle" border="0" alt="%s"></a>',
							[GetRscPath('LoadMovieList.png'),GetLangStr(2800)])
					+format('&nbsp;<a href="custom900200000:1"><img src="%s" width="16" align="absmiddle" border="0" alt="%s"></a>',
							[GetRscPath('List1.png'),GetLangStr(2801)]);

	Actions:=Actions+'<li><a href="add_person">'+GetLangStr(22)+'</a>';	// 20090703-JeF

	//20101224-JeF
	Actions:=Actions+format('&nbsp;<a href="custom900300000:2"><img src="%s" width="16" align="absmiddle" border="0" alt="%s"></a>',
							[GetRscPath('LoadPersonList.png'),GetLangStr(2803)])
					+format('&nbsp;<a href="custom900300000:1"><img src="%s" width="16" align="absmiddle" border="0" alt="%s"></a>',
							[GetRscPath('List1.png'),GetLangStr(2804)]);

    Actions:=Actions+'<li><a href="search">'+GetLangStr(97)+'</a>';		// 20090703-JeF
	Actions:=Actions+'<li>'+jExtraBar;
    Actions:=Actions+'</ul>';	// 20090703-JeF
	all:=all+GetLangStr(94)+Actions;	// 20090703-JeF

    all:=all+'</td><td valign=top>';
    if GetBoolOption('ShowFeaturedDVD') then all:=all+GetFeaturedDVD;  // 20090601-JeF
    all:=all+'</td></tr></table>';

	// 20090828-JeF : new Last added/modified from original script v5	
    all:=all+'<hr>';

	//jListWidth:=trunc((GetHTMLWidth-380)/2);
	jListWidth:=(GetHTMLWidth-GetIntThmOption('HomeOffWidth')) div 2;
	if GetBoolOption('LastAddUpdThumbs') then jLstFace:=6 else jLstFace:=1;
	
	//last created movies
	MovieLastAdd:=BuildList(jSqlStrings.Values['C0051'],'Movie',jLstFace,80,jListWidth,1,10,0,1);
	
    //last updated movies   // 20090828-JeF
	MovieLastUpd:=BuildList(jSqlStrings.Values['C0052'],'Movie',jLstFace,80,jListWidth,1,10,0,1);

    //last added persons
	PersonLastAdd:=BuildList(jSqlStrings.Values['C0053'],'Person',jLstFace,80,jListWidth,1,10,0,1);

    //last updated persons   // 20090828-JeF
	PersonLastUpd:=BuildList(jSqlStrings.Values['C0054'],'Person',jLstFace,80,jListWidth,1,10,0,1);

	if DatabaseFileName='test.dmo' then
		all:=all+'<hr><font color="red" size=+1>'+GetLangStr(368)+'</font><br>';
	
    //links to partners
    with sl do begin
      Clear;
      Add('select * from partners');
      Add(format('where language like ''%s''', [ProgramLanguage]));
      Add('order by sort_order');
    end;
    SiteLinks:='';	//20090703-JeF
	RecordCount:=ExecuteSQL(sl.text);
    if RecordCount>0 then begin
		//20090703-JeF // all:=all+'<br>'+GetLangStr(438)+'<br>';
		SiteLinks:=GetLangStr(438)+'<br>';	//20090703-JeF
	end;
    for i:=1 to RecordCount do begin
    	site:=GetStringField('site');
    	url:=GetStringField('url');
    	postfix:=GetStringField('postfix');
    	if postfix<>'' then
    		url:=url+postfix;
		s:=GetRscPath(GetStringField('picture'));
		if GetStringField('picture')='' then
			SiteLinks:=SiteLinks+'<a href="link:'+jRedir+url+format('">%s</a> ', [site])	//20090703-JeF
		else
			SiteLinks:=SiteLinks+'<a href="link:'+jRedir+url+'">'+format('<img src="%s" border="0" align="absmiddle"></a> ', [s]);
		GetNextRecord;
    end;
	all:=all+'<br>'+SiteLinks;	//20090703-JeF
	
	if ProgramLanguage='Russian' then
		all:=all+'<br>'+format(GetLangStr(570), ['http://www.movienizer.com/ru/donate.htm']);

	// 20090703-JeF
	if ThemeActive then begin
	  
		ThmPage:=GetTemplate('home');
		
		if (ThmPage<>'') then begin

			// Other Tags
			i:=pos('<!--#',ThmPage);
			while (i>0) do begin
				j:=MyPos('-->',ThmPage,i);
				tag:='';
				info:='';
				if (j>i) then begin
					tag:=copy(ThmPage,i+5,j-i-5);
					while (copy(tag,1,1)=' ') do tag:=copy(tag,2,length(tag)-1);
					while (copy(tag,length(tag),1)=' ') do tag:=copy(tag,1,length(tag)-1);
					k:=pos(' ',tag);
					if (k>0) then tag:=copy(tag,1,k-1);
					j:=j+3;
				end;
				
				if (tag='title')    				then info:='<center><img src="'+GetRscPath('movienizer4.gif')+'" width="202" height="66" alt="" /></center>';
				if (tag='title_img')			then info:='<img src="'+GetRscPath('icon4.png')+'" alt="" />';
				if (tag='count_lbl')   		then info:='<strong>1..2..3..4..</strong>';
				if (tag='incoll_lbl')   		then info:=GetLangStr(57);
				if (tag='indb_lbl')    		then info:=GetLangStr(56);
				if (tag='mcount_lbl')   		then info:=GetLangStr(42);
				if (tag='pcount_lbl')   		then info:=GetLangStr(43);
				if (tag='mincol')  			then info:=IntToStr(MovieInColCount);
				
				if (tag='mindb')  				then info:=IntToStr(MovieDbCount)
				else if (tag='pincol')  			then info:=IntToStr(PersonInColCount)
				else if (tag='pindb')  				then info:=IntToStr(PersonDbCount)
				else if (tag='actions_lbl')   		then info:=copy(GetLangStr(94),1,pos(':',GetLangStr(94))-1)
				else if (tag='actions')   			then info:=Actions
				else if (tag='featured_lbl')   		then begin
					if GetBoolOption('ShowFeaturedDVD') then begin
						info:=GetFeaturedDVD;
						info:=copy(info,1,pos(':',info)-1)
							+'&nbsp;&nbsp;'
							+'<a href="custom100100000:1">'
							+'<img src="'+GetRscPath('Featured.png')+'" border="0"></a>';
						info:='<td class="NameC">'+info+'</td>';
					end
					else begin
						info:=GetLangStr(176)
							+'&nbsp;&nbsp;'
							+'<a href="custom100100000:1">'
							+'<img src="'+GetRscPath('Wanted.png')+'" border="0"></a>&nbsp;'
							+'<a href="custom005500000:1">'
							+'<img src="'+GetRscPath('magnifier.png')+'" alt="" Border=0 /></a>';
						info:='<td class="NameC">'+info+'</td>';
					end;
				end;
				
				if (tag='featured')   			then begin
					if GetBoolOption('ShowFeaturedDVD') then begin
						info:=GetFeaturedDVD;
						k:=pos(':',info);
						info:=copy(info,k+1,length(info)-k);
						info:='<td>'+info+'</td>';
					end
					else begin
						jListWidth:=(GetHTMLWidth-GetIntThmOption('HomeOffWidth')) div 2;
						info:='<td>'+BuildList(jSqlStrings.Values['C0055'],'Movie',1,80,jListWidth,1,4,0,1)+'</td>';
					end;
				end;
				
				if (tag='mlastadded_lbl')		
					then info:=copy(GetLangStr(98),1,pos(':',GetLangStr(98))-1)
							+'&nbsp;&nbsp;'
							+'<a href="custom020500000:2"><img src="'+GetRscPath('List1.png')+'" border="0" align="absmiddle"></a>&nbsp;'
							+'<a href="custom020500000:6"><img src="'+GetRscPath('List3.png')+'" border="0" align="absmiddle"></a>'
							+'&nbsp;&nbsp;'
							+'<a href="custom005100000:1"><img src="'+GetRscPath('magnifier.png')+'" alt="" Border=0 /></a>';
				if (tag='mlastadded')			then info:=MovieLastAdd;
				if (tag='mlastmodified_lbl')	
					then info:=copy(GetLangStr(99),1,pos(':',GetLangStr(99))-1)
							+'&nbsp;&nbsp;'
							+'<a href="custom020500000:2"><img src="'+GetRscPath('List1.png')+'" border="0" align="absmiddle"></a>&nbsp;'
							+'<a href="custom020500000:6"><img src="'+GetRscPath('List3.png')+'" border="0" align="absmiddle"></a>'
							+'&nbsp;&nbsp;'
							+'<a href="custom005200000:1"><img src="'+GetRscPath('magnifier.png')+'" alt="" Border=0 /></a>';
				if (tag='mlastmodified')		then info:=MovieLastUpd;
				if (tag='plastadded_lbl')		
					then info:=copy(GetLangStr(100),1,pos(':',GetLangStr(100))-1)
							+'&nbsp;&nbsp;'
							+'<a href="custom020500000:2"><img src="'+GetRscPath('List1.png')+'" border="0" align="absmiddle"></a>&nbsp;'
							+'<a href="custom020500000:6"><img src="'+GetRscPath('List3.png')+'" border="0" align="absmiddle"></a>'
							+'&nbsp;&nbsp;'
							+'<a href="custom005300000:1"><img src="'+GetRscPath('magnifier.png')+'" alt="" Border=0 /></a>';
				if (tag='plastadded')			then info:=PersonLastAdd;
				if (tag='plastmodified_lbl')	
					then info:=copy(GetLangStr(101),1,pos(':',GetLangStr(101))-1)
							+'&nbsp;&nbsp;'
							+'<a href="custom020500000:2"><img src="'+GetRscPath('List1.png')+'" border="0" align="absmiddle"></a>&nbsp;'
							+'<a href="custom020500000:6"><img src="'+GetRscPath('List3.png')+'" border="0" align="absmiddle"></a>'
							+'&nbsp;&nbsp;'
							+'<a href="custom005400000:1"><img src="'+GetRscPath('magnifier.png')+'" alt="" Border=0 /></a>';
				if (tag='plastmodified')		then info:=PersonLastUpd;
				if (tag='comments')				
					then info:=SiteLinks+'<br><br>'+GetLangStr(1112)+' : <span style="color:#E9032E;font-weight:bold;">JeF v.'+IntToStr(ScriptVersion)+'</span>&nbsp;&nbsp;'
								+'<a href="link:http://www.movienizer.com/forum/viewtopic.php?f=7&t=652">'+GetLangStr(1113)+'</a>';
				

				if (info<>'') then ThmPage:=copy(ThmPage,1,j-1) + info + copy(ThmPage,j,length(ThmPage)-j+1);

				i:=MyPos('<!--#',ThmPage,i+1);
			end;
			
			all:=ThmPage;
		end;
	end;
  result:=all;
  sl.Free;
  jLastPage:=result;
end;

function GetMoviesHome: string;
var
  i, j, k, RecordCount, jBoxMaxWidth, jBoxes, jSelectedBox, jLetterWidth, jListWidth, jTargetPageNbr: integer;
  s, s2, s3, jGenre, jGenresBar, jLettersBar, jPageTitle, jList: string;
  
begin
	jTargetPageNbr:=GetIntXFvar(301,'CurPageNbr');
	jHomePageCode:='030100000000000000';
	jLetterWidth:=GetIntThmOption('ABCellMaxWidth');
	// Build Letters Bar
	if jInCollection then s:=' where movies.in_collection=1 ' else s:='';
	s:='select distinct substr(title,1,1) from movies '+s+' order by title;';
	RecordCount:=ExecuteSQL(s);
	jBoxes:=(GetHTMLWidth-300) div (jLetterWidth);
	s2:='<td><a href="custom103200000:1"><img src="'+GetRscPath('NoFilter.png')+'" border="0" alt="Filter Reset"></a></td>';
	for i:=1 to RecordCount do begin 
		s3:=UpperCase(GetStringField('substr(title,1,1)'));
		if (pos('>'+s3+'<',s2)>0) then continue;
		if ((i mod jBoxes) = 1) and (i>1) then s2:=s2+'</tr><tr>';
		s2:=s2+'<td class="SelectBox"';
		if (pos('"'+s3+'%"',GetStrXFvar(301,'sql'))>0)
			then s2:=s2+' bgcolor="#b1c5ff"';
		s2:=s2+'><a href="custom103400000:'+IntToStr(ord(s3))+'"><font size="+1">'+s3+'</font></a></td>';
		GetNextRecord;
	end;
	jLettersBar:='<table class="lettersbar" style="text-align:middle;"><tr>'+s2+'</tr></table>';
	// Build genres list
	jLetterWidth:=GetIntThmOption('GBLetterMaxWidth');
	s2:='';
	jSelectedBox:=0;
	if jInCollection then s:=' and m.in_collection=1 ' else s:='';
	s:='select distinct man.code, man.name from movies_manuals mm '
		+'inner join manuals man on man.code=mm.ref_code '
		+'inner join movies m on m.code=mm.movie '
		+'where man.reference=8 '
		+s
		+'order by man.name';
	RecordCount:=ExecuteSQL(s);
	jBoxMaxWidth:=0;
	for i:=1 to RecordCount do begin 
		s3:='';
		jGenre:=GetStringField('man.name');
		if (length(jGenre)*jLetterWidth>jBoxMaxWidth) then jBoxMaxWidth:=length(jGenre)*jLetterWidth;
		jBoxes:=(GetHTMLWidth-300) div jBoxMaxWidth;
		if (pos('='+IntToStr(GetIntegerField('man.code'))+' ',GetStrXFvar(301,'sql'))>0) then begin
			s3:=s3+' bgcolor="#b1c5ff" ';
			jSelectedBox:=i;
		end;
		s3:='<td class="SelectBox" '+s3+format('><a href="custom103300000:%d"><nobr><font size="+1">%s</font></nobr></a></td>',
							[GetIntegerField('man.code'), jGenre]);
		s2:=s2+s3;
		GetNextRecord;
	end;
	j:=0;
	if (GetIntOption('MHomeGListState')=0) then begin  // Collapsed Genre List
		if (jBoxes>2) then k:=2 else k:=1;
		for i:=1 to jSelectedBox-k do begin
			j:=Pos('</td>',s2);
			delete(s2,1,j+4);
		end;
		s2:='<td><a href="custom103100000:1"><img src="'+GetRscPath('Expand.png')+'" border="0"></a></td>'+s2;
		for i:=1 to jBoxes do j:=MyPos('<td',s2,j+1);
		j:=MyPos('</td>',s2,j+1);
		s2:=copy(s2,1,j+4);
	end
	else begin   // Expanded Genre List
		s2:='<td><a href="custom103100000:1"><img src="'+GetRscPath('Collapse.png')+'" border="0"></a></td>'+s2;
		for i:=1 to RecordCount+1 do begin 
			j:=MyPos('<td',s2,j+1);
			if (i>1) and (((i mod jBoxes)=1) or (jBoxes=1)) then begin
				insert('</tr><tr>',s2,j);
				j:=j+10;
			end;
		end;
	end;
	jGenresBar:='<table class="genresbar" style="width:100%;text-align:center;"><tr>'+s2+'</tr></table>';
	// Build Current List from current sql request and set MainNavTag
	jListWidth:=GetHTMLWidth-GetIntThmOption('ListOffWidth');
	jList:='<center>'+jGenresBar+'</center>'
			+'<center>'+jLettersBar+'</center>'
			+BuildList(GetStrXFvar(301,'sql'),'Movie',0,0,jListWidth,jTargetPageNbr,0,3,301);
	// Build Page Title
	if (GetStrXFvar(301,'sqldesc')>'') then jPageTitle:=' : '+GetStrXFvar(301,'sqldesc') else jPageTitle:='';
	jPageTitle:=GetLangStr(1100)+jPageTitle;
	// Build Page
	jLastPage:=BuildExtraPage(jPageTitle,jList);
	result:=jLastPage;
end;

function GetPersonsHome: string;
var
  jTargetPageNbr, jLetterWidth, jBoxes, jListWidth, RecordCount, i, j, k: integer;
  jLettersBar, jPageTitle, jList, s, s2, s3: string;
  sl: TStringList;  
begin
	sl:=TStringList.create;
	jTargetPageNbr:=GetIntXFvar(302,'CurPageNbr');
	jHomePageCode:='030200000000000000';
	jLetterWidth:=GetIntThmOption('ABCellMaxWidth');
	// Build Letters Bar
	sl.clear;
	if jInCollection then s:=' where persons.in_collection=1 ' else s:='';
	s:='select distinct name from persons '+s+';';
	RecordCount:=ExecuteSQL(s);
	for i:=1 to RecordCount do begin 
		s2:=UpperCase(ClearExtraBlankCharacters(GetStringField('name')));
		s3:=copy(s2,1,1);
		if (pos(s3,sl.Text)=0) then sl.Add(s3);
		j:=pos(' ',s2);
		while (j>0) do begin
			delete(s2,1,j);
			s3:=copy(s2,1,1);
			if (s3>'') and (s3<>' ') and (pos(s3,sl.Text)=0) then sl.Add(s3);
			j:=pos(' ',s2);
		end;
		GetNextRecord;
	end;
	s2:='<td><a href="custom105100000:1"><img src="'+GetRscPath('NoFilter.png')+'" border="0" alt="Filter Reset"></a></td>';
	sl.sort;
	jBoxes:=(GetHTMLWidth-300) div (jLetterWidth);
	for i:=1 to sl.Count do begin 
		s3:=sl[i-1];
		if s3='' then continue;
		if ((i mod jBoxes) = 1) and (i>1) then s2:=s2+'</tr><tr>';
		s2:=s2+'<td class="SelectBox"';
		if (pos('"'+s3+'%"',GetStrXFvar(302,'sql'))>0)
			then s2:=s2+' bgcolor="#b1c5ff"';
		s2:=s2+'><a href="custom105200000:'+IntToStr(ord(s3))+'"><font size="+1">'+s3+'</font></a></td>';
	end;
	jLettersBar:='<table class="lettersbar" style="text-align:middle;"><tr>'+s2+'</tr></table>';
	// Build Current List from current sql request
	jListWidth:=GetHTMLWidth-GetIntThmOption('ListOffWidth');
	jList:='<center>'+jLettersBar+'</center>'
			+BuildList(GetStrXFvar(302,'sql'),'Person',0,0,jListWidth,jTargetPageNbr,0,3,302);
	jPageTitle:=GetLangStr(1101);
	// Build Page
	jLastPage:=BuildExtraPage(jPageTitle,jList);

	sl.free;
	result:=jLastPage;
end;


//показ уменьшенной карточки фильма
function GetThumbMovie2(code, mode: integer; title, year: string; InCollection, rating: integer): string;
var
  sl: TStringList;
  s, rate, IMDBRate, FileName, genres, directors, actors, img, seasons: string;
  fl: boolean;
  i, j, k, kol, dir, amount, RecordCount: integer;
  ThumbsPath, FileIn, FileOut, CheckMode: string;
  FirstAirYear, LastAirYear, jDiskNbr, ThmPage, tag, info, txtpos: string;
  jThumb, jTitle, jGenres, jDirectors, jActors: string;
  IMDBRating: double;
  jDiscLabelInfo: string;   // 20100910-JeF
  jDiscLabelCode: integer;   // 20100910-JeF
begin
  result:='';
  if jSearchBuild then begin
	jSearchType:='movie';
	jBuildPage.Add(IntToStr(code));
	exit;
  end;
  sl:=TStringList.Create;
  amount:=mode;
  {
  if mode=1 then
    amount:=3
  else
    amount:=7;
	}
  s:='';

  ThumbsPath:=ImagesPath+'Thumbs\';
  
    fl:=false;
    genres:='';
    directors:='';
    actors:='';

    //получаем дополнительную информацию о фильме, если он из коллекции
    if InCollection=1 then begin

      // movie extra infos
	  jDiskNbr:='';
	  with sl do begin
        Clear;
        Add('select * from movies');
        Add(format('where code=%d', [code]));
      end;
	  RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
		jDiskNbr:=GetStringField('disc_nom');
		IMDBRating:=GetFloatField('imdb_rating');
	  end;

      // edition extra infos
	  with sl do begin
        Clear;
        Add('select * from editions');
        Add(format('where movie=%d and disc_nom > '''' limit 1', [code]));
      end;
	  RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
		jDiskNbr:=GetStringField('disc_nom');
		if (jDiskNbr>'') 
			then jDiskNbr:=format('<a href="custom005600001:%d">%s</a>', [GetIntegerField('code'), jDiskNbr]);
	  end;

      // edition disc label // 20100910-JeF
	  jDiscLabelInfo:='';
	  if (pos('#medialabel',ThmPage)>0) then begin
		with sl do begin
			Clear;
			Add('select * from editions');
			Add(format('where movie=%d and disc_label > 0 limit 1', [code]));
		end;
		RecordCount:=ExecuteSQL(sl.text);
		if RecordCount>0 then begin
			jDiscLabelCode:=GetIntegerField('disc_label');
			with sl do begin
				Clear;
				Add('select * from manuals');
				Add(format('where code=%d', [jDiscLabelCode]));
			end;
			RecordCount:=ExecuteSQL(sl.text);
			if RecordCount>0 then begin
				jDiscLabelInfo:=format('<a href="disc_label:%d">%s</a>', [jDiscLabelCode, GetStringField('name')]);
			end;
		end;
	  end;
	  
	  //обложка
      with sl do begin
        Clear;
        Add('select * from images');
        Add(format('where movie=%d and mode=1', [code]));
		Add('order by sort_order');
      end;

	  RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
	    FileIn:=ImagesPath+GetStringField('path');
      	FileName:=ThumbsPath+GetStringField('path');
        if MyFileExists(FileIn) then begin
	      	if not MyFileExists(FileName) then begin
		      	//ShowMessage('Convert '+FileIn+' to '+FileName);
						ConvertThumb(FileIn, FileName, 50, 0);
					end;
	        if not MyFileExists(FileName) then 
	        	FileName:=FileIn;
        end
        else
	        FileName:=FileIn;
        if MyFileExists(FileName) then
          fl:=true;
      end;
      if not fl then begin
        FileName:=GetRscPath('no-cover.gif');
        fl:=true;
      end;

     	if amount>0 then begin 
			//жанры
			with sl do begin
			  Clear;
			  Add('select * from movies_manuals mm');
			  Add('join manuals m on m.code=mm.ref_code');
			  Add(format('where mm.movie=%d and m.reference=8', [code]));
			end;
			RecordCount:=ExecuteSQL(sl.text);
			for j:=1 to RecordCount do begin
			  genres:=genres+format('<a href="genre:%d">%s</a>, ', [GetIntegerField('m.code'), GetStringField('m.name')]);
			  GetNextRecord;
			end;
			if genres<>'' then
			  genres:=copy(genres, 1, length(genres)-2);

			//режиссеры и актеры
			with sl do begin
			  Clear;
			  Add('select distinct p.code, p.name, p.in_collection, d.mode from data d');
			  Add('inner join persons p on p.code=d.person');
			  Add(format('where d.movie=%d and d.mode in (1,3)', [code]));
			  //Add('and d.sort_order<10');//27.07.07
			  Add('order by d.mode, d.sort_order');
			end;

			RecordCount:=ExecuteSQL(sl.text);
				i:=0;
				j:=0;
				dir:=0;
				while (i<RecordCount) and (j<amount) do begin
			//for j:=1 to kol do begin
				if GetIntegerField('p.in_collection')=1 then
						img:=format('<img src="%s" align="absmiddle">', [GetRscPath('person.gif')])
					else
						img:='';
					if GetIntegerField('d.mode')=1 then begin
						if dir<=3 then begin
					directors:=directors+img+format('<a href="person:%d">%s</a>, ', [GetIntegerField('p.code'), GetStringField('p.name')]);
					dir:=dir+1;
							end;
						end
					else begin
				actors:=actors+img+format('<a href="person:%d">%s</a>, ', [GetIntegerField('p.code'), GetStringField('p.name'), j]);
				j:=j+1;
					end;
					i:=i+1;
			  GetNextRecord;
			end;
		end;
      if directors<>'' then
				directors:=copy(directors, 1, length(directors)-2);

      if actors<>'' then
        actors:=copy(actors, 1, length(actors)-2);


			if GroupOperation then begin

			  with sl do begin
			    Clear;
			    Add('select checked from movies');
			    Add(format('where code=%d', [code]));
			  end;
			  RecordCount:=ExecuteSQL(sl.text);
			  if (RecordCount>0) and (GetIntegerField('checked')=1) then
					CheckMode:=format('<a href="group_check_off:%d"><img src="%s" align="absmiddle" border="0"></a>',  [code, GetRscPath('check.gif')])
				else
					CheckMode:=format('<a href="group_check_on:%d"><img src="%s" align="absmiddle" border="0"></a>',  [code, GetRscPath('checkout.gif')]);
			end
			else
				CheckMode:='';
      
      if year<>'' then
        year:=format(' (<a href="year:%s">%s</a>)',[year,year]);

		if rating>0 then
			rate:=format(' <img src="%s" width=16 height=16 border=0 align="absmiddle">%d', [GetRscPath('BarUnit-On.gif'), rating])
		else
			rate:='';

		if imdbrating>0 then
			imdbrate:=format(' <img src="%s" width=16 height=16 border=0 align="absmiddle">%s', [GetRscPath('Bar2Unit-On.gif'), FloatToStr(imdbrating)])
		else
			imdbrate:='';

	  if MyFileExists(FileIn) and (pos('no-cover.gif',FileName)=0) then FileName:=FileIn;
	  if GetBoolOption('ShowMtDiskNbr') and (jDiskNbr>'') then rate:=rate+'&nbsp;(#'+jDiskNbr+')';
	  s:=format('<a href="movie:%d"><img src="%s" height="%d" border=0 align=left></a>', [code, FileName, GetIntOption('ThumbsHeigh')]);
	  jThumb:=s;
	  s:=s+CheckMode+format('<a href="movie:%d"><b>%s</b></a><small>%s%s<br>', [code, title, year, rate]);
	  jTitle:=format('<a href="movie:%d"><b>%s</b></a>', [code, title]);
	  
	  jGenres:=genres;
      if genres<>'' then begin
		if mode<>1 then
       		//Genres
			genres:=GetLangStr(179)+': '+genres;
        s:=s+genres+'<br>';
	  end;

	  jDirectors:=directors;
      if (mode<>1) and (directors<>'') then
      	//Directors
        s:=s+GetLangStr(181)+': '+directors+'<br>';

	  jActors:=actors;
      if actors<>'' then begin
		if mode<>1 then
        	//Actors
          actors:=GetLangStr(164)+': '+actors;
        s:=s+actors+'<br>';
      end;
      s:=s+'</small>';
    end
    else begin
      if year<>'' then
        year:=format(' (%s)', [year]);
      s:=format('<a href="movie:%d"><b>%s</b></a>%s', [code, title, year]);
    end;

	//seasons
	seasons:='';
	with sl do begin
		Clear;
		Add('select distinct season from episodes');
		Add(format('where movie=%d', [code]));
		Add('order by season');
	end;
	RecordCount:=ExecuteSQL(sl.text);
	for i:=1 to RecordCount do begin
		seasons:=seasons+format('<a href="movie:%d#%d">%d</a> ', [code, GetIntegerField('season'), GetIntegerField('season')]);
		GetNextRecord;
	end;
	if seasons<>'' then begin
		
		//20101010-JeF
		if (year>'') and (GetStrOption('TitleTVTag')>'')
			then insert(GetStrOption('TitleTVTag'),year,3);

		//20090621-JeF_Begin
		FirstAirYear:='';
		LastAirYear:='';
		if GetBoolOption('ShowMtAirYears') then begin
		  with sl do begin
			Clear;
			Add('select min(original_air_date) from episodes');
			Add(format('where movie=%d and original_air_date > "1899-01-01"', [code]));
		  end;
		  RecordCount:=ExecuteSQL(sl.text);
		  if (RecordCount>0) then begin
			FirstAirYear:=GetStringField('min(original_air_date)');
			FirstAirYear:=copy(FirstAirYear,1,4);
			if (FirstAirYear<'1900') then FirstAirYear:='';
		  end;
		  with sl do begin
			Clear;
			Add('select max(original_air_date) from episodes');
			Add(format('where movie=%d and original_air_date > "1899-01-01"', [code]));
		  end;
		  RecordCount:=ExecuteSQL(sl.text);
		  if (RecordCount>0) then begin
			LastAirYear:=GetStringField('max(original_air_date)');
			LastAirYear:=copy(LastAirYear,1,4);
			if (LastAirYear<'1900') or (LastAirYear<FirstAirYear) or (LastAirYear<year) then LastAirYear:='';
		  end;
		end;
		if GetBoolOption('ShowMtAirYears') and ((FirstAirYear>'') or (LastAirYear>'')) then
			seasons:=copy(GetLangStr(517),1,length(GetLangStr(517))-1)+' ('+FirstAirYear+'-'+LastAirYear+') : '+seasons
		else
		//20090621-JeF_End
			seasons:=GetLangStr(517)+' '+seasons;
		delete(seasons, length(seasons), 1);
		s:=s+seasons;
	end;

	if mode=7 then
	  s:='<tr><td>'+s+'</td></tr>';

	ThmPage:='';
	if GetCutePreview then ThmPage:=GetTemplate('ThumbMovie');
	if (ThmPage<>'') then begin
		i:=pos('<!--#',ThmPage);
		while (i>0) do begin
			j:=MyPos('-->',ThmPage,i);
			tag:='';
			info:='';
			if (j>i) then begin
				tag:=copy(ThmPage,i+5,j-i-5);
				while (copy(tag,1,1)=' ') do tag:=copy(tag,2,length(tag)-1);
				while (copy(tag,length(tag),1)=' ') do tag:=copy(tag,1,length(tag)-1);
				k:=pos(' ',tag);
				if (k>0) then tag:=copy(tag,1,k-1);
				j:=j+3;
			end;
			
			if (tag='_cover_') 					then info:=Filename
			else if (tag='moviethumb') 			then info:=jThumb
			else if (tag='checkmode') 			then info:=CheckMode
			else if (tag='title') 	 			then info:=jTitle
			else if (tag='year') 	 			then info:=year
			else if (tag='rate') 	 			then info:=rate
			else if (tag='IMDBrate')    		then info:=IMDBRate
			else if (tag='discnumber')			then info:='#'+jDiskNbr
			else if (tag='medialabel_lbl')    	then info:=GetLangStr(673)+' '  // 20100910-JeF
			else if (tag='medialabel')    		then info:=jDiscLabelInfo  // 20100910-JeF

			else if (tag='genre')				then info:=jGenres
			else if (tag='genrefulltxt')		then info:=GetLangStr(179)+': '+jGenres
			else if (tag='genrefulltxtbr')		then info:=GetLangStr(179)+': '+jGenres+'<br>'
			else if (tag='seasons')	and (seasons>'')
				then info:=seasons
			else if (tag='seasonsfulltxt')	and (seasons>'')
				then info:=GetLangStr(517)+' '+seasons
			else if (tag='directors') and (jDirectors>'')
				then info:=jDirectors
			else if (tag='directorsfulltxt') and (jDirectors>'')
				then info:=GetLangStr(181)+': '+jDirectors
			else if (tag='directorsfulltxtbr') and (jDirectors>'')
				then info:=GetLangStr(181)+': '+jDirectors+'<br>'
			else if (tag='actors')				then info:=jActors
			else if (tag='actorsfulltxt') and (jActors>'')
				then info:=GetLangStr(164)+': '+jActors
			else if (tag='actorsfulltxtbr') and (jActors>'')
				then info:=GetLangStr(164)+': '+jActors+'<br>'
			else begin
			end;

			if (info<>'') then ThmPage:=copy(ThmPage,1,j-1) + info + copy(ThmPage,j,length(ThmPage)-j+1);

			i:=MyPos('<!--#',ThmPage,i+1);
		end;
		
		s:=ThmPage;
	end;
	  
  result:=s;
  sl.Free;
end;

function GetThumbMovie(code, mode: integer; title, year: string; InCollection: integer): string;
begin
	result:=GetThumbMovie2(code, mode, title, year, InCollection, 0);
end;

//2011-06-26 by Vlad
//roughly adapted from Modern
//uses cover height and cover width parameters that can be set by Modern
function GetThumbMovie3(code, mode: integer; title: string): string;
var
  sl: TStringList;
  s, rate, FileName, genres, directors, actors, img, seasons: string;
  fl, CutThumb: boolean;
  i, j, k, kol, dir, amount, RecordCount, CoverWidth, CoverHeight: integer;
	ThumbsPath, FileIn, FileOut, CheckMode: string;
	jThumb, jTitle, tag, info, ThmPage: String;
begin
	result:='';
  sl:=TStringList.Create;
  amount:=mode;
  {
  if mode=1 then
    amount:=3
  else
    amount:=7;
	}
  s:='';

  ThumbsPath:=ImagesPath+'Thumbs2\';

  fl:=false;
  genres:='';
  directors:='';
  actors:='';


    //обложка
    with sl do begin
      Clear;
      Add('select * from images');
      Add(format('where movie=%d and mode=1', [code]));
	  Add('order by sort_order');
    end;
    RecordCount:=ExecuteSQL(sl.text);

	  CoverWidth := RegReadInteger('ThCoverWidth', DefaultMultiCoverWidth);
	  CoverHeight := RegReadInteger('ThCoverHeight', DefaultMultiCoverHeight);
  
	  if (CoverWidth <= 0) and (CoverHeight <= 0) then begin
		  CoverWidth := DefaultMultiCoverWidth;
		  CoverHeight := DefaultMultiCoverHeight;
	  end;

    if RecordCount>0 then
    begin
		  i := RegReadInteger('CutThumb', 1);
		  if i = 1 then
				CutThumb := True
		  else
				CutThumb := False;
      FileIn:=ImagesPath+GetStringField('path');
      FileName:=ThumbsPath+GetStringField('path');
      if MyFileExists(FileName) then
        fl:=true
      else if MyFileExists(FileIn) then
        ConvertThumb2(FileIn, FileName, CoverWidth, CoverHeight, CutThumb);
      if MyFileExists(FileName) then
        fl:=true;
    end;
    if not fl then
    begin
      FileIn:=GetRscPath('no-cover.gif');
      FileName:=ThumbsPath+'no-cover.jpg';
      //ShowMessage('FileName='+FileName);
      //ShowMessage('FileIn='+FileIn);
      if ((not MyFileExists(FileName)) and
          (MyFileExists(FileIn))) then
      begin
        ConvertThumb2(FileIn, FileName, CoverWidth, CoverHeight, CutThumb);
      end;
      fl:=true;
    end;

	if GroupOperation then begin

	  with sl do begin
	    Clear;
	    Add('select checked from movies');
	    Add(format('where code=%d', [code]));
	  end;
	  RecordCount:=ExecuteSQL(sl.text);
	  if (RecordCount>0) and (GetIntegerField('checked')=1) then
			CheckMode:=format('<a href="group_check_off:%d"><img src="%s" align="absmiddle" border="0"></a>',  [code, GetRscPath('check.gif')])
		else
			CheckMode:=format('<a href="group_check_on:%d"><img src="%s" align="absmiddle" border="0"></a>',  [code, GetRscPath('checkout.gif')]);
	end
	else
		CheckMode:='';

    s:=format('<table><tr><td align="center"><div class="thumb-big-cover-border"><a href="movie:%d"><img class="thumb-big-cover" src="%s"></a></div></td></tr><tr><td>', [code, FileName])+CheckMode+format('<a href="movie:%d"><b>%s</b></a></td></tr></table>', [code, title]);

	jThumb := format('<a href="movie:%d"><img src="%s" height="%d" border=0 align=left></a>', [code, FileName, CoverHeight]);
	
	jTitle:=format('<a href="movie:%d"><b>%s</b></a>', [code, title]);
		
	ThmPage:=GetTemplate('Thumb3Movie');
	if (ThmPage<>'') then begin
		i:=pos('<!--#',ThmPage);
		while (i>0) do begin
			j:=MyPos('-->',ThmPage,i);
			tag:='';
			info:='';
			if (j>i) then begin
				tag:=copy(ThmPage,i+5,j-i-5);
				while (copy(tag,1,1)=' ') do tag:=copy(tag,2,length(tag)-1);
				while (copy(tag,length(tag),1)=' ') do tag:=copy(tag,1,length(tag)-1);
				k:=pos(' ',tag);
				if (k>0) then tag:=copy(tag,1,k-1);
				j:=j+3;
			end;
			
			if (tag='_cover_') 					then info:=Filename
			else if (tag='moviethumb') 			then info:=jThumb
			else if (tag='checkmode') 			then info:=CheckMode
			else if (tag='title') 	 			then info:=jTitle
			else begin
			end;

			if (info<>'') then ThmPage:=copy(ThmPage,1,j-1) + info + copy(ThmPage,j,length(ThmPage)-j+1);

			i:=MyPos('<!--#',ThmPage,i+1);
		end;
		
		s:=ThmPage;
	end;
	
  result:=s;
  sl.Free;
end;



//показ уменьшенной карточки человека
function GetThumbPerson(code, mode: integer): string;
var
  sl: TStringList;
  s, rate, FileName, img, PersonName, BirthDate: string;
  DeathDate: string;  // 20090620-JeF
  fl: boolean;
  i, j, k, kol, amount, RecordCount, DirectorCount, WriterCount, ActorCount, years: integer;
  InCollection, rating, jActorCountInColl: integer;
  ThumbsPath, FileIn, FileOut: string;
  ThmPage, tag, info, txtpos, jThumb, jName, jLifeTime, jActorCount, CheckMode: string;
begin
	result:='';
	if jSearchBuild then begin
		jSearchType:='person';
		jBuildPage.Add(IntToStr(code));
		exit;
	end;

	sl:=TStringList.Create;

	if mode=1 then
		amount:=3
	else
		amount:=7;
	s:='';

	ThumbsPath:=ImagesPath+'Thumbs\';

    fl:=false;

		with sl do begin
			Clear;
			// Add('select p.code, p.name, p.in_collection, p.birth_date, p.rating, i.path from persons p');   // 20090620-JeF
			Add('select p.code, p.name, p.in_collection, p.birth_date, p.death_date, p.rating, i.path from persons p');  // 20090620-JeF
			Add('left outer join images i on i.person=p.code and i.mode=6');
			Add(format('where p.code=%d', [code]));
			Add('order by i.sort_order');
		end;
		RecordCount:=ExecuteSQL(sl.text);
		PersonName:=GetStringField('p.name');
		InCollection:=GetIntegerField('p.in_collection');
		rating:=GetIntegerField('p.rating');

		if rating>0 then
			rate:=format(' <img src="%s" width=16 height=16 border=0 align="absmiddle">%d', [GetRscPath('BarUnit-On.gif'), rating])
		else
			rate:='';

    BirthDate:=GetStringField('p.birth_date');
	if (length(BirthDate)>=10) then BirthDate:=GetSmartDate(BirthDate);

	// 20090620-JeF
	if GetBoolOption('ShowPtDateOfDeath') then begin
	    DeathDate:=GetStringField('p.death_date');
		if (length(DeathDate)>=10) then DeathDate:=GetSmartDate(DeathDate);
	end;
	
    jThumb:='';
	jLifeTime:='';
	jName:=format('<a href="person:%d"><b>%s</b></a>', [Code, PersonName]);
	if InCollection=1 then begin
      if GetStringField('i.path')<>'' then begin
	      FileIn:=ImagesPath+GetStringField('i.path');
      	FileName:=ThumbsPath+GetStringField('i.path');
        if MyFileExists(FileIn) then begin
	      	if not MyFileExists(FileName) then begin
		      	//ShowMessage('Convert '+FileIn+' to '+FileName);
						ConvertThumb(FileIn, FileName, 50, 0);
					end;
	        if not MyFileExists(FileName) then 
	        	FileName:=FileIn;
        end
        else
	        FileName:=FileIn;

        if MyFileExists(FileName) then
          fl:=true;
      end;
      if not fl then begin
        FileName:=GetRscPath('no-cover.gif');
        fl:=true;
      end;
     
      //кол-во фильмов, как актер
			with sl do begin
        Clear;
        Add('select distinct movie as amount from data');
        Add(format('where person=%d and mode=3', [code]));
      end;
      ActorCount:=ExecuteSQL(sl.text);
	  // Movies as Actor and in collection
	  sl.Text:='select distinct d.movie as amount from data d inner join movies m on m.code=d.movie '
		  +format('where d.person=%d and d.mode=3 and m.in_collection=1', [code]);
	  jActorCountInColl:=ExecuteSQL(sl.Text);

	  if MyFileExists(FileIn) and (pos('no-cover.gif',FileName)=0) then FileName:=FileIn;   //20091016-JeF
	  jThumb:=format('<a href="person:%d"><img src="%s" height="%d" border=0 align=left></a>',[code, FileName, GetIntOption('ThumbsHeigh')]);
	  s:=jThumb+jName+format('<small>%s<br>', [rate]);
	  
	  if BirthDate<>'' then begin
      	//ShowMessage(BirthDate);
      	s:=s+BirthDate;
		jLifeTime:=BirthDate;
      	try
      		//years:=StrToDate(BirthDate);
      	except
      	end;
      end; // 20090620-JeF

      // 20090620-JeF_Begin
	  if GetBoolOption('ShowPtDateOfDeath') and (DeathDate<>'') then begin
      	s:=s+' - '+DeathDate;
		jLifeTime:=jLifeTime+' - '+DeathDate;
	  end;
	  // 20090620-JeF_End
	  
	  if (BirthDate<>'') or (DeathDate<>'') then begin  // 20090620-JeF
	    s:=s+'<br>';
		jLifeTime:=jLifeTime+'<br>';
	  end;

	  jActorCount:=format('%s: %d', [GetLangStr(301), ActorCount]);
	  s:=s+jActorCount;

      s:=s+'</small>';
    end
    else begin
      s:=jName;
    end;
	
	if GroupOperation then begin

	  with sl do begin
	    Clear;
	    Add('select checked from persons');
	    Add(format('where code=%d', [code]));
	  end;
	  RecordCount:=ExecuteSQL(sl.text);
	  if (RecordCount>0) and (GetIntegerField('checked')=1) then
			CheckMode:=format('<a href="group_person_check_off:%d"><img src="%s" align="absmiddle" border="0"></a>',  [code, GetRscPath('check.gif')])
		else
			CheckMode:=format('<a href="group_person_check_on:%d"><img src="%s" align="absmiddle" border="0"></a>',  [code, GetRscPath('checkout.gif')]);
	end
	else
		CheckMode:='';

	s:='<tr><td>'+s+'</td></tr>';

	ThmPage:='';
	if GetCutePreview then ThmPage:=GetTemplate('ThumbPerson');
	if (ThmPage<>'') then begin
		i:=pos('<!--#',ThmPage);
		while (i>0) do begin
			j:=MyPos('-->',ThmPage,i);
			tag:='';
			info:='';
			if (j>i) then begin
				tag:=copy(ThmPage,i+5,j-i-5);
				while (copy(tag,1,1)=' ') do tag:=copy(tag,2,length(tag)-1);
				while (copy(tag,length(tag),1)=' ') do tag:=copy(tag,1,length(tag)-1);
				k:=pos(' ',tag);
				if (k>0) then tag:=copy(tag,1,k-1);
				j:=j+3;
			end;
			
			if (tag='_cover_') 					then info:=Filename
			else if (tag='personthumb') 		then info:=jThumb
			else if (tag='name') 	 			then info:=jName
			else if (tag='checkmode') 			then info:=CheckMode
			else if (tag='rate') 	 			then info:=rate
			else if (tag='lifetime') 	 		then info:=jLifeTime
			else if (tag='brlifetimebr') and (jLifeTime>'')
				then info:='<br>'+jLifeTime+'<br>'
			else if (tag='actorcount') 	 		then begin
				info:=jActorCount;
				if (jActorCountInColl>0) then info:=info+' ('+IntToStr(jActorCountInColl)+')';
			end
			else begin
			end;

			if (info<>'') then ThmPage:=copy(ThmPage,1,j-1) + info + copy(ThmPage,j,length(ThmPage)-j+1);

			i:=MyPos('<!--#',ThmPage,i+1);
		end;
		
		s:=ThmPage;
	end;
  
	result:=s;
	sl.Free;
end;

//2011-06-26 by Vlad
//depends on Registry variables set by Modern 
//(uses contants if no variables were set by Modern)
function GetThumbPerson3(code, mode: integer): string;
var
  sl: TStringList;
  CheckMode: string;
  s, rate, FileName, img, PersonName, BirthDate: string;
  fl, CutThumb: boolean;
  i, j, k, kol, amount, RecordCount, DirectorCount, WriterCount, ActorCount, years: integer;
  InCollection, rating, PhotoWidth, PhotoHeight: integer;
  ThumbsPath, FileIn, FileOut, tag, info, ThmPage, jThumb, jName: string;
begin
	result:='';
  sl:=TStringList.Create;

  if mode=1 then
    amount:=3
  else
    amount:=7;
  s:='';

  ThumbsPath:=ImagesPath+'Thumbs2\';

    fl:=false;

		with sl do begin
			Clear;
			Add('select p.code, p.name, p.in_collection, p.birth_date, p.rating, i.path from persons p');
			Add('left outer join images i on i.person=p.code and i.mode=6');
			Add(format('where p.code=%d', [code]));
			Add('order by i.sort_order');
		end;
		RecordCount:=ExecuteSQL(sl.text);
		PersonName:=GetStringField('p.name');

		InCollection:=GetIntegerField('p.in_collection');
	jName:=format('<a href="person:%d"><b>%s</b></a>', [Code, PersonName]);
    //получаем дополнительную информацию о фильме, если он из коллекции
    if InCollection=1 then
    begin
      //обложка
	  PhotoWidth := RegReadInteger('ThPhotoWidth', DefaultMultiCoverWidth);
	  PhotoHeight := RegReadInteger('ThPhotoHeight', DefaultMultiCoverHeight);

	  if (PhotoWidth <= 0) and (PhotoHeight <= 0) then begin
		  PhotoWidth := DefaultMultiCoverWidth;
		  PhotoHeight := DefaultMultiCoverHeight;
	  end;


		if GetStringField('i.path')<>'' then
		begin
			i := RegReadInteger('CutPhotoThumb', 1);
				if i = 1 then
					CutThumb := True
				else
					CutThumb := False;
//      	FileName:=ImagesPath+GetStringField('i.path');
        FileIn:=ImagesPath+GetStringField('i.path');
        FileName:=ThumbsPath+GetStringField('i.path');
        if MyFileExists(FileName) then
          fl:=true
        else if MyFileExists(FileIn) then
          ConvertThumb2(FileIn, FileName, PhotoWidth, PhotoHeight, CutThumb);
        if MyFileExists(FileName) then
          fl:=true;
    end;

	if GroupOperation then begin

	  with sl do begin
	    Clear;
	    Add('select checked from persons');
	    Add(format('where code=%d', [code]));
	  end;
	  RecordCount:=ExecuteSQL(sl.text);
	  if (RecordCount>0) and (GetIntegerField('checked')=1) then
			CheckMode:=format('<a href="group_person_check_off:%d"><img src="%s" align="absmiddle" border="0"></a>',  [code, GetRscPath('check.gif')])
		else
			CheckMode:=format('<a href="group_person_check_on:%d"><img src="%s" align="absmiddle" border="0"></a>',  [code, GetRscPath('checkout.gif')]);
	end
	else
		CheckMode:='';

      if not fl then begin
        FileIn:=GetRscPath('no-cover.gif');
        //FileName:=ThumbsPath+'no-cover.jpeg';
        FileName:=ThumbsPath+'no-photo.jpeg';
        if ((not MyFileExists(FileName)) and
            (MyFileExists(FileIn))) then
        begin
          ConvertThumb2(FileIn, FileName, PhotoWidth, PhotoHeight, CutThumb);
        end;
        fl:=true;
      end;

      s:=format('<table><tr><td align="center"><div class="thumb-big-cover-border"><a href="person:%d"><img class="thumb-big-cover" src="%s"></a></div></td></tr><tr><td>' + CheckMode + '<a href="person:%d"><b>%s</b></a></td></tr></table>', [code, FileName, code, PersonName]);
    end
    else
      s:=format(CheckMode + '<a href="person:%d"><b>%s</b></a>', [code, PersonName]);
  //s:='<tr><td>'+s+'</td></tr>';
  
	jThumb:=format('<a href="person:%d"><img src="%s" height="%d" border=0 align=left></a>',[code, FileName, PhotoHeight]);
  
	ThmPage:=GetTemplate('Thumb3Person');
	if (ThmPage<>'') then begin
		i:=pos('<!--#',ThmPage);
		while (i>0) do begin
			j:=MyPos('-->',ThmPage,i);
			tag:='';
			info:='';
			if (j>i) then begin
				tag:=copy(ThmPage,i+5,j-i-5);
				while (copy(tag,1,1)=' ') do tag:=copy(tag,2,length(tag)-1);
				while (copy(tag,length(tag),1)=' ') do tag:=copy(tag,1,length(tag)-1);
				k:=pos(' ',tag);
				if (k>0) then tag:=copy(tag,1,k-1);
				j:=j+3;
			end;
			
			if (tag='_cover_') 					then info:=Filename
			else if (tag='personthumb') 		then info:=jThumb
			else if (tag='checkmode') 			then info:=CheckMode
			else if (tag='name') 	 			then info:=jName;

			if (info<>'') then ThmPage:=copy(ThmPage,1,j-1) + info + copy(ThmPage,j,length(ThmPage)-j+1);

			i:=MyPos('<!--#',ThmPage,i+1);
		end;
		
		s:=ThmPage;
	end;  
  
  result:=s;
  sl.Free;
end;

begin

end.