const
	script_name='Original';
	language='';
	version=27;
	author='movienizer.com';
//version 4.2
//23.04.11
var

	genre, country, movie, person, title, year, studio, ProgramReference, myRating: string;
	InCollection: integer;
	GroupOperation: boolean;
  // 22.04.2011 - Fade variables for show images in some rows (multi show)
  MultiShowRowHeight: Integer;
  // width of the image that will be displayed (with of the column will be MultiShowImageWidth + 2*MiltiShowDistanceBetweenCovers)
  MultiShowImageWidth: Integer;
  // distance between images on the show area
  MiltiShowDistanceBetweenCovers: Integer;

function GetUserReference(code: integer; text: string): string;
begin
	result:=GetUserReference2(code, text, 1);
end;


//show movies by selected user reference
//links like <a href='userref:Code,Text'>link</a>
function GetUserReference2(code: integer; text: string; page: integer): string;
begin
	if code=100 then
		result:=ShowScriptOptions
	else
		result:=format('code=%d, text=%s', [code, text]);
end;

//show movies by selected manual
function GetReference(ReferenceCode, reference: integer): string;
begin
	result:=GetReference2(ReferenceCode, reference, 0);
end;

//show movies by selected manual
function GetReference2(ReferenceCode, reference, page: integer): string;
var
  sl: TStringList;
  i, kol, RecordCount, rating, this_code, last_code: integer;
  s, description, year, director, writer, actor, bio, character, movie: string;
  all, FileName, img, rate: string;
  AudioEditionMediaType: Integer;
begin
	result:='';
	ProgramReference:='';
  sl:=TStringList.Create;
	last_code := 0;
  all:='';
  if ReferenceCode>0 then begin
    //LastMode:=4;
      with sl do begin
        Clear;
        Add('select name from manuals');
        if ((Reference = 6) or (Reference = 7)) then
          Add(format('where code=%d and reference=%d', [ReferenceCode, 23]))
        else
          Add(format('where code=%d and reference=%d', [ReferenceCode, reference]));
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount<=0 then
        exit;
      ProgramReference:=GetStringField('name');

      //показываем фильмы по выбранному справочнику
      with sl do begin
        Clear;
        //custom fields
        if (reference>=13) and (reference<=16) then begin
        	i:=reference-12;
	        Add('select m.title, m.code, m.year, m.in_collection, m.rating');
        	Add('from movies m');
  	      Add(format('where m.custom%d=%d', [i, ReferenceCode]));
	        Add('order by m.year desc, m.title');
        end
        //aspect ratio
        else if (reference=4) then begin
	        Add('select m.title, m.code, m.year, m.in_collection, m.rating');
        	Add('from movies m');
        	Add('left outer join editions e on e.movie=m.code');
  	      Add(format('where e.screen_ratio=%d', [ReferenceCode]));
	        Add('order by m.year desc, m.title');
        end
        //resolution
        else if (reference=5) then begin
	        Add('select m.title, m.code, m.year, m.in_collection, m.rating');
        	Add('from movies m');
        	Add('left outer join editions e on e.movie=m.code');
  	      Add(format('where e.resolution=%d', [ReferenceCode]));
	        Add('order by m.year desc, m.title');
        end
        //location
        else if (reference=10) then begin
	        Add('select m.title, m.code, m.year, m.in_collection, m.rating');
        	Add('from movies m');
        	Add('left outer join editions e on e.movie=m.code');
        	Add('inner join loans l on l.edition=e.code');
  	      Add(format('where l.location=%d', [ReferenceCode]));
  	      Add('and (l.return_date='''' or l.return_date is null)');
	        Add('order by m.year desc, m.title');
        end
        //disc label
        else if (reference=20) then begin
	        Add('select m.title, m.code, m.year, m.in_collection, m.rating');
        	Add('from movies m');
        	Add('left outer join editions e on e.movie=m.code');
  	      Add(format('where e.disc_label=%d', [ReferenceCode]));
	        Add('order by m.year desc, m.title');
        end
        //subtitles or audio tracks
        else if (reference=6) or (reference=7) then
        begin
			Add('select m.title, m.code, m.year, m.in_collection, m.rating, m.imdb_rating, m.series_nom');
			Add('from movies m');
			AudioEditionMediaType := 7 - reference;
        	Add('left outer join editions e on e.movie=m.code');
    		Add(format('inner join audio_manuals em on em.edition=e.code and em.media_type=%d', [AudioEditionMediaType]));
    	    Add('inner join manuals mg on mg.code=em.reference');
    		Add(format('where mg.code=%d and m.in_collection=1', [ReferenceCode]));
	        Add('order by m.title');
        end	
        //media format
        else if (reference=2) then begin
	        Add('select m.title, m.code, m.year, m.in_collection, m.rating');
        	Add('from movies m');
        	Add('left outer join editions e on e.movie=m.code');
  	      Add(format('where e.media_format=%d', [ReferenceCode]));
	        Add('order by m.year desc, m.title');
        end
        // MPAA
        else if (reference = 11) then
        begin
	        Add('select m.title, m.code, m.year, m.in_collection, m.rating');
        	Add('from movies m');
  	      Add(format('where m.mpaa=%d', [ReferenceCode]));
	        Add('order by m.year desc, m.title');
        end
        // video standard
        else if (reference = 1) then
        begin
	        Add('select m.title, m.code, m.year, m.in_collection, m.rating');
        	Add('from movies m');
        	Add('left outer join editions e on e.movie=m.code');
  	      Add(format('where e.video_standard=%d', [ReferenceCode]));
	        Add('order by m.year desc, m.title');
        end
        // media types
        else if (reference = 3) then
        begin
	        Add('select m.title, m.code, m.year, m.in_collection, m.rating');
        	Add('from movies m');
  	      Add(format('where m.media_type=%d', [ReferenceCode]));
	        Add('order by m.year desc, m.title');          
        end
        // video codec
        else if (reference = 19) then
        begin
	        Add('select m.title, m.code, m.year, m.in_collection, m.rating');
        	Add('from movies m');
        	Add('left outer join editions e on e.movie=m.code');
  	      Add(format('where e.video_codec=%d', [ReferenceCode]));
	        Add('order by m.year desc, m.title');
        end
		//sequence
		else if (reference=22) then begin
	        Add('select m.title, m.code, m.year, m.in_collection, m.rating');
        	Add('from movies m');
			Add('left outer join images i on i.movie=m.code and i.mode=1');
			Add(format('where m.series=%d and m.in_collection=1', [ReferenceCode]));
	        Add('order by m.series_nom, i.sort_order');
        end
        else begin
	        Add('select m.title, m.code, m.year, m.in_collection, m.rating');
        	Add('from movies m');
      	  Add(format('inner join movies_manuals mm on mm.movie=m.code and mm.reference=%d', [reference]));
    	    Add('inner join manuals mg on mg.code=mm.ref_code');
  	      Add(format('where mg.code=%d', [ReferenceCode]));
	        Add('order by m.year desc, m.title');
        end;
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
        //movie:=format('<h1>Movies by genre %s</h1><ol>', [genre]);
        case reference of
          // video standard
          1: s := GetLangStr(3278) + ': ' + ProgramReference;
        	//media format
        	2: s:=format(GetLangStr(1526), [ProgramReference]);
          // media type
          3: s := GetLangStr(3279) + ': ' + ProgramReference;
        	//aspect ratio
        	4: s:=format(GetLangStr(645), [ProgramReference]);
        	//resolution
        	5: s:=format(GetLangStr(633), [ProgramReference]);
        	//subtitles
        	6: s:=format(GetLangStr(634), [ProgramReference]);
        	//audio track
        	7: s:=format(GetLangStr(632), [ProgramReference]);
        	//genre
        	8: s:=format(GetLangStr(313), [ProgramReference]);
        	//country
        	9: s:=format(GetLangStr(314), [ProgramReference]);
        	//location
        	10: s:=format(GetLangStr(569), [ProgramReference]);
          // MPAA
          11: S:= GetLangStr(3277) + ': ' + ProgramReference;
        	//studios
        	12: s:=format(GetLangStr(1527), [ProgramReference]);
        	//custom 1-4
        	13: s:=GetLangStr(540)+': '+ProgramReference;
        	14: s:=GetLangStr(541)+': '+ProgramReference;
        	15: s:=GetLangStr(542)+': '+ProgramReference;
        	16: s:=GetLangStr(543)+': '+ProgramReference;
        	//custom list 1-2
        	17: s:=GetLangStr(544)+': '+ProgramReference;
        	18: s:=GetLangStr(545)+': '+ProgramReference;
          // video codec
          19: s := GetLangStr(3280) + ': ' + ProgramReference;
        	//disc label
        	20: s:=format(GetLangStr(1001), [ProgramReference]);
			//sequence
			22: s:=GetLangStr(3327)+ ': ' + ProgramReference;
				else
					s:='';
        end;
        movie:='<h1>'+s+'</h1><ol>';
        for i:=1 to RecordCount do begin
		  this_code := GetIntegerField('m.code');
		  if this_code <> last_code then begin
				rating:=GetIntegerField('m.rating');
				if rating>0 then
					rate:=format(' <img src="%sstar.gif" width=16 height=16 border=0 align="absmiddle">%d', [PicturesPath, rating])
				else
					rate:='';

				if GetIntegerField('m.in_collection')=1 then
						img:=format('<img src="%smovie.gif" align="absmiddle">', [PicturesPath])
					else
						img:='';

				year:=GetStringField('m.year');
				if year='' then
					movie:=movie+format('<li>%s<a href="movie:%d">%s</a>',
					[img, GetIntegerField('m.code'), GetStringField('m.title')])+
				else
					movie:=movie+format('<li>%s<a href="movie:%d">%s</a> (%s)',
					[img, GetIntegerField('m.code'), GetStringField('m.title'), year]);

				movie:=movie+rate;
				last_code := this_code;
		  end;					          
          GetNextRecord;
        end;
        movie:=movie+'</ol>';
        all:=all+movie;
      end;
  end;
  sl.Free;
  result:=all;
end;


//show movies by selected year
function GetYear(YearCode: integer): string;
begin
	result:=GetYear2(YearCode, 1);
end;

//show movies by selected year
function GetYear2(YearCode, page: integer): string;
var
  sl: TStringList;
  i, kol, RecordCount: integer;
  s, description, director, writer, actor, bio, character, movie: string;
  all, FileName, img, y: string;
begin
	result:='';
  sl:=TStringList.Create;

  all:='';
  if YearCode>0 then begin
      with sl do begin
        Clear;
        Add('select m.title, m.code, m.year, m.in_collection');
        Add('from movies m');
        Add(format('where m.year=%d', [YearCode]));
        Add('order by m.year desc, m.title');
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
        movie:='<h1>'+format(GetLangStr(315), [YearCode])+'</h1><ol>';
        for i:=1 to RecordCount do begin
      	if GetIntegerField('m.in_collection')=1 then
					img:=format('<img src="%smovie.gif" align="absmiddle">', [PicturesPath])
				else
					img:='';

          y:=GetStringField('m.year');
          if y='' then
            movie:=movie+format('<li>%s<a href="movie:%d">%s</a>',
              [img, GetIntegerField('m.code'), GetStringField('m.title')])
          else
            movie:=movie+format('<li>%s<a href="movie:%d">%s</a> (%s)',
              [img, GetIntegerField('m.code'), GetStringField('m.title'), y]);
          GetNextRecord;
        end;
        movie:=movie+'</ol>';
        all:=all+movie;

      end;
			year:=IntToStr(YearCode);
  end;
  sl.Free;
  result:=all;
end;

function GetMyRating(rating: integer): string;
begin
  Result := GetMyRating2(rating, 1);
end;

//show movies by selected rating
function GetMyRating2(rating, page: integer): string;
var
  sl: TStringList;
  i, kol, RecordCount: integer;
  s, description, director, writer, actor, bio, character, movie: string;
  all, FileName, img, y: string;
  rating_s: string;
begin
	result:='';
  sl:=TStringList.Create;

  all:='';
  with sl do begin
    Clear;
    Add('select m.title, m.code, m.rating, m.in_collection');
    Add('from movies m');
    Add(format('where m.rating=%d', [rating]));
    Add('and m.in_collection = 1');
    Add('order by m.rating desc, m.title');
  end;
  RecordCount:=ExecuteSQL(sl.text);
  if RecordCount>0 then
  begin
    if (Rating > 0) then
      rating_s := IntToStr(rating)
    else
      rating_s := GetLangStr(3885);
    rating_s := '"' + rating_s + '"';

    movie:='<h1>' + format(GetLangStr(3884), [rating_s]) + '</h1><ol>';
    for i:=1 to RecordCount do begin
    if GetIntegerField('m.in_collection')=1 then
      img:=format('<img src="%smovie.gif" align="absmiddle">', [PicturesPath])
    else
      img:='';

      y:=GetStringField('m.rating');
      if y='' then
        movie:=movie+format('<li>%s<a href="movie:%d">%s</a>',
          [img, GetIntegerField('m.code'), GetStringField('m.title')])
      else
        movie:=movie+format('<li>%s<a href="movie:%d">%s</a> (%s)',
          [img, GetIntegerField('m.code'), GetStringField('m.title'), y]);
      GetNextRecord;
    end;
    movie:=movie+'</ol>';
    all:=all+movie;
  end;
  myRating := IntToStr(rating);
  sl.Free;
  result:=all;
end;


function GetMovie(MovieCode, season: integer; FullDesc: boolean): string;
begin
	result:=GetMovie3(MovieCode, season, FullDesc, false, false);
end;

//show movie's info
function GetMovie3(MovieCode, season: integer; FullDesc, FullComments, FullPersons: boolean): string;
begin
	result:=GetMovie4(MovieCode, season, FullDesc, FullComments, FullPersons, false);
end;


//show movie's info
function GetMovie4(MovieCode, season: integer; FullDesc, FullComments, FullPersons, FullAwards: boolean): string;
var
  sl: TStringList;
  duration, i, j, kol, RecordCount: integer;
  all, s, s2, country, description, comment, genre, year, director, writer, composer, actor, character, down, salary, awards: string;
  rating: integer;
  IMDBRating, SiteRating: double;
  FileName, img, OriginalTitle, site, url, postfix: string;
  flCollection, flSeen, flWanted, flForSale, LastCode: integer;
  collection, seen, wanted, sale, rate, mpaa, mpaa_desc, disc_nom, seasons: string;
  VideoStandard, ScreenRatio, resolution, mpaa_code, VideoBitrate, VideoCodec, filesize: integer;
  DiscLabel: integer;
  custom: array[1..6] of integer;
  cust: string;
  DateAdd, DateUpdate, DateSeen: string;
  editions: array [1..1000] of integer;
  EditionsCount, EditionCode, MediaFormat, ie, EditionCost: integer;
  ed, barcode, links, studio, EditionFeatures, EditionComment, OtherRating, TagLine: string;
begin
	result:='';
  sl:=TStringList.Create;

  all:='';

      //links
      links:='';
      OtherRating:='';
      with sl do begin
        Clear;
        Add('select * from movies_codes mc');
        Add('left outer join partners p on mc.script=p.site');
        Add(format('where movie=%d', [MovieCode]));
	      Add('and p.site<>''trailer''');
        Add('order by p.sort_order');
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then
      	links:=links+'';
      for i:=1 to RecordCount do begin
      	site:=GetStringField('mc.script');
      	url:=GetStringField('mc.site');
      	postfix:=GetStringField('p.postfix');
      	SiteRating:=GetFloatField('mc.rating');
      	if postfix<>'' then
      		url:=url+postfix;
      	if site='buy' then
      		links:=links+format(GetLangStr(1324), ['http://www.movienizer.com/redirect/'+url])+' '
				else begin
					s:=GetStringField('p.picture');
					if s='' then
						links:=links+'<a href="link:http://www.movienizer.com/redirect/'+url+format('">%s</a> ', [site])
					else
						links:=links+'<a href="link:http://www.movienizer.com/redirect/'+url+'">'+format('<img src="%s%s" border="0" align="absmiddle"></a> ', [PicturesPath, s]);
				end;
				if (SiteRating>0) and (site<>'imdb') then
					OtherRating:=OtherRating+format('%s: %f', [site, SiteRating])+'<br>';
				GetNextRecord;
      end;

      //get movie info
      with sl do begin
        Clear;
        Add('select * from movies');
        Add(format('where code=%d', [MovieCode]));
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount=0 then
        exit;
      s:='movie:'+IntToStr(MovieCode);
      movie:=GetStringField('title');

      OriginalTitle:=GetStringField('original_title');
      rating:=GetIntegerField('rating');
      IMDBRating:=GetFloatField('imdb_rating');

			flCollection:=GetIntegerField('in_collection');
			flSeen:=GetIntegerField('seen');
			flWanted:=GetIntegerField('wanted');
			flForSale:=GetIntegerField('for_sale');

			mpaa_code:=GetIntegerField('mpaa');
			mpaa_desc:=GetStringField('mpaa_rating');

			DateAdd:=GetStringField('date_add');
			DateUpdate:=GetStringField('date_update');
			DateSeen:=GetStringField('date_seen');
			
	  TagLine := GetStringField('tagline');

      //GoForward(s, GetStringField('m.title'), ChangeHistory);

      description:=GetStringField('description');
      salary:=GetStringField('salary');
      awards:=GetStringField('awards');
			comment:=GetStringField('comment');

			custom[1]:=GetIntegerField('custom1');
			custom[2]:=GetIntegerField('custom2');
			custom[3]:=GetIntegerField('custom3');
			custom[4]:=GetIntegerField('custom4');

      if GetStringField('duration')='' then
        duration:=0
      else
        duration:=GetIntegerField('duration');
      year:=GetStringField('year');

      if flCollection=0 then
	      collection:=format('<a href="collection:%d"><img src="%snot_collection.gif" border="0" alt="%s"></a>',  [MovieCode, PicturesPath, GetLangStr(326)])
      else
	      collection:=format('<a href="collection:%d"><img src="%scollection.gif" border="0" alt="%s"></a>',  [MovieCode, PicturesPath, GetLangStr(325)]);


      if flSeen=0 then
	      seen:=format('<a href="seen:%d"><img src="%snot_seen.gif" border="0" alt="%s"></a>',  [MovieCode, PicturesPath, GetLangStr(328)])
      else
	      seen:=format('<a href="seen:%d"><img src="%sseen.gif" border="0" alt="%s"></a>',  [MovieCode, PicturesPath, GetLangStr(327)]);

      if flWanted=0 then
	      wanted:=format('<a href="wanted:%d"><img src="%snot_wanted.gif" border="0" alt="%s"></a>',  [MovieCode, PicturesPath, GetLangStr(330)])
      else
	      wanted:=format('<a href="wanted:%d"><img src="%swanted.gif" border="0" alt="%s"></a>',  [MovieCode, PicturesPath, GetLangStr(329)]);

      if flForSale=0 then
	      sale:=format('<a href="sale:%d"><img src="%snot_sale.gif" border="0" alt="%s"></a>',  [MovieCode, PicturesPath, GetLangStr(332)])
      else
	      sale:=format('<a href="sale:%d"><img src="%ssale.gif" border="0" alt="%s"></a>',  [MovieCode, PicturesPath, GetLangStr(331)]);

			rate:='';
			for i:=1 to 10 do begin
				if i mod 2=1 then begin
					if i<=rating then
		      	rate:=rate+format('<a href="rating:%d"><img src="%sstar-left.gif" align="absmiddle" border="0"></a>',  [i, PicturesPath])
					else
	    	  	rate:=rate+format('<a href="rating:%d"><img src="%snot-star-left.gif" align="absmiddle" border="0"></a>',  [i, PicturesPath])
				end
				else begin
					if i<=rating then
		      	rate:=rate+format('<a href="rating:%d"><img src="%sstar-right.gif" align="absmiddle" border="0"></a>',  [i, PicturesPath])
					else
	    	  	rate:=rate+format('<a href="rating:%d"><img src="%snot-star-right.gif" align="absmiddle" border="0"></a>',  [i, PicturesPath])
				end;
			end;

			//description
      if description<>'' then begin
				if not FullDesc and (length(description)>DescriptionLength) then begin
					description:=copy(description, 1, DescriptionLength);
					description:=description+format(GetLangStr(566), [MovieCode]);
				end;
			end;

			if description='' then
				down:='<br>'+format('<a href="DownloadMovie:%d">', [MovieCode])+GetLangStr(324)+'</a>'
			else
				down:='';

	    all:='<table border="0" width="100%"><tr><td width="100%">';
	    all:=all+format('<h1>%s</h1></td><td>', [movie])+links+'</td>'+format('<td width="120" align="right">%s%s%s%s<a href="UpdateMovie:%d"><br><small>%s%s</small></a></td></tr></table><table border="0" cellpadding="0" cellspacing="0" width="100%%"><tr><td width="100%%">', [collection, seen, wanted, sale, MovieCode, GetLangStr(304), down]);
      //pictures
      with sl do begin
        Clear;
        Add('select * from images');
        Add(format('where movie=%d and mode=1', [MovieCode]));
		Add('order by sort_order');
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
        FileName:=ImagesPath+GetStringField('path');
        if (MyFileExists(FileName)) then
          all:=all+format('<a href="ShowImage:%d"><img src="%s" height="200" align="left" border="0"></a>',
            [GetIntegerField('code'), FileName]);
      end
      else
        ;//all:=format('%s<a href="UpdateFrontCover:%d">Add front cover</a><br>', [all, MovieCode]);

      //mpaa
      mpaa:='';
      if mpaa_code>0 then begin
	      with sl do begin
  	      Clear;
    	    Add('select name from manuals');
      	  Add(format('where code=%d', [mpaa_code]));
	      end;
  	    RecordCount:=ExecuteSQL(sl.text);
    	  if RecordCount>0 then
        begin
    	  	s:=GetStringField('name');
	        //s:=PicturesPath+GetStringField('name')+'.gif';
	        //if MyFileExists(s) then
	        	//mpaa:=format('<img border="0" src="%s" align="right" width="100">', [s]);
          s:= Format('<a href="mpaa:%d">%s</a>', [mpaa_code, GetStringField('name')]);
	        mpaa:=format(GetLangStr(374), [s]);
	        if mpaa_desc<>'' then
	        	mpaa:=mpaa+' '+mpaa_desc;
	      end;
			end;
			
	  if TagLine <> '' then
		all := all + format('%s: "%s"<br>', [GetLangStr(3406), TagLine]);
			
	  if (OriginalTitle<>'') and (movie<>OriginalTitle) then
			//Original title
        all:=all+format('%s: <b>%s</b><br> ', [GetLangStr(296), OriginalTitle]);
		
		//movie series
		with sl do begin
			Clear;
			Add('select m.code, m.name, mov.series_nom from movies mov');
			Add('join manuals m on m.code=mov.series'); 
			Add(format('where mov.code=%d', [MovieCode]));
		end;
		RecordCount:=ExecuteSQL(sl.text);
		if RecordCount > 0 then begin
			all := all + GetLangStr(20271) + ': ' + Format('<a href="sequence:%d">%s</a> %s%d', [GetIntegerField('m.code'), GetStringField('m.name'), GetLangStr(529), GetIntegerField('mov.series_nom')]) + '<br>';
		end;
		
      if year<>'' then
      	//year
        all:=all+format('%s: <a href="year:%s">%s</a>. ', [GetLangStr(169), year, year]);
      //продолжительность
      if duration>0 then
      	//Duration
        all:=all+format(GetLangStr(297)+' <small>(%0.2d:%0.2d)</small>', [duration, duration div 60, duration mod 60]);

			if (year<>'') or (duration>0) then
				all:=all+'<br>';
			//all:=all+'My rating: '+rate+format(', IMDB rating: %0.1f<br>', [IMDBRating]);
			if IMDBRating=0 then
				all:=all+GetLangStr(298)+': '+rate+'<br>'
			else
				all:=all+GetLangStr(298)+': '+rate+', '+format(GetLangStr(299), [IMDBRating])+'<br>';

			if RegReadInteger('cbShowSiteRating', 1)=1 then
				all:=all+OtherRating;
			//ShowMessage(OtherRating);

			if mpaa<>'' then
				all:=all+mpaa+'<br>';

      //countries
      with sl do begin
        Clear;
        Add('select * from movies_manuals mm');
        Add('join manuals m on m.code=mm.ref_code');//and m.reference=mm.reference надо ли???
        Add(format('where mm.movie=%d and m.reference=9', [MovieCode]));
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
      	//'Countries: '
        country:=GetLangStr(73)+': ';
        for i:=1 to RecordCount do begin
          country:=country+format('<a href="country:%d">%s</a> / ', [GetIntegerField('m.code'), GetStringField('m.name')]);
          GetNextRecord;
        end;
        country:=copy(country, 1, length(country)-3);
        all:=all+format('%s<br>', [country]);
      end;

      //genres
      with sl do begin
        Clear;
        Add('select * from movies_manuals mm');
        Add('join manuals m on m.code=mm.ref_code');//and m.reference=mm.reference надо ли???
        Add(format('where mm.movie=%d and m.reference=8', [MovieCode]));
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
      	//'Genres: '
        genre:=GetLangStr(72)+': ';
        for i:=1 to RecordCount do begin
          genre:=genre+format('<a href="genre:%d">%s</a> / ', [GetIntegerField('m.code'), GetStringField('m.name')]);
          GetNextRecord;
        end;
        genre:=copy(genre, 1, length(genre)-3);
        all:=all+genre+'<br>';
      end;

      //studios
      with sl do begin
        Clear;
        Add('select * from movies_manuals mm');
        Add('join manuals m on m.code=mm.ref_code');//and m.reference=mm.reference надо ли???
        Add(format('where mm.movie=%d and m.reference=12', [MovieCode]));
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
      	//'Studios: '
        studio:=GetLangStr(493)+': ';
        for i:=1 to RecordCount do begin
          studio:=studio+format('<a href="studio:%d">%s</a> / ', [GetIntegerField('m.code'), GetStringField('m.name')]);
          GetNextRecord;
        end;
        studio:=copy(studio, 1, length(studio)-3);
        all:=all+studio+'<br>';
      end;

      //directors
      with sl do begin
        Clear;
        Add('select distinct * from data d');
        Add('inner join persons p on p.code=d.person');
        Add(format('where d.movie=%d and d.mode=1', [MovieCode]));
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
	      //'Directors: ';
        s:=GetLangStr(66)+': ';
        i:=1;
        while i<=RecordCount do begin
          if GetIntegerField('p.in_collection')=1 then
            img:=format('<img src="%sperson.gif" align="absmiddle">', [PicturesPath])
          else
            img:='';
          s:=s+img+format('<a href="person:%d">', [GetIntegerField('p.code')])+GetStringField('p.name')+'</a>, ';
          if (i=5) and (i<RecordCount) and not FullPersons then
						i:=RecordCount;          	
          i:=i+1;
          GetNextRecord;
        end;
        s:=copy(s, 1, length(s)-2);
        if (RecordCount>=6) and not FullPersons then
        	s:=s+format(GetLangStr(1251), [MovieCode]);
        all:=all+s+'<br>';
      end;

      //writers
      with sl do begin
        Clear;
        Add('select distinct * from data d');
        Add('inner join persons p on p.code=d.person');
        Add(format('where d.movie=%d and d.mode=2', [MovieCode]));
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
      	//'Writers: '
        s:=GetLangStr(67)+': ';
        i:=1;
        while i<=RecordCount do begin
          if GetIntegerField('p.in_collection')=1 then
            img:=format('<img src="%sperson.gif" align="absmiddle">', [PicturesPath])
          else
            img:='';
          s:=s+img+format('<a href="person:%d">', [GetIntegerField('p.code')])+GetStringField('p.name')+'</a>, ';
          if (i=5) and (i<RecordCount) and not FullPersons then
						i:=RecordCount;          	
          i:=i+1;
          GetNextRecord;
        end;
        s:=copy(s, 1, length(s)-2);
        if (RecordCount>=6) and not FullPersons then
        	s:=s+format(GetLangStr(1251), [MovieCode]);
        all:=all+s+'<br>';
      end;

      //composers
      with sl do begin
        Clear;
        Add('select distinct * from data d');
        Add('inner join persons p on p.code=d.person');
        Add(format('where d.movie=%d and d.mode=4', [MovieCode]));
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
      	//'Composers: '
        s:=GetLangStr(491)+': ';
        i:=1;
        while i<=RecordCount do begin
          if GetIntegerField('p.in_collection')=1 then
            img:=format('<img src="%sperson.gif" align="absmiddle">', [PicturesPath])
          else
            img:='';
          s:=s+img+format('<a href="person:%d">', [GetIntegerField('p.code')])+GetStringField('p.name')+'</a>, ';
          if (i=5) and (i<RecordCount) and not FullPersons then
						i:=RecordCount;          	
          i:=i+1;
          GetNextRecord;
        end;
        s:=copy(s, 1, length(s)-2);
        if (RecordCount>=6) and not FullPersons then
        	s:=s+format(GetLangStr(1251), [MovieCode]);
        all:=all+s+'<br>';
      end;

      //actors
      with sl do begin
        Clear;
        Add('select distinct p.code, p.name, p.in_collection, c.name from data d');
        Add('inner join persons p on p.code=d.person');
        Add('left outer join characters c on c.person=p.code and c.movie=d.movie');
        Add(format('where d.movie=%d and d.mode=3', [MovieCode]));
        Add('order by d.sort_order');
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
      	//'Actors: '
        s:=GetLangStr(68)+': ';
        i:=1;
        while i<=RecordCount do begin
          if GetIntegerField('p.in_collection')=1 then
            img:=format('<img src="%sperson.gif" align="absmiddle">', [PicturesPath])
          else
            img:='';
          character:=GetStringField('c.name');
          if character='' then
            //actor:=actor+format('%s<a href="person:%d">%s</a>, ', [img, GetIntegerField('p.code'), GetStringField('p.name')])
            s:=s+img+format('<a href="person:%d">', [GetIntegerField('p.code')])+GetStringField('p.name')+'</a>, '
          else
            s:=s+img+format('<a href="person:%d">', [GetIntegerField('p.code')])+GetStringField('p.name')+format('</a> (%s), ', [character]);
          if (i=10) and (i<RecordCount) and not FullPersons then
						i:=RecordCount;          	
          i:=i+1;
          GetNextRecord;
        end;
        s:=copy(s, 1, length(s)-2);
        if (RecordCount>=11) and not FullPersons then
        	s:=s+format(GetLangStr(1251), [MovieCode]);
        all:=all+s+'<br>';
      end;
      all:=all+'</td></tr></table>';

			//dates
			s:='';
			if DateAdd<>'' then
				s:=s+format(GetLangStr(620), [SQLDate2Str(DateAdd)])+', ';
			if DateUpdate<>'' then
				s:=s+format(GetLangStr(621), [SQLDate2Str(DateUpdate)])+', ';
			if DateSeen<>'' then
				s:=s+format(GetLangStr(622), [SQLDate2Str(DateSeen)])+', ';
			if s<>'' then
				all:=all+copy(s, 1, length(s)-2)+'<br>';

      //custom fields
      cust:='';
      for i:=1 to 4 do begin
      	if custom[i]>0 then begin
		      with sl do begin
  		      Clear;
    		    Add('select name from manuals');
      		  Add(format('where code=%d', [custom[i]]));
		      end;
	  	    RecordCount:=ExecuteSQL(sl.text);
  	  	  if RecordCount>0 then begin
		        cust:=cust+format(GetLangStr(539+i), [s])+format(': <a href="custom%d:%d">', [i, custom[i]])+GetStringField('name')+'</a><br>';
	    	  end;
	      end;
			end;
			all:=all+cust;

			//custom list1
			cust:='';
      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=17', [MovieCode]));
        Add('order by m.name');
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
        cust:=cust+GetLangStr(544)+': ';
        for i:=1 to RecordCount do begin
		      cust:=cust+format('<a href="custom5:%d">%s</a>, ', [GetIntegerField('m.code'), GetStringField('m.name')]);
          GetNextRecord;
        end;
        cust:=copy(cust, 1, length(cust)-2);
        all:=all+cust+'<br>';
      end;

			//custom list2
			cust:='';
      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=18', [MovieCode]));
        Add('order by m.name');
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
        cust:=cust+GetLangStr(545)+': ';
        for i:=1 to RecordCount do begin
		      cust:=cust+format('<a href="custom6:%d">%s</a>, ', [GetIntegerField('m.code'), GetStringField('m.name')]);
          GetNextRecord;
        end;
        cust:=copy(cust, 1, length(cust)-2);
        all:=all+cust+'<br>';
      end;

			//description
      if description<>'' then
      	all:=all+'<hr height="1">'+description+'';

			//awards
			if awards<>'' then begin
				if not FullAwards and (length(awards)>DescriptionLength) then begin
					awards:=copy(awards, 1, DescriptionLength);
					awards:=awards+format(GetLangStr(1331), [MovieCode]);
				end;
				all:=all+'<hr height="1">'+awards+'';
			end;

			//salary
			if salary<>'' then
				all:=all+'<br><small>'+salary+'</small>';

			//comments
			if comment<>'' then begin
				if not FullComments and (length(comment)>DescriptionLength) then begin
					comment:=copy(comment, 1, DescriptionLength);
					comment:=comment+format(GetLangStr(1250), [MovieCode]);
				end;
				all:=all+'<hr height="1">'+comment+'';
			end;
      
      //pictures from movie
      with sl do begin
        Clear;
        Add('select * from images');
        Add(format('where movie=%d', [MovieCode]));
        Add('order by sort_order');
      end;
      RecordCount:=ExecuteSQL(sl.text);
      kol:=0;
      for i:=1 to RecordCount do begin
      	if GetIntegerField('mode')=1 then
      		kol:=kol+1;
				GetNextRecord;
			end;
			if (RecordCount-kol>0) or (kol>1) then begin
				RecordCount:=ExecuteSQL(sl.text);
	      if RecordCount>0 then
  	    	all:=all+'<hr>';
	      for i:=1 to RecordCount do begin
  	      FileName:=ImagesPath+GetStringField('path');
    	    if (MyFileExists(FileName)) then
      	    all:=all+format('<a href="ShowImage:%d"><img src="%s" height="100" border="0"></a>&nbsp;',
        	    [GetIntegerField('code'), FileName]);
					GetNextRecord;
  	    end;
			end;

      //photos of people
			LastCode:=0;
      with sl do begin
        Clear;
        Add('select distinct i.path as path, i.person as code');
        Add('from data d');
        Add('inner join images i on i.person=d.person');
        Add(format('where d.movie=%d', [MovieCode]));
        //Add('order by i.person, i.path');
        Add('order by d.mode, d.sort_order, i.sort_order');
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then
      	all:=all+'<hr>';
      for i:=1 to RecordCount do begin
      	if (LastCode=0) or (GetIntegerField('code')<>LastCode) then begin
      		LastCode:=GetIntegerField('code');

	        FileName:=ImagesPath+GetStringField('path');
    	    if (MyFileExists(FileName)) then
      	    all:=all+format('<a href="person:%d"><img src="%s" height="100" border="0"></a>&nbsp;',
        	    [GetIntegerField('code'), FileName]);
				end;
				GetNextRecord;
      end;


      //editons
      with sl do begin
      	Clear;
      	Add('select * from editions e');
      	Add('left outer join manuals m on m.code=e.edition and m.reference=21');
      	Add(format('where e.movie=%d', [MovieCode]));
      	Add('order by e.sort_order');
      end;
      EditionsCount:=ExecuteSQL(sl.text);

      for ie:=1 to EditionsCount do begin
      	editions[ie]:=GetIntegerField('e.code');
      	GetNextRecord;
      end;

      if EditionsCount>0 then
      	all:=all+'<hr>'
			else
      	all:=all+'<br>';

      for ie:=1 to EditionsCount do begin
      	EditionCode:=editions[ie];
        //editons
        with sl do begin
        	Clear;
        	Add('select * from editions e');
        	Add('left outer join manuals m on m.code=e.edition and m.reference=21');
        	Add(format('where e.code=%d', [EditionCode]));
        end;
        ExecuteSQL(sl.text);

      	ed:='';

      	//video parameters
      	VideoBitrate:=GetIntegerField('e.video_bitrate');
      	filesize:=GetIntegerField('e.filesize');
      	DiscLabel:=GetIntegerField('e.disc_label');
      	disc_nom:=GetStringField('e.disc_nom');
      	duration:=GetIntegerField('e.duration');
      	barcode:=GetStringField('e.barcode');

      	VideoStandard:=GetIntegerField('e.video_standard');
      	ScreenRatio:=GetIntegerField('e.screen_ratio');
      	resolution:=GetIntegerField('e.resolution');
      	VideoCodec:=GetIntegerField('e.video_codec');
      	MediaFormat:=GetIntegerField('e.media_format');

      	EditionFeatures:=GetStringField('e.features');
      	EditionComment:=GetStringField('e.comment');
		EditionCost := GetIntegerField('e.cost');


      	if EditionsCount>1 then
	      	ed:=ed+'<b>'+format(GetLangStr(1260), [ie])+'</b>. ';

      	if GetStringField('m.name')<>'' then
      		ed:=ed+GetStringField('m.name')+'. ';
      	if not ((GetIntegerField('e.season')=0) and (GetIntegerField('e.episode')=0)) then
      		ed:=ed+format(GetLangStr(1252), [GetIntegerField('e.season'), GetIntegerField('e.episode')])+'. ';
      	if (GetStringField('e.filename')<>'') and (EditionsCount>1) then
      		ed:=ed+format('<a href="play:%s">', [GetStringField('e.filename')])+format('<img src="%splay.png" align="absmiddle" border="0"></a> ', [PicturesPath]);

      	if duration>0 then begin
      		ed:=ed+Format('<small>(%0.2d:%0.2d)</small> ', [duration div 60, duration mod 60]);
      	end;
      	//ed:=ed+'<br>';
      	if GetStringField('e.barcode')<>'' then
      		ed:=ed+GetLangStr(1259)+': '+GetStringField('e.barcode');

      	all:=all+ed;

      	if EditionFeatures<>'' then
      		all:=all+'<br>'+EditionFeatures;

      	if EditionComment<>'' then
      		all:=all+'<br>'+EditionComment;

      	//if (EditionFeatures<>'') or (EditionComment<>'') then
      		//all:=all+'<hr>';

        //parameters
        with sl do begin
          Clear;
          Add('select * from manuals');
          Add(format('where code in (%d, %d, %d, %d, %d)', [VideoStandard, ScreenRatio, resolution, VideoCodec, MediaFormat]));
          Add('order by reference');
        end;
        RecordCount:=ExecuteSQL(sl.text);
        if (RecordCount>0) and (ed<>'') then begin
        	all:=all+'<br>';
        end;
        for i:=1 to RecordCount do begin
        	if GetIntegerField('reference')=1 then
        		//Video standard
        		all:=all+GetLangStr(201)+': ' + format('<a href="video_standard:%d">%s</a>, ', [VideoStandard, GetStringField('name')])
        	else if GetIntegerField('reference')=2 then
        		//media format
            all:=all+GetLangStr(249)+': '+format('<a href="media_format:%d">%s</a>, ', [MediaFormat, GetStringField('name')])
            //all:=all+GetLangStr(249)+': '+format('%s, ', [GetStringField('name')])
        	else if GetIntegerField('reference')=4 then
        		//screen ratio
            all:=all+GetLangStr(202)+': '+format('<a href="aspect_ratio:%d">%s</a>, ', [ScreenRatio, GetStringField('name')])
        	
        	else if GetIntegerField('reference')=5 then
        		//resolution
        		//all:=all+GetLangStr(203)+': '+GetStringField('name')+', '
            all:=all+GetLangStr(203)+': '+format('<a href="resolution:%d">%s</a>, ', [resolution, GetStringField('name')])

        	else if GetIntegerField('reference')=19 then
        		//video codec
        		all:=all+GetLangStr(571)+': '+format('<a href="video_codec:%d">%s</a>, ', [VideoCodec, GetStringField('name')]);
          GetNextRecord;
        end;

        s2:='';
        //video bitrate
        if VideoBitrate>0 then
        	s2:=format(GetLangStr(572), [VideoBitrate])+', ';
        //filesize
        if filesize>0 then
        	s2:=s2+format(GetLangStr(629), [filesize])+', ';
        
        if RecordCount>0 then begin
        	all:=all+s2;
        	all:=copy(all, 1, length(all)-2);
        end
        else begin
        	if length(s2)>0 then begin
          	all:=all+'<hr>'+s2;
          	all:=copy(all, 1, length(all)-2);
        	end;
        end;

        //audio tracks
        with sl do begin
          Clear;
		  Add('select m.code, m.name, am.Bitrate, am.Codec, am.Channel, am.Short_Comment from audio_manuals am');
		  Add('left join manuals m on m.code=am.reference');
		  Add(format('where am.edition=%d and am.media_type=0', [EditionCode]));
        end;
        RecordCount:=ExecuteSQL(sl.text);
        if RecordCount>0 then begin
        	//Audio tracks
          all:=all+'<br>'+GetLangStr(254)+': ';
          for i:=1 to RecordCount do begin
			s2 := ' ' + GetStringField('am.codec');
			if (s2 <> '') then
			  S2 := s2 + ' ';
			if (GetIntegerField('am.channel') > 0) then
			  s2 := s2 + GetStringField('am.channel') + ' ch ';
			if (GetIntegerField('am.Bitrate') > 0) then
			  s2 := s2 + GetStringField('am.Bitrate') + ' kbps ';
			if (GetStringField('am.Short_Comment') <> '') then
			  s2 := s2 + GetStringField('am.Short_Comment') + ' ';
			  
			if (s2<>'') then
				s2 := copy(s2, 1, length(s2) - 1);
            all:=all+format('<a href="audio_track:%d">%s</a>%s, ', [GetIntegerField('m.code'), GetStringField('m.name'), s2]);

            GetNextRecord;
          end;
          all:=copy(all, 1, length(all)-2);
        end;

        //subtitles
        with sl do begin
          Clear;
		  Add('select m.code, m.name, am.Short_Comment from audio_manuals am');
		  Add('join manuals m on m.code=am.reference');
		  Add(format('where am.edition=%d and am.media_type=1', [EditionCode]));
        end;
        RecordCount:=ExecuteSQL(sl.text);
        if RecordCount>0 then begin
        	//Subtitles
          all:=all+'<br>'+GetLangStr(253)+': ';
          for i:=1 to RecordCount do begin
			s2 := '';
			if (GetStringField('am.Short_Comment') <> '') then
				s2 := ' ' + Trim(GetStringField('am.Short_Comment'));
            //all:=all+GetStringField('m.name')+', ';
            all:=all+format('<a href="subtitle:%d">%s</a>%s, ', [GetIntegerField('m.code'), GetStringField('m.name'), s2]);
            GetNextRecord;
          end;
          all:=copy(all, 1, length(all)-2);
        end;

        ed:='';
        //loans
        with sl do begin
          Clear;
          Add('select * from loans l');
          Add('inner join manuals m on m.code=l.location');
          Add(format('where edition=%d', [EditionCode]));
          Add('and (return_date='''' or return_date is null)');
          Add('order by loan_date desc');
        end;
        RecordCount:=ExecuteSQL(sl.text);
        if RecordCount>0 then begin
        	ed:=ed+''+GetLangStr(333)+format(': <a href="location:%d">%s</a>. ', [GetIntegerField('l.location'), GetStringField('m.name')]);
        end;

        //disk #
        if disc_nom<>'' then
        	ed:=ed+GetLangStr(391)+': '+disc_nom+'. ';


        //disc label
        if DiscLabel>0 then begin
          with sl do begin
            Clear;
            Add('select * from manuals');
            Add(format('where code=%d', [DiscLabel]));
          end;
          RecordCount:=ExecuteSQL(sl.text);
          if RecordCount>0 then begin
          	ed:=ed+format('%s: <a href="disc_label:%d">%s</a>', [GetLangStr(1000), DiscLabel, GetStringField('name')]);
        	end;
        end;
        if ed<>'' then
        	all:=all+'<br>'+ed;
			
		//Edition cost
		if EditionCost>0 then
			all := all + format('<br>%s: %d', [GetLangStr(3350), EditionCost]);

      	if ie<>EditionsCount then
      		all:=all+'<br><br>'
      	else begin
      		if ed<>'' then
	      		all:=all+'<br>';
				end;
      end;

      //end editions

			//seasons
			seasons:='';
			kol:=0;
      with sl do begin
        Clear;
        Add('select * from episodes e');
        Add('left outer join editions ed on ed.movie=e.movie and e.season=ed.season and e.episode=ed.episode');
        Add(format('where e.movie=%d', [MovieCode]));
        Add(' order by e.season, e.episode');
      end;
      RecordCount:=ExecuteSQL(sl.text);
      for i:=1 to RecordCount do begin
      	if GetIntegerField('e.season')>kol then begin
      		kol:=GetIntegerField('e.season');
	      	seasons:=seasons+'<h3>'+format(GetLangStr(514), [kol])+'</h3>';
      	end;
	      seasons:=seasons+'<h3>'+format(GetLangStr(518), [GetIntegerField('e.episode')])+'. '+GetStringField('e.title');

	      s:=GetStringField('e.original_air_date');
	      s:=SQLDate2Str(s);
	      if s<>'' then
		      seasons:=seasons+'. '+s;

	      if GetStringField('ed.filename')<>'' then
	      	seasons:=seasons+format('&nbsp;<a href="play:%s">', [GetStringField('ed.filename')])+format('<img src="%splay.png" align="absmiddle" border="0"></a>', [PicturesPath]);

	      seasons:=seasons+'</h3>';
	      seasons:=seasons+GetStringField('e.description');
				GetNextRecord;
			end;
			if seasons<>'' then
				all:=all+''+seasons+'';
      
  sl.Free;

  //all:='<div class="logo">'+all+'</div>';

  result:=all;
end;

function GenerateHTMLForDescriptionSearching(AFullDescription: string;
  ASearchDescription: string; AMaxLength: Integer = 350): string;
var
  StartPos: Integer;
  EndPos: Integer;
  SearchPos: Integer;
begin
  Result := '';
  if ((ASearchDescription <> '') and
      (AFullDescription <> '')) then
  begin
    SearchPos := Pos(LowerCase(ASearchDescription), LowerCase(AFullDescription));
    if (SearchPos > 0) then
    begin
      // calculate start pos
      StartPos := SearchPos - ((AMaxLength - Length(ASearchDescription)) div 2);
      if (StartPos <= 0) then
        StartPos := 1
      else
      begin
        // in other case search first space from StartPos position
        if (MyPos(' ', AFullDescription, StartPos) > 0) then
          StartPos := MyPos(' ', AFullDescription, StartPos) + 1;
      end;
      // find end pos of the text
      EndPos := StartPos + AMaxLength;
      if (EndPos > Length(AFullDescription)) then
        EndPos := Length(AFullDescription)
      else
      while ((Length(AFullDescription) > 0) and
             (AFullDescription[EndPos] <> ' ')) do
      begin
        Dec(EndPos);
      end;
      //
      Result := Copy(AFullDescription, StartPos, EndPos - StartPos);
      Result := StringReplaceIgnoreCase(Result, ASearchDescription,
        '<font style="background-color: yellow">' + ASearchDescription + '</font>');
      Result := Trim(StringReplaceIgnoreCase(Result, '<br>', ' '));
      if (StartPos <> 1) then
        Result := '...' + Result;
      if (EndPos <> Length(AFullDescription)) then
        Result := Result + '...';
    end;
  end;
  if (Result <> '') then
    Result := '<br>"' + Result + '"<br>';
end;

function StringReplaceIgnoreCase(AText: string; AReplaceWhat: string; AReplaceTo: string): string;
var
  TextPos: integer;
begin
  Result := AText;
  AReplaceWhat := LowerCase(AReplaceWhat);
  TextPos := Pos(AReplaceWhat, LowerCase(Result));
  while (TextPos > 0) do
  begin
    Delete(Result, TextPos, Length(AReplaceWhat));
    Insert(AReplaceTo, Result, TextPos);
    TextPos := MyPos(AReplaceWhat, LowerCase(Result), TextPos + Length(AReplaceTo));
  end;
end;

function MyPos(ssearch, shost: string; start: integer): integer;
var
	stemp: string;
	newpos: integer;
begin
	stemp:=copy(shost,start,length(shost)-start+1);
	newpos:=pos(ssearch, stemp);
	if (newpos>0) then newpos:=newpos+start-1;
	result:=newpos;
end;

//показ уменьшенной карточки фильма
function GetThumbMovie2(code, mode: integer; title, year: string; InCollection, rating: integer; AFullDescription: string = ''; ASearchDescription: string = ''): string;
var
  sl: TStringList;
  s, rate, FileName, genres, directors, actors, img, seasons: string;
  fl, ThisChecked, ShowCurrent: boolean;
  i, j, kol, dir, amount, RecordCount: integer;
	ThumbsPath, FileIn, FileOut, CheckMode: string;
	kinopoisk_rating, imdb_rating, current_rating, current_site: String;
	imdb_rating_number, kinopoisk_rating_number, current_rating_number: double;
begin
	result:='';
  sl:=TStringList.Create;
  amount:=mode;
  {
  if mode=1 then
    amount:=3
  else
    amount:=7;
	}
  s:='';

  ThumbsPath:=ImagesPath+'Thumbs\';

    fl:=false;
    genres:='';
    directors:='';
    actors:='';
	ThisChecked := False;
	imdb_rating := '';
	kinopoisk_rating := '';
	current_rating := '';
	current_site := '';
	imdb_rating_number := 0;
	kinopoisk_rating_number := 0;
	current_rating_number := 0;
	
	if RegReadInteger('ShowCurrentRatingInList', 1) = 1 then
		ShowCurrent := True
	else
		ShowCurrent := False;

    //получаем дополнительную информацию о фильме, если он из коллекции
    if InCollection=1 then begin
      //обложка
      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
//      	FileName:=ImagesPath+GetStringField('path');

	      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:=PicturesPath+'no-cover.gif';
        fl:=true;
      end;
	  
	  
		if ShowCurrent then begin
			current_site := RegReadString('MovieScript', '');
			i := Pos(' (', current_site);
			if i>0 then
				delete(current_site, i, length(current_site)-i+1);
			if (current_site <> '') and (current_site <> 'kinopoisk') and (current_site <> 'imdb') then begin
				with sl do begin
					Clear;
					Add('select rating from movies_codes');
					Add(format('where movie=%d and script=''%s''', [code, current_site]));
				end;
				RecordCount := ExecuteSQL(sl.text);
				if RecordCount > 0 then begin
					current_rating_number := GetFloatField('rating');
					if current_rating_number > 0 then
						current_rating := '<NOBR>' + current_site + ': ' + FloatToStr(current_rating_number) + '</NOBR>; ';
				end;
			end;
		end;
		
		if (RegReadInteger('ShowIMDBRatingInList', 0) = 1) or GroupOperation or ((current_site = 'imdb') and ShowCurrent) then begin
			with sl do begin
				Clear;
				Add('select checked, imdb_rating from movies');
				Add(format('where code=%d', [code]));
			end;
			RecordCount:=ExecuteSQL(sl.text);
			if RecordCount > 0 then begin
				if (GetIntegerField('checked') = 1) then
					ThisChecked := True;
				imdb_rating_number := GetFloatField('imdb_rating');
				if (imdb_rating_number > 0) then
					imdb_rating := Format('<NOBR><img align="absmiddle" src="%simdb-ico.png"> ', [PicturesPath]) + FloatToStr(imdb_rating_number) + '</NOBR>; ';
			end;
		end;
		
		if (RegReadInteger('ShowKinopoiskRatingInList', 0) = 1) or ((current_site = 'kinopoisk') and ShowCurrent) then begin
			with sl do begin
				Clear;
				Add('select rating from movies_codes');
				Add(format('where movie=%d and script=''kinopoisk''', [code]));
			end;
			RecordCount := ExecuteSQL(sl.text);
			if RecordCount > 0 then begin
				kinopoisk_rating_number := GetFloatField('rating');
				if (kinopoisk_rating_number > 0) then
					kinopoisk_rating := Format('<NOBR><img align="absmiddle" src="%skinopoisk-ico.png"> ', [PicturesPath]) + Format('%.1f', [kinopoisk_rating_number]) + '</NOBR>; ';
			end;
		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="%sperson.gif" align="absmiddle">', [PicturesPath])
  				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);
	      //directors:=format('<img src="%sperson.gif" align="absmiddle">%s', [ImagesPath, copy(directors, 1, length(directors)-2)]);

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


			if GroupOperation then begin

			  if ThisChecked then
					CheckMode:=format('<a href="group_check_off:%d"><img src="%scheck.gif" align="absmiddle" border="0"></a>',  [code, PicturesPath])
				else
					CheckMode:=format('<a href="group_check_on:%d"><img src="%scheckout.gif" align="absmiddle" border="0"></a>',  [code, PicturesPath]);
			end
			else
				CheckMode:='';
      
      if year<>'' then
        year:=format(' (%s)', [year]);

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

      s:=format('<a href="movie:%d"><img src="%s" width=50 border=0 align=left></a>', [code, FileName])+CheckMode+format('<a href="movie:%d"><b>%s</b></a><small>%s%s<br>', [code, title, year, rate]);
	  
		//site ratings
		if (imdb_rating <> '') or (kinopoisk_rating <> '') or (current_rating <> '') then begin
			s := s + current_rating + imdb_rating + kinopoisk_rating;
			s := copy(s, 1, length(s)-2) + '<br>';
		end;
	  
      if genres<>'' then begin
       	if mode<>1 then
       		//Genres
          genres:=GetLangStr(179)+': '+genres;
        s:=s+genres+'<br>';
			end;
      if (mode<>1) and (directors<>'') then
      	//Directors
        s:=s+GetLangStr(181)+': '+directors+'<br>';

      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
		seasons:=GetLangStr(517)+' '+seasons;
		delete(seasons, length(seasons), 1);
		s:=s+seasons;
	end;
	
	s := s + '<small>' + GenerateHTMLForDescriptionSearching(AFullDescription, ASearchDescription) + '</small>';

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

	if amount=3 then
		s:='<div class="preview">'+s+'</div>'; 
  result:=s;
  sl.Free;
end;

function GetThumbMovie3(code, mode: integer; title: string): string;
var
  sl: TStringList;
  s, rate, FileName, genres, directors, actors, img, seasons: string;
  fl: boolean;
  i, j, kol, dir, amount, RecordCount: integer;
	ThumbsPath, FileIn, FileOut, CheckMode: 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);

    if RecordCount>0 then
    begin
      FileIn:=ImagesPath+GetStringField('path');
      FileName:=ThumbsPath+GetStringField('path');
      if MyFileExists(FileName) then
        fl:=true
      else if MyFileExists(FileIn) then
        ConvertThumb2(FileIn, FileName, 100, 150, True);
      if MyFileExists(FileName) then
        fl:=true;
    end;
    if not fl then
    begin
      FileIn:=PicturesPath+'no-cover.gif';
      FileName:=ThumbsPath+'no-cover.jpg';
      if ((not MyFileExists(FileName)) and
          (MyFileExists(FileIn))) then
      begin
        ConvertThumb2(FileIn, FileName, 100, 150, True);
      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="%scheck.gif" align="absmiddle" border="0"></a>',  [code, PicturesPath])
      else
        CheckMode:=format('<a href="group_check_on:%d"><img src="%scheckout.gif" align="absmiddle" border="0"></a>',  [code, PicturesPath]);
    end
    else
      CheckMode:='';

    s:=format('<a href="movie:%d"><img src="%s" width=100 border=0 align=left></a>', [code, FileName])+CheckMode+format('<a href="movie:%d"><b>%s</b></a><br>', [code, title]);


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

	if amount=3 then
		s:='<div class="preview">'+s+'</div>';
  result:=s;
  sl.Free;
end;


//показ уменьшенной карточки человека
function GetThumbPerson(code, mode: integer): string;
var
  sl: TStringList;
  s, rate, FileName, img, PersonName, BirthDate: string;
  fl: boolean;
  j, kol, amount, RecordCount, DirectorCount, WriterCount, ActorCount, years: integer;
	InCollection, rating: integer;
	ThumbsPath, FileIn, FileOut: string;
begin
	result:='';
  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');
			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="%sstar.gif" width=16 height=16 border=0 align="absmiddle">%d', [PicturesPath, rating])
		else
			rate:='';

    BirthDate:=GetStringField('p.birth_date');
    if BirthDate<>'' then
    	BirthDate:=Date2Str(BirthDate);

    //получаем дополнительную информацию о фильме, если он из коллекции
    if InCollection=1 then begin
      //обложка
      if GetStringField('i.path')<>'' then begin
//      	FileName:=ImagesPath+GetStringField('i.path');
     
	      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:=PicturesPath+'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);
      //RecordCount:=ExecuteSQL(sl.text);
			//ActorCount:=GetIntegerField('amount');

      s:=format('<a href="person:%d"><img src="%s" width=50 border=0 align=left><b>%s</b></a><small>%s<br>', [code, FileName, PersonName, rate]);
      if BirthDate<>'' then begin
      	//ShowMessage(BirthDate);
      	s:=s+BirthDate;
      	try
      		//years:=StrToDate(BirthDate);
      	except
      	end;
      	s:=s+'<br>';
			end;
			s:=s+format('%s: %d', [GetLangStr(301), ActorCount]);

      s:=s+'</small>';
    end
    else begin
      s:=format('<a href="person:%d"><b>%s</b></a>', [code, PersonName]);
    end;

  s:='<tr><td>'+s+'</td></tr>';
  result:=s;
  sl.Free;
end;

function GetThumbPerson3(code, mode: integer): string;
var
  sl: TStringList;
  s, rate, FileName, img, PersonName, BirthDate: string;
  fl: boolean;
  j, kol, amount, RecordCount, DirectorCount, WriterCount, ActorCount, years: integer;
	InCollection, rating: integer;
	ThumbsPath, FileIn, FileOut: 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');
    //получаем дополнительную информацию о фильме, если он из коллекции
    if InCollection=1 then
    begin
      //обложка
      if GetStringField('i.path')<>'' then
      begin
//      	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, 100, 150, True);
        if MyFileExists(FileName) then
          fl:=true;
      end;
      if not fl then
      begin
        FileIn:=PicturesPath+'no-cover.gif';
        FileName:=ThumbsPath+'no-cover.jpeg';
        if ((not MyFileExists(FileName)) and
            (MyFileExists(FileIn))) then
        begin
          ConvertThumb2(FileIn, FileName, 100, 150, True);
        end;
        fl:=true;
      end;

      s:=format('<a href="person:%d"><img src="%s" width=100 border=0 align=left><b>%s</b></a><br>', [code, FileName, PersonName]);
    end
    else
      s:=format('<a href="person:%d"><b>%s</b></a>', [code, PersonName]);
  s:='<tr><td>'+s+'</td></tr>';
  result:=s;
  sl.Free;
end;


//показ домашней страницы
function GetHome: string;
begin
	result:=GetHomeModern(1, 1);
	//result:=GetHomeOld;
end;

//показ домашней страницы
function GetHomeOld: string;
var
  sl: TStringList;
  PersonCode, i, kol, kol2: integer;
  all, description, genre, year, director, writer, actor, bio, character, movie, LastAdd: string;
  FileName, site, url, postfix, s, rate: string;
  RecordCount, rating: integer;
begin
	result:='';

	SetToolbarVisibility(true);
	SetMenuVisibility(true);
	SetNavigationVisibility(true);

  sl:=TStringList.Create;

  all:=format('<h1><img src="%sicon.gif" align=right>%s</h1>', [PicturesPath, 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');

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

    //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');

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

    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>';
    all:=all+GetLangStr(94)+'<ul>';
    all:=all+'<li><a href="add_movie">'+GetLangStr(95)+'</a>';
    all:=all+'<li><a href="add_person">'+GetLangStr(96)+'</a>';
    all:=all+'<li><a href="search">'+GetLangStr(97)+'</a>';
    all:=all+'</ul>';

    all:=all+'</td><td valign=top>';
    all:=all+GetFeaturedDVD;
    all:=all+'</td></tr></table>';

    all:=all+'<hr><table width=100% border=0><tr>';


		with sl do begin
			Clear;
      Add('select code, title, year, rating from movies');
      Add('where date_add is not null');
			Add('and in_collection=1');
      Add('order by date_add desc');
		end;
		RecordCount:=ExecuteSQL(sl.text);

    if RecordCount>0 then begin
      if RecordCount>10 then
        kol:=10
      else
        kol:=RecordCount;
      LastAdd:='<td width=50% valign=top>'+GetLangStr(98)+'<br>';
      for i:=1 to kol do begin
        year:=GetStringField('year');
        rating:=GetIntegerField('rating');
  			if rating>0 then
  				rate:=format(' <img src="%sstar.gif" width=16 height=16 border=0 align="absmiddle">%d', [PicturesPath, rating])
  			else
  				rate:='';

        if year='' then
          LastAdd:=LastAdd+format('<a href="movie:%d">%s</a>%s<br>', [GetIntegerField('code'), GetStringField('title'), rate])
        else
          LastAdd:=LastAdd+format('<a href="movie:%d">%s</a> (%s)%s<br>', [GetIntegerField('code'), GetStringField('title'), year, rate]);
        GetNextRecord;
      end;
      LastAdd:=LastAdd+'</td>';
      all:=all+LastAdd;
    end;

		with sl do begin
			Clear;
      Add('select code, title, year, rating from movies');
      Add('where date_update is not null');
			Add('and in_collection=1');
      Add('order by date_update desc');
		end;
		RecordCount:=ExecuteSQL(sl.text);

    if RecordCount>0 then begin
      if RecordCount>10 then
        kol:=10
      else
        kol:=RecordCount;
      LastAdd:='<td width=50% valign=top>'+GetLangStr(99)+'<br>';
      for i:=1 to kol do begin
        year:=GetStringField('year');
        rating:=GetIntegerField('rating');
  			if rating>0 then
  				rate:=format(' <img src="%sstar.gif" width=16 height=16 border=0 align="absmiddle">%d', [PicturesPath, rating])
  			else
  				rate:='';
        if year='' then
          LastAdd:=LastAdd+format('<a href="movie:%d">%s</a>%s<br>', [GetIntegerField('code'), GetStringField('title'), rate])
        else
          LastAdd:=LastAdd+format('<a href="movie:%d">%s</a> (%s)%s<br>', [GetIntegerField('code'), GetStringField('title'), year, rate]);
        GetNextRecord;
      end;
      LastAdd:=LastAdd+'</td></tr></table>';
      all:=all+LastAdd;
    end;

    all:=all+'<br><table width=100% border=0><tr>';
		with sl do begin
			Clear;
      Add('select code, name, rating from persons');
      Add('where date_add is not null');
			Add('and in_collection=1');
      Add('order by date_add desc');
		end;
		RecordCount:=ExecuteSQL(sl.text);

    if RecordCount>0 then begin
      if RecordCount>5 then
        kol:=5
      else
        kol:=RecordCount;

      LastAdd:='<td width=50% valign=top>'+GetLangStr(100)+'<br>';
      for i:=1 to kol do begin
        rating:=GetIntegerField('rating');
  			if rating>0 then
  				rate:=format(' <img src="%sstar.gif" width=16 height=16 border=0 align="absmiddle">%d', [PicturesPath, rating])
  			else
  				rate:='';
        LastAdd:=LastAdd+format('<a href="person:%d">%s</a>%s<br>', [GetIntegerField('code'), GetStringField('name'), rate]);
        GetNextRecord;
      end;
      LastAdd:=LastAdd+'</td>';
      all:=all+LastAdd;
    end;

		with sl do begin
			Clear;
      Add('select code, name, rating from persons');
      Add('where date_update is not null');
			Add('and in_collection=1');
      Add('order by date_update desc');
		end;
		RecordCount:=ExecuteSQL(sl.text);

    if RecordCount>0 then begin
      if RecordCount>5 then
        kol:=5
      else
        kol:=RecordCount;
      LastAdd:='<td width=50% valign=top>'+GetLangStr(101)+'<br>';
      for i:=1 to kol do begin
        rating:=GetIntegerField('rating');
  			if rating>0 then
  				rate:=format(' <img src="%sstar.gif" width=16 height=16 border=0 align="absmiddle">%d', [PicturesPath, rating])
  			else
  				rate:='';
        LastAdd:=LastAdd+format('<a href="person:%d">%s</a>%s<br>', [GetIntegerField('code'), GetStringField('name'), rate]);
        GetNextRecord;
      end;
      LastAdd:=LastAdd+'</td></tr></table>';
      all:=all+LastAdd;
    end;
		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;
    
    RecordCount:=ExecuteSQL(sl.text);
    if RecordCount>0 then
			all:=all+'<br>'+GetLangStr(438)+'<br>';

    for i:=1 to RecordCount do begin
    	site:=GetStringField('site');
    	url:=GetStringField('url');
    	postfix:=GetStringField('postfix');
    	if postfix<>'' then
    		url:=url+postfix;
			s:=GetStringField('picture');
			if s='' then
				all:=all+'<a href="link:http://www.movienizer.com/redirect/'+url+format('">%s</a> ', [site])
			else
				all:=all+'<a href="link:http://www.movienizer.com/redirect/'+url+'">'+format('<img src="%s%s" border="0" align="absmiddle"></a> ', [PicturesPath, s]);
			GetNextRecord;
    end;

		//if ProgramLanguage='Russian' then
			//all:=all+'<br>'+format(GetLangStr(570), ['http://www.movienizer.com/ru/donate.htm']);
  result:=all;
  sl.Free;
end;

//показ домашней страницы
function GetHomeModern(HomeMovieMode, HomePersonMode: integer): string;
var
  sl: TStringList;
  PersonCode, i, j, kol, kol2, code: integer;
  all, description, genre, year, director, writer, actor, bio, character, movie, LastAdd: string;
  FileName, site, url, postfix, s, rate, ThumbsPath: string;
  RecordCount, rating, LastCode: integer;
begin
	result:='';

	ThumbsPath:=ImagesPath+'Thumbs\';

	SetToolbarVisibility(true);
	SetMenuVisibility(true);
	SetNavigationVisibility(true);

  sl:=TStringList.Create;

  all:=format('<h1><img src="%sicon.gif" align=right>%s</h1>', [PicturesPath, 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');

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

    //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');

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

    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>';
    all:=all+GetLangStr(94)+'<ul>';
    all:=all+'<li><a href="add_movie">'+GetLangStr(95)+'</a>';
    all:=all+'<li><a href="add_person">'+GetLangStr(96)+'</a>';
    all:=all+'<li><a href="search">'+GetLangStr(97)+'</a>';
    all:=all+'</ul>';

    all:=all+'</td><td valign=top>';

    //popular genres
		with sl do begin
			Clear;
			Add('select mn.code as code, mn.name as name, count(mn.code) as amount');
			Add('from movies m');
			Add('inner join movies_manuals mm on mm.movie=m.code');
			Add('inner join manuals mn on mn.code=mm.ref_code');
			Add('where m.in_collection=1');
			Add('and mm.reference=8');
			Add('group by mn.code, mn.name');
			Add('order by 3 desc');
		end;
		kol:=ExecuteSQL(sl.text);
		kol2:=RegReadInteger('PopularGenresAmount', 5);
		if kol>kol2 then
			kol:=kol2;
		if kol>0 then
			s:=GetLangStr(1300)
		else
			s:='';
		s:=s+'<ul>';
		for i:=1 to kol do begin
			s:=s+format('<li><a href="genre:%d">%s (%d)</a>', [GetIntegerField('code'), GetStringField('name'), GetIntegerField('amount')]);
			GetNextRecord;
		end;
		if kol>0 then
			s:=s+'</ul>';
		all:=all+s;

    //all:=all+GetFeaturedDVD;
    all:=all+'</td></tr></table>';

    all:=all+'<hr>';

		with sl do begin
			Clear;
      Add('select m.code as code, m.title as title, m.year as year, m.rating as rating, i.path as path');
      Add('from movies m');
      Add('left outer join images i on i.movie=m.code');
      case HomeMovieMode of
      	2: Add('where 1=1');
      	3: Add('where seen<>1');
			else
				Add('where m.date_add is not null');
      end;
      Add('and m.in_collection=1');
      Add('and i.mode=1');
      case HomeMovieMode of
      	2: Add('order by m.rating desc, m.date_add desc, m.code, i.sort_order');
			else
	      Add('order by m.date_add desc, m.code, i.sort_order');
      end;

		end;
		RecordCount:=ExecuteSQL(sl.text);

    if RecordCount>0 then begin
    	kol:=(GetHTMLWidth div 90);
      //LastAdd:=''+GetLangStr(98)+'<br>';

      LastAdd:='<td width=50% valign=top>';
      if HomeMovieMode=1 then
      	LastAdd:=LastAdd+'<b>'+GetLangStr(1402)+'</b> | '
			else
      	LastAdd:=LastAdd+format('<a href="modern_home:1,%d">', [HomePersonMode])+GetLangStr(1402)+'</a> | ';
      if HomeMovieMode=2 then
      	LastAdd:=LastAdd+'<b>'+GetLangStr(1403)+'</b> | '
			else
      	LastAdd:=LastAdd+format('<a href="modern_home:2,%d">', [HomePersonMode])+GetLangStr(1403)+'</a> | ';
      if HomeMovieMode=3 then
      	LastAdd:=LastAdd+'<b>'+GetLangStr(1404)+'</b> '
			else
      	LastAdd:=LastAdd+format('<a href="modern_home:3,%d">', [HomePersonMode])+GetLangStr(1404)+'</a> ';
      LastAdd:=LastAdd+'<br>';

      i:=1;
      j:=0;
			LastCode:=0;
      while (j<kol) and (i<=RecordCount) do begin
      	code:=GetIntegerField('code');
      	if LastCode<>code then begin
      		if j=0 then
      			LastAdd:=LastAdd+'<table><tr>';
	        j:=j+1;
	        LastCode:=code;
	
          year:=GetStringField('year');

          rating:=GetIntegerField('rating');
    			if rating>0 then
    				rate:=GetLangStr(298)+': '+IntToStr(rating)
    			else
    				rate:='';
					
					if year<>'' then
						s:=GetLangStr(169)+': '+year+'. '+rate
					else
						s:=rate;

					FileName:=ImagesPath+GetStringField('path');
					if not MyFileExists(FileName) then
						FileName:=ImagesPath+'no-cover.gif';

					LastAdd:=LastAdd+format('<td width="80" valign="top"><a href="movie:%d"><img src="%s" width="80" border="0" alt="%s"><br>%s</a>&nbsp;</td>',
						[code, FileName, s, GetStringField('title')]);

      	end;

        GetNextRecord;
        i:=i+1;
      end;
      if j>0 then
      	LastAdd:=LastAdd+'</tr></table>';

      all:=all+LastAdd;
    end;

    //last added persons
		with sl do begin
			Clear;

      Add('select p.code as code, p.name as name, p.rating as rating, i.path as path');
      Add('from persons p');
      Add('left outer join images i on i.person=p.code');
      case HomePersonMode of
      	2: Add('where 1=1');
			else
				Add('where p.date_add is not null');
      end;
      Add('and p.in_collection=1');
      Add('and i.mode=6');
      case HomePersonMode of
      	2: Add('order by p.rating desc, p.date_add desc, p.code, i.sort_order');
			else
				Add('order by p.date_add desc, p.code, i.sort_order');
      end;
		end;
		RecordCount:=ExecuteSQL(sl.text);

    if RecordCount>0 then begin
    	kol:=(GetHTMLWidth div 90);
      //LastAdd:=''+GetLangStr(100)+'<br>';

      LastAdd:='<td width=50% valign=top>';
      if HomePersonMode=1 then
      	LastAdd:=LastAdd+'<b>'+GetLangStr(1405)+'</b> | '
			else
      	LastAdd:=LastAdd+format('<a href="modern_home:%d,1">', [HomeMovieMode])+GetLangStr(1405)+'</a> | ';
      if HomePersonMode=2 then
      	LastAdd:=LastAdd+'<b>'+GetLangStr(1406)+'</b>'
			else
      	LastAdd:=LastAdd+format('<a href="modern_home:%d,2">', [HomeMovieMode])+GetLangStr(1406)+'</a>';
      LastAdd:=LastAdd+'<br>';

      i:=1;
      j:=0;
			LastCode:=0;
      while (j<kol) and (i<=RecordCount) do begin
      	code:=GetIntegerField('code');
      	if LastCode<>code then begin
      		if j=0 then
      			LastAdd:=LastAdd+'<table><tr>';
	        j:=j+1;
	        LastCode:=code;

          rating:=GetIntegerField('rating');
    			if rating>0 then
    				rate:=GetLangStr(298)+': '+IntToStr(rating)
    			else
    				rate:='';
					
					s:=rate;

					FileName:=ImagesPath+GetStringField('path');
					if not MyFileExists(FileName) then
						FileName:=ImagesPath+'no-cover.gif';

					LastAdd:=LastAdd+format('<td width="80" valign="top"><a href="person:%d"><img src="%s" width="80" border="0" alt="%s"><br>%s</a>&nbsp;</td>',
						[code, FileName, s, GetStringField('name')]);

      	end;

        GetNextRecord;
        i:=i+1;
      end;
      if j>0 then
      	LastAdd:=LastAdd+'</tr></table>';

      all:=all+LastAdd;
    end;

		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;
    
    RecordCount:=ExecuteSQL(sl.text);
    if RecordCount>0 then
			all:=all+'<br>'+GetLangStr(438)+'<br>';

    for i:=1 to RecordCount do begin
    	site:=GetStringField('site');
    	url:=GetStringField('url');
    	postfix:=GetStringField('postfix');
    	if postfix<>'' then
    		url:=url+postfix;
			s:=GetStringField('picture');
			if s='' then
				all:=all+'<a href="link:http://www.movienizer.com/redirect/'+url+format('">%s</a> ', [site])
			else
				all:=all+'<a href="link:http://www.movienizer.com/redirect/'+url+'">'+format('<img src="%s%s" border="0" align="absmiddle"></a> ', [PicturesPath, s]);
			GetNextRecord;
    end;

		//if ProgramLanguage='Russian' then
			//all:=all+'<br>'+format(GetLangStr(570), ['http://www.movienizer.com/ru/donate.htm']);

	all:=all+'<br><small>'+format('<a href="userref:100,Options">%s</a>', [GetLangStr(1333)])+'</small>';

  result:=all;
  sl.Free;
end;


//показ карточки человека
function GetPerson(PersonCode: integer; FullBio, FullDesc: boolean): string;
var
  sl: TStringList;
  s, all, OriginalName, collection, bio, description, BirthDateStr, DeathDateStr, birthplace: string;
  rate, FileName, movie, year, character, img, title, comment, down: string;
  rating, flCollection, i, j: integer;
  RecordCount, height, LastCode, SpouseStatus, ChildrenCount, SpousePersonCode: integer;
	site, url, postfix, links, SpousePerson, SpouseStatusStr: string;
begin
	result:='';
  sl:=TStringList.Create;

  all:='';
      
      //links
      links:='';
      with sl do begin
        Clear;
        Add('select * from persons_codes pc');
        Add('left outer join partners p on pc.script=p.site');
        Add(format('where person=%d', [PersonCode]));
        Add('order by p.sort_order');
      end;
      RecordCount:=ExecuteSQL(sl.text);
      for i:=1 to RecordCount do begin
      	site:=GetStringField('pc.script');
      	url:=GetStringField('pc.site');
      	if url<>'' then begin
	      	postfix:=GetStringField('p.postfix');
  	    	if postfix<>'' then
    	  		url:=url+postfix;
					s:=GetStringField('p.picture');
					if s='' then
						links:=links+'<a href="link:http://www.movienizer.com/redirect/'+url+format('">%s</a> ', [site])
					else
						links:=links+'<a href="link:http://www.movienizer.com/redirect/'+url+'">'+format('<img src="%s%s" border="0" align="absmiddle"></a> ', [PicturesPath, s]);
				end;
				GetNextRecord;
      end;

      with sl do begin
        Clear;
        Add('select * from persons');
        Add(format('where code=%d', [PersonCode]));
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount=0 then
        exit;
      s:='person:'+IntToStr(PersonCode);
      person:=GetStringField('name');
      OriginalName:=GetStringField('original_name');
      rating:=GetIntegerField('rating');

			flCollection:=GetIntegerField('in_collection');

      bio:=GetStringField('biography');
      description:=GetStringField('description');
      comment:=GetStringField('comment');
      BirthDateStr:=GetStringField('birth_date');
      birthplace:=GetStringField('birthplace');
      DeathDateStr:=GetStringField('death_date');
      height:=GetIntegerField('height');

      if flCollection=0 then
	      collection:=format('<a href="collection_person:%d"><img src="%snot_collection.gif" border="0" alt="Person is not in collection"></a>',  [PersonCode, PicturesPath])
      else
	      collection:=format('<a href="collection_person:%d"><img src="%scollection.gif" border="0" alt="Person in collection"></a>',  [PersonCode, PicturesPath]);

			rate:='';
			for i:=1 to 10 do begin
				if i mod 2=1 then begin
					if i<=rating then
		      	rate:=rate+format('<a href="rating_person:%d"><img src="%sstar-left.gif" align="absmiddle" border="0"></a>',  [i, PicturesPath])
					else
	    	  	rate:=rate+format('<a href="rating_person:%d"><img src="%snot-star-left.gif" align="absmiddle" border="0"></a>',  [i, PicturesPath])
				end
				else begin
					if i<=rating then
		      	rate:=rate+format('<a href="rating_person:%d"><img src="%sstar-right.gif" align="absmiddle" border="0"></a>',  [i, PicturesPath])
					else
	    	  	rate:=rate+format('<a href="rating_person:%d"><img src="%snot-star-right.gif" align="absmiddle" border="0"></a>',  [i, PicturesPath])
				end;
			end;

			if (description='') and (BirthDateStr='') then
				down:='<br>'+format('<a href="DownloadPerson:%d">', [PersonCode])+GetLangStr(324)+'</a>'
			else
				down:='';
			
			//ShowMessage(rate);

	    all:='<table border="0" width="100%">'+format('<tr><td width=100%%><h1>%s</h1></td><td align="right">%s</td><td width="120" align="right">%s<a href="UpdatePerson:%d"><small><br>%s%s</small></a></td></tr></table><table border="0" cellpadding="0" cellspacing="0" width="100%%"><tr><td width="100%%">', [person, links, collection, PersonCode, GetLangStr(304), down]);
      //поиск картинок
      with sl do begin
        Clear;
        Add('select * from images');
        Add(format('where person=%d and mode=6', [PersonCode]));
		Add('order by sort_order');
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
        FileName:=ImagesPath+GetStringField('path');
        if (MyFileExists(FileName)) then
          all:=all+format('<a href="ShowImage:%d"><img src="%s" height="120" align="left" border="0"></a>',
            [GetIntegerField('code'), FileName]);
      end
      else
        ;//all:=format('%s<a href="UpdateFrontCover:%d">Add front cover</a><br>', [all, MovieCode]);
			if (OriginalName<>'') and (person<>OriginalName) then
				//Original name
        all:=all+GetLangStr(300)+format(': <b>%s</b><br> ', [OriginalName]);

      if BirthDateStr<>'' then
      	//Date of birth
				all:=all+GetLangStr(294)+': '+GetSmartDate(BirthDateStr)+'<br>';
      
      if birthplace<>'' then
      	//Location of birth
        all:=all+GetLangStr(287)+format(': %s<br>', [birthplace]);

      if DeathDateStr<>'' then
      	//Date of death
				all:=all+GetLangStr(295)+': '+GetSmartDate(DeathDateStr)+'<br>';

			//My rating
			all:=all+GetLangStr(298)+': '+rate+'<br>';

			//height
			if height>0 then begin
				if height<100 then
					s:='0.'+IntToStr(height)
				else begin
					s:=IntToStr(height);
					s:=copy(s, 1, 1)+DecimalSeparator+copy(s, 2, length(s)-1);
				end;
				all:=all+format(GetLangStr(371), [s])+'<br>';
			end;
			

			//biography
      if bio<>'' then begin
				if not FullBio and (length(bio)>700) then begin
					bio:=copy(bio, 1, 700);
					bio:=bio+format(GetLangStr(369), [PersonCode]);
				end;
        all:=all+bio+'<br>';
			end;
			
    // Fade. 04.12.2012 - Person's spouses
     with sl do begin
        Clear;
        Add('SELECT p.Name, p.Code, ps.children_count, ps.spouse_status ');
        Add('FROM persons_spouses_codes psc');
        Add('LEFT JOIN persons_spouses ps ON psc.person_spouses_code = ps.code');
        Add('LEFT JOIN persons p ON psc.person_secondary = p.code');
        Add('WHERE psc.person_primary = ' + IntToStr(PersonCode));
        Add('ORDER BY psc.sort_order');
      end;
      RecordCount:=ExecuteSQL(sl.text);


      if RecordCount>0 then
      begin
        movie:='<br>' + GetLangStr(3897)+':<ol>';
        for i:=1 to RecordCount do
        begin
          SpouseStatus := GetIntegerField('ps.spouse_status');
          ChildrenCount := GetIntegerField('ps.children_count');
          SpousePerson := GetStringField('p.Name');
          SpousePersonCode := GetIntegerField('p.Code');;

    		  movie := movie + '<li>';
          movie:=movie + format('<a href="person:%d">%s</a>', [SpousePersonCode, SpousePerson]);
          SpouseStatusStr := '';
          case SpouseStatus of
            2: SpouseStatusStr := GetLangStr(3899);
            3: SpouseStatusStr := GetLangStr(3905);
            4: SpouseStatusStr := GetLangStr(3913);
            5: SpouseStatusStr := GetLangStr(3914);
            6: SpouseStatusStr := GetLangStr(3926);
          end;
          if (SpouseStatusStr <> '') then
            movie := movie + ' (' + SpouseStatusStr + ')';
          if (ChildrenCount > 0) then
            movie := movie + '. ' + Format(GetLangStr(3898), [IntToStr(ChildrenCount)]);
    		  movie := movie + '</li>';
  	      GetNextRecord;
        end;
        movie:=movie+'</ol>';
        all:=all+movie;
      end;

			//description
      if description<>'' then begin
				if not FullDesc and (length(description)>1000) then begin
					description:=copy(description, 1, 1000);
					description:=description+format(GetLangStr(370), [PersonCode]);
				end;
			end;

      all:=all+'</td></tr></table>';
      if description<>'' then
        all:=all+'<hr height="1"><small>'+description+'</small>';
      
      all:=all+'<hr>';

      if comment<>'' then
	      all:=all+comment+'<hr>';

			//movies by actor
      with sl do begin
        Clear;
        Add('select distinct m.title, m.code, m.year, c.name, m.in_collection from data d');
        Add('inner join movies m on m.code=d.movie');
        Add('left outer join characters c on c.person=d.person and c.movie=d.movie');
        Add(format('where d.person=%d', [PersonCode]));
        Add('and d.mode=3');
        //17.03.09
        //Add('order by d.movies_sort_order');
        Add('order by m.year desc, d.movies_sort_order');
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
				//Actor - filmography
        movie:=GetLangStr(289)+':<ol>';
        for i:=1 to RecordCount do begin
          year:=GetStringField('m.year');
          character:=GetStringField('c.name');
          if GetIntegerField('m.in_collection')=1 then
            img:=format('<img src="%smovie.gif" align="absmiddle">&nbsp;', [PicturesPath])
          else
            img:='';
					title:=GetStringField('m.title');
          if year='' then
            movie:=movie+format('<li>%s<a href="movie:%d">',
              [img, GetIntegerField('m.code')])+title+'</a>'
          else
            movie:=movie+format('<li>%s<a href="movie:%d">%s</a> (%s)',
              [img, GetIntegerField('m.code'), title, year]);
          if character<>'' then
            movie:=movie+format(' ... <i>%s</i>', [character]);

	        GetNextRecord;
        end;
        movie:=movie+'</ol>';
        all:=all+movie;
      end;

			//movies by director
      with sl do begin
        Clear;
        Add('select distinct m.title, m.code, m.year, c.name, m.in_collection from data d');
        Add('inner join movies m on m.code=d.movie');
        Add('left outer join characters c on c.person=d.person and c.movie=d.movie');
        Add(format('where d.person=%d', [PersonCode]));
        Add('and d.mode=1');
        //17.03.09
        //Add('order by d.movies_sort_order');
        Add('order by m.year desc, d.movies_sort_order');
      end;
      RecordCount:=ExecuteSQL(sl.text);

      if RecordCount>0 then begin
      	//Director - filmography
        movie:=GetLangStr(290)+':<ol>';
        for i:=1 to RecordCount do begin
          year:=GetStringField('m.year');
          character:=GetStringField('c.name');
          if GetIntegerField('m.in_collection')=1 then
            img:=format('<img src="%smovie.gif" align="absmiddle">&nbsp;', [PicturesPath])
          else
            img:='';
					title:=GetStringField('m.title');
          if year='' then
            movie:=movie+format('<li>%s<a href="movie:%d">',
              [img, GetIntegerField('m.code')])+title+'</a>'
          else
            movie:=movie+format('<li>%s<a href="movie:%d">%s</a> (%s)',
              [img, GetIntegerField('m.code'), title, year]);
          //if character<>'' then
            //movie:=movie+format(' ... <i>%s</i>', [character]);

	        GetNextRecord;
        end;
        movie:=movie+'</ol>';
        all:=all+movie;
      end;

			//movies by writer
      with sl do begin
        Clear;
        Add('select distinct m.title, m.code, m.year, c.name, m.in_collection from data d');
        Add('inner join movies m on m.code=d.movie');
        Add('left outer join characters c on c.person=d.person and c.movie=d.movie');
        Add(format('where d.person=%d', [PersonCode]));
        Add('and d.mode=2');
        //17.03.09
        //Add('order by d.movies_sort_order');
        Add('order by m.year desc, d.movies_sort_order');
      end;
      RecordCount:=ExecuteSQL(sl.text);

      //movies by writer
      if RecordCount>0 then begin
				//Writer - filmography
        movie:=GetLangStr(291)+':<ol>';
        for i:=1 to RecordCount do begin
          year:=GetStringField('m.year');
          character:=GetStringField('c.name');
          if GetIntegerField('m.in_collection')=1 then
            img:=format('<img src="%smovie.gif" align="absmiddle">&nbsp;', [PicturesPath])
          else
            img:='';
					title:=GetStringField('m.title');
          if year='' then
            movie:=movie+format('<li>%s<a href="movie:%d">',
              [img, GetIntegerField('m.code')])+title+'</a>'
          else
            movie:=movie+format('<li>%s<a href="movie:%d">%s</a> (%s)',
              [img, GetIntegerField('m.code'), title, year]);
          //if character<>'' then
            //movie:=movie+format(' ... <i>%s</i>', [character]);

	        GetNextRecord;
        end;
        movie:=movie+'</ol>';
        all:=all+movie;
      end;

			//movies by composer
      with sl do begin
        Clear;
        Add('select distinct m.title, m.code, m.year, c.name, m.in_collection from data d');
        Add('inner join movies m on m.code=d.movie');
        Add('left outer join characters c on c.person=d.person and c.movie=d.movie');
        Add(format('where d.person=%d', [PersonCode]));
        Add('and d.mode=4');
        //17.03.09
        //Add('order by d.movies_sort_order');
        Add('order by m.year desc, d.movies_sort_order');
      end;
      RecordCount:=ExecuteSQL(sl.text);

      //movies by composer
      if RecordCount>0 then begin
				//composer - filmography
        movie:=GetLangStr(492)+':<ol>';
        for i:=1 to RecordCount do begin
          year:=GetStringField('m.year');
          character:=GetStringField('c.name');
          if GetIntegerField('m.in_collection')=1 then
            img:=format('<img src="%smovie.gif" align="absmiddle">&nbsp;', [PicturesPath])
          else
            img:='';
					title:=GetStringField('m.title');
          if year='' then
            movie:=movie+format('<li>%s<a href="movie:%d">',
              [img, GetIntegerField('m.code')])+title+'</a>'
          else
            movie:=movie+format('<li>%s<a href="movie:%d">%s</a> (%s)',
              [img, GetIntegerField('m.code'), title, year]);
          //if character<>'' then
            //movie:=movie+format(' ... <i>%s</i>', [character]);

	        GetNextRecord;
        end;
        movie:=movie+'</ol>';
        all:=all+movie;
      end;

      //фотки человека
      with sl do begin
        Clear;
        Add('select * from images');
        Add(format('where person=%d order by sort_order', [PersonCode]));
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>1 then begin
      	all:=all+'<hr>';
	      for i:=1 to RecordCount do begin
  	      FileName:=ImagesPath+GetStringField('path');
    	    if (MyFileExists(FileName)) then
      	    all:=all+format('<a href="ShowImage:%d"><img src="%s" height="100" border="0"></a>&nbsp;',
        	    [GetIntegerField('code'), FileName]);
					GetNextRecord;
  	    end;
      end;

      //обложки фильмов
      LastCode:=0;
      with sl do begin
        Clear;
        Add('select distinct i.path as path, i.movie as code');
        Add('from data d');
        Add('inner join images i on i.movie=d.movie');
        Add(format('where d.person=%d', [PersonCode]));
        Add('and i.mode=1');
		Add('order by i.sort_order');
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then
      	all:=all+'<hr>';
      for i:=1 to RecordCount do begin
      	if (LastCode=0) or (GetIntegerField('code')<>LastCode) then begin
      		LastCode:=GetIntegerField('code');
	        FileName:=ImagesPath+GetStringField('path');
  	      if (MyFileExists(FileName)) then
    	      all:=all+format('<a href="movie:%d"><img src="%s" height="120" border="0"></a>&nbsp;',
      	      [GetIntegerField('code'), FileName]);
				end;
				GetNextRecord;
      end;

  sl.Free;
  result:=all;
end;

function GetSearchTop: string;
begin
	result:='<h1>'+GetLangStr(1293)+'</h1>';
end;

function GetSearchBottom: string;
begin
	result:='';
end;

//show script's options window
function ShowScriptOptions: string;
var
	my_value: Integer;
	s: string;
	f: TForm;
	b: TButton;
	edPopularGenres: TEdit;
	labPopularGenres: TLabel;
	cbShowSiteRating: TCheckBox;
	boxShowCurrentRatingInList, boxShowIMDBRatingInList, boxShowKinopoiskRatingInList: TCheckBox;
	RatingInListGroup: TGroupBox;

procedure ButtonClick(Sender: TButton);
var
	zn: integer;
begin
	try
		zn:=StrToInt(edPopularGenres.Text);
		RegWriteInteger('PopularGenresAmount', zn);
		RegWriteString('OriginalVersion', IntToStr(version));
		if cbShowSiteRating.Checked then
			RegWriteInteger('cbShowSiteRating', 1)
		else
			RegWriteInteger('cbShowSiteRating', 0);
		if boxShowCurrentRatingInList.Checked then
			zn := 1
		else
			zn := 0;
		RegWriteInteger('ShowCurrentRatingInList', zn);
		if boxShowIMDBRatingInList.Checked then
			zn := 1
		else
			zn := 0;
		RegWriteInteger('ShowIMDBRatingInList', zn);
		if boxShowKinopoiskRatingInList.Checked then
			zn := 1
		else
			zn := 0;
		RegWriteInteger('ShowKinopoiskRatingInList', zn);

		s:=GetLangStr(1334);
	except
	end;

	f.ModalResult := mrOk;
end;

begin
	s:=GetLangStr(1335);
	f := TForm.Create(nil);
	f.Caption := GetLangStr(1333);
	f.BorderStyle := bsDialog;
	f.Position := poScreenCenter;
	f.Width:=400;
	f.Height:=200;
	f.AutoSize:=false;

	labPopularGenres := TLabel.Create(f);
	labPopularGenres.Name := 'labPopularGenres';
	labPopularGenres.Parent := f;
	labPopularGenres.SetBounds(10, 40, 110, 25);
	labPopularGenres.Caption := GetLangStr(1300);

	edPopularGenres := TEdit.Create(f);
	edPopularGenres.Name := 'edPopularGenres';
	edPopularGenres.Parent := f;
	edPopularGenres.SetBounds(120, 40, 20, 25);
	edPopularGenres.Text := IntToStr(RegReadInteger('PopularGenresAmount', 5));

	cbShowSiteRating := TCheckBox.Create(f);
	cbShowSiteRating.Name := 'cbShowSiteRating';
	cbShowSiteRating.Parent := f;
	cbShowSiteRating.SetBounds(10, 65, 200, 17);
	cbShowSiteRating.Caption := GetLangStr(1533);
	if RegReadInteger('cbShowSiteRating', 1)=1 then
		cbShowSiteRating.Checked:=true
	else
		cbShowSiteRating.Checked:=false;
		
	RatingInListGroup := TGroupBox.Create(f);
	RatingInListGroup.Name := 'RatingInListGroup';
	RatingInListGroup.Parent := f;
	RatingInListGroup.SetBounds(10, 85, 340, 40);
	RatingInListGroup.Caption := GetLangStr(2996);
	
	boxShowCurrentRatingInList := TCheckBox.Create(f);
	boxShowCurrentRatingInList.Name := 'boxShowCurrentRatingInList';
	boxShowCurrentRatingInList.Parent := f;
	boxShowCurrentRatingInList.SetBounds(20, 100, 100, 20);
	boxShowCurrentRatingInList.Caption := GetLangStr(2997);
	my_value := RegReadInteger('ShowCurrentRatingInList', 1);
	if my_value=1 then
		boxShowCurrentRatingInList.Checked := True;
		
	boxShowIMDBRatingInList := TCheckBox.Create(f);
	boxShowIMDBRatingInList.Name := 'boxShowIMDBRatingInList';
	boxShowIMDBRatingInList.Parent := f;
	boxShowIMDBRatingInList.SetBounds(140, 100, 80, 20);
	boxShowIMDBRatingInList.Caption := 'IMDb';
	my_value := RegReadInteger('ShowIMDBRatingInList', 0);
	if my_value=1 then
		boxShowIMDBRatingInList.Checked := True;
		
	boxShowKinopoiskRatingInList := TCheckBox.Create(f);
	boxShowKinopoiskRatingInList.Name := 'boxShowKinopoiskRatingInList';
	boxShowKinopoiskRatingInList.Parent := f;
	boxShowKinopoiskRatingInList.SetBounds(220, 100, 80, 20);
	boxShowKinopoiskRatingInList.Caption := 'КиноПоиск';
	my_value := RegReadInteger('ShowKinopoiskRatingInList', 0);
	if my_value=1 then
		boxShowKinopoiskRatingInList.Checked := True;

	b := TButton.Create(f);
	b.Name := 'btnOK';
	b.Parent := f;
	b.SetBounds(160, 130, 80, 25);
	b.Caption := '&OK';
	b.OnClick := @ButtonClick;

	f.ShowModal;
	f.Free;

	result:=s;	
end;

begin
  MultiShowRowHeight := 180;
  MultiShowImageWidth := 100;
  MiltiShowDistanceBetweenCovers := 12;
  SetHintWindowSize(290, 110);

end.
