program modern_program_part3;

uses 'modern_program_variables.pas';

function GetSortIcons(sort_order: String; PersonCode: Integer; PersonType: String): String;
begin
	result := '';
	if (RegReadInteger('SortIcons', 0) = 1) then begin
		if (sort_order <> 'title asc') then
			result := result + format('&nbsp;<a class="picture" href="userref:131,%s;%d">', [PersonType, PersonCode]) + format('<img alt="%s" src="%stitle-sort-ascend.png" align="absmiddle"></a>', [GetLangStr(1477), ImgPath]);
		if (sort_order <> 'title desc') then
			result := result + format('&nbsp;<a class="picture" href="userref:132,%s;%d">', [PersonType, PersonCode]) + format('<img alt="%s" src="%stitle-sort-descend.png" align="absmiddle"></a>', [GetLangStr(1478), ImgPath]);
		if (sort_order <> 'year asc') then
			result := result + format('&nbsp;<a class="picture" href="userref:133,%s;%d">', [PersonType, PersonCode]) + format('<img alt="%s" src="%syear-sort-ascend.png" align="absmiddle"></a>', [GetLangStr(1479), ImgPath]);
		if (sort_order <> 'year desc') then
			result := result + format('&nbsp;<a class="picture" href="userref:134,%s;%d">', [PersonType, PersonCode]) + format('<img alt="%s" src="%syear-sort-descend.png" align="absmiddle"></a>', [GetLangStr(1480), ImgPath]);
	end;
end;

//показ карточки человека
function GetPerson(PersonCode: integer; FullBio, FullDesc: boolean): string;
var
  sl, PreviousCodes: TStringList;
  s, all, OriginalName, collection, bio, description, BirthDateStr, DeathDateStr, birthplace: string;
  rate, FileName, movie, year, character, img, title, comment, down, SpousePerson, SpouseStatusStr: string;
  rating, flCollection, i, j, k, l: integer;
  RecordCount, height, LastCode: integer;
	site, url, postfix: string;
	tmp, age, CurrentDateStr: String;
	kol, PhotoCnt, MaxPhotos, page_width, page_width_copy, PicHeight, PicWidth, SpouseStatus, ChildrenCount, SpousePersonCode: integer;
	ShowSeenIcon, ShowInCollectionIcon, ShowWantedIcon, ShowForSaleIcon, IconsOnTheRight: boolean;
	sort_order, awards, temp, cust, temp_name, new_name, name, s2, s3: string;
begin
	result:='';

  sl:=TStringList.Create;
  PreviousCodes := TStringList.Create;
  
	with sl do begin
		Clear;
		Add('select code from images');
		Add(format('where person=%d and mode=6', [PersonCode]));
	end;
	PhotoCnt:=ExecuteSQL(sl.text);
	
	if (RegReadInteger('ShowInCollectionIcon', 1) = 1) then
		ShowInCollectionIcon := true
	else
		ShowInCollectionIcon := false;
	
	if (RegReadInteger('ShowSeenIcon', 0) = 1) then
		ShowSeenIcon := true
	else
		ShowSeenIcon := false;
		
	if (RegReadInteger('ShowWantedIcon', 0) = 1) then
		ShowWantedIcon := true
	else
		ShowWantedIcon := false;
		
	if (RegReadInteger('ShowForSaleIcon', 0) = 1) then
		ShowForSaleIcon := true
	else
		ShowForSaleIcon := false;
	if (RegReadInteger('IconsOnTheRight', 0) = 1) then
		IconsOnTheRight := true
	else
		IconsOnTheRight := false;

  all:='';
  //CurrentDateStr := FormatDateTime('mm/dd/yyyy', Now());    
	CurrentDateStr := FormatDateTimeEx('mm/dd/yyyy', Now());
      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');
	  awards:=GetStringField('awards');
	  
	  //if no info for the tab, switch to the main
	  if ((PersonActiveTab = 2) and (PhotoCnt = 0)) or
	  ((PersonActiveTab = 3) and (bio = '')) or
	  ((PersonActiveTab = 4) and (description = '')) or 
	  ((PersonActiveTab = 6) and (awards = '')) then begin
		PersonActiveTab := 1;
		result := GetPerson(PersonCode, FullBio, FullDesc);
		exit;
	  end;

      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, ImgPath])
      else
	      collection:=format('<a href="collection_person:%d"><img src="%scollection.gif" border="0" alt="Person in collection"></a>',  [PersonCode, ImgPath]);

			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, ImgPath])
					else
	    	  	rate:=rate+format('<a href="rating_person:%d"><img src="%snot-star-left.gif" align="absmiddle" border="0"></a>',  [i, ImgPath])
				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, ImgPath])
					else
	    	  	rate:=rate+format('<a href="rating_person:%d"><img src="%snot-star-right.gif" align="absmiddle" border="0"></a>',  [i, ImgPath])
				end;
			end;

			if (description='') and (BirthDateStr='') then
				down:='<br>'+format('<a href="DownloadPerson:%d">', [PersonCode])+GetLangStr(324)+'</a>'
			else
				down:='';
			
			//ShowMessage(rate);
	all:=GetPatternTop;
	all:=all + '<p class="title">' + person;
	while (Pos(' ', OriginalName) = 1) do
		delete(OriginalName, 1, 1);
	if (OriginalName <> '') and (OriginalName <> person) then
		all:=all+' <font size="3"><b>(' + OriginalName + ')</b></font>';
	all:=all+Format('</p></td><td class="icons"><table class="icons-container"><tr><td class="icons-border"><img src="%sicons-left.gif"></td><td class="icons-place">%s&nbsp;&nbsp;</td><td class="edit-link"><p><a href="UpdatePerson:%d">%s</a></p></td><td class="icons-border"><img src="%sicons-right.gif"></td></tr></table>', [ImgPath, collection, PersonCode, GetLangStr(304), ImgPath])+ClosePatternTop;
	
	if (PersonActiveTab <> 2) then begin
	
		all:=all+'<tr><td class="left"><table class="left-pane">';
		  //поиск картинок
		  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
			if (MyFileExists(ImagesPath + GetStringField('path'))) then begin
				FileName := GetImageFileName(GetStringField('path'), 'ThumbsMainPhoto', 'nophoto.gif', MainPhotoWidth, 0);
				
				all:=all+format('<tr><td><div class="cover"><a href="ShowImage:%d"><img class="cover-image" src="%s"></a></div></td></tr><tr><td class="free-space"></td></tr>',
							[GetIntegerField('code'), FileName]);
			end;
		  end
		  else begin
			FileName:=ImgPath+'nophoto.gif';
			if (MyFileExists(FileName)) then
				all:=all+format('<tr><td><div class="cover"><img class="cover-image" src="%s"></div></td></tr><tr><td class="free-space"></td></tr>', [FileName]);
		  end;

		  
		  //links
		  with sl do begin
			Clear;
			Add('select distinct pc.code, pc.script, pc.site, p.* 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);
		  if RecordCount>0 then begin
			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
								all:=all+'<tr><td class="site-link"><a href="link:http://www.movienizer.com/redirect/'+url+format('">%s</a></td></tr>', [site])
							else
								all:=all+'<tr><td class="site-link"><a class="picture" href="link:http://www.movienizer.com/redirect/'+url+'">'+format('<img src="%s%s" border="0" align="absmiddle"></a></td></tr>', [PicturesPath, s]);
						end;
						GetNextRecord;
			  end;
			  all:=all+'<tr><td class="free-space"></td></tr>';
			end;
		//My rating
		i := RegReadInteger('ShowRating', 1);
		if i=1 then
			all:=all+'<tr><td class="my-rating-cell-name">'+GetLangStr(298)+'</td></tr><tr><td class="my-rating-cell-value">'+rate+'</td></tr><tr><td class="free-space"></td></tr>';
		all := all + '</table>';
		
		  //фотки человека
		  MaxPhotos := RegReadInteger('PhotosCnt', 3);
		  with sl do begin
				Clear;
				Add('select path, code from images');
				Add(format('where person=%d order by mode desc, sort_order', [PersonCode]));
		  end;
		  RecordCount:=ExecuteSQL(sl.text);
		  if RecordCount>1 then begin
			all:=all+'<table class="shots">';
			if RecordCount <= MaxPhotos then
				MaxPhotos := RecordCount - 1;
			GetNextRecord;
			PhotoCnt := 0;
			for i:=1 to MaxPhotos do begin
			  PhotoCnt := PhotoCnt + 1;
			  FileName:=ImagesPath+GetStringField('path');
				if (MyFileExists(FileName)) then begin
					FileName := GetImageFileName(GetStringField('path'), 'ThumbsPhotos', 'nophoto.gif', 150, 0);
					all:=all+format('<tr><td class="shots-cell"><a class="picture" href="ShowImage:%d"><img class="shot-image" src="%s"></a></td></tr>',
							[GetIntegerField('code'), FileName]);
				end;
				GetNextRecord;
			end;
			if PhotoCnt < RecordCount - 1 then 
				all:=all+format('<tr><td class="shots-cell"><a href="userref:121,%d"><img src="%sshots-more.gif" border="0"></a></td></tr>', [PersonCode, ImgPath]);
			all:=all+'</table>'
		  end;
		//end left pane  
		all := all + '</td>';
	end; //if PersonActiveTab <> 2
	
	//right pane
	all:=all+'<td width="100%"><table class="contents">';
	//tabs
	if RegReadInteger('HideNavigationMenu', 0) = 0 then
		all := all + GetPersonTabs(PersonCode, bio<>'', description<>'', PhotoCnt>1, awards <> '');
	
	//photos
	if PersonActiveTab = 2 then begin
		all := all + GetShots(PersonCode, 6, 'person', 5);
	end;

	  
	tmp := '';
	if (PersonActiveTab = 1) then begin
		if BirthDateStr<>'' then begin
			//Date of birth
			tmp:=tmp+'<tr><td class="field-names"><p class="border">'+GetLangStr(294)+'</p></td><td class="field-values"><p class="border">'+GetSmartDate(BirthDateStr);
			//ShowMessage(GetAge(CurrentDateStr, BirthDateStr));
			if DeathDateStr='' then
				tmp := tmp + Format(GetLangStr(838), [GetAge(CurrentDateStr, BirthDateStr)]);
			tmp := tmp + '</p></td></tr>';
		end;
		  
		if birthplace<>'' then
			//Location of birth
			tmp:=tmp+format('<tr><td class="field-names"><p class="border">%s</p></td><td class="field-values"><p class="border">%s</p></td></tr>', [GetLangStr(287), birthplace]);
			
		age := '';
		if DeathDateStr<>'' then begin
			//Date of death
			tmp:=tmp+format('<tr><td class="field-names"><p class="border">%s</p></td><td class="field-values"><p class="border">%s ', [GetLangStr(295), GetSmartDate(DeathDateStr)]);
			tmp:=tmp+format(GetLangStr(839), [GetAge(DeathDateStr, BirthDateStr)]);
			tmp:=tmp+'</p></td></tr>';
		end;


		//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;
			tmp:=tmp+format('<tr><td class="field-names"><p class="border">%s</p></td><td class="field-values"><p class="border">%s</p></td></tr>', [GetLangStr(372), s]);
		end;
	end;
	
	if tmp<>'' then
		all := all + '<tr><td><table class="names-values"><tr style="background:url('+WhiteBarFile+') repeat-x;"><td colspan="2">&nbsp;&nbsp;</td></tr>' + tmp + '</table></td></tr><tr><td class="free-space"></td></tr>';
		
		
	if (PersonActiveTab = 1) then begin
		//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:='<tr><td><p class="header">'+GetLangStr(3897)+':';
			movie:=movie + '</p><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></td></tr><tr><td class="free-space"></td></tr>';
			all:=all+movie;
		end;
	end;

	//biography
    if (bio<>'') and ((PersonActiveTab = 1) or (PersonActiveTab = 3)) then begin
		if not FullBio and (length(bio)>700) and (PersonActiveTab <> 3) then begin
			bio:=copy(bio, 1, 700);
			bio:=RemoveUnclosedTags(bio);
			bio:=bio+format(GetLangStr(369), [PersonCode]);
		end;
       all:=all+'<tr style="background:url('+WhiteBarFile+') repeat-x;"><td>&nbsp;&nbsp;</td></tr><tr><td><p class="description">'+bio+'</p></td></tr><tr><td class="free-space"></td></tr>';
	end;
	
      //обложки фильмов
	if (RegReadInteger('HidePhotosCovers', 0) = 0) and (PersonActiveTab = 1) then begin
		LastCode:=0;
		with sl do begin
			Clear;
			Add('select distinct i.path as path, i.width, i.height, i.movie as code, m.year, m.in_collection, m.title, c.name');
			Add('from data d');
			Add('inner join images i on i.movie=d.movie');
			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 i.mode=1');
			Add('order by i.sort_order');
		end;
		RecordCount:=ExecuteSQL(sl.text);
		page_width := GetHTMLWidth - LeftColumnWidth - SmallImageBorder;
		page_width_copy := page_width;
		tmp := '';
		PreviousCodes.Clear;
		if RecordCount>0 then begin
			all:=all+Format('<tr><td><p class="header">%s:</p></td></tr><tr><td><table class="boxes">', [GetLangStr(1401)]);
			j := 0;
			all := all + '<tr>';
			sl.Clear;
			for i:=1 to RecordCount do begin
				if (sl.IndexOf(IntToStr(GetIntegerField('code')))<0) then begin
					sl.Add(IntToStr(GetIntegerField('code')));
					PicHeight:=GetIntegerField('i.height');
					if PicHeight > 0 then
						PicWidth := Round(GetIntegerField('i.width') * (SmallCoverHeight / PicHeight)) + SmallImageBorder
					else
						PicWidth := CoverCellWidth;
					if PicWidth < CoverCellWidth then
						PicWidth := CoverCellWidth;
					FileName:=ImagesPath+GetStringField('path');
					if (MyFileExists(FileName)) and (GetStringField('m.in_collection') = '1') then begin
						{
						age := '';
						if BirthDateStr <> '' then begin
							age := BirthDateStr;
							while Pos('/', age) > 0 do
								delete(age, 1, Pos('/', age));
							try
								age := IntToStr(StrToInt(GetStringField('m.year')) - StrToInt(age));
							except
							end;
						end;
						}
            age := '';
            if (YearsBetween(BirthDateStr, GetStringField('m.year')) > 0) then
              age := IntToStr(YearsBetween(BirthDateStr, GetStringField('m.year')));
							
						j := j + 1;

						FileName := GetImageFileName(GetStringField('path'), 'ThumbsPersonMovies', 'no-cover.gif', 0, 120);
						tmp:=tmp+format('<td class="small-cover"><a class="picture" href="movie:%d"><img src="%s"></a><p><a href="movie:%d">%s</a>', [GetIntegerField('code'), FileName, GetIntegerField('code'), GetStringField('m.title')]);

						
						
						if age <>'' then
							tmp := tmp +'<br>'+ format(GetLangStr(1410), [age]);
						if GetStringField('c.name') <> '' then
							tmp := tmp + format(' (%s)', [GetStringField('c.name')]);
						tmp := tmp + ('</p></td>');

						page_width_copy := page_width_copy - PicWidth;
						if page_width_copy >= 0 then begin
							all := all + tmp;
							tmp := '';
						end
						else begin
							all := all + '</tr></table></td></tr><tr><td><table class="boxes"><tr>' + tmp;
							tmp := '';
							page_width_copy := page_width - PicWidth;
						end;
					end;
				end;
				GetNextRecord;
			end;
			all := all + '</tr></table></td></tr><tr><td class="free-space"></td></tr>';
		end;
	end;

	//description
    if (description<>'') and ((PersonActiveTab = 1) or (PersonActiveTab = 4)) then begin
		if not FullDesc and (length(description)>1000) and (PersonActiveTab <> 4) then begin
			description:=copy(description, 1, 1000);
			description:=RemoveUnclosedTags(description);
			description:=description+format(GetLangStr(370), [PersonCode]);
		end;
		all:=all+'<tr style="background:url('+WhiteBarFile+') repeat-x;"><td>&nbsp;&nbsp;</td></tr><tr><td><p class="description">'+description+'</p></td></tr><tr><td class="free-space"></td></tr>';
	end;

	//comments
	if (PersonActiveTab = 1) then begin
		if comment<>'' then
			all:=all+'<tr style="background:url('+WhiteBarFile+') repeat-x;"><td>&nbsp;&nbsp;</td></tr><tr><td><p class="description">'+comment+'</p></td></tr><tr><td class="free-space"></td></tr>';
	end;

	if (PersonActiveTab = 1) or (PersonActiveTab = 5) then begin
	//movies by actor
	  sort_order := RegReadString('ActorFilmSort', 'year desc');
      with sl do begin
        Clear;
        Add('select distinct m.title, m.code, m.year, c.name, m.in_collection, m.seen, m.wanted, m.for_sale 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');
		if (RegReadInteger('ShowOnlyMoviesInCollection', 0) = 1) then
			Add('and m.in_collection=1');
        //17.03.09
        //Add('order by d.movies_sort_order');
        Add(format('order by m.%s, d.movies_sort_order', [sort_order]));
      end;
      RecordCount:=ExecuteSQL(sl.text);
      if RecordCount>0 then begin
				//Actor - filmography
        movie:='<tr><td><p class="header">'+GetLangStr(289)+':';
		movie:=movie + GetSortIcons(sort_order, PersonCode, 'Actor');
		movie:=movie + '</p><ol>';
        for i:=1 to RecordCount do begin
          year:=GetStringField('m.year');
          character:=GetStringField('c.name');
		  img:='';
		  //in collection, seen, wanted, for sale icons
          if (GetIntegerField('m.in_collection')=1) and ShowInCollectionIcon then
            img:=format('<img src="%smovie.gif" align="absmiddle">', [ImgPath]); 
		  if (GetIntegerField('m.seen')=1) and ShowSeenIcon then
			img := img + format('<img src="%sseen.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.wanted')=1) and ShowWantedIcon then
			img := img + format('<img src="%swanted.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.for_sale')=1) and ShowForSaleIcon then
			img := img + format('<img src="%ssale.gif" align="absmiddle">', [ImgPath]);
		  if img<>'' then begin
			if IconsOnTheRight then
				img := '&nbsp;' + img
			else
				img := img + '&nbsp;';
		  end;
		  title:=GetStringField('m.title');
		  movie := movie + '<li>';
		  if not IconsOnTheRight then
			movie := movie + img;
          if year='' then
            movie:=movie+format('<a href="movie:%d">',
              [GetIntegerField('m.code')])+title+'</a>'
          else
            movie:=movie+format('<a href="movie:%d">%s</a> (%s)',
              [GetIntegerField('m.code'), title, year]);
          if character<>'' then
            movie:=movie+format(' ... <i>%s</i>', [character]);
		  if IconsOnTheRight then
			movie := movie + img;
		  movie := movie + '</li>';

	      GetNextRecord;
        end;
        movie:=movie+'</ol></td></tr><tr><td class="free-space"></td></tr>';
        all:=all+movie;
      end;

	  //movies by director
	  sort_order := RegReadString('DirectorFilmSort', 'year desc');
      with sl do begin
        Clear;
        Add('select distinct m.title, m.code, m.year, c.name, m.in_collection, m.seen, m.wanted, m.for_sale 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');
		if (RegReadInteger('ShowOnlyMoviesInCollection', 0) = 1) then
			Add('and m.in_collection=1');
        //17.03.09
        //Add('order by d.movies_sort_order');
        Add(format('order by m.%s, d.movies_sort_order', [sort_order]));
      end;
      RecordCount:=ExecuteSQL(sl.text);

      if RecordCount>0 then begin
      	//Director - filmography
        movie:='<tr><td><p class="header">'+GetLangStr(290)+':';
		movie := movie + GetSortIcons(sort_order, PersonCode, 'Director');
		movie := movie + '</p><ol>';
        for i:=1 to RecordCount do begin
          year:=GetStringField('m.year');
          character:=GetStringField('c.name');
		  img:='';
          if (GetIntegerField('m.in_collection')=1) and ShowInCollectionIcon then
            img:=format('<img src="%smovie.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.seen')=1) and ShowSeenIcon then
			img := img + format('<img src="%sseen.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.wanted')=1) and ShowWantedIcon then
			img := img + format('<img src="%swanted.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.for_sale')=1) and ShowForSaleIcon then
			img := img + format('<img src="%ssale.gif" align="absmiddle">', [ImgPath]);
		  if img<>'' then begin
			if IconsOnTheRight then
				img := '&nbsp;' + img
			else
				img := img + '&nbsp;';
		  end;
		  title:=GetStringField('m.title');
		  movie := movie + '<li>';
		  if not IconsOnTheRight then
			movie := movie + img;
          if year='' then
            movie:=movie+format('<a href="movie:%d">',
              [GetIntegerField('m.code')])+title+'</a>'
          else
            movie:=movie+format('<a href="movie:%d">%s</a> (%s)',
              [GetIntegerField('m.code'), title, year]);
		  if IconsOnTheRight then
			movie := movie + img;
		  movie := movie + '</li>';

	      GetNextRecord;
        end;
        movie:=movie+'</ol></td></tr><tr><td class="free-space"></td></tr>';
        all:=all+movie;
      end;

			//movies by writer
	  sort_order := RegReadString('WriterFilmSort', 'year desc');
      with sl do begin
        Clear;
        Add('select distinct m.title, m.code, m.year, c.name, m.in_collection, m.seen, m.wanted, m.for_sale 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');
		if (RegReadInteger('ShowOnlyMoviesInCollection', 0) = 1) then
			Add('and m.in_collection=1');
        //17.03.09
        //Add('order by d.movies_sort_order');
        Add(Format('order by m.%s, d.movies_sort_order', [sort_order]));
      end;
      RecordCount:=ExecuteSQL(sl.text);

      //movies by writer
      if RecordCount>0 then begin
				//Writer - filmography
        movie:='<tr><td><p class="header">'+GetLangStr(291)+':';
		//sort icons
		movie := movie + GetSortIcons(sort_order, PersonCode, 'Writer');
		movie := movie + '</p><ol>';
        for i:=1 to RecordCount do begin
          year:=GetStringField('m.year');
          character:=GetStringField('c.name');
		  img:='';
          if (GetIntegerField('m.in_collection')=1) and ShowInCollectionIcon then
            img:=format('<img src="%smovie.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.seen')=1) and ShowSeenIcon then
			img := img + format('<img src="%sseen.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.wanted')=1) and ShowWantedIcon then
			img := img + format('<img src="%swanted.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.for_sale')=1) and ShowForSaleIcon then
			img := img + format('<img src="%ssale.gif" align="absmiddle">', [ImgPath]);
		  if img<>'' then begin
			if IconsOnTheRight then
				img := '&nbsp;' + img
			else
				img := img + '&nbsp;';
		  end;
		  title:=GetStringField('m.title');
		  movie := movie + '<li>';
		  if not IconsOnTheRight then
			movie := movie + img;
          if year='' then
            movie:=movie+format('<a href="movie:%d">',
              [GetIntegerField('m.code')])+title+'</a>'
          else
            movie:=movie+format('<a href="movie:%d">%s</a> (%s)',
              [GetIntegerField('m.code'), title, year]);
          if IconsOnTheRight then
			movie := movie + img;			
		  movie := movie + '</li>';
		  GetNextRecord;
        end;
        movie:=movie+'</ol></td></tr><tr><td class="free-space"></td></tr>';
        all:=all+movie;
      end;

			//movies by composer
	  sort_order := RegReadString('ComposerFilmSort', 'year desc');
      with sl do begin
        Clear;
        Add('select distinct m.title, m.code, m.year, c.name, m.in_collection, m.seen, m.wanted, m.for_sale 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');
		if (RegReadInteger('ShowOnlyMoviesInCollection', 0) = 1) then
			Add('and m.in_collection=1');
        //17.03.09
        //Add('order by d.movies_sort_order');
        Add(format('order by m.%s, d.movies_sort_order', [sort_order]));
      end;
      RecordCount:=ExecuteSQL(sl.text);

      //movies by composer
      if RecordCount>0 then begin
				//composer - filmography
        movie := '<tr><td><p class="header">'+GetLangStr(492)+':';
		movie := movie + GetSortIcons(sort_order, PersonCode, 'Composer');
		movie := movie + '</p><ol>';
        for i:=1 to RecordCount do begin
          year:=GetStringField('m.year');
          character:=GetStringField('c.name');
		  img:='';
          if (GetIntegerField('m.in_collection')=1) and ShowInCollectionIcon then
            img:=format('<img src="%smovie.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.seen')=1) and ShowSeenIcon then
			img := img + format('<img src="%sseen.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.wanted')=1) and ShowWantedIcon then
			img := img + format('<img src="%swanted.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.for_sale')=1) and ShowForSaleIcon then
			img := img + format('<img src="%ssale.gif" align="absmiddle">', [ImgPath]);
		  if img<>'' then begin
			if IconsOnTheRight then
				img := '&nbsp;' + img
			else
				img := img + '&nbsp;';
		  end;
		  title:=GetStringField('m.title');
		  movie := movie + '<li>';
		  if not IconsOnTheRight then
			movie := movie + img;
          if year='' then
            movie:=movie+format('<a href="movie:%d">',
              [GetIntegerField('m.code')])+title+'</a>'
          else
            movie:=movie+format('<a href="movie:%d">%s</a> (%s)',
              [GetIntegerField('m.code'), title, year]);
          if IconsOnTheRight then
			movie := movie + img;
		  movie := movie + '</li>';
	      GetNextRecord;
        end;
        movie:=movie+'</ol></td></tr><tr><td class="free-space"></td></tr>';
        all:=all+movie;
      end;
	end;
	
	//awards
	if (PersonActiveTab = 6) then begin
		cust := '';
		if awards<>'' then begin
			sl.Clear;
			sl.LoadFromFile(PicturesPath + 'awards\awards.txt');
			temp := '';
			while awards <> '' do begin
				i := Pos('<br><br>', awards) + 8;
				if i = 8 then
					i := Pos('<br> <br>', awards) + 9;
				if i = 9 then
					i := length(awards) + 1;
				s := copy(awards, 1, i-1);
				j := Pos(',', s);
				k := Pos('<br>', s);
				if (k < j) or (j = 0) then
					j := k;
				temp_name := copy(s, 1, j-1);
				new_name := '<a href="userref:136,' + temp_name + '">' + temp_name + '</a>';
				s := ReplaceStr(s, temp_name, new_name);
				for k := 0 to sl.Count - 1 do begin
					name := sl.Names[k];
					s3 := sl.Values[sl.Names[k]];
					s3 := ReplaceStr(s3, '-mid.', '.');
					j := Pos(':', name);
					if j > 0 then
						delete(name, j, length(name)-j+1);
					j := Pos('">' + LowerCase(name), LowerCase(new_name));
					if (j > 0) then
						l := Pos(LowerCase(new_name), LowerCase(s))
					else
						l := Pos(LowerCase(name), LowerCase(s));
					if (l = 1) or ((l > 0) and (s[l-1] = '>')) then begin
						s2 := '<div style="clear:both"><div style="float: left; margin: 5px;"><a class="pic" href="userref:136,'+ name + '"><img border="0" alt="' + name + '" src="' + PicturesPath + 'awards\' + s3 + '"></a></div>';
						insert(s2, s, l);
					end;
				end;
				temp := temp + s;
				delete(awards, 1, i - 1);
			end;
			cust:=cust+Format('<tr style="background:url(%s) repeat-x;"><td>&nbsp;</td></tr>', [WhiteBarFile])+'<tr><td><p class="description">'+temp+'</p></td></tr>';
		end;
		all := all + cust;
	end;
	
	all := all + '</table>' + GetPatternBottom;

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

function GetHomeModern(HomeMovieMode, HomePersonMode: Integer): string;
var
  sl, PreviousCodes: TStringList;
  PersonCode, i, j, kol, kol2, page_width, code, my_i: integer;
  all, description, genre, year, director, writer, actor, bio, character, movie, LastAdd: string;
  FileName, site, url, postfix, s, rate: string;
  RecordCount, rating, LastCode, ShowLastUpdated: integer;
  IMDBRating: double;
  MyTabs, TabNameMode, TabName1, TabName2, TabName3, TabName4, Tab1Mode, Tab2Mode, Tab3Mode, Tab4Mode, MoviesMore, PersonsMore, LastUpdatedTab, BirthdayTab: String;
  myDate: TDateTime;
  full_date, tmp, dbfilename: String;
  PicWidth, PicHeight, page_width_copy, loaned_movies, NumberOfBirthdays, AllPicWidth: Integer;
begin
	result:='';
	
	SetToolbarVisibility(true);
	SetMenuVisibility(true);
	SetNavigationVisibility(true);
	
	ShowLastUpdated := RegReadInteger('ShowLastUpdated', 0);

	sl:=TStringList.Create;
	PreviousCodes := TStringList.Create;
	HomeMovieTab := HomeMovieMode;
	HomePersonTab := HomePersonMode;
  
	all:=GetPatternTop;
	all:= all + '<p class="title">' + GetLangStr(91);

    all:=all+'</p></td>'+ClosePatternTop;
	
	all := all + Format('<tr><td><table class="content"><tr><td style="background:url(%s) repeat-x;">&nbsp;</td></tr><tr><td><table class="text-links"><tr><td class="controls">', [WhiteBarFile]);

	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+'<p class="usual-text">'+format('%s %d', [GetLangStr(700), kol2])+'</p>';

	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');
	
	with sl do begin
	  Clear;
	  Add('select l.movie from loans l');
	  Add('inner join manuals m on m.code=l.location'); 
	  Add('where (return_date='''' or return_date is null)');
	end;
	loaned_movies:=ExecuteSQL(sl.text);
	if (loaned_movies = 1) and (GetIntegerField('l.movie') = 0) then
		loaned_movies := 0;


    all:=all+'<p class="usual-text">'+format('%s %d', [GetLangStr(702),kol2])+'</p>';
	
	all := all + '<p class="usual-text">&nbsp;</p>';

    //'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+'<p class="usual-text">'+GetLangStr(94)+'</p>';
    all:=all+'<p class="text-link"><a href="add_movie">'+GetLangStr(95)+'</a></p>';
    all:=all+'<p class="text-link"><a href="add_person">'+GetLangStr(96)+'</a></p>';

	//show as covers
	all := all + format('<p class="text-link"><a href="userref:5,%s">%s</a></p>', [GetLangStr(1452), GetLangStr(1453)]);
    all:=all+'</td>';

	all:=all+'<td class="recommended">';

		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');}
      // 26.01.11 - Fade: speed is increased 90 - 100 times for database with 200 000 movies
      Add('select mn.code as code, mn.name as name, count(mn.code) as amount');
      Add('from manuals mn');
      Add('left join movies_manuals mm ON mn.code=mm.ref_code');
      Add('left join movies m on m.Code = mm.Movie');
      Add('Where mm.reference=8');
      Add('AND m.in_collection=1');
      Add('group by mn.code, mn.name');
      Add('order by 3 desc');
      // End - 26.01.11 
		end;
		kol:=ExecuteSQL(sl.text);
		kol2:=RegReadInteger('PopularGenresAmount', 6);
		if kol>kol2 then
			kol:=kol2;
		if kol>0 then
			s:='<p class="usual-text">'+GetLangStr(1300)+'</p>'
		else
			s:='';
		for i:=1 to kol do begin
			s:=s+format('<p class="text-link"><a href="genre:%d">%s (%d)</a></p>', [GetIntegerField('code'), GetStringField('name'), GetIntegerField('amount')]);
			GetNextRecord;
		end;
		all:=all+s;
		
    all:=all+'</td>';
	
	all:=all+Format('<td class="logo"><p><img src="%sicon4.png"></p><p></td></tr>', [PicturesPath]);
	
	//test database warning
	s := RegReadString('Recent1', '');
	s := LowerCase(s);
	dbfilename := s;
	i := Pos('\', dbfilename);
	while i>0 do begin
		delete(dbfilename, 1, i);
		i := Pos('\', dbfilename);
	end;
	tmp := GetLangStr(1463);
	if (Pos('tes', dbfilename)=1) and (length(dbfilename) < 12) and
		((Pos(LowerCase(GetLangStr(1466)), s)>0) or (Pos('appdata', s) >0)) then
		all := all+'<tr><td colspan="3" class="demodb">'+GetLangStr(368)+format(' <a href="link:http://%s">%s</a></td></tr>', [tmp, GetLangStr(1462)]);
	
	//program files folder warning
	if (Pos(LowerCase(GetLangStr(1464)), s)>0) then
		all := all + '<tr><td colspan="3" class="demodb">'+GetLangStr(1465)+format(' <a href="link:http://%s">%s</a></td></tr>', [tmp, GetLangStr(1462)]);
		
	all := all + '</table></td></tr>';
	

	with sl do begin
	  Clear;

      Add('select m.code as code, m.title as title, m.year as year, m.rating as rating, m.imdb_rating as imdb_rating, i.path as path, i.width, i.height');
      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');
				4: Add('where m.date_update is not null');
			else begin
				if ShowLastUpdated = 0 then
					Add('where m.date_add is not null')
				else
					Add('where m.date_update is not null');
			end;
      end;
      Add('and m.in_collection=1');
      if RegReadInteger('ShowDummyCovers', 0) = 0 then 
		Add('and i.mode=1');
      case HomeMovieMode of
      	2: Add('order by m.rating desc, m.date_add desc, m.code, i.sort_order, i.code');
		4: Add ('order by m.date_update desc, m.code, i.sort_order, i.code');
			else begin
				if ShowLastUpdated = 0 then
					Add('order by m.date_add desc, m.code, i.sort_order, i.code')
				else
					Add('order by m.date_update desc, m.code, i.sort_order, i.code');
			end;
      end;
	end;
	RecordCount:=ExecuteSQL(sl.text);
	
	MyTabs := '';
	LastAdd := '';
	AllPicWidth := 0;
	page_width:=GetHTMLWidth - WholePageBorder;
	page_width_copy := page_width;
    if RecordCount>0 then begin
		i:=1;
		j:=0;
		LastCode:=0;
		PreviousCodes.Clear;
		while (page_width_copy >= 0) and (i<=RecordCount) do begin
			code:=GetIntegerField('code');
			if PreviousCodes.IndexOf(IntToStr(code)) < 0 then begin
				PreviousCodes.Add(IntToStr(code));
				if j=0 then
					LastAdd:=LastAdd+'<tr><td><table class="boxes"><tr style="background:url('+BackgroundMovies+') repeat-x;">';
				j:=j+1;
				LastCode:=code;
				PicHeight := GetIntegerField('i.height');
				if PicHeight > 0 then
					PicWidth := Round(GetIntegerField('i.width') * (CoverHeight / PicHeight)) + NormalImageBorders
				else
					PicWidth := StandardCoverWidth;
				page_width_copy := page_width_copy - PicWidth;
				AllPicWidth := AllPicWidth + PicWidth;
				if page_width_copy >= 0 then begin
					year:=GetStringField('year');

					rating:=GetIntegerField('rating');
					rate:='';
					if rating>0 then begin
						for my_i:=1 to 10 do begin
							if my_i mod 2=1 then begin
								if my_i<=rating then
							rate:=rate+format('<img src="%sstar-left.gif" align="absmiddle" border="0">',  [ImgPath])
								else
							rate:=rate+format('<img src="%snot-star-left.gif" align="absmiddle" border="0">',  [ImgPath])
							end
							else begin
								if my_i<=rating then
							rate:=rate+format('<img src="%sstar-right.gif" align="absmiddle" border="0">',  [ImgPath])
								else
							rate:=rate+format('<img src="%snot-star-right.gif" align="absmiddle" border="0">',  [ImgPath])
							end;
						end;
					end;
					IMDBRating := GetFloatField('imdb_rating');
					if year<>'' then
						s:=GetLangStr(169)+': '+year+'. '+rate
					else
						s:=rate;

					FileName := GetImageFileName(GetStringField('path'), 'ThumbsHomeMovies', 'no-cover.gif', 0, CoverHeight);
					LastAdd:=LastAdd+'<td class="box-movies">'+format('<a href="movie:%d"><img class="movie-cover" src="%s"></a>', [code, FileName]);
					
					if (rate<>'') and (RegReadInteger('ShowRating', 1) = 1) then
						LastAdd := LastAdd+'<br>'+rate;
					LastAdd := LastAdd + '<p>';
					if IMDBRating <> 0 then
						LastAdd:=LastAdd+format('IMDB: %0.1f<br>', [IMDBRating]);
					
					LastAdd := LastAdd+format('<a href="movie:%d">%s</a></p></td>', [code, GetStringField('title')]);
				end;

			end;

			GetNextRecord;
			i:=i+1;
		end;
		if (page_width > 450) and (AllPicWidth < 450) then
			LastAdd := LastAdd + Format('<td style="width: %dpx;"></td>', [450 - AllPicWidth]);
		
		//full tab names
		if (page_width > 650) and (AllPicWidth > 650) then begin
			TabNameMode := '';
			if ShowLastUpdated = 0 then
				TabName1 := GetLangStr(1402)
			else
				TabName1 := GetLangStr(1421);
			TabName2 := GetLangStr(1403);
			TabName3 := GetLangStr(1404);
			TabName4 := GetLangStr(1421);
		end
		//short tab names
		else if (page_width >= 400) then begin
			TabNameMode := '-short';
			TabName1 := GetLangStr(1407);
			TabName2 := GetLangStr(1408);
			TabName3 := GetLangStr(1409);
			TabName4 := '';
		end
		//no tabs
		else
			TabNameMode := 'none';
			
		//creating hyperlinks and defining tab types (active, inactive)
		Tab1Mode := 'in';
		Tab2Mode := 'in';
		Tab3Mode := 'in';
		Tab4Mode := 'in';
		MoviesMore := '';
		if HomeMovieMode=1 then begin
			Tab1Mode := '';
			TabName2 := Format('<a href="modern_home:2,%d">%s</a>', [HomePersonMode, TabName2]);
			TabName3 := Format('<a href="modern_home:3,%d">%s</a>', [HomePersonMode, TabName3]);
			TabName4 := Format('<a href="modern_home:4,%d">%s</a>', [HomePersonMode, TabName4]);
			if ShowLastUpdated = 0 then
				MoviesMore := Format('<a href="userref:1,%s">', [GetLangStr(1402)])
			else
				MoviesMore := Format('<a href="userref:1,%s">', [GetLangStr(1421)]);
		end;
		if HomeMovieMode=2 then begin
			Tab2Mode := '';
			TabName1 := Format('<a href="modern_home:1,%d">%s</a>', [HomePersonMode, TabName1]);
			TabName3 := Format('<a href="modern_home:3,%d">%s</a>', [HomePersonMode, TabName3]);
			TabName4 := Format('<a href="modern_home:4,%d">%s</a>', [HomePersonMode, TabName4]);
			MoviesMore := Format('<a href="userref:2,%s">', [GetLangStr(1403)]);
		end;
		if HomeMovieMode=3 then begin
			Tab3Mode := '';
			TabName1 := Format('<a href="modern_home:1,%d">%s</a>', [HomePersonMode, TabName1]);
			TabName2 := Format('<a href="modern_home:2,%d">%s</a>', [HomePersonMode, TabName2]);
			TabName4 := Format('<a href="modern_home:4,%d">%s</a>', [HomePersonMode, TabName4]);
			MoviesMore := Format('<a href="userref:3,%s">', [GetLangStr(1404)]);
		end;
		if HomeMovieMode=4 then begin
			Tab4Mode := '';
			TabName1 := Format('<a href="modern_home:1,%d">%s</a>', [HomePersonMode, TabName1]);
			TabName2 := Format('<a href="modern_home:2,%d">%s</a>', [HomePersonMode, TabName2]);
			TabName3 := Format('<a href="modern_home:3,%d">%s</a>', [HomePersonMode, TabName3]);
			MoviesMore := Format('<a href="userref:4,%s">', [GetLangStr(1421)]);
		end;
		
		//create tabs
		LastUpdatedTab := '';
		if (TabNameMode = '') and (page_width > 810) and (j*130 > 810) and (ShowLastUpdated = 0) then
			LastUpdatedTab := format('<td class="tab-space"></td><td class="tab-border-left"><img src="%s%sactive-tab-left.gif"></td><td class="%sactive-tab%s">%s</td><td class="tab-border"><img src="%s%sactive-tab-right.gif"></td>', [ImgPath, Tab4Mode, Tab4Mode, TabNameMode, TabName4, ImgPath, Tab4Mode]);
			
		
		if TabNameMode <> 'none' then			
		  MyTabs:=format('<tr><td><table class="tabs"><tr><td class="tab-border-left"><img src="%s%sactive-tab-left.gif"></td><td class="%sactive-tab%s">%s</td><td class="tab-border"><img src="%s%sactive-tab-right.gif"></td>', [ImgPath, Tab1Mode, Tab1Mode, TabNameMode, TabName1, ImgPath, Tab1Mode]) + LastUpdatedTab + format('<td class="tab-space"></td><td class="tab-border-left"><img src="%s%sactive-tab-left.gif"></td><td class="%sactive-tab%s">%s</td><td class="tab-border"><img src="%s%sactive-tab-right.gif"></td>', [ImgPath, Tab2Mode, Tab2Mode, TabNameMode, TabName2, ImgPath, Tab2Mode]) + format('<td class="tab-space"></td><td class="tab-border-left"><img src="%s%sactive-tab-left.gif"></td><td class="%sactive-tab%s">%s</td><td class="tab-border"><img src="%s%sactive-tab-right.gif"></td><td></td></tr></table></td></tr>', [ImgPath, Tab3Mode, Tab3Mode, TabNameMode, TabName3, ImgPath, Tab3Mode]);
		  if j>0 then
			LastAdd:=LastAdd+Format('<td class="more">%s<img src="%smovies-more.gif" border="0"></a></td><td><td></tr></table></td></tr>', [MoviesMore, ImgPath]);

		  all:=all+MyTabs+LastAdd;
    end;

    //last added persons
	MyTabs := '';
	LastAdd := '';
	AllPicWidth := 0;
	
	with sl do begin
		Clear;
		Add('select count(code) as amount from persons where birth_date like ''%' + Format('%s', [GetTodaysDate]) + '%''');
	end;
	RecordCount:=ExecuteSQL(sl.text);
	NumberOfBirthdays:=GetIntegerField('amount');
	
	with sl do begin
		Clear;

      Add('select p.code as code, p.name as name, p.rating as rating, i.path as path, i.width, i.height');
      Add('from persons p');
      Add('left outer join images i on i.person=p.code');
      case HomePersonMode of
      	2: Add('where 1=1');
		3: Add('where p.date_add is not null');
		4: Add('where p.birth_date like ''%' + Format('%s', [GetTodaysDate]) + '%''');
			else begin
				if ShowLastUpdated = 0 then
					Add('where p.date_add is not null')
				else
					Add('where p.date_update is not null');
			end;
      end;
      Add('and p.in_collection=1');
	  if RegReadInteger('ShowDummyCovers', 0) = 0 then
		Add('and i.mode=6');
      case HomePersonMode of
      	2: Add('order by p.rating desc, p.date_add desc, p.code, i.sort_order, i.code');
		3: Add('order by p.date_update desc, p.code, i.sort_order, i.code');
			else begin
				if ShowLastUpdated = 0 then
					Add('order by p.date_add desc, p.code, i.sort_order, i.code')
				else
					Add('order by p.date_update desc, p.code, i.sort_order, i.code');
			end;
      end;
		end;
		RecordCount:=ExecuteSQL(sl.text);
	page_width_copy := page_width;
    if RecordCount>0 then begin
		i:=1;
		j:=0;
		LastCode:=0;
		PreviousCodes.Clear;
		while (page_width_copy >= 0) and (i<=RecordCount) do begin
			code:=GetIntegerField('code');
			if PreviousCodes.IndexOf(IntToStr(code)) < 0 then begin
				PreviousCodes.Add(IntToStr(code));
				if j=0 then
					LastAdd:=LastAdd+'<tr><td><table class="boxes"><tr style="background:url('+BackgroundPeople+') repeat-x;">'+format('<td class="more"><img src="%sadditional-people-block.gif"></td>', [ImgPath]);
				j:=j+1;
				LastCode:=code;
				PicHeight := GetIntegerField('i.height');
				if PicHeight > 0 then
					PicWidth := Round(GetIntegerField('i.width') * (PhotoHeight / PicHeight)) + NormalImageBorders
				else
					PicWidth := CoverCellWidth;
				if PicWidth < CoverCellWidth then
					PicWidth := CoverCellWidth;
				AllPicWidth := AllPicWidth + PicWidth;
				page_width_copy := page_width_copy - PicWidth;
				if page_width_copy >= 0 then begin
					rating:=GetIntegerField('rating');
					if rating>0 then
						rate:=GetLangStr(298)+': '+IntToStr(rating)
					else
						rate:='';
						
						s:=rate;

					FileName := GetImageFileName(GetStringField('path'), 'ThumbsMoviePhotos', 'nophoto.gif', 0, PhotoHeight);
					LastAdd:=LastAdd+format('<td class="box-photos"><a href="person:%d"><img class="person-photo" src="%s" alt="%s"><p>%s</a></p></td>',
								[code, FileName, s, GetStringField('name')]);
				end;

			end;

			GetNextRecord;
			i:=i+1;
		end;
		if (page_width > 400) and (AllPicWidth < 400) then
			LastAdd := LastAdd + Format('<td style="width: %dpx;"></td>', [300 - AllPicWidth]);

	//long tab names
	if (page_width >= 650) and (AllPicWidth >=650) then begin
		TabNameMode := '';
		if ShowLastUpdated = 0 then
			TabName1 := GetLangStr(1405)
		else
			TabName1 := GetLangStr(1422);
		TabName2 := GetLangStr(1406);
		TabName3 := GetLangStr(1422);
		if NumberOfBirthdays > 0 then
			TabName4 := GetLangStr(2974)
		else
			TabName4 := '';
	end
	//short tab names
	else if (page_width >= 400) then begin
		TabNameMode := '-short';
		TabName1 := GetLangStr(1407);
		TabName2 := GetLangStr(1408);
		TabName3 := '';
		TabName4 := '';
	end
	//no tabs
	else
		TabNameMode := 'none';
	Tab1Mode := 'in';
	Tab2Mode := 'in';
	Tab3Mode := 'in';
	Tab4Mode := 'in';
	PersonsMore := '';
	if HomePersonMode = 1 then begin
		Tab1Mode := '';
		TabName2 := Format('<a href="modern_home:%d,2">%s</a>', [HomeMovieMode, TabName2]);
		TabName3 := Format('<a href="modern_home:%d,3">%s</a>', [HomeMovieMode, TabName3]);
		if TabName4 <> '' then
			TabName4 := Format('<a href="modern_home:%d,4">%s</a>', [HomeMovieMode, TabName4]);
		if ShowLastUpdated = 0 then
			PersonsMore := Format('<a href="userref:6,%s">', [GetLangStr(1405)])
		else
			PersonsMore := Format('<a href="userref:6,%s">', [GetLangStr(1422)]);
	end;
	if HomePersonMode = 2 then begin
		Tab2Mode := '';
		TabName1 := Format('<a href="modern_home:%d,1">%s</a>', [HomeMovieMode, TabName1]);
		TabName3 := Format('<a href="modern_home:%d,3">%s</a>', [HomeMovieMode, TabName3]);
		if TabName4 <> '' then
			TabName4 := Format('<a href="modern_home:%d,4">%s</a>', [HomeMovieMode, TabName4]);
		PersonsMore := Format('<a href="userref:7,%s">', [GetLangStr(1406)]);
	end;
	if HomePersonMode = 3 then begin
		Tab3Mode := '';
		TabName1 := Format('<a href="modern_home:%d,1">%s</a>', [HomeMovieMode, TabName1]);
		TabName2 := Format('<a href="modern_home:%d,2">%s</a>', [HomeMovieMode, TabName2]);
		if TabName4 <> '' then
			TabName4 := Format('<a href="modern_home:%d,4">%s</a>', [HomeMovieMode, TabName4]);
		PersonsMore := Format('<a href="userref:8,%s">', [GetLangStr(1422)]);
	end;
	if HomePersonMode = 4 then begin
		Tab4Mode := '';
		TabName1 := Format('<a href="modern_home:%d,1">%s</a>', [HomeMovieMode, TabName1]);
		TabName2 := Format('<a href="modern_home:%d,2">%s</a>', [HomeMovieMode, TabName2]);
		TabName3 := Format('<a href="modern_home:%d,3">%s</a>', [HomeMovieMode, TabName3]);
		PersonsMore := Format('<a href="userref:9,%s">', [GetLangStr(2974)]);
	end;
	
	LastUpdatedTab := '';
	if (TabNameMode = '') and (page_width > 810) and (j*90 > 810) and (ShowLastUpdated = 0) then
		LastUpdatedTab := format('<td class="tab-space"></td><td class="tab-border-left"><img src="%s%sactive-tab-left.gif"></td><td class="%sactive-tab%s">%s</td><td class="tab-border"><img src="%s%sactive-tab-right.gif"></td>', [ImgPath, Tab3Mode, Tab3Mode, TabNameMode, TabName3, ImgPath, Tab3Mode]);
		
	if (TabName4 <> '') then
		BirthdayTab := format('<td class="tab-space"></td><td class="tab-border-left"><img src="%s%sactive-tab-left.gif"></td><td class="%sactive-tab%s">%s</td><td class="tab-border"><img src="%s%sactive-tab-right.gif"></td>', [ImgPath, Tab4Mode, Tab4Mode, TabNameMode, TabName4, ImgPath, Tab4Mode])
	else
		BirthdayTab := '';
	
	if TabNameMode <> 'none' then
      MyTabs:=format('<tr><td><table class="tabs"><tr><td class="tab-border-left"><img src="%s%sactive-tab-left.gif"></td><td class="%sactive-tab%s">%s</td><td class="tab-border"><img src="%s%sactive-tab-right.gif"></td>', [ImgPath, Tab1Mode, Tab1Mode, TabNameMode, TabName1, ImgPath, Tab1Mode]) + LastUpdatedTab + format('<td class="tab-space"></td><td class="tab-border-left"><img src="%s%sactive-tab-left.gif"></td><td class="%sactive-tab%s">%s</td><td class="tab-border"><img src="%s%sactive-tab-right.gif"></td>', [ImgPath, Tab2Mode, Tab2Mode, TabNameMode, TabName2, ImgPath, Tab2Mode]) + BirthdayTab + '<td></td></tr></table></td></tr>';
      if j>0 then
      	LastAdd:=LastAdd+Format('<td class="more">%s<img src="%speople-more.gif" border="0"></a></td><td></td></tr></table></td></tr>', [PersonsMore, ImgPath]);

      all:=all+MyTabs+LastAdd;
    end;
	
	//script setup link
	all := all + '<tr><td class="free-space"></td></tr><tr><td><p class="text-link"><a href="userref:100,Options">'+GetLangStr(1333)+'</a></p></td></tr>';

    //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 begin
		all:=all+'<tr><td class="free-space"></td></tr><tr><td><p class="usual-text">'+GetLangStr(438)+'</p></td></tr><tr><td>';

		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;
		all := all + '</td></tr>';
	end;

	//all:=all+'<a href="userref:666,Sex">test</a>';
	
	all := all + '</table>' + GetPatternBottom;

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

begin

end.