program jef_program_getperson;

uses 'jef_program_variables.pas';

//показ карточки человека
function GetPerson(PersonCode: integer; FullBio, FullDesc: boolean): string;
var
  sl: TStringList;
  s, s2, 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: integer;
  site, url, postfix: string;
  ThmPage, PhotoPath, HeightStr, links, jRatedLinks, txtpos, tag, info: string;
  MoviesAsActor, MoviesAsDirector, MoviesAsWriter, MoviesAsComposer: string;
  jSeenIcon, jWantedIcon, DateAdd, DateUpdate, MovieThumbsInfo, PeopleThumbsInfo: string;
  k, jSeen, jWanted, PhotoCode, Age, ActorCount, jActorCountInColl, jListWidth: integer;
  StartDate, EndDate : TDateTime;
  ThumbFileName: string;

begin

	SetIntXFvar(3,'PersonCode',PersonCode);
	SetBoolXFvar(3,'FullBio',FullBio);
	SetBoolXFvar(3,'FullDesc',FullDesc);
	s:=IntToStr(PersonCode);
	while (length(s)<9) do s:='0'+s;
	jHomePageCode:='000300000'+s;

	result:='';
	sl:=TStringList.Create;
	BuildMainNavBar(3,'111101111111');

	all:='';
      
      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');
	  if (length(BirthDateStr)>=10) then BirthDateStr:=GetSmartDate(BirthDateStr);
      birthplace:=GetStringField('birthplace');
      DeathDateStr:=GetStringField('death_date');
	  if (length(DeathDateStr)>=10) then DeathDateStr:=GetSmartDate(DeathDateStr);
      height:=GetIntegerField('height');
	
	  DateAdd:=GetStringField('date_add');    // 20090703-JeF
	  DateUpdate:=GetStringField('date_update');    // 20090703-JeF

      rate:='';  // 20090717-JeF
	  if flCollection=0 then
	      collection:=format('<a href="collection_person:%d"><img src="%s" border="0" alt="%s"></a>',  [PersonCode, GetRscPath('not_collection.gif'),GetLangStr(96)])
      else
	      collection:=format('<a href="collection_person:%d"><img src="%s" border="0" alt="%s"></a>',  [PersonCode, GetRscPath('collection.gif'),GetLangStr(1192)]);

			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="%s" align="absmiddle" border="0"></a>',  [i, GetRscPath('BarUnitLeft-On.gif')])
					else
	    	  	rate:=rate+format('<a href="rating_person:%d"><img src="%s" align="absmiddle" border="0"></a>',  [i, GetRscPath('BarUnitLeft-Off.gif')])
				end
				else begin
					if i<=rating then
		      	rate:=rate+format('<a href="rating_person:%d"><img src="%s" align="absmiddle" border="0"></a>',  [i, GetRscPath('BarUnitRight-On.gif')])
					else
	    	  	rate:=rate+format('<a href="rating_person:%d"><img src="%s" align="absmiddle" border="0"></a>',  [i, GetRscPath('BarUnitRight-Off.gif')])
				end;
			end;

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

	    all:=format('<table border="0" width="100%%"><tr><td><h1>%s</h1></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, collection, PersonCode, GetLangStr(304), down]);
      
	  with sl do begin
        Clear;
        Add('select distinct movie as amount from data');
        Add(format('where person=%d and mode=3', [PersonCode]));
      end;
      ActorCount:=ExecuteSQL(sl.text);
	  // Movies as Actor and in collection
	  sl.Text:='select distinct d.movie as amount from data d inner join movies m on m.code=d.movie '
		  +format('where d.person=%d and d.mode=3 and m.in_collection=1', [PersonCode]);
	  jActorCountInColl:=ExecuteSQL(sl.Text);

	  //поиск картинок
      PhotoPath:='';   // 20090703-JeF
	  PhotoCode:=0;    // 20090717-JeF
	  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 begin
          all:=all+format('<a href="ShowImage:%d"><img src="%s" height="120" align="left" border="0"></a>',
            [GetIntegerField('code'), FileName]);
        end;
        PhotoPath:=FileName;   // 20090703-JeF
		PhotoCode:=GetIntegerField('code');   // 20090703-JeF
	  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)+': '+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)+': '+DeathDateStr+'<br>';

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

			//height
			HeightStr:='';	 // 20090703-JeF
			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;
				HeightStr:=GetLangStr(371);   // 20090703-JeF
				k:=pos('%',HeightStr);   // 20090703-JeF
				HeightStr:=copy(HeightStr,k,length(HeightStr)-k+1);   // 20090703-JeF
				HeightStr:=format(HeightStr, [s]);   // 20090703-JeF
				// 20090728-JeF // all:=all+format(GetLangStr(371), [s])+'<br>';
				all:=all+format(GetLangStr(371), [s]);   // 20090728-JeF
				// 20090728-JeF_Begin
				if GetBoolOption('EnglishMetrics') then begin
					i:=int(height/2.54);  // convert centimeters to inches
					j:=int(i/12);   // number of feet
					k:=i-(j*12);    // inches remainder
					i:=int(((height/2.54)-int(height/2.54))*1000);
					s:='';
					if (i>=750) then s:=#190
					else if (i>=500) then s:=#189
					else if (i>=250) then s:=#188;
					HeightStr:=HeightStr+' ('+IntToStr(j)+ '''';
					if (k>0) or (s>'') then HeightStr:=HeightStr+' ';
					if (k>0) then HeightStr:=HeightStr+IntToStr(k);
					if (s>'') then HeightStr:=HeightStr+s;
					if (k>0) or (s>'') then HeightStr:=HeightStr+ '''''';
					HeightStr:=HeightStr+')';
					all:=all+' ('+IntToStr(j)+ ''' '+IntToStr(k)+s+ ''''')';
				end;
				all:=all+'<br>';
				// 20090728-JeF_End
			end;

	//biography
	if bio<>'' then begin
		if (length(bio)>DescriptionLength) and not FullBio and (length(bio)>700) then begin
			bio:=copy(bio, 1, DescriptionLength)
				+BlockTag('', 'TB0006+', 'tag-s')
				+'...<a href="custom110100061:1"><img src="'+GetRscPath('toggle-right.png')
				+'" border="0" align="absmiddle"></a>&nbsp;'
				+BlockTag('', 'TB0006+', 'tag-e')
				+BlockTag('', 'TB0006-', 'tag-s')
				+copy(bio, DescriptionLength+1, length(bio)-DescriptionLength)
				+'<a href="custom110100060:1"><img src="'+GetRscPath('toggle-left.png')
				+'" border="0" align="absmiddle"></a>&nbsp;'
				+BlockTag('', 'TB0006-', 'tag-e');
			bio:=BlockTag(bio, 'TB0006+', 'show');
		end;
	end;

	//description
    if description<>'' then begin
		if (length(description)>DescriptionLength) and not FullDesc then begin
			description:=copy(description, 1, DescriptionLength)
						+BlockTag('', 'TB0005+', 'tag-s')
						+'...<a href="custom110100051:1"><img src="'+GetRscPath('toggle-right.png')
						+'" border="0" align="absmiddle"></a>&nbsp;'
						+BlockTag('', 'TB0005+', 'tag-e')
						+BlockTag('', 'TB0005-', 'tag-s')
						+copy(description, DescriptionLength+1, length(description)-DescriptionLength)
						+'<a href="custom110100050:1"><img src="'+GetRscPath('toggle-left.png')
						+'" border="0" align="absmiddle"></a>&nbsp;'
						+BlockTag('', 'TB0005-', 'tag-e');
			description:=BlockTag(description, 'TB0005+', 'show');
		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
	  MoviesAsActor:='';  // 20090717-JeF
	  with sl do begin
        Clear;
        Add('select distinct m.title, m.code, m.year, m.seen, m.wanted, 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');
		  jSeen:=GetIntegerField('m.seen');
		  jWanted:=GetIntegerField('m.wanted');
		  if jSeen=0 then
			jSeenIcon:=format('<img src="%s" border="0" align="absmiddle" alt="%s">',  [GetRscPath('not_seen.gif'), GetLangStr(328)])
		  else
			jseenIcon:=format('<img src="%s" border="0" align="absmiddle" alt="%s">',  [GetRscPath('seen.gif'), GetLangStr(327)]);
		  if jWanted=0 then
			jWantedIcon:=format('<img src="%s" border="0" align="absmiddle" alt="%s">',  [GetRscPath('not_wanted.gif'), GetLangStr(330)])
		  else
			jWantedIcon:=format('<img src="%s" border="0" align="absmiddle" alt="%s">',  [GetRscPath('wanted.gif'), GetLangStr(329)]);
          character:=GetStringField('c.name');



            img:='';
          if GetIntegerField('m.in_collection')=1 then  img:=format('<img src="%s" align="absmiddle">&nbsp;', [GetRscPath('movie.gif')]);
		  if jSeen=1 then img:=img+jSeenIcon+'&nbsp;';
		  if jWanted=1 then img:=img+jWantedIcon+'&nbsp;';
		  title:=GetStringField('m.title');
          if year='' then
            movie:=movie+'<li>'+img+format('<a href="movie:%d">',
              [GetIntegerField('m.code')])+title+'</a>'
          else
            movie:=movie+'<li>'+img+format('<a href="movie:%d">%s</a> (%s)',
              [GetIntegerField('m.code'), title, year]);
          if character<>'' then
            movie:=movie+format(' ... <i>%s</i>', [character]);

	        GetNextRecord;
        end;
        movie:=movie+'</ol>';
        i:=pos(':<ol>',movie);   // 20090703-JeF
		MoviesAsActor:=copy(movie,i+1,length(movie)-i);   // 20090703-JeF
		all:=all+movie;
      end;

	  //movies by director
      MoviesAsDirector:='';  // 20090717-JeF
	  with sl do begin
        Clear;
        Add('select distinct m.title, m.code, m.year, m.seen, m.wanted, 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');
		  jSeen:=GetIntegerField('m.seen');
		  jWanted:=GetIntegerField('m.wanted');
		  if jSeen=0 then
			jSeenIcon:=format('<img src="%s" border="0" align="absmiddle" alt="%s">',  [GetRscPath('not_seen.gif'), GetLangStr(328)])
		  else
			jseenIcon:=format('<img src="%s" border="0" align="absmiddle" alt="%s">',  [GetRscPath('seen.gif'), GetLangStr(327)]);
		  if jWanted=0 then
			jWantedIcon:=format('<img src="%s" border="0" align="absmiddle" alt="%s">',  [GetRscPath('not_wanted.gif'), GetLangStr(330)])
		  else
			jWantedIcon:=format('<img src="%s" border="0" align="absmiddle" alt="%s">',  [GetRscPath('wanted.gif'), GetLangStr(329)]);
          character:=GetStringField('c.name');
          if GetIntegerField('m.in_collection')=1 then
            img:=format('<img src="%s" align="absmiddle">&nbsp;', [GetRscPath('movie.gif')])
          else
            img:='';
		  if jSeen=1 then img:=img+jSeenIcon+'&nbsp;';
		  if jWanted=1 then img:=img+jWantedIcon+'&nbsp;';
		  title:=GetStringField('m.title');
          if year='' then
            movie:=movie+'<li>'+img+format('<a href="movie:%d">',
              [GetIntegerField('m.code')])+title+'</a>'
          else
            movie:=movie+'<li>'+img+format('<a href="movie:%d">%s</a> (%s)',
              [GetIntegerField('m.code'), title, year]);
          //if character<>'' then
            //movie:=movie+format(' ... <i>%s</i>', [character]);

	        GetNextRecord;
        end;
        movie:=movie+'</ol>';
        i:=pos(':<ol>',movie);   // 20090703-JeF
		MoviesAsDirector:=copy(movie,i+1,length(movie)-i);   // 20090703-JeF
        all:=all+movie;
      end;

	  //movies by writer
      MoviesAsWriter:='';  // 20090717-JeF
	  with sl do begin
        Clear;
        Add('select distinct m.title, m.code, m.year, m.seen, m.wanted, 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');
		  jSeen:=GetIntegerField('m.seen');
		  jWanted:=GetIntegerField('m.wanted');
		  if jSeen=0 then
			jSeenIcon:=format('<img src="%s" border="0" align="absmiddle" alt="%s">',  [GetRscPath('not_seen.gif'), GetLangStr(328)])
		  else
			jseenIcon:=format('<img src="%s" border="0" align="absmiddle" alt="%s">',  [GetRscPath('seen.gif'), GetLangStr(327)]);
		  if jWanted=0 then
			jWantedIcon:=format('<img src="%s" border="0" align="absmiddle" alt="%s">',  [GetRscPath('not_wanted.gif'), GetLangStr(330)])
		  else
			jWantedIcon:=format('<img src="%s" border="0" align="absmiddle" alt="%s">',  [GetRscPath('wanted.gif'), GetLangStr(329)]);
          character:=GetStringField('c.name');
          if GetIntegerField('m.in_collection')=1 then
            img:=format('<img src="%s" align="absmiddle">&nbsp;', [GetRscPath('movie.gif')])
          else
            img:='';
		  if jSeen=1 then img:=img+jSeenIcon+'&nbsp;';
		  if jWanted=1 then img:=img+jWantedIcon+'&nbsp;';
		  title:=GetStringField('m.title');
          if year='' then
            movie:=movie+'<li>'+img+format('<a href="movie:%d">',
              [GetIntegerField('m.code')])+title+'</a>'
          else
            movie:=movie+'<li>'+img+format('<a href="movie:%d">%s</a> (%s)',
              [GetIntegerField('m.code'), title, year]);
          //if character<>'' then
            //movie:=movie+format(' ... <i>%s</i>', [character]);

	        GetNextRecord;
        end;
        movie:=movie+'</ol>';
        i:=pos(':<ol>',movie);   // 20090703-JeF
		MoviesAsWriter:=copy(movie,i+1,length(movie)-i);   // 20090703-JeF
        all:=all+movie;
      end;

	  //movies by composer
      MoviesAsComposer:='';  // 20090717-JeF
	  with sl do begin
        Clear;
        Add('select distinct m.title, m.code, m.year, m.seen, m.wanted, 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');
		  jSeen:=GetIntegerField('m.seen');
		  jWanted:=GetIntegerField('m.wanted');
		  if jSeen=0 then
			jSeenIcon:=format('<img src="%s" border="0" align="absmiddle" alt="%s">',  [GetRscPath('not_seen.gif'), GetLangStr(328)])
		  else
			jseenIcon:=format('<img src="%s" border="0" align="absmiddle" alt="%s">',  [GetRscPath('seen.gif'), GetLangStr(327)]);
		  if jWanted=0 then
			jWantedIcon:=format('<img src="%s" border="0" align="absmiddle" alt="%s">',  [GetRscPath('not_wanted.gif'), GetLangStr(330)])
		  else
			jWantedIcon:=format('<img src="%s" border="0" align="absmiddle" alt="%s">',  [GetRscPath('wanted.gif'), GetLangStr(329)]);
          character:=GetStringField('c.name');
          if GetIntegerField('m.in_collection')=1 then
            img:=format('<img src="%s" align="absmiddle">&nbsp;', [GetRscPath('movie.gif')])
          else
            img:='';
		  if jSeen=1 then img:=img+jSeenIcon+'&nbsp;';
		  if jWanted=1 then img:=img+jWantedIcon+'&nbsp;';
		  title:=GetStringField('m.title');
          if year='' then
            movie:=movie+'<li>'+img+format('<a href="movie:%d">',
              [GetIntegerField('m.code')])+title+'</a>'
          else
            movie:=movie+'<li>'+img+format('<a href="movie:%d">%s</a> (%s)',
              [GetIntegerField('m.code'), title, year]);
          //if character<>'' then
            //movie:=movie+format(' ... <i>%s</i>', [character]);

	        GetNextRecord;
        end;
        movie:=movie+'</ol>';
        i:=pos(':<ol>',movie);   // 20090703-JeF
		MoviesAsComposer:=copy(movie,i+1,length(movie)-i);   // 20090703-JeF
        all:=all+movie;
      end;

      //Person Thumbs
      PeopleThumbsInfo:='';    // 20090703-JeF
	  with sl do begin
        Clear;
        Add('select * from images');
        Add(format('where person=%d order by mode, 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');
          ThumbFileName := GetThumb(GetStringField('path'),ThumbsPhotos,'',0,0,false);
    	    if (MyFileExists(FileName)) then
      	      all:=all+format('<a href="ShowImage:%d"><img src="%s" height="100" border="0"></a>&nbsp;',
        	    [GetIntegerField('code'), ThumbFileName]);
          PeopleThumbsInfo:=PeopleThumbsInfo+format('<a href="ShowImage:%d"><img src="%s" height="100" border="0"></a>&nbsp;',
    	    [GetIntegerField('code'), ThumbFileName]);
          GetNextRecord;
        end;
      end;

      //Movies Thumbs
      MovieThumbsInfo:='';    // 20090703-JeF
	  LastCode:=0;
      with sl do begin
        Clear;
        //Add('select distinct i.path as path, i.movie, m.title, m.year, m.code');  // JeF
        Add('select distinct m.title, m.year, m.code');  // JeF
        Add('from data d');
        //Add('inner join images i on i.movie=d.movie');
        Add('inner join movies m on m.code=d.movie');  // JeF
        Add(format('where d.person=%d', [PersonCode]));
        //Add('and i.mode=1');
		Add('and m.in_collection>0');
		Add('order by m.year desc, m.title');
      end;
  	  SetStrXFvar(3,'StartDate',BirthDateStr);
	  jListWidth:=GetHTMLWidth-GetIntThmOption('PersonOffWidth');
	  MovieThumbsInfo:=BuildList(sl.Text,'Movie',5,80,jListWidth,1,9999,0,3);

      //links
      links:='';   // 20090703-JeF
	  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);
	  sl.clear;
      for i:=1 to RecordCount do begin
      	site:=GetStringField('pc.script');
      	url:=GetStringField('pc.site');
      	s2:='';
		if url<>'' then begin
	      	postfix:=GetStringField('p.postfix');
  	    	if postfix<>'' then
    	  		url:=url+postfix;
			s:=GetRscPath(GetStringField('p.picture'));
			if GetStringField('p.picture')='' then
				s2:='<a href="link:'+jRedir+url+format('">%s</a> ', [site])
			else
				s2:='<a href="link:'+jRedir+url+'">'
					+format('<img src="%s" border="0" align="absmiddle"></a> ', [s]);
		end;
		links:=links+s2;
		sl.Add(format('%.4d-%.3d=',[GetIntegerField('pc.rating'),i])+s2);
		if (GetIntegerField('pc.rating')>0) 
			then links:=links+format('(%d)&nbsp;',[GetIntegerField('pc.rating')]);
		GetNextRecord;
      end;
	jRatedLinks:='';
	if (RecordCount>0) then begin
		sl.sort;
		if (StrToIntDef(copy(sl[sl.Count-1],1,4),0)>0) then begin
			s:='<table class="ratedlinks">';
			for i:=sl.Count downto 1 do begin
				j:=StrToIntDef(copy(sl[i-1],1,4),0);
				s2:=copy(sl[i-1],10,length(sl[i-1]));
				if (j>0) then s2:='<tr><td class="ratedlinklogo">'
								+s2+'</td><td class="ratedlinkrate">'+IntToStr(j)+'</td></tr>'
				else if (pos('</table>',s)=0) then s:=s+'</table>';
				s:=s+s2;
			end;
			if (pos('</table>',s)=0) then s:=s+'</table>';
			jRatedLinks:=s;
		end
		else jRatedLinks:=links;
	end;
	
	// 20090703-JeF
	if ThemeActive then begin
	  
		ThmPage:=GetTemplate('person');
	  
		if (ThmPage<>'') then begin

			// Main Photo
			txtpos:='<img SRC="_cover_"';
			i:=pos(txtpos,ThmPage);
			if (i>0) then begin
				s:=format('<a href="ShowImage:%d"><img src="%s"', [PhotoCode, PhotoPath]); 
				ThmPage:=copy(ThmPage,1,i-1) + s + copy(ThmPage,i+length(txtpos),length(ThmPage)-i-length(txtpos));
				i:=MyPos('</td>',ThmPage,i);
				if (i>0) then ThmPage:=copy(ThmPage,1,i-1) + '</a>' + copy(ThmPage,i,length(ThmPage)-i+1);
			end;

			// Other Tags
			i:=pos('<!--#',ThmPage);
			while (i>0) do begin
				j:=MyPos('-->',ThmPage,i);
				tag:='';
				info:='';
				if (j>i) then begin
					tag:=copy(ThmPage,i+5,j-i-5);
					while (copy(tag,1,1)=' ') do tag:=copy(tag,2,length(tag)-1);
					while (copy(tag,length(tag),1)=' ') do tag:=copy(tag,1,length(tag)-1);
					k:=pos(' ',tag);
					if (k>0) then tag:=copy(tag,1,k-1);
					j:=j+3;
				end;
				
				if (tag='rating_lbl') 				then info:=GetLangStr(298)
				else if (tag='rating')  			then info:=rate
				else if (tag='ratevalue')   		then info:=IntToStr(rating)
				else if (tag='ratedlinks') 			then info:=jRatedLinks
				else if (tag='lifetime')    		then begin
					info:=birthplace;
					while (pos(',',info)>0) do delete(info,1,pos(',',info));
					info:=ClearExtraBlankCharacters(info);
					if (info<>'') then info:=GetFlagTag(info);
					if (info<>'') then info:=info+'&nbsp;';
					Age:=GetAge(BirthDateStr,DeathDateStr);
					if (Age>0) then begin
						info:=info+'<strong>'+copy(BirthDateStr,length(BirthDateStr)-3,4);
						if DeathDateStr<>'' then info:=info+'-'+copy(DeathDateStr,length(DeathDateStr)-3,4);
						info:=info+'</strong>'+' ('+IntToStr(Age)+')';
					end;
				end
				else if (tag='height_lbl')  		then info:=copy(GetLangStr(371),1,pos(':',GetLangStr(371))-1)+' '
				else if (tag='height')  			then info:=HeightStr
				else if (tag='actorcount_lbl')  	then info:=GetLangStr(42)+' '
				else if (tag='actorcount')  		then begin
					info:=IntToStr(ActorCount);
					if (jActorCountInColl>0) then info:=info+' ('+IntToStr(jActorCountInColl)+')';
				end
				else if (tag='dateadd_lbl')    		then begin
					info:=GetLangStr(620);
					k:=pos('(',info);
					if (k>0) then info:=copy(info,1,k-1);
					k:=pos(':',info);
					if (k>0) then info:=copy(info,1,k-1);
					if (copy(info,length(info),1)<>' ') then info:=info+' ';
				end
				else if (tag='dateadd')    			then info:=SQLDate2Str(DateAdd)
				else if (tag='dateupd_lbl')    		then begin
					info:=GetLangStr(621);
					k:=pos('(',info);
					if (k>0) then info:=copy(info,1,k-1);
					k:=pos(':',info);
					if (k>0) then info:=copy(info,1,k-1);
					if (copy(info,length(info),1)<>' ') then info:=info+' ';
				end
				else if (tag='dateupd')    			then info:=SQLDate2Str(DateUpdate);
		
				if (tag='bday_lbl')  				then info:=GetLangStr(294)
				else if (tag='bdaytxt')  			then info:=BirthDateStr
				else if (tag='bday') and (BirthDateStr>'')
					then info:='<tr><td class="Name">'+ClearExtraBlankCharacters(GetLangStr(294))+'</td><td>'
							+BirthDateStr+'</td></tr>'
				else if (tag='dday_lbl')  			then info:=''
				else if (tag='dday')    			then begin
					if (length(DeathDateStr)>0) then begin
						info:='<tr><td class="Name">'+ClearExtraBlankCharacters(GetLangStr(295));
						info:=info+'</td><td>'+DeathDateStr+'&nbsp;';
            Age:=GetAge(BirthDateStr,DeathDateStr);
            if (Age>0) 
              then info:=info+format(GetLangStr(839), [inttostr(Age)]);
            info:=info+'</td></tr>';
					end;
				end
				else if (tag='bplace_lbl')			then info:=GetLangStr(287)
				else if (tag='bplacetxt')			then info:=birthplace
				else if (tag='bplace') and (birthplace>'')
					then info:='<tr><td class="Name">'+GetLangStr(287)+'</td><td>'
							+birthplace+'</td></tr>';
				
				if (tag='name')  					then begin
					info:='<a href="custom110100011:1"><img src="'+GetRscPath('IconsBarShow.png')
							+'" border="0" align="absmiddle" width="16"></a>&nbsp;';
					info:=BlockTag(info, 'TB0001+', 'tag');
					if (GetStrOption('NavBarState')='0') 
						then info:=BlockTag(info, 'TB0001+', 'show');
					info:=info+Person;
				end
				else if (tag='smallmainnavbar') 	then begin
					BuildMainNavBar(3,'111000111100');
					info:=jMainNavBar;
				end
				else if (tag='mainnavbar')  		then begin
					BuildMainNavBar(3,'111111111111');
					info:=jMainNavBar;
				end
				else if (tag='site_links')  		then info:=links
				else if (tag='btn_shortcuts') 		then begin
					info:='<span>'
						+collection+'&nbsp;'
						+format('<a href="person:%d"><img src="%s" border="0" alt="%s"></a>', [PersonCode,GetRscPath('Refresh.png'),GetLangStr(598)])
						+'&nbsp;'
						+format('<a href="UpdatePerson:%d"><img src="%s" border="0" alt="%s"></a>', [PersonCode,GetRscPath('editperson.png'),GetLangStr(304)]);
					if (down>'')
						then info:=info+'&nbsp;'+format('<a href="DownloadPerson:%d"><img src="%s" border="0" alt="%s"></a>', [PersonCode,GetRscPath('dlperson.png'),GetLangStr(324)]);
					info:=info+'</span>';
				end
				else if (tag='btn_collection')  	then info:=collection
				else if (tag='btn_edit')
					then info:=format('<a href="UpdatePerson:%d"><img src="%s" border="0" alt="%s"></a>', [PersonCode,GetRscPath('editperson.png'),GetLangStr(304)])
				else if (tag='btn_download') and (down>'')
					then info:=format('<a href="DownloadPerson:%d"><img src="%s" border="0" alt="%s"></a>', [PersonCode,GetRscPath('dlperson.png'),GetLangStr(324)])
				else if (tag='btn_refresh')
					then info:=format('<a href="person:%d"><img src="%s" border="0" alt="%s"></a>', [PersonCode,GetRscPath('Refresh.png'),GetLangStr(598)])
				else if (tag='txt_shortcuts')  			then begin
					info:='<span style="vertical-align:middle;font-size:12px;">';
					info:=info+format('<a href="UpdatePerson:%d">%s%s</a>', [PersonCode, GetLangStr(304), down]);
					info:=info+'</span>';
				end;

				if (tag='altnamefulltxtbr')			then begin
					if (OriginalName<>'') and (person<>OriginalName)
						then info:=GetLangStr(300)+format(': <b>%s</b><br> ', [OriginalName]);
				end
				else if (tag='altname')					then begin
					if (OriginalName<>'') and (person<>OriginalName) then begin
						info:='<tr><td class="Name">'+ClearExtraBlankCharacters(GetLangStr(284));
						info:=info+'</td><td><h2 class="DeepColor">'+OriginalName +'</h2></td></tr>';
					end;
				end;
			
				if (tag='biography') and (bio<>'') 	then info:='<a id="TB0006"></a><div class="Comments">'+bio+'</div>'
				else if (tag='screenshots') 		then begin
					if (MovieThumbsInfo<>'') or (PeopleThumbsInfo<>'') then info:='<div class="SplitLine">&nbsp;</div>';
					info:=info+PeopleThumbsInfo;
					if (MovieThumbsInfo<>'') and (PeopleThumbsInfo<>'') then info:=info+'<div class="SplitLine">&nbsp;</div>';
					info:=info+MovieThumbsInfo;
					if (MovieThumbsInfo<>'') or (PeopleThumbsInfo<>'') then info:=info+'<div class="SplitLine">&nbsp;</div>';
				end
				else if (tag='description') and (description<>'')		then begin
					info:='<a id="TB0005"></a><div class="Comments">'+description+'</div><div class="SplitLine">&nbsp;</div>';
				end
				else if (tag='comments') and (comment<>'')		then begin
					info:='<div class="Comments">'+comment+'</div><div class="SplitLine">&nbsp;</div>';
				end;

				if (tag='filmography')				then begin
					if (MoviesAsActor>'') then begin
						info:=info+'<table cellpadding="0" cellspacing="1" class="MovieInfo">'+
							'<tr><td class="NameC">'+GetLangStr(289)+'</td></tr><tr><td>'+
							MoviesAsActor+'</td></tr></table>'+
							'<div class="SplitLine">&nbsp;</div>';
					end;
					if (MoviesAsDirector>'') then begin
						info:=info+'<table cellpadding="0" cellspacing="1" class="MovieInfo">'+
							'<tr><td class="NameC">'+GetLangStr(290)+'</td></tr><tr><td>'+
							MoviesAsDirector+'</td></tr></table>'+
							'<div class="SplitLine">&nbsp;</div>';
					end;
					if (MoviesAsWriter>'') then begin
						info:=info+'<table cellpadding="0" cellspacing="1" class="MovieInfo">'+
							'<tr><td class="NameC">'+GetLangStr(291)+'</td></tr><tr><td>'+
							MoviesAsWriter+'</td></tr></table>'+
							'<div class="SplitLine">&nbsp;</div>';
					end;
					if (MoviesAsComposer>'') then begin
						info:=info+'<table cellpadding="0" cellspacing="1" class="MovieInfo">'+
							'<tr><td class="NameC">'+GetLangStr(492)+'</td></tr><tr><td>'+
							MoviesAsComposer+'</td></tr></table>'+
							'<div class="SplitLine">&nbsp;</div>';
					end;
				end;

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

				i:=MyPos('<!--#',ThmPage,i+1);
			end;

			all:=ThmPage;
		end;
	end;

	sl.Free;
	result:=all;
	jLastPage:=result;
end;

begin

end.