program orange_program_part3;

uses 'orange_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, all, OriginalName, collection, bio, description, BirthDateStr, DeathDateStr, birthplace: string;
  rate, FileName, movie, year, character, img, title, comment, down, awards: 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: integer;
	fl, ShowSeenIcon, ShowInCollectionIcon, ShowWantedIcon, ShowForSaleIcon, IconsOnTheRight, ShowScreenshots: boolean;
	sort_order, border, bckgrnd: string;
  // spouses variables
  SpouseStatus: Integer;
  SpouseStatusStr: string;
  ChildrenCount: Integer;
  SpousePerson: string;
  SpousePersonCode: Integer;
  temp, cust, temp_name, new_name, name, status, ends_with, temp2: String;
begin
	result:='';
	bckgrnd := ' style="background-image: url('+BackgroundImg+');" ';
  sl:=TStringList.Create;
  
	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 + '<p class="title">' + person;
	while (Pos(' ', OriginalName) = 1) do
		delete(OriginalName, 1, 1);
	if (OriginalName <> '') and (OriginalName <> person) then
		all:=all+' <span style="color: #999999; font-size: 16pt;">(' + OriginalName + ')</span>';
	all:=all+'</p>' + Format('<div class="icons">%s&nbsp;&nbsp;</div>', [collection]) + ClosePatternTop;
	
	AdditionalSpace := 0;
	
	if (PersonActiveTab <> 2) then begin
	
		all:=all+'<div class="left-block">';
		  //поиск картинок
		  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+'<div class="cover-info">';
		  if RecordCount>0 then begin
			if (MyFileExists(ImagesPath + GetStringField('path'))) then begin
				FileName := GetImageFileName(GetStringField('path'), ThumbsMainPhoto, 'nophoto.jpg', MainPhotoWidth, 0);
				
				all:=all+'<div class="img-outer"><div class="img-inner"><a class="pic"'+format(' href="ShowImage:%d"><img style="width: %dpx" border="0" src="%s"></a></div></div>',
							[GetIntegerField('code'), MainPhotoWidth, FileName]);
			end;
		  end
		  else begin
			FileName:=ImgPath+'nophoto.jpg';
			if (MyFileExists(FileName)) then
				all:=all+format('<div class="img-outer"><div class="img-inner"><img class="cover-image" border="0" src="%s"></div></div>', [FileName]);
		  end;

		  all:=all+'</div><div style="background-color: '+CellColor+'; height: 10px;"></div>';
		  
		  //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+'<div class="site-link">';
					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"></div>';
		//My rating
		i := RegReadInteger('ShowRating', 1);
		if i=1 then begin
			all := all + '<div class="site-rating">';
			all := all + Format('%s:<br/>', [GetLangStr(298)]);
			all := all + rate + '</div>';
		end;
		
		tmp := '';
		if BirthDateStr<>'' then begin
			//Date of birth
			tmp := tmp + '<div class="rating"><b>' + GetLangStr(294) + ':</b><br/>'+GetSmartDate(BirthDateStr); 
			if DeathDateStr='' then
      begin
				s := Format(GetLangStr(838), [GetAge(CurrentDateStr, BirthDateStr)]);
				s := ReplaceStr(s, ', ', '<br/>');
				tmp := tmp + s;
			end;
			tmp := tmp + '</div>';
		end;
			  
		if birthplace<>'' then begin
			//Location of birth
			s3 := '';
			s := birthplace;
			i := Pos(', ', s);
			while i > 0 do begin
				s2 := copy(s, 1, i-1);
				delete(s, 1, i+1);
				s3 := s3 + Format('<a href="userref:200,%s">%s</a><br>', [s2, s2]);
				i := Pos(', ', s);
			end;
			s3 := s3 + Format('<a href="userref:200,%s">%s</a><br>', [s, s]);
			//birthplace := ReplaceStr(birthplace, ', ', '<br/>');
			tmp:=tmp+'<div class="rating"><b>' + GetLangStr(287) + ':</b><br/>'+ s3 + '</div>';
		end;
		
		age := '';
		if DeathDateStr<>'' then begin
			//Date of death
			tmp:=tmp+'<div class="rating"><b>' + GetLangStr(295) + ':</b><br/>'+'<a href="userref:201,' + GetLangStr(3905) + '">' + GetSmartDate(DeathDateStr) + '</a>';
			tmp:=tmp+'<br/>'+format(GetLangStr(839), [GetAge(DeathDateStr, BirthDateStr)]);
			tmp:=tmp+'</div>';
		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+'<div class="rating"><b>' + GetLangStr(372) + ':</b><br/>' + s + '</div>';
		end;
		
		if tmp<>'' then
			all := all + '<div style="background-color: '+CellColor+'; height: 10px;"></div>' + tmp;
			
		//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 + '<div style="background-color: '+CellColor+'; height: 10px;"></div>' + '<div style="margin: 10 0; text-align: center;">' + s2 + '</div>';
		end;			

		
		all := all + '</div>'; //end photo block
		
		
		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+'<table class="right-block"><tr><td>';
			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="shot-place"><div class="img-outer"><div class="img-inner"><a class="pic" '+format('href="ShowImage:%d"><img style="width: %dpx" src="%s"></a></div></div></div>',
							[GetIntegerField('code'), ScreenshotWidth, FileName]);
				end;
				GetNextRecord;
			end;
			if PhotoCnt < (RecordCount - 1) then 
				all:=all+format('<div class="arrow-down"><a class="pic" href="userref:121,%d"><img border="0" alt="%s" src="%sarrow-down.gif"></a></div>', [PersonCode, person, ImgPath]);
			all:=all+'</td></tr></table>'
		  end;
		end
		else
			PhotoCnt := RecordCount;
	end; //if PersonActiveTab <> 2
	
	
	//tabs
	if RegReadInteger('HideNavigationMenu', 0) = 0 then
		all := all + GetPersonTabs(PersonCode, bio<>'', description<>'', PhotoCnt>1, awards <> '', AdditionalSpace);
	
	all := all + '<table style="width: 100%; border-collapse: collapse;">';
	
	//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+'<tr><td><table class="text-box"><tr><td>'+bio+'</td></tr></table></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 d.movies_sort_order, m.year desc, i.sort_order');
		end;
		RecordCount:=ExecuteSQL(sl.text);
		page_width := GetHTMLWidth - LeftColumnWidth - WholePageBorder;
		if PhotoCnt > 0 then
			page_width := page_width - ScreenshotWidth - ScreenshotPadding;
		page_width_copy := page_width;
		tmp := '';
		if RecordCount>0 then begin
			all:=all+'<tr><td><table class="white-bck-block">';
			j := 0;
			kol := 1;
			all := all + '<tr><td>';
			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+'<div class="image-place"><div class="photo-outer"><div class="photo-inner"><a class="pic" ' + format('href="movie:%d"><img style="height: %dpx" border="0" src="%s"></a></div></div><div class="image-text"><a href="movie:%d">%s</a>', [GetIntegerField('code'), SmallCoverHeight, 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 + ('</div></div>');

						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></tr><tr><td>' + tmp;
								kol := kol + 1;
								page_width_copy := page_width - PicWidth;
								tmp := '';
							end;
						end;
					end;
				end;
				i := i + 1;
				GetNextRecord;
			end;
			all := all + '</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></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><td><table class="text-box"><tr><td>'+description+'</td></tr></table></td></tr>';
	end;

	//comments
	if (PersonActiveTab = 1) then begin
		if comment<>'' then
			all:=all+'<tr><td><table class="text-box"><tr><td>'+comment+'</td></tr></table></td></tr>';
	end;

	if (PersonActiveTab = 1) then
  begin
     // Fade. 04.12.2012 - Person's spouses
     with sl do begin
        Clear;
        Add('SELECT p.Name, p.Code, ps.children_count, ps.spouse_status ');
        Add('FROM persons_spouses_codes psc');
        Add('LEFT JOIN persons_spouses ps ON psc.person_spouses_code = ps.code');
        Add('LEFT JOIN persons p ON psc.person_secondary = p.code');
        Add('WHERE psc.person_primary = ' + IntToStr(PersonCode));
        Add('ORDER BY psc.sort_order');
      end;
      RecordCount:=ExecuteSQL(sl.text);


      if RecordCount>0 then
      begin
        movie:='<tr><td><p class="header">'+GetLangStr(3897)+':';
		    movie:=movie + '</p><div class="filmography"><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></div></td></tr><tr><td class="free-space"></td></tr>';
        all:=all+movie;
      end;
      // End - Fade. 04.12.2012 - Person's spouses
	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><div class="filmography"><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="%sin-collection-small.gif" align="absmiddle">', [ImgPath]); 
		  if (GetIntegerField('m.seen')=1) and ShowSeenIcon then
			img := img + format('<img src="%sseen-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.wanted')=1) and ShowWantedIcon then
			img := img + format('<img src="%swanted-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.for_sale')=1) and ShowForSaleIcon then
			img := img + 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(' ... <i>%s</i>', [character]);
		  if IconsOnTheRight then
			movie := movie + img;
		  movie := movie + '</li>';

	      GetNextRecord;
        end;
        movie:=movie+'</ol></div></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><div class="filmography"><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="%sin-collection-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.seen')=1) and ShowSeenIcon then
			img := img + format('<img src="%sseen-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.wanted')=1) and ShowWantedIcon then
			img := img + format('<img src="%swanted-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.for_sale')=1) and ShowForSaleIcon then
			img := img + 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></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><div class="filmography"><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="%sin-collection-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.seen')=1) and ShowSeenIcon then
			img := img + format('<img src="%sseen-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.wanted')=1) and ShowWantedIcon then
			img := img + format('<img src="%swanted-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.for_sale')=1) and ShowForSaleIcon then
			img := img + 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></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><div class="filmography"><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="%sin-collection-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.seen')=1) and ShowSeenIcon then
			img := img + format('<img src="%sseen-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.wanted')=1) and ShowWantedIcon then
			img := img + format('<img src="%swanted-small.gif" align="absmiddle">', [ImgPath]);
		  if (GetIntegerField('m.for_sale')=1) and ShowForSaleIcon then
			img := img + 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></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+'<table class="text-box"><tr><td>'+temp+'</td></tr></table>';
		end;
		all := all + cust;
	end;
	
	all := all + '</table>';

  sl.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, SiteRating: double;
  MyTabs, TabNameMode, TabName1, TabName2, TabName3, TabName4, Tab1Mode, Tab2Mode, Tab3Mode, Tab4Mode, MoviesMore, PersonsMore, LastUpdatedTab: String;
  myDate: TDateTime;
  full_date, tmp, dbfilename, mylogo, bckgrnd, prefix: String;
  PicWidth, PicHeight, page_width_copy, loaned_movies, FullTabsWidth, ShortTabsWidth, StandardHomeCoverWidth: Integer;
  ShowLinks, Birthdays: Boolean;
  RatingIndex: Integer;
  current_site, sortby_string, mc_rating: String;
begin
	result:='';
	current_site := '';
	ShowLinks := False;
	bckgrnd := 'background-color: '+CellColor;
	PreviousCodes := TStringList.Create;
	
	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;
  
	all:=GetPatternTop;
	mylogo := GetProperPath(Format('%sicon4.png', [ImgPath]));
	all:= all + '<div style="float: left; margin-left: 5px; background-image: url('+mylogo+'); width: 96px; height: 96px;">&nbsp;</div>' + '<p class="gen-title">' + GetLangStr(91);

    all:=all+'</p>'+ClosePatternTop;
	

	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 + '<table class="db-stats"><tr>';
    //all:=all+format('Movies in collection: %d, in database: %d<br>', [kol2, kol]);
    all:=all+'<td class="left-indent">';
	if GetHTMLWidth > 820 then
		s := GetLangStr(700)
	else
		s := GetLangStr(807);
	all:= all + format('<div style="float: left;">%s %d</div>', [s, kol2]);
	//add button
	s := ReplaceStr(GetLangStr(3012), ' ', '&nbsp;');
	all := all + '<div class="add-btn">' + '<div style="float: right; width: 4px; height: 36px; background-image: url(' + GetProperPath(Format('%sadd-btn-end.gif', [ImgPath])) + ');"></div>' + '<div style="float: left; width: 23px; height: 36px; background-image: url(' + GetProperPath(Format('%sadd-plus.gif', [ImgPath])) + ');"></div>' + '<div class="add-btn-body" style="background-image: url(' + GetProperPath(Format('%sadd-btn-body.gif', [ImgPath])) + ');"><a class="pic" href="add_movie">' + s + '</a></div>';
	all := all + '</div>'; //end add button
	all:=all+'</td>';

	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 + '<td style="width: 20px; '+bckgrnd+'">&nbsp;</td>';
	if GetHTMLWidth > 820 then
		s := GetLangStr(702)
	else
		s := GetLangStr(1498);
    all:=all+'<td class="left-indent">'+format('<div style="float: left;">%s %d</div>', [s,kol2]);
	//add button
	s := ReplaceStr(GetLangStr(1493), ' ', '&nbsp;');
	all := all + '<div class="add-btn">' + '<div style="float: right; width: 4px; height: 36px; background-image: url(' + GetProperPath(Format('%sadd-btn-end.gif', [ImgPath])) + ');"></div>' + '<div style="float: left; width: 23px; height: 36px; background-image: url(' + GetProperPath(Format('%sadd-plus.gif', [ImgPath])) + ');"></div>' + '<div class="add-btn-body" style="background-image: url(' + GetProperPath(Format('%sadd-btn-body.gif', [ImgPath])) + ');"><a class="pic" href="add_person">' + s + '</a></div>';
	all := all + '</div>'; //end add button
	all:=all + '</td></tr>';

	if ShowLinks then begin
		//show as covers
		all := all + format('<tr><td class="left-indent" style="%s font-weight: normal;"><a href="userref:5,%s">%s</a></td>', [bckgrnd, GetLangStr(1452), GetLangStr(1453)]);
		all := all + '<td style="width: 20px; '+ bckgrnd + '">&nbsp;</td>';
		//script setup link
		all := all + '<td class="left-indent" style="'+bckgrnd+' font-weight: normal;"><a href="userref:100,Options">'+GetLangStr(1333)+'</a></td></tr></table>';
	end
	else
		all := all + '</table><div class="small-space"></div>';

	kol2:=RegReadInteger('PopularGenresAmountOrange', 0);
	if kol2 > 0 then begin
	//Popular genres
		with sl do begin
		  Clear;
		  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;
		kol:=ExecuteSQL(sl.text);
		if kol>kol2 then
			kol:=kol2;
		if kol>0 then
			s:='<div class="header">&nbsp;'+GetLangStr(1300)+'</div><table class="genres"><tr>'
		else
			s:='';
		for i:=1 to kol do begin
			s:=s+format('<td class="left-indent"><a href="genre:%d">%s (%d)</a></td>', [GetIntegerField('code'), GetStringField('name'), GetIntegerField('amount')]);
			GetNextRecord;
		end;
		if s<>'' then
			s := s+'</tr></table>';
		all:=all+s;
	end;

	
	//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+''+GetLangStr(368)+format(' <a href="link:http://%s">%s</a>', [tmp, GetLangStr(1462)]);
	
	//program files folder warning
	if (Pos(LowerCase(GetLangStr(1464)), s)>0) then
		all := all + GetLangStr(1465)+format(' <a href="link:http://%s">%s</a>', [tmp, GetLangStr(1462)]);
	
	
	//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';
	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.year as year, m.rating as rating, m.imdb_rating as imdb_rating, i.path as path, i.width, i.height, i.mode%s', [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 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(sortby_string);
		4: Add ('order by m.date_update desc, m.code, i.sort_order');
			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);
	
	MyTabs := '';
	LastAdd := '';
	page_width:=GetHTMLWidth - WholePageBorder - ArrowWidth;
	page_width_copy := page_width;
	if ShowLastUpdated = 0 then begin
		FullTabsWidth := HomeTabWidth * 4;
		ShortTabsWidth := TabWidth * 4;
	end
	else begin
		FullTabsWidth := HomeTabWidth * 3;
		ShortTabsWidth := TabWidth * 3;
	end;
	StandardHomeCoverWidth := Round(NoCoverWidth * (HomeCoverHeight / NoCoverHeight));
    if RecordCount>0 then begin
		i:=1;
		j:=0;
		LastCode:=0;
		while (page_width_copy >= 0) and (i<=RecordCount) do begin
			code:=GetIntegerField('code');
			if PreviousCodes.IndexOf(IntToStr(code)) < 0 then begin
				if j=0 then
					LastAdd:=LastAdd+'<table class="white-bck-block"><tr><td>';
				j:=j+1;
				LastCode:=code;
				PreviousCodes.Add(IntToStr(code));
				PicHeight := GetIntegerField('i.height');
				if (PicHeight > 0) and (GetIntegerField('i.mode') = 1) then
					PicWidth := Round(GetIntegerField('i.width') * (HomeCoverHeight / PicHeight)) + BigImageBorders
				else
					PicWidth := StandardHomeCoverWidth + BigImageBorders;
				page_width_copy := page_width_copy - 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="%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 year<>'' then
						s:=GetLangStr(169)+': '+year+'. '+rate
					else
						s:=rate;
						
					if (GetIntegerField('i.mode') = 1) then
						FileName := GetImageFileName(GetStringField('path'), ThumbsHomeMovies, 'no-cover.jpg', 0, HomeCoverHeight)
					else
						FileName := GetImageFileName('', ThumbsHomeMovies, 'no-cover.jpg', 0, HomeCoverHeight);
					LastAdd:=LastAdd+'<div class="home-image-place"><div class="img-outer"><div class="img-inner"><a class="pic"'+format(' href="movie:%d"><img style="height: %dpx" border="0" src="%s"></a></div></div>', [code, HomeCoverHeight, FileName]);
					LastAdd:=LastAdd + '<div class="image-text">';
					
					if (rate<>'') and (RegReadInteger('ShowRating', 1) = 1) then
						LastAdd := LastAdd+rate+'<br>';
					//LastAdd := LastAdd + '<p>';
					if (RatingIndex = 2) and (HomeMovieMode = 2) and (current_site <> 'imdb') then begin
						SiteRating := GetFloatField('SiteRating');
						if SiteRating <> 0 then
							LastAdd:=LastAdd+'<div style="vertical-align: middle; margin-top: 5;">' + format('%s: %0.1f</div>', [GetLangStr(3419), SiteRating]);
					end
					else if IMDBRating <> 0 then
						LastAdd:=LastAdd+format('IMDB: %0.1f<br>', [IMDBRating]);
					
					LastAdd := LastAdd+format('<a href="movie:%d">%s</a></div>', [code, GetStringField('title')]);
					LastAdd := LastAdd + '</div>';
				end;

			end;

			GetNextRecord;
			i:=i+1;
		end;
		PreviousCodes.Clear;
		
		//full tab names
		//if (page_width > FullTabsWidth) and (j*(StandardHomeCoverWidth + BigImageBorders) > FullTabsWidth) then begin
		if (page_width > FullTabsWidth) 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 >= ShortTabsWidth) and (j*StandardHomeCoverWidth >= ShortTabsWidth) then begin
		else if (page_width >= ShortTabsWidth) then begin
			TabNameMode := '-short';
			TabName1 := GetLangStr(1407);
			TabName2 := GetLangStr(1408);
			TabName3 := GetLangStr(1409);
			TabName4 := '';
		end
		//no tabs
		else
			TabNameMode := 'none';
			
		if TabNameMode <> 'none' then begin			
			all := all + '<table class="tabs" style="margin: 10px 0px 10px 0px;"><tr><td>';
			all := all + GetTab(HomeMovieMode=1, -100, TabName1, Format('modern_home:1,%d', [HomePersonMode]));
			i := HomeMovieMode;
			if (TabName4 <> '') and (ShowLastUpdated = 0) then begin
				all := all + GetTab(HomeMovieMode=4, i, TabName4, Format('modern_home:4,%d', [HomePersonMode]));
				i := HomeMovieMode - 3;
			end;
			all := all + GetTab(HomeMovieMode=2, i, TabName2, Format('modern_home:2,%d', [HomePersonMode]));
			i := HomeMovieMode - 1;
			all := all + GetTab(HomeMovieMode=3, i, TabName3, Format('modern_home:3,%d', [HomePersonMode]));
			i := HomeMovieMode - 2;
			if i = 1 then
				prefix := ''
			else
				prefix := 'in';
			if HomeMovieMode = 1 then 
				MoviesMore := Format('userref:1,%s', [GetLangStr(1402)]);
			if HomeMovieMode = 2 then
				MoviesMore := Format('userref:2,%s', [GetLangStr(1403)]);
			if HomeMovieMode = 3 then
				MoviesMore := Format('userref:3,%s', [GetLangStr(1404)]);
			if (HomeMovieMode = 4) or (ShowLastUpdated = 1) then
				MoviesMore := Format('userref:4,%s', [GetLangStr(1421)]);
			all := all + '<div class="tab-end" style="background-image: url('+ GetProperPath(Format('%s%sactive-tab-end.gif', [ImgPath, prefix])) + ');"></div>';
			all := all + '<div class="orange-line"></div></td></tr></table>';
		end
		else
			all := all + '<div class="free-space"></div>';

		  all:=all+LastAdd+'</td>';
		  all := all + '<td class="arrow-left-home"><a class="pic" href="'+MoviesMore+'">' + Format('<img border="0" src="%sarrow-left.gif"/>', [ImgPath])+'</a></td>';
		  all:=all+'</tr></table>';
    end;

    //last added persons
	MyTabs := '';
	LastAdd := '';
	
	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');
	if kol2 > 0 then
		Birthdays := True
	else
		Birthdays := False;
	
	
	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');
		3: Add('order by p.date_update desc, p.code, i.sort_order');
		4: Add('order by 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);
	page_width_copy := page_width;
	if ShowLastUpdated = 0 then begin
		FullTabsWidth := HomeTabWidth * 3;
		ShortTabsWidth := TabWidth * 3;
	end
	else begin
		FullTabsWidth := HomeTabWidth * 2;
		ShortTabsWidth := TabWidth * 2;
	end;
    if RecordCount>0 then begin
		i:=1;
		j:=0;
		LastCode:=0;
		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+'<table class="white-bck-block"><tr><td>';
				j:=j+1;
				LastCode:=code;
				PicHeight := GetIntegerField('i.height');
				if PicHeight > 0 then
					PicWidth := Round(GetIntegerField('i.width') * (HomePhotoHeight / PicHeight)) + BigImageBorders
				else
					PicWidth := StandardHomeCoverWidth + BigImageBorders;
					
				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'), ThumbsHomePhotos, 'nophoto.jpg', 0, HomePhotoHeight);
					LastAdd:=LastAdd+'<div class="home-image-place"><div class="img-outer"><div class="img-inner">'+format('<a class="pic" href="person:%d"><img style="height: %dpx" border="0" src="%s" alt="%s">', [code, HomePhotoHeight, FileName, s]) + '</a></div></div><div class="image-text">'+ Format('<a href="person:%d">%s</a></div></div>',[code, GetStringField('name')]);
				end;

			end;

			GetNextRecord;
			i:=i+1;
		end;

	//long tab names
	//if (page_width >= FullTabsWidth) and (j*(StandardHomeCoverWidth + BigImageBorders) >=FullTabsWidth) then begin
	if (page_width >= FullTabsWidth) then begin
		TabNameMode := '';
		if ShowLastUpdated = 0 then
			TabName1 := GetLangStr(1405)
		else
			TabName1 := GetLangStr(1422);
		TabName2 := GetLangStr(1406);
		TabName3 := GetLangStr(1422);
		if Birthdays then
			TabName4 := GetLangStr(2974)
		else
			TabName4 := '';
	end
	//short tab names
	//else if (page_width >= ShortTabsWidth) and (j*StandardHomeCoverWidth >= ShortTabsWidth) then begin
	else if (page_width >= ShortTabsWidth) then begin
		TabNameMode := '-short';
		TabName1 := GetLangStr(1407);
		TabName2 := GetLangStr(1408);
		TabName3 := '';
	end
	//no tabs
	else
		TabNameMode := 'none';
	
	if TabNameMode <> 'none' then begin
		all := all + '<table class="tabs" style="margin: 10px 0px 10px 0px;"><tr><td>';
		all := all + GetTab(HomePersonMode=1, -100, TabName1, Format('modern_home:%d,1', [HomeMovieMode]));
		i := HomePersonMode;
		if (TabName3 <> '') and (ShowLastUpdated = 0) then begin
			all := all + GetTab(HomePersonMode=3, i, TabName3, Format('modern_home:%d,3', [HomeMovieMode]));
			i := HomePersonMode - 2;
		end;
		all := all + GetTab(HomePersonMode=2, i, TabName2, Format('modern_home:%d,2', [HomeMovieMode]));
		i := HomePersonMode - 1;
		if (TabName4 <> '') then begin
			all := all + GetTab(HomePersonMode=4, i, TabName4, Format('modern_home:%d,4', [HomeMovieMode]));
			i := HomePersonMode - 3;
		end;
		if i = 1 then
			prefix := ''
		else
			prefix := 'in';
		all := all + '<div class="tab-end" style="background-image: url('+ GetProperPath(Format('%s%sactive-tab-end.gif', [ImgPath, prefix])) + ');"></div>';
		all := all + '<div class="orange-line"></div></td></tr></table>';
		
		if HomePersonMode = 1 then
			PersonsMore := Format('userref:6,%s', [GetLangStr(1405)]);
		if HomePersonMode = 2 then
			PersonsMore := Format('userref:7,%s', [GetLangStr(1406)]);
		if (HomePersonMode = 3) or (ShowLastUpdated = 1) then
			PersonsMore := Format('userref:8,%s', [GetLangStr(1422)]);
		if HomePersonMode = 4 then
			PersonsMore := Format('userref:9,%s', [GetLangStr(2974)]);
	end
	else
		all := all + '<div class="free-space"></div><div class="orange-line"></div>';

      all:=all+LastAdd+'</td>';
	  all := all + '<td class="arrow-left-home"><a class="pic" href="'+PersonsMore+'">' + Format('<img border="0" src="%sarrow-left.gif"/>', [ImgPath])+'</a></td>';
	  all := all + '</tr></table>';
    end;
	


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

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

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

begin

end.