program breeze_program_part3;

uses 'breeze_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="pic" href="userref:131,%s;%d">', [PersonType, PersonCode]) + format('<img alt="%s" border="0" src="%stitle-sort-ascend.png" align="absmiddle"></a>', [GetLangStr(1477), ImgPath]);
		if (sort_order <> 'title desc') then
			result := result + format('&nbsp;<a class="pic" href="userref:132,%s;%d">', [PersonType, PersonCode]) + format('<img alt="%s" border="0" src="%stitle-sort-descend.png" align="absmiddle"></a>', [GetLangStr(1478), ImgPath]);
		if (sort_order <> 'year asc') then
			result := result + format('&nbsp;<a class="pic" href="userref:133,%s;%d">', [PersonType, PersonCode]) + format('<img alt="%s" border="0" src="%syear-sort-ascend.png" align="absmiddle"></a>', [GetLangStr(1479), ImgPath]);
		if (sort_order <> 'year desc') then
			result := result + format('&nbsp;<a class="pic" href="userref:134,%s;%d">', [PersonType, PersonCode]) + format('<img alt="%s" border="0" src="%syear-sort-descend.png" align="absmiddle"></a>', [GetLangStr(1480), ImgPath]);
	end;
end;

//показ карточки человека
function GetPerson(PersonCode: integer; FullBio, FullDesc: boolean): string;
var
  sl: TStringList;
  s, s2, s3, temp, temp2, cust, name, all, OriginalName, collection, bio, description, BirthDateStr, DeathDateStr, birthplace, awards, status, ends_with, temp_name, new_name: string;
  rate, FileName, movie, year, character, img, title, comment, down: string;
  rating, flCollection, i, j, k, l: integer;
  RecordCount, height, LastCode, AdditionalSpace: integer;
	site, url, postfix: string;
	tmp, age, CurrentDateStr: String;
	kol, PhotoCnt, MaxPhotos, page_width, page_width_copy, PicHeight, PicWidth, ThisBlockCnt, LineSpace, SpouseStatus, ChildrenCount, SpousePersonCode: integer;
	fl, ShowSeenIcon, ShowInCollectionIcon, ShowWantedIcon, ShowForSaleIcon, IconsOnTheRight, ShowScreenshots: boolean;
	sort_order, border, bckgrnd, SpousePerson, SpouseStatusStr: string;
begin
	result:='';
	bckgrnd := ' style="background-image: url(' + GetProperPath(ImgPath + 'list-background.gif') + ');"';
	sl:=TStringList.Create;
	LineSpace := Round((BasicFontSize + BiggerModifier) * LineSpacings[RegReadInteger('BreezeLineSpacing', DefaultLineSpacing)]);
  
	with sl do begin
		Clear;
		Add('select code from images');
		Add(format('where person=%d and mode=6', [PersonCode]));
		Add('order by sort_order');    
	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 := 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 class="pic" href="collection_person:%d"><img src="%snot-in-collection.gif" border="0" alt="Person is not in collection"></a>',  [PersonCode, ImgPath])
      else
	      collection:=format('<a class="pic" href="collection_person:%d"><img src="%sin-collection.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 class="pic" href="rating_person:%d"><img src="%shalfstar-left.gif" align="absmiddle" border="0"></a>',  [i, ImgPath])
					else
	    	  	rate:=rate+format('<a class="pic" href="rating_person:%d"><img src="%snot-halfstar-left.gif" align="absmiddle" border="0"></a>',  [i, ImgPath])
				end
				else begin
					if i<=rating then
		      	rate:=rate+format('<a class="pic" href="rating_person:%d"><img src="%shalfstar-right.gif" align="absmiddle" border="0"></a>',  [i, ImgPath])
					else
	    	  	rate:=rate+format('<a class="pic" href="rating_person:%d"><img src="%snot-halfstar-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 + Format('<p class="page-header" style="font-size: %dpt;">', [BasicFontSize + PageHeaderModifier]) + person;
	while (Pos(' ', OriginalName) = 1) do
		delete(OriginalName, 1, 1);
	if (OriginalName <> '') and (OriginalName <> person) then
		all:=all+ Format(' / <span style="font-size: %dpt;">', [BasicFontSize + SubHeaderModifier]) + OriginalName + '</span>';
	all:=all+'</p>';
	
	//tab background
	all := all + '<table style="border-collapse: collapse; width: 100%;"><tr><td><div style="margin-left: 10px; margin-right: 10px; height: 46px; background-image: url(' +GetProperPath(ImgPath + 'inactive-tab-button.gif') + ');">';
	//icons
	all := all + '<div class="icon-container">' + collection + '</div>';
	
	//tabs
	if RegReadInteger('HideNavigationMenu', 0) = 0 then
		all := all + GetPersonTabs(PersonCode, bio<>'', description<>'', PhotoCnt>1, awards<>'', AdditionalSpace);
	
	AdditionalSpace := 0;
	
	all := all + '</div></td></tr></table>';
	
	if (PersonActiveTab <> 2) then begin
	
		ShowScreenshots := False;
		MaxPhotos := RegReadInteger('PhotosCnt', 5);
		if (PersonActiveTab = 1) and (MaxPhotos > 0) then begin
			showScreenshots := True;
			i := GetHTMLWidth - LeftColumnWidth - ScreenshotWidth;
			if (i < DataArea) then
				showScreenshots := False;
		end;
			
		PhotoCnt := 0;
		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 ShowScreenshots then begin
		  AdditionalSpace := ScreenshotWidth + NormalImageBorders;
		  //фотки человека
		  if RecordCount>1 then begin
			all:=all+'<div class="shot-space">';
			if RecordCount <= MaxPhotos then
				MaxPhotos := RecordCount-1;
			GetNextRecord;
			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.jpg', ScreenshotWidth, 0);
					all:=all+'<div class="shots"><div><a class="pic" '+format('href="ShowImage:%d"><img style="width: %dpx" src="%s"></a></div></div>', [GetIntegerField('code'), ScreenshotWidth, FileName]);
				end;
				GetNextRecord;
			end;
			if PhotoCnt < (RecordCount - 1) then begin
				all:=all+format('<div class="arrow-down"><a class="pic" href="userref:141,%d"><img border="0" alt="%s" src="%sarrow-down.gif"></a></div>', [PersonCode, person, ImgPath]);
				//ScreenshotTotalHeight := ScreenshotTotalHeight + 30; //arrow down width
			end;
			all:=all+'</div>'
		  end;
		end
		else
			PhotoCnt := RecordCount;
	
		  //поиск картинок
		  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);
		  all:=all+ Format('<div class="cover-space" style="font-size: %dpt;">', [BasicFontSize + BiggerModifier]);
		  if RecordCount>0 then begin
			if (MyFileExists(ImagesPath + GetStringField('path'))) then begin
				FileName := GetImageFileName(GetStringField('path'), ThumbsMainPhoto, 'nophoto.jpg', MainPhotoWidth, 0);
				
				all:=all+'<div style="padding: 3px;"><a class="pic"'+format(' href="ShowImage:%d"><img style="width: %dpx" border="0" src="%s"></a></div>',
							[GetIntegerField('code'), MainPhotoWidth, FileName]);
			end;
		  end
		  else begin
			FileName:=ImgPath+'nophoto.jpg';
			if (MyFileExists(FileName)) then
				all:=all+format('<div style="padding: 3px;"><img class="cover-image" border="0" src="%s"></div>', [FileName]);
		  end;
		  
		//My rating
		i := RegReadInteger('ShowRating', 1);
		if i=1 then begin
			all := all + '<div style="vertical-align: middle; color: #666666; margin-bottom: 5px;">' + Format('<b>%s:</b></div>', [GetLangStr(298)]);
			all := all + '<div>' + rate + '</div>';
		end;
		
		//awards
		if awards <> '' then begin
			s2 := '';
			temp := LowerCase(awards);
			sl.LoadFromFile(PicturesPath + 'awards\awards.txt');
			for i := 0 to sl.Count - 1 do begin
				name := sl.Names[i];
				j := Pos(':', name);
				status := '';
				ends_with := '';
				if j > 0 then begin
					status := LowerCase(copy(name, j+1, length(name)-j));
					delete(name, j, length(name)-j+1);
					j := Pos(':', status);
					if j > 0 then begin
						ends_with := copy(status, j+1, length(status)-j);
						delete(status, j, length(status)-j+1);
					end;
				end;
				s := '';
				temp2 := temp;
				j := Pos(LowerCase(name), temp2);
				while j > 0 do begin
					k := MyPos(ends_with, temp2, j+1);
					if k = 0 then
						k := MyPos('<br><br>', temp2, j+1);
					if k = 0 then
						k := Length(temp2);
					s := copy(temp2, j, k-j);
					if (Pos('<br>'+ status, s) > 0) and (Pos(name, s2) = 0) then begin
						s2 := s2 + '<a class="pic" href="userref:136,' + name + '"><img border="0" alt="'+ name + '" src="' + PicturesPath + 'awards\' + sl.Values[sl.Names[i]] + '"></a>';
					end;
					delete(temp2, 1, j);
					j := Pos(LowerCase(name), temp2);
				end;
			end;
			if s2 <> '' then
				all := all + '<hr class="line-separator"><div>' + s2 + '</div>';
		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');
					all := all+'<hr class="line-separator"><div>';
					if postfix<>'' then
						url:=url+postfix;
					s:=GetStringField('p.picture');
					if s='' then
						all:=all+'<a href="link:http://www.movienizer.com/redirect/'+url+format('">%s</a>', [site])
					else
						all:=all+'<a class="pic" href="link:http://www.movienizer.com/redirect/'+url+'">'+format('<img src="%s%s" border="0" align="absmiddle"></a>', [PicturesPath, s]);
					all := all + '</div>';
				end;
				GetNextRecord;
			end;
		  end;
		
		all := all + '<div class="free-space-bottom" style="background-image: url(' + GetProperPath(ImgPath + 'bottom-shadow.gif') +')"></div></div>'; //end photo block		
	end; //if PersonActiveTab <> 2
	
	all := all + Format('<div style="font-size: %dpt; line-height: %dpx; text-align: center;">', [BasicFontSize, LineSpace]) + RemainingTabs + '</div>';
	
	//photos
	if PersonActiveTab = 2 then begin
		all := all + GetShots(PersonCode, 6, 'person', 5);
	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+ Format('<table class="description" style="font-size: %dpt; line-height: %dpx;"><tr><td><p>', [BasicFontSize + BiggerModifier, LineSpace])+bio+'</p><hr></td></tr></table>';
	end;
	
	//personal details
	if PersonActiveTab = 1 then begin
		tmp := '';
		ThisBlockCnt := 0;
		if BirthDateStr<>'' then begin
			ThisBlockCnt := ThisBlockCnt + 1;
			if (ThisBlockCnt mod 2 = 1) then
				border := bckgrnd
			else
				border := '';
			//Date of birth
			tmp := tmp + '<tr><td class="small-table-name"' + border + Format('>%s</td><!-- <td class="middle-cell"></td> --><td class="small-table-value"', [GetLangStr(294)])+ border + '>' + GetSmartDate(BirthDateStr); 
			if DeathDateStr='' then begin
				s := Format(GetLangStr(838), [GetAge(CurrentDateStr, BirthDateStr)]);
				tmp := tmp + s;
			end;
			tmp := tmp + '</td></tr>';
		end;
				  
		if birthplace<>'' then begin
			ThisBlockCnt := ThisBlockCnt + 1;
			if (ThisBlockCnt mod 2 = 1) then
				border := bckgrnd
			else
				border := '';
			//Location of birth
			tmp:=tmp + '<tr><td class="small-table-name"' + border + Format('>%s</td><!-- <td class="middle-cell"></td> --><td class="small-table-value"', [GetLangStr(287)])+ border + '>';
			s := birthplace;
			i := Pos(', ', s);
			while i > 0 do begin
				s2 := copy(s, 1, i-1);
				delete(s, 1, i+1);
				tmp := tmp + Format('<a href="userref:200,%s">%s</a>, ', [s2, s2]);
				i := Pos(', ', s);
			end;
			tmp := tmp + Format('<a href="userref:200,%s">%s</a>', [s, s]);
			tmp := tmp + '</td></tr>';
		end;
					
		age := '';
		if DeathDateStr<>'' then begin
			ThisBlockCnt := ThisBlockCnt + 1;
			if (ThisBlockCnt mod 2 = 1) then
				border := bckgrnd
			else
				border := '';
			//Date of death
			tmp:=tmp+'<tr><td class="small-table-name"' + border + Format('>%s</td><!-- <td class="middle-cell"></td> --><td class="small-table-value"', [GetLangStr(295)])+ border + '><a href="userref:201,' + GetLangStr(3905) + '">' + GetSmartDate(DeathDateStr) + '</a>';
			tmp:=tmp +  ' ' + format(GetLangStr(839), [GetAge(DeathDateStr, BirthDateStr)]);
			tmp:=tmp+'</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;
			ThisBlockCnt := ThisBlockCnt + 1;
			if (ThisBlockCnt mod 2 = 1) then
				border := bckgrnd
			else
				border := '';
			tmp:=tmp+'<tr><td class="small-table-name"' + border + Format('>%s</td><!-- <td class="middle-cell"></td> --><td class="small-table-value"', [GetLangStr(372)])+ border + '>' + s + '</td></tr>';
		end;
		
		
		if tmp <> '' then begin
			all := all + Format('<table class="names-values" style="font-size: %dpt; line-height: %dpx;">', [BasicFontSize + BiggerModifier, LineSpace]) + tmp + '</table>';
		end;
	end;
	
	if (PersonActiveTab = 1) then begin
		all := all + '<table class="envelope"><tr><td>';
		//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:=Format('<p class="header" style="font-size: %dpt;">', [BasicFontSize + SubHeaderModifier])+GetLangStr(3897)+':';
			movie:=movie + Format('</p><div class="filmography" style="font-size: %dpt; line-height: %dpx;"><ol>', [BasicFontSize + BiggerModifier, LineSpace]);
			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></div>';
			all:=all+movie;
		end;
		all := all + '</td></tr></table>';
	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 d.movies_sort_order, m.year desc, i.sort_order');
		end;
		RecordCount:=ExecuteSQL(sl.text);
		page_width := GetHTMLWidth - WholePageBorder;
		if PhotoCnt > 0 then
			page_width := page_width - ScreenshotWidth;
		page_width_copy := page_width;
		tmp := '';
		if RecordCount>0 then begin
			all:=all+'<div style="clear:left;"></div><table class="photos" style="margin-left: 10px;"><tr>';
			j := 0;
			kol := 1;
			sl.Clear;
			i := 0;
			fl := True;
			while (i < RecordCount) and fl 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)) + NormalImageBorders
					else
						PicWidth := StandardCoverWidth + NormalImageBorders;
					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')));


						FileName := GetImageFileName(GetStringField('path'), ThumbsPersonMovies, 'no-cover.gif', 0, SmallCoverHeight);
						tmp:=tmp+'<td class="one-photo"><a class="pic" ' + format('href="movie:%d"><img style="height: %dpx" border="0" src="%s"></a><p><a href="movie:%d">%s</a></p>', [GetIntegerField('code'), SmallCoverHeight, FileName, GetIntegerField('code'), GetStringField('m.title')]);

						
						
						if age <>'' then
							tmp := tmp +'<p style="color: #666666;">'+ format(GetLangStr(1410), [age]) + '</p>';
						if GetStringField('c.name') <> '' then
							tmp := tmp + format('<p>%s</p>', [GetStringField('c.name')]);

						page_width_copy := page_width_copy - PicWidth;
						if page_width_copy >= 0 then begin
							all := all + tmp;
							j := j + 1;
							tmp := '';
						end
						else begin
							if (kol >= RegReadInteger('CoverRows', DefaultCoverRows)) then
								fl := false
							else begin
								all := all + '<td></td></tr><tr>' + tmp;
								kol := kol + 1;
								page_width_copy := page_width - PicWidth;
								tmp := '';
							end;
						end;
					end;
				end;
				i := i + 1;
				GetNextRecord;
			end;
			all := all + '<td></td>';
(*			if kol > j then
				all := all + '<td class="arrow-left-page"><a class="pic" href="#">' + Format('<img border="0" src="%sarrow-left.gif"/>', [ImgPath])+'</a></td>';
*)			all := all + '</tr></table>';
		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+Format('<table class="description" style="font-size: %dpt; line-height: %dpx;"><tr><td>', [BasicFontSize + BiggerModifier, LineSpace]);
		if (PersonActiveTab = 1) then
			all := all + '<hr>';
		all := all + '<p>'+description+'</p><hr></td></tr></table>';
	end;

	//comments
	if (PersonActiveTab = 1) then begin
		if comment<>'' then
			all:=all+Format('<table class="description" style="font-size: %dpt; line-height: %dpx;"><tr><td><p>', [BasicFontSize + BiggerModifier, LineSpace])+comment+'</p></td></tr></table>';
	end;

	if (PersonActiveTab = 1) or (PersonActiveTab = 5) then begin
		all := all + '<table class="envelope"><tr><td>';
	//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:=Format('<p class="header" style="font-size: %dpt;">', [BasicFontSize + SubHeaderModifier])+GetLangStr(289)+':';
		movie:=movie + GetSortIcons(sort_order, PersonCode, 'Actor');
		movie:=movie + Format('</p><div class="filmography" style="font-size: %dpt; line-height: %dpx;"><ol>', [BasicFontSize + BiggerModifier, LineSpace]);
        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="%sin-collection-small.gif" align="absmiddle">', [ImgPath]); 
		  if (GetIntegerField('m.seen')=1) and ShowSeenIcon then
			img := img + '&nbsp;' + format('<img src="%sseen-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.wanted')=1) and ShowWantedIcon then
			img := img + '&nbsp;' + format('<img src="%swanted-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.for_sale')=1) and ShowForSaleIcon then
			img := img + '&nbsp;' + format('<img src="%sfor-sale-small.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(' ... %s', [character]);
		  if IconsOnTheRight then
			movie := movie + img;
		  movie := movie + '</li>';

	      GetNextRecord;
        end;
        movie:=movie+'</ol></div>';
        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:=Format('<hr><p class="header" style="font-size: %dpt;">', [BasicFontSize + SubHeaderModifier])+GetLangStr(290)+':';
		movie := movie + GetSortIcons(sort_order, PersonCode, 'Director');
		movie := movie + Format('</p><div class="filmography" style="font-size: %dpt; line-height: %dpx;"><ol>', [BasicFontSize + BiggerModifier, LineSpace]);
        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="%sin-collection-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.seen')=1) and ShowSeenIcon then
			img := img + '&nbsp;' + format('<img src="%sseen-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.wanted')=1) and ShowWantedIcon then
			img := img + '&nbsp;' + format('<img src="%swanted-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.for_sale')=1) and ShowForSaleIcon then
			img := img + '&nbsp;' + format('<img src="%sfor-sale-small.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></div>';
        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:=Format('<hr><p class="header" style="font-size: %dpt;">', [BasicFontSize + SubHeaderModifier])+GetLangStr(291)+':';
		//sort icons
		movie := movie + GetSortIcons(sort_order, PersonCode, 'Writer');
		movie := movie + Format('</p><div class="filmography" style="font-size: %dpt; line-height: %dpx;"><ol>', [BasicFontSize + BiggerModifier, LineSpace]);
        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="%sin-collection-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.seen')=1) and ShowSeenIcon then
			img := img + '&nbsp;' + format('<img src="%sseen-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.wanted')=1) and ShowWantedIcon then
			img := img + '&nbsp;' + format('<img src="%swanted-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.for_sale')=1) and ShowForSaleIcon then
			img := img + '&nbsp;' + format('<img src="%sfor-sale-small.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></div>';
        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 := Format('<hr><p class="header" style="font-size: %dpt;">', [BasicFontSize + SubHeaderModifier])+GetLangStr(492)+':';
		movie := movie + GetSortIcons(sort_order, PersonCode, 'Composer');
		movie := movie + Format('</p><div class="filmography" style="font-size: %dpt; line-height: %dpx;"><ol>', [BasicFontSize + BiggerModifier, LineSpace]);
        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="%sin-collection-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.seen')=1) and ShowSeenIcon then
			img := img + '&nbsp;' + format('<img src="%sseen-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.wanted')=1) and ShowWantedIcon then
			img := img + '&nbsp;' + format('<img src="%swanted-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.for_sale')=1) and ShowForSaleIcon then
			img := img + '&nbsp;' + format('<img src="%sfor-sale-small.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></div>';
        all:=all+movie;
      end;
	  all := all + '</td></tr></table>';
	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('<table class="description" style="font-size: %dpt; line-height: %dpx;"><tr><td>', [BasicFontSize + BiggerModifier, LineSpace])+temp+'</td></tr></table>';
		end;
		all := all + cust;
	end;

  sl.Free;
  result:=all;
end;

function GetHomeModern(HomeMovieMode, HomePersonMode: Integer): string;
var
  sl: 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, temp: string;
  RecordCount, rating, LastCode, ShowLastUpdated: integer;
  IMDBRating, SiteRating: double;
  MyTabs, TabNameMode, MoviesMore, PersonsMore, LastUpdatedTab: String;
  myDate: TDateTime;
  full_date, tmp, dbfilename, mylogo, bckgrnd, prefix: String;
  PicWidth, PicHeight, page_width_copy, loaned_movies, FullTabsWidth, ShortTabsWidth, StandardHomeCoverWidth, NumberOfMovies, NumberOfPeople: Integer;
  ShowLinks: Boolean;
  TabName: array [1..4] of string;
  RatingIndex: Integer;
  current_site, sortby_string, mc_rating, kinopoisk_rating: String;
begin
	result:='';
	current_site := '';
	ShowLinks := False;
	bckgrnd := 'background-color: '+CellColor;
	
	SetToolbarVisibility(true);
	SetMenuVisibility(true);
	SetNavigationVisibility(true);
	
	RatingIndex := RegReadInteger('MyRatingTabRating', 1);
	if RatingIndex = 2 then begin
		current_site := RegReadString('MovieScript', '');
		i := Pos(' (', current_site);
		if i>0 then
			delete(current_site, i, length(current_site)-i+1);
	end;
	
	ShowLastUpdated := RegReadInteger('ShowLastUpdated', 0);

	sl:=TStringList.Create;
	HomeMovieTab := HomeMovieMode;
	HomePersonTab := HomePersonMode;
	
	//number of movies in the collection
(*	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);
	NumberOfMovies:=GetIntegerField('amount');
	
	//number of persons in the collection
(*	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);
	NumberOfPeople:=GetIntegerField('amount');
  
	all:=GetPatternTop;
	
	if ShowHomeLogo or (NumberOfMovies < 1) or (NumberOfPeople < 1) then begin	
		//Logo
		all := all + #13#10 + '<div class="home-logo"><img src="' + GetProperPath(ImgPath + 'logo-transp.png') + '"></div>';
		
		//Add movie button
		all := all + #13#10 + '<div style="float: right; height: 46px; margin: 20px; "><div style="float: right; color: white; padding-right: 10px; font-size: 15pt; background-image: url(' + GetProperPath(ImgPath + 'movie-plus-bckgrnd.gif') + '); padding-left: 42px; line-height: 46px; text-align: center; font-weight: bold;"><a class="pic" href="add_movie"><NOBR>' + GetLangStr(3012) + '</NOBR></a></div><div style="float: left; width: 41px; height: 46px;"><img src="' + GetProperPath(ImgPath + 'plus-movie.gif') + '" border="0"/></div></div>';

		all := all + #13#10 + ClosePatternTop;
		all := all + #13#10 + '<hr style="margin-top: -10px;">';
	end;
	
	//movie tabs
	MyTabs := GetHomeTabsTop;
	if GetHTMLWidth >= FullTabsScreenWidth then begin
		MyTabs := MyTabs + GetMoviePersonTab('movies-icon.gif');
		MyTabs := MyTabs + Format('<a class="pic" href="userref:5,%s">', [GetLangStr(1452)]) + GetLangStr(42) + '</a>';
		
		if RegReadInteger('ShowStats', 1) = 1 then
			MyTabs := MyTabs + ' (' + IntToStr(NumberOfMovies) + ')';
		MyTabs := MyTabs + '</div>';
	end;
	
	TabName[1] := GetLangStr(2972);
	TabName[2] := GetLangStr(2973);
	TabName[3] := GetLangStr(2962);
	TabName[4] := GetLangStr(1409);
	s := 'background-image: url(' + GetProperPath(ImgPath + 'active-tab-button.gif') + ');';
	//build tabs
	for i := 1 to 4 do begin
		temp := Format('<div class="inactive-tabs" style="font-size: %dpt;', [BasicFontSize + BiggerModifier]);
		if HomeMovieTab = i then
			temp := temp + s + 'margin-top: 5px;">' + TabName[i] + '</div>'
		else
			temp := temp + 'margin-top: 5px;"><a class="pic" href="' + Format('modern_home:%d,%d', [i, HomePersonMode]) + '">' + TabName[i] + '</a></div>';
		MyTabs := MyTabs + temp;
	end;
	
	MyTabs := MyTabs + CloseHomeTabsTop;
	
	//Get movies
	sortby_string := 'order by m.rating desc, m.date_add desc, m.code, i.sort_order';
	if RatingIndex = 3 then
		sortby_string := 'order by m.imdb_rating desc, m.date_add desc, m.code, i.sort_order';
	kinopoisk_rating := ', (select Rating from movies_codes where movie=m.code and script=''kinopoisk'' LIMIT 1) as KinopoiskRating';
	mc_rating := '';
	if RatingIndex = 2 then begin
		if (current_site <> 'imdb') then begin
			sortby_string := 'order by SiteRating desc, m.rating desc, m.date_add desc, m.code, i.sort_order';
			mc_rating := Format(', (select Rating from movies_codes where movie=m.code and script=''%s'' LIMIT 1) as SiteRating', [current_site]);
		end
		else
			sortby_string := 'order by m.imdb_rating desc, m.date_add desc, m.code, i.sort_order';
	end;
	with sl do begin
	  Clear;
      Add(Format('select m.code as code, m.title as title, m.imdb_rating as imdb_rating, m.rating as rating, i.path as path, i.width, i.height, i.mode%s%s', [kinopoisk_rating, mc_rating]));
      Add('from movies m');
	  if (RatingIndex = 2) and (current_site <> 'imdb') then
		Add('left outer join movies_codes mc on mc.movie=m.code');
      Add('left outer join images i on i.movie=m.code');
      case HomeMovieMode of
		2: Add('where m.date_update is not null');
      	3: Add('where 1=1');
      	4: Add('where seen<>1');
			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.date_update desc, m.code, i.sort_order');
      	3: Add(sortby_string);
			else begin
				if ShowLastUpdated = 0 then
					Add('order by m.date_add desc, m.code, i.sort_order')
				else
					Add('order by m.date_update desc, m.code, i.sort_order');
			end;
      end;
	end;
	RecordCount:=ExecuteSQL(sl.text);
	
	LastAdd := '';
	page_width:=GetHTMLWidth - WholePageBorder - ArrowWidth;
	page_width_copy := page_width;

    if RecordCount>0 then begin
		i:=1;
		j:=0;
		LastCode:=0;
		sl.Clear;
		while (page_width_copy >= 0) and (i<=RecordCount) do begin
			code:=GetIntegerField('code');
			if sl.IndexOf(IntToStr(code)) < 0 then begin
				if j=0 then
					LastAdd:=LastAdd+'<table style="text-align: center; vertical-align: top; margin: 10px;"><tr>';
				j:=j+1;
				LastCode:=code;
				page_width_copy := page_width_copy - HomeCoverWidth - NormalImageBorders;
				if page_width_copy >= 0 then begin
					sl.Add(IntToStr(code));
					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="%shalfstar-left.gif" align="absmiddle" border="0">',  [ImgPath])
								else
							rate:=rate+format('<img src="%snot-halfstar-left.gif" align="absmiddle" border="0">',  [ImgPath])
							end
							else begin
								if my_i<=rating then
							rate:=rate+format('<img src="%shalfstar-right.gif" align="absmiddle" border="0">',  [ImgPath])
								else
							rate:=rate+format('<img src="%snot-halfstar-right.gif" align="absmiddle" border="0">',  [ImgPath])
							end;
						end;
					end;

					IMDBRating := GetFloatField('imdb_rating');
						
					if (GetIntegerField('i.mode') = 1) then
						FileName := GetImageFileName(GetStringField('path'), ThumbsHomeMovies, 'no-cover.jpg', HomeCoverWidth, HomeCoverHeight, True)
					else
						FileName := GetImageFileName('', ThumbsHomeMovies, 'no-cover.jpg', HomeCoverWidth, HomeCoverHeight, True);
					LastAdd:=LastAdd+'<td style="background-color: white; border: 1px solid ' + WhiteAreaBorderColor + '; width: 100px; padding: 5px;"><a class="pic"'+format(' href="movie:%d"><img style="height: %dpx" border="0" src="%s"></a>', [code, HomeCoverHeight, FileName]);
					
					SiteRating := GetFloatField('KinopoiskRating');
					if (SiteRating <> 0) and (RegReadInteger('ShowKinopoiskRatingUnderCovers', 0) = 1) then begin
							s := Format('<img src="%skinopoisk-ico.png">&nbsp;', [PicturesPath]);
							LastAdd:=LastAdd+'<div style="vertical-align: middle; font-weight: bold; margin-top: 5;">' + s + format('%s: %0.1f</div>', [GetLangStr(3419), SiteRating]);
					end;
					if (IMDBRating <> 0) and (RegReadInteger('ShowIMDBRatingUnderCovers', 1) = 1) then
						LastAdd:=LastAdd+'<div style="vertical-align: middle; font-weight: bold; margin-top: 5;"><img ' + Format('src="%stag-icon.gif"/>&nbsp;', [ImgPath]) + format('IMDB: %0.1f</div>', [IMDBRating]);
					if (rate<>'') and (RegReadInteger('ShowUserRatingUnderCovers', 1) = 1) then
						LastAdd := LastAdd + '<div style="vertical-align: bottom; margin-top: 5;">' + rate + '</div>';
					
					LastAdd := LastAdd+'<p style="margin: 5 0;">' + format('<a href="movie:%d">%s</a></p>', [code, GetStringField('title')]);
					LastAdd := LastAdd + '</td>';
				end;

			end;
			GetNextRecord;
			i:=i+1;
		end;
		if HomeMovieMode = 1 then 
			MoviesMore := Format('userref:1,%s', [GetLangStr(1402)]);
		if HomeMovieMode = 2 then
			MoviesMore := Format('userref:4,%s', [GetLangStr(1421)]);
		if HomeMovieMode = 3 then
			MoviesMore := Format('userref:2,%s', [GetLangStr(1403)]);
		if (HomeMovieMode = 4) then
			MoviesMore := Format('userref:3,%s', [GetLangStr(1404)]);
		if LastAdd <> '' then begin
			if (sl.IndexOf(IntToStr(GetIntegerField('code'))) < 0) then
				LastAdd := LastAdd + '<td style="vertical-align: middle; padding-left: 20;"><a class="pic" href="' + MoviesMore + '"><img border="0" ' + Format('src="%sarrow-right.gif"></td>', [ImgPath]);
			LastAdd := LastAdd + '<td></td></tr></table>';
			LastAdd := MyTabs + LastAdd;
		end;
	end;
	
	all := all + LastAdd;
	
	//all := all + '<hr style="margin:0;">';
	
	
	//persons tabs
	MyTabs := GetHomeTabsTop;
	if GetHTMLWidth >= FullTabsScreenWidth then begin
		MyTabs := MyTabs + GetMoviePersonTab('persons-icon.gif');
		MyTabs := MyTabs + GetLangStr(43);
		
		if RegReadInteger('ShowStats', 1) = 1 then
			MyTabs := MyTabs + ' (' + IntToStr(NumberOfPeople) + ')';
		MyTabs := MyTabs + '</div>';
	end;
	
	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);
	kol2:=GetIntegerField('amount');
	
	TabName[1] := GetLangStr(2972);
	TabName[2] := GetLangStr(2973);
	TabName[3] := GetLangStr(2962);
	TabName[4] := GetLangStr(2974);
	s := 'background-image: url(' + GetProperPath(ImgPath + 'active-tab-button.gif') + ');';
	if kol2 > 0 then 
		j := 4
	else 
		j :=3;
	//build tabs
	for i := 1 to j do begin
		temp := Format('<div class="inactive-tabs" style="font-size: %dpt;', [BasicFontSize + BiggerModifier]);
		if HomePersonTab = i then
			temp := temp + s + 'margin-top: 5px;">' + TabName[i] + '</div>'
		else
			temp := temp + 'margin-top: 5px;"><a class="pic" href="' + Format('modern_home:%d,%d', [HomeMovieMode, i]) + '">' + TabName[i] + '</a></div>';
		MyTabs := MyTabs + temp;
	end;
	
	MyTabs := MyTabs + CloseHomeTabsTop;
	
	//Get persons
	with sl do begin
	  Clear;
      Add('select p.code as code, p.name as name, p.rating as rating, p.birth_date, 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');
		2: 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.date_update desc, p.code, i.sort_order');
      	3: Add('order by p.rating desc, p.date_add desc, p.code, i.sort_order');
			else begin
				if ShowLastUpdated = 0 then
					Add('order by p.date_add desc, p.code, i.sort_order')
				else
					Add('order by p.date_update desc, p.code, i.sort_order');
			end;
      end;
	end;
	RecordCount:=ExecuteSQL(sl.text);
	
	LastAdd := '';
	page_width:=GetHTMLWidth - WholePageBorder - ArrowWidth;
	page_width_copy := page_width;

    if RecordCount>0 then begin
		i:=1;
		j:=0;
		LastCode:=0;
		sl.Clear;
		while (page_width_copy >= 0) and (i<=RecordCount) do begin
			code:=GetIntegerField('code');
			if sl.IndexOf(IntToStr(code)) < 0 then begin
				if j=0 then
					LastAdd:=LastAdd+'<table style="text-align: center; vertical-align: top; margin: 10px;"><tr>';
				j:=j+1;
				//LastCode:=code;
				page_width_copy := page_width_copy - HomeCoverWidth - NormalImageBorders;
				if page_width_copy >= 0 then begin
					sl.Add(IntToStr(code));
					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="%shalfstar-left.gif" align="absmiddle" border="0">',  [ImgPath])
								else
							rate:=rate+format('<img src="%snot-halfstar-left.gif" align="absmiddle" border="0">',  [ImgPath])
							end
							else begin
								if my_i<=rating then
							rate:=rate+format('<img src="%shalfstar-right.gif" align="absmiddle" border="0">',  [ImgPath])
								else
							rate:=rate+format('<img src="%snot-halfstar-right.gif" align="absmiddle" border="0">',  [ImgPath])
							end;
						end;
					end;
					FileName := GetImageFileName(GetStringField('path'), ThumbsHomePhotos, 'nophoto.jpg', HomeCoverWidth, HomeCoverHeight, True);
					LastAdd:=LastAdd+'<td style="background-color: white; border: 1px solid ' + WhiteAreaBorderColor + '; width: 100px; padding: 5px;"><a class="pic"'+format(' href="person:%d"><img style="height: %dpx" border="0" src="%s"></a>', [code, HomeCoverHeight, FileName]);
					if (rate<>'') and (RegReadInteger('ShowRating', 1) = 1) then
						LastAdd := LastAdd + '<div style="vertical-align: bottom; margin-top: 5;">' + rate + '</div>';
					
					LastAdd := LastAdd+'<p style="margin: 5 0;">' + format('<a href="person:%d">%s</a>', [code, GetStringField('name')]);
					if (HomePersonMode = 4) then begin
						DecodeDate(Date, kol, kol2, my_i);
						LastAdd := LastAdd + ' (' + IntToStr(YearsBetween(GetStringField('p.birth_date'), IntToStr(kol))) + ')';
					end;
					LastAdd := LastAdd + '</div>';
					LastAdd := LastAdd + '</td>';
				end;

			end;

			GetNextRecord;
			i:=i+1;
		end;
		if HomePersonMode = 1 then
			PersonsMore := Format('userref:6,%s', [GetLangStr(1405)]);
		if HomePersonMode = 2 then
			PersonsMore := Format('userref:7,%s', [GetLangStr(1422)]);
		if (HomePersonMode = 3) then
			PersonsMore := Format('userref:8,%s', [GetLangStr(1406)]);
		if HomePersonMode = 4 then
			PersonsMore := Format('userref:9,%s', [GetLangStr(2974)]);
		if LastAdd <> '' then begin
			if (sl.IndexOf(IntToStr(GetIntegerField('code'))) < 0) then begin
				LastAdd := LastAdd + '<td style="vertical-align: middle; padding-left: 20;"><a class="pic" href="' + PersonsMore + '"><img border="0" ' + Format('src="%sarrow-right.gif"></td>', [ImgPath]);
			end;
			LastAdd := LastAdd + '<td></td></tr></table>';
			LastAdd := MyTabs + LastAdd;
		end;
	end;
	
	all := all + LastAdd;
	
	//all := all + '<hr style="margin:0;">';
	LastAdd := '';
	//shots from movies
	with sl do begin
		Clear;
		Add('select i.path as path, i.code as code, i.movie, i.width, i.height, m.title as title from images i');
		Add('join movies m on m.code=i.movie');
		Add('where i.mode=3');
		if (RegReadInteger('OnlyUnseenShots', 0) = 1) then
			Add('and m.seen=0');
		Add('order by random () limit 20');
	end;
	RecordCount := ExecuteSQL(sl.text);
	if RecordCount > 0 then begin
		LastAdd := '<table style="color: white; text-align: center; vertical-align: top; margin: 10px; border-collapse: collapse;"><tr>';
		page_width:=GetHTMLWidth - WholePageBorder;
		page_width_copy := page_width;
		i:=1;
		j:=0;
		LastCode:=0;
		while (page_width_copy >= 0) and (i<=RecordCount) do begin
			code:=GetIntegerField('code');
			if (LastCode<>code) and (GetIntegerField('i.width') > GetIntegerField('i.height')) then begin
				LastCode:=code;
				page_width_copy := page_width_copy - HomeShotWidth - BigImageBorders - SmallImageBorder;
				if page_width_copy >= 0 then begin
						
					FileName := GetImageFileName(GetStringField('path'), ThumbsHomeShots, 'no-cover.jpg', HomeShotWidth, HomeShotHeight, True);
					LastAdd := LastAdd + '<td style="background-image: url(' + GetProperPath(ImgPath) + 'home-shots-background.gif); width: ' + IntToStr(HomeShotWidth) + '; padding-bottom: 10px;"><div style="background-color: white; border: 1px solid ' + WhiteAreaBorderColor + '; padding: 2px; margin: 10 ' + IntToStr(BigImageBorders div 2) + '">';
					LastAdd := LastAdd + Format('<a class="pic" href="movie:%d"><img width="', [GetIntegerField('i.movie')]) + IntToStr(HomeShotWidth)+ '" src="' + FileName + '"></a></div>';
					LastAdd := LastAdd + Format('<a class="pic" href="movie:%d">', [GetIntegerField('i.movie')]) + GetStringField('title') + '</a>';
					LastAdd := LastAdd + '</td>';					
				end;

			end;

			GetNextRecord;
			i:=i+1;
		end;
		LastAdd := LastAdd + '</tr></table>';
	end;
	
	all := all + LastAdd;
	




    //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+'<div class="partners"><p>'+GetLangStr(438)+'</p>';

		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 + '</div>';
		//all := all + '<div class="free-space"></div>';
	end;

	//all:=all+'<a href="userref:666,Sex">test</a>';
	
  all := all + GetPatternBottom;
  result:=all;
  sl.Free;
end;
	

begin

end.