program breeze_program_variables;
//Constants, Global variables and general utilities

const
	DefaultLinesPeople=3; //default number of rows for people (e.g. last added people)
	DefaultLinesMovies=3; //default number of rows for movies (e.g. movies by genre)
	DefaultMultiCoverWidth=90; //the width of cover for the coverflow view
	DefaultMultiCoverHeight=120; //the height of cover for the coverflow view
	DefaultMultiCellWidth=100; //width of the cell for cover in the coverflow mode
	DefaultMultiCellHeight=165; //height of the cell for cover in the coverflow mode (cell contains the cover and some text, e.g. title of the movie)
	WholePageBorder=60; //the space that cannot be used on the page (borders)
	NormalImageBorders=15; //space to take into account when calculating image width 
	BigImageBorders=20; //like above for home page
	SmallCoverHeight=130; //height of movie covers on person's page and of photos on movie page
	TabWidth=110; //approximate width of a "normal" tab on the movie/person page
	HomeTabWidth=180; //the same for the home page
	SeasonTabWidth=40;
	TabBorders=300; //space that cannot be used by tabs
	StandardShotWidth=350; //just a value, does not mean much
	ShotHeight=250; // height of images on the shots/posters/photos tabs
	HomeCoverHeight=170; //height of covers on the home page
	HomeCoverWidth=125;
	HomePhotoHeight=170; //height of photos on the home page
	HomePhotoWidth=125;
	HomeShotWidth=230;
	HomeShotHeight=130;
	MainPhotoWidth=150; //main photo on the person's page
	MainCoverWidth=150; //main cover on the movie page
	ScreenshotWidth=215; //width of shots and photos on movie/person page
	EpisodeCoverWidth=215;
	ArrowWidth=40; //width of the arrow shown next to covers and/or photos
	SmallImageBorder=5;
	LeftColumnWidth=200;
	MaxNameLength=12;
	MaxCharacterNameLength=10;
	DefaultThumbWidth=80; //width of thumbs in the detailed list (movies and people)
	DefaultTrailerWidth=200;
	DefaultCoverRows=1; //how many rows of covers should be displayed on the person's page
	NoCoverWidth=150; //full width of the 'no cover' image, assumed for photos, too
	NoCoverHeight=204; //full height of the 'no cover' image
	ScreenshotAreaHeight=600; //if screenshot area is shorter than this number, the full page width will be used to
							 //display photos
	IconSpace=210;
	ScreenshotPadding=40; //space around screenshots
	WidePage=1000; //if the available area bigger than this, photos are expected to be placed higher on the page
	SuperWidePage=1600;
	DataArea=320; //minimum space for the table with details on the movie page
	SeasonCoverSpace=30;
	SeasonTitleSize=10;
	FullTabsScreenWidth=800;
	WhiteAreaBorderColor='#d8d8d8';
	
	//font sizes
	DefaultBasicFontSize=11;
	ListModifier=-1;
	NormalModifier=1;
	BiggerModifier=2;
	SubHeaderModifier=4;
	SeasonHeaderModifier=6;
	PageHeaderModifier=9;
	DefaultLineSpacing=2;
	LineSpacings=[1.0, 1.5, 2.0, 2.5, 3.0];
	
	CellColor='#ececec';
	//folder names for thumbs
	ThumbsMoviePhotos='ThumbsBreezeMoviePhotos';
	ThumbsHomeMovies='ThumbsBreezeHomeMovies';
	ThumbsPersonMovies='ThumbsBreezePersonMovies';
	Thumbs = 'ThumbsBreeze';
	Thumbs2 = 'ThumbsBreeze2';
	ThumbsCovers = 'ThumbsBreezeCovers';
	ThumbsMainPhoto = 'ThumbsBreezeMainPhoto';
	ThumbsPhotos = 'ThumbsBreezePhotos';
	ThumbsScreenshots = 'ThumbsBreezeScreenshots';
	ThumbsHomePhotos = 'ThumbsBreezeHomePhotos';
	ThumbsHomeShots = 'ThumbsBreezeHomeShots';
	ThumbsEpisodeCovers = 'ThumbsBreezeEpisodes';
	ShowHomeLogo = False;

  MY_RATING_REFERENCE = 100;

var
  // 22.04.2011 - Fade variables for show images in some rows (multi show)
  MultiShowRowHeight: Integer;
  // width of the image that will be displayed (with of the column will be MultiShowImageWidth + 2*MiltiShowDistanceBetweenCovers)
  MultiShowImageWidth: Integer;
  // distance between images on the show area
  MiltiShowDistanceBetweenCovers: Integer;

  CountedPages: integer;
	genre, country, movie, person, title, year, studio, ProgramReference: string;
	InCollection, StandardCoverWidth: integer;
	GroupOperation: boolean;
	ImgPath, WhiteBarFile, BackgroundMovies, BackgroundPeople, BackgroundImg: String;
	TempReferenceCode, TempReference, ActiveTab, PersonActiveTab, ProcessedRecords: integer;
	TempText, UnseenEpisodes: string;
	HomeMovieTab, HomePersonTab: Integer;
	BasicFontSize, ListFontSize, SingleViewDeltaY: Integer;
	RemainingTabs: string;
	
function GetProperPath(s: String): string;
begin
	s := ReplaceStr(s, '(', '%28');
	s := ReplaceStr(s, ')', '%29');
	s := ReplaceStr(s, ' ', '%20');
	result := s;
end;
	
function GetPatternTop: String;
var
	s: String;
begin
	s := GetProperPath(ImgPath + 'background.gif');
	Result := '<div style="background-image: url('+ s + Format('); font-size: %dpt">', [RegReadInteger('BreezeFontSize', DefaultBasicFontSize)]);
end;

function ClosePatternTop: String;
begin
	result:= '<div style="clear:both">';
end;  

function GetReferenceTop: String;
var
	s: String;
begin
	s := GetProperPath(ImgPath + 'background.gif');
	Result := '<div style="background-image: url('+ s +  Format('); font-size: %dpt">', [BasicFontSize]);
end;

function GetPatternBottom: String;
begin
	Result := '</div>';
end;

function GetHomeTabsTop: String;
begin
	Result := '<table style="border-collapse: collapse; width: 100%;"><tr><td><div style="margin-left: 10px; margin-right: 10px; margin-top: 5px; background: url('+ GetProperPath(ImgPath + 'inactive-tab-button.gif') + ') repeat-x left bottom;">';
end;

function GetMoviePersonTab(icon: String): String;
begin
	Result := '<div style="border-left: 1px solid #0a78a9; border-right: 1px solid #0a78a9; float: left; height: 51px; background-image: url(' + GetProperPath(ImgPath + 'movies-persons-tab.gif') + '); color: white; line-height: 51px; vertical-align: middle; font-size: ' + Format('%dpt;', [BasicFontSize + SubHeaderModifier]) + ' font-weight: bold; padding-right: 15px;"><img style="margin: 0 10 0 15;" src="' + GetProperPath(ImgPath + icon) + '">';
end;

function CloseHomeTabsTop: String;
begin
	Result := '</div></td></tr></table>';
end;

//make sure that each word of Name is no longer than MaxLength
function TruncateName(Name: String; MaxLength: Integer): String;
var
	i: Integer;
	s: String;
begin
	Result := '';
	i := Pos(' ', Name);
	while i>0 do begin
		s := copy(Name, 1, i-1);
		if Length(s) > MaxLength then begin
			delete(s, MaxLength - 1, Length(s) - MaxLength + 2);
			s := s + '.';
		end;
		Result := Result + s + ' ';
		delete(Name, 1, i);
		i := Pos(' ', Name);
	end;
	if Length(name) > MaxLength then begin
		delete(name, MaxLength - 1, Length(name) - MaxLength + 2);
		name := name + '.';
	end;
	Result := Result + name;
end;

function GetAge(datenow, birthdate: string): string;
var
  YearsBetweenDate: Integer;
begin
	result := '???';
  YearsBetweenDate := YearsBetween(dateNow, birthdate);
  if (YearsBetweenDate) then
    Result := IntToStr(YearsBetweenDate);
  
	{if (length(datenow) = 10) and (length(birthdate) = 10) then begin
		imanyear := StrToInt(copy(birthdate, 7, 4));
		inowyear := StrToInt(copy(datenow, 7, 4));
		iage := inowyear - imanyear;

		month := copy(birthdate, 1, 2);
		day := copy(birthdate, 4, 2);
		imonthdaybirth := StrToInt(month + day);
		month := copy(datenow, 1, 2);
		day := copy(datenow, 4, 2);
		imonthdaynow := StrToInt(month + day);
//ShowMessage(IntToStr(imonthdaybirth) + ' - ' + IntToStr(imonthdaynow));
		if imonthdaybirth > imonthdaynow then
			iage := iage - 1;

		result := IntToStr(iage);

		if iage > 111 then
			result := result + '(wtf?)';

	end;}
end;

function RemoveUnicodeCharacters(txt: string): string;
var
	i, txtlen, iPos, iLen, iHasDigit, iHasDotComma: integer;
	oneletter: string;
begin
	iPos := Pos('&#', txt);
//ShowMessage('has unicode = ' + IntToStr(iPos));
	while iPos > 0 do begin
		iHasDigit := 0;
		iHasDotComma := 0;
		iLen := 2;
		txtlen := length(txt);
		for i := (iPos + 2) to txtlen do begin
			oneletter := copy(txt, i, 1);
			if ((CompareText('0', oneletter) <= 0) and (CompareText('9', oneletter) >= 0)) then begin
				iHasDigit := 1;
				iLen := iLen + 1;
//ShowMessage('has digit = ' + oneletter);
			end
			else if oneletter = ';' then begin
				iHasDotComma := 1;
				iLen := iLen + 1;
				i := txtlen + 1;
//ShowMessage('has dotcomma = ' + oneletter);
			end
			else begin
				i := txtlen + 1;
			end;
		end;

		if (iHasDigit = 1) and (iHasDotComma = 1) then begin
			Delete(txt, iPos, iLen);
			Insert('%', txt, iPos);
//ShowMessage(txt);
		end;

		iPos := Pos('&#', txt);
	end;

	result := txt;
end;

function DateIsInThePast(some_date: String): Boolean;
var
	ddd1, ddd2, mmm1, mmm2, yyy1, yyy2: Integer;
	day, month, year: String;
begin
	if (some_date <> '') then begin
		DecodeDate(Date, yyy1, mmm1, ddd1);
		day := copy(some_date, 4, 2);
		month := copy(some_date, 1, 2);
		year := copy(some_date, 7, 4);
		try
			ddd2 := StrToInt(day);
			mmm2 := StrToInt(month);
			yyy2 := StrToInt(year);
		except
			result := False;
			exit;
		end;
		if (ddd2 - ddd1) < 0 then
			mmm2 := mmm2 - 1;
		if (mmm2 - mmm1) < 0 then
			yyy2 := yyy2 - 1;
		if (yyy2 - yyy1) < 0 then
			result := True
		else
			result := False;
	end
	else
		result := False;
end;

function GetTodaysDate: String;
var
	ddd1, mmm1, yyy1: Integer;
begin
	DecodeDate(Date, yyy1, mmm1, ddd1);
	Result := Format('-%.2d-%.2d', [mmm1, ddd1]);
end;
function MyPos(ssearch, shost: string; start: integer): integer;
var
	stemp: string;
	newpos: integer;
begin
	stemp:=copy(shost,start,length(shost)-start+1);
	newpos:=pos(ssearch, stemp);
	if (newpos>0) then newpos:=newpos+start-1;
	result:=newpos;
end;

//remove not closed html tags
function RemoveUnclosedTags(text: string): string;
var
	i, j, k: Integer;
begin
	i := Pos('<', text);
	if i>0 then
		k := i - 1
	else
		k := 0;
	while i > k do begin
		j := MyPos('>', text, i+1);
		if (j<i) then begin
			delete(text, i, length(text)-i+1);
			j := i;
		end;
		k := j;
		i := MyPos('<', text, k + 1);
	end;
	result := text;
end;

function ReplaceStr(SrcStr, SrcVal, DstVal: string): string;
var
	DstStr : string;
	i: integer;
begin
	DstStr:=SrcStr;
	i:=MyPos(SrcVal, SrcStr, 1);
	while (i>0) do begin
		delete(DstStr,i,length(SrcVal));
		insert(DstVal,DstStr,i);
		i:=MyPos(SrcVal, DstStr, i+length(DstVal));
	end;
	result:=DstStr;
end;

function GetStr(str: String; s1: String; s2: String): String;
var
  I: Integer;
  J: Integer;
begin
  // Вырезает кусок строки между s1 и s2
	Result := '';
	I := Pos(LowerCase(s1), LowerCase(str));
	if I > 0 then
	begin
		J := MyPos(LowerCase(s2), LowerCase(str), I + Length(s1));
		if J > 0 then
  		Result := Copy(str, I + Length(s1), J - I - Length(s1));
	end;
end;

//gets the image filename as it is stored in the database
//returns the full path to the image file or to the image thumb
//depending on settings
function GetImageFileName(ImageFile, ThumbsDir, DefaultCover: String; ImageWidth, ImageHeight: Integer; CutThumb: Boolean=False): String;
var
	Filename, ThumbsPath, ThumbsFileName: String;
begin
	Filename := ImagesPath + ImageFile;
	if not MyFileExists(Filename) then
		Filename:=ImgPath+DefaultCover;
	Result := Filename;
	if (RegReadInteger('UseThumbs', 1) = 1) or CutThumb then begin
		ThumbsPath := ImagesPath + ThumbsDir + '\';
		ThumbsFileName:=ThumbsPath+ImageFile;
		if GetFileDateTime(ThumbsFileName)<GetFileDateTime(FileName) then
			DeleteFile(ThumbsFileName);
		if not MyFileExists(ThumbsFileName) then
			ConvertThumb3(FileName, ThumbsFileName, ThumbsDir, ImageWidth, ImageHeight, CutThumb);
		if MyFileExists(ThumbsFileName) then
			result := ThumbsFileName;
	end;
end;


begin

end.
