program jef_program_variables;

const

	DefaultMultiCoverWidth=90;
	DefaultMultiCoverHeight=120;
	DefaultMultiCellWidth=100;
	DefaultMultiCellHeight=165;
	
	//folder names for thumbs
	Thumbs              = 'ThumbsJeF';
	Thumbs2             = 'ThumbsJeF2';
	ThumbsCovers        = 'ThumbsJeFCovers';
	ThumbsCoversList    = 'ThumbsJeFCoversList';
	ThumbsEpisodeCovers = 'ThumbsJeFEpisodes';
	ThumbsMoviePhotos   = 'ThumbsJeFMoviePhotos';
	ThumbsHomeMovies    = 'ThumbsJeFHomeMovies';
	ThumbsMainPhoto     = 'ThumbsJeFMainPhoto';
	ThumbsPhotos        = 'ThumbsJeFPhotos';
	ThumbsPhotosList    = 'ThumbsJeFPhotosList';
	ThumbsHomePhotos    = 'ThumbsJeFHomePhotos';
	ThumbsPersonMovies  = 'ThumbsJeFPersonMovies';
	ThumbsScreenshots   = 'ThumbsJeFScreenshots';

var
	genre, country, movie, person, title, year, studio, ProgramReference: string;
	InCollection: integer;
	GroupOperation: boolean;
	
	// 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;

	jLongMonthNames: array [1..12] of string;
	jLongDayNames: array [1..7] of string;
	jInitialize, jInCollection, ThemeActive: boolean;
	jThemesList, jLastPage, jMainNavBar: string;
	jMoviesHomeTag, jPersonsHomeTag, jHomePageCode : string;
	jOriginalThemePath, jOriginalPictPath, jThmPath, jThmPictPath, jThmCustomPath, jThmCssList, jEpRootDir: string;
	jHomeCard, jMovieCard, jPersonCard, jListCard, jThumbjMovieCard, jThumbjMovie3Card, jThumbjPersonCard, jThumbjPerson3Card: TStringList;	// 20090830-JeF
	jXFvars, jSqlStrings, jBuildPage, jFileList, jRscPath, jDialogs: TStringList;
	jScriptOptions, jThemes, jThemeOptions, jThemeOptText: TStringList;
	jSearchBuild: boolean;
	jSearchType, jRedir: string;
	ScriptName: String;
	ScriptVersion: Integer;
  
// 20090828-JeF
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;

// 20090703-JeF
function MyPos(pattern, SrcStr: string; StartPos: integer): integer;
var
	StrTemp: string;
	NewPos: integer;
begin
	StrTemp:=copy(SrcStr,StartPos,length(SrcStr)-StartPos+1);
	NewPos:=pos(pattern, StrTemp);
	if (NewPos>0) then NewPos:=NewPos+StartPos-1;
	result:=NewPos;
end;

// Clear extra simple or double Quotes Tool // 20090731-JeF
function ClearExtraSDQuotes(name: string): string;
begin
	if length(name)>0 then
		if (name[1]='"') and (name[length(name)]='"')
		or (name[1]='''') and (name[length(name)]='''') then
			name := copy(name,2,length(name)-2);
	Result := name;
end;

// 20090703-JeF
function ClearExtraBlankCharacters(text: string): string;
begin
	// Clear all extra blank charackters
	while (length(text) > 0) and (ord(text[1]) in [1..20, 32]) do
		delete(text, 1, 1);
	while (length(text) > 0) and (ord(text[length(text)]) in [1..20, 32]) do
		delete(text, length(text), 1);
	Result := text;
end;

// 20090703-JeF
function GetFlagTag(Country: string): string;
var
	FlagTag, FlagName, ImgFileName: string;
begin
	FlagTag:='';
	FlagName:=Country;
	
	ImgFileName:=GetRscPath('flags\' +FlagName+ '.gif');
	if NOT (MyFileExists(ImgFileName)) then begin
		if (pos(FlagName,'Allemagne de l''Ouest')>0) then FlagName:='Germany'
		else if (pos(FlagName,'Grande-Bretagne,Angleterre')>0) then FlagName:='uk'
		else if (pos(Uppercase(FlagName),UpperCase('Etats-Unis'))>0) then FlagName:='usa'
		else if (pos(FlagName,'Italie')>0) then FlagName:='Italy'
		else if (pos(FlagName,'Japon')>0) then FlagName:='Japan';
	end;
	
	ImgFileName:=GetRscPath('flags\' +FlagName+ '.gif');
	if(MyFileExists(ImgFileName)) then
		FlagTag:=format('<img style="vertical-align:middle;" src="%s" height=17 border=0 alt="%s" />', [ImgFileName, Country]);
	result:=FlagTag;
end;

function GetPlayTag(VideoFilePath: string): string;
var
	PlayTag, ImgFileName: string;
begin
	ImgFileName:=GetRscPath('Play.png');
	PlayTag:='<a href="link:file://'+ClearExtraSDQuotes(VideoFilePath)+'"><img style="vertical-align:middle" src="'
			+ImgFileName+'" border=0 alt="'+ClearExtraSDQuotes(VideoFilePath)+'"></a> ';
	result:=PlayTag;
end;

// Boolean/String Conversion Tool // 20090731-JeF
function StrToBool(BoolStr: string): boolean;
var
	str: string;
begin
	str:=ClearExtraBlankCharacters(BoolStr);
	if (pos(uppercase(str),'TRUE,YES')>0) or (StrToIntDef(str,-1)>0) 
		then result:=true else result:=false;
end;
function BoolToStr(Bool: boolean): string;
begin
	if (Bool) then result:='True' else result:='False';
end;

function InList(Item, List: string): boolean;
begin
	result:=(pos(','+Item+',',','+List+',')>0);
end;

function DateTimeStrEval(DateTimeFormat, DateTimeStr : string) : TDateTime;
// Evaluate a date time string into a TDateTime obeying the
// rules of the specified DateTimeFormat string
// eg. DateTimeStrEval('dd-MMM-yyyy hh:nn','23-May-2002 12:34)
//
// Supports DateTimeFormat Specifiers
//
// dd    the day as a number with a leading zero or space (01-31).
// ddd the day as an abbreviation (Sun-Sat)
// dddd the day as a full name (Sunday-Saturday)
// mm    the month as a number with a leading zero or space (01-12).
// mmm the month as an abbreviation (Jan-Dec)
// mmmm the month as a full name (January-December)
// yy    the year as a two-digit number (00-99).
// yyyy the year as a four-digit number (0000-9999).
// hh    the hour with a leading zero or space (00-23)
// nn    the minute with a leading zero or space (00-59).
// ss    the second with a leading zero or space (00-59).
// zzz the millisecond with a leading zero (000-999).
// ampm  Specifies am or pm flag hours (AM/PM)
// ap    Specifies a or p flag hours (A/P)
var
	i,ii,iii : integer;
    Specs: TStringList;
	Fmt,Data,Spec : string;
    Year,Month,Day,Hour,Minute,Second,MSec : word;
    AmPm : integer;
begin
	Specs:=TStringList.Create;
	Specs.CommaText:='DDDD,MMMM,YYYY,AMPM,DDD,MMM,DD,MM,YY,HH,NN,SS,ZZZ,AP';
	Year := 1;
	Month := 1;
	Day := 1;
	Hour := 0;
	Minute := 0;
	Second := 0;
	MSec := 0;
	Fmt := UpperCase(DateTimeFormat);
	Data := UpperCase(DateTimeStr);
	AmPm := 0;

	for i:=1 to Specs.Count do begin
		ii:=pos(Specs[i-1],Fmt);
		if (ii=0) then continue;
		if (Specs[i-1]='DDDD') then begin
			delete(Fmt,ii,4);
			for ii:=1 to 7 do begin
				iii:=pos(Uppercase(jLongDayNames[ii]),Data);
				if (iii>0) then begin
					Day:=ii;
					delete(Data,iii,length(jLongDayNames[ii]));
					break;
				end;
			end;
		end
		else if (Specs[i-1]='DDD') then begin
			delete(Fmt,ii,3);
			for ii:=1 to 7 do begin
				iii:=pos(Copy(Uppercase(jLongDayNames[ii]),1,3),Data);
				if (iii>0) then begin
					Day:=ii;
					delete(Data,iii,3);
					break;
				end;
			end;
		end
		else if (Specs[i-1]='MMMM') then begin
			delete(Fmt,ii,4);
			for ii:=1 to 12 do begin
				iii:=pos(Uppercase(jLongMonthNames[ii]),Data);
				if (iii>0) then begin
					Month:=ii;
					delete(Data,iii,length(jLongMonthNames[ii]));
					break;
				end;
			end;
		end
		else if (Specs[i-1]='MMM') then begin
			delete(Fmt,ii,3);
			for ii:=1 to 12 do begin
				iii:=pos(copy(Uppercase(jLongMonthNames[ii]),1,3),Data);
				if (iii>0) then begin
					Month:=ii;
					delete(Data,iii,3);
					break;
				end;
			end;
		end
		else if (Specs[i-1]='YYYY') then begin
			delete(Fmt,ii,4);
			Year:=StrToIntDef(copy(Data,ii,4),0);
			delete(Data,ii,4);
		end
		else if (Specs[i-1]='AMPM') then begin
			delete(Fmt,ii,4);
			if (UpperCase(copy(Data,ii,2))='PM') then AmPm := 1 else AmPm := -1;
			delete(Data,ii,2);
		end
		else if (Specs[i-1]='AP') then begin
			if (UpperCase(copy(Data,ii,1))='P') then AmPm := 1 else AmPm := -1;
		end
		else begin
			iii:=StrToIntDef(copy(Data,ii,length(Specs[i-1])),0);
			if (Specs[i-1]='DD') then Day:=iii
			else if (Specs[i-1]='MM') then Month:=iii
			else if (Specs[i-1]='YY') then Year:=iii
			else if (Specs[i-1]='HH') then Hour:=iii
			else if (Specs[i-1]='NN') then Minute:=iii
			else if (Specs[i-1]='SS') then Second:=iii
			else if (Specs[i-1]='ZZZ') then MSec:=iii;
		end;
	end;
	Specs.Free;
	if AmPm = 1 then Hour := Hour + 12;
	result:=EncodeDateTimeDef(Year,Month,Day,Hour,Minute,Second,MSec,0.0);
end;


function StrToIntDef (strval : string; dftval : integer) : integer;
begin
	try
		result:=StrToInt(strval);
	except 
		result:=dftval;
	end;
end;

function EncodeDateTimeDef(const AYear,AMonth,ADay,AHour,AMinute,ASecond,AMilliSecond : word;
                           dftval : TDateTime) : TDateTime;
begin
  Try
	Result := EncodeDate(AYear, AMonth, ADay);
  except
	Result:=dftval;
  end;
  Try
	Result := Result + EncodeTime(AHour, AMinute, ASecond, AMilliSecond); 
  except
	Result:=dftval;
  end;
end;

function StrToDateTimeDef(SrcDate: string; dftval: TDateTime): TDateTime;
begin
  Try
	Result := StrToDateTime(SrcDate);
  except
	Result:=dftval;
  end;
end;

function FormatDate(SrcDate: string): string;
var
	NewDate: string;
begin
	NewDate:=SrcDate;
	if (ProgramLanguage='French') then NewDate:=ReplaceStr(NewDate,'.','/');
	if (length(SrcDate)<10) then begin
		if (NewDate[2]<'0') or (NewDate[2]>'9') then NewDate:='0'+NewDate;
		if (NewDate[5]<'0') or (NewDate[5]>'9') then insert('0',NewDate,4);
	end;
	result:=NewDate;
end;

function GetStrOption(OptName: string): string;
begin
	result:=ClearExtraSDQuotes(RegReadString('JeF_'+OptName,''));
end;
procedure SetStrOption(OptName, StrVal: string);
begin
	jScriptOptions.Values[OptName]:=StrVal;
	RegWriteString('JeF_'+OptName,StrVal);
end;

function GetIntOption(OptName: string): integer;
begin
	result:=-1;
	Try
		result:=StrToInt(RegReadString('JeF_'+OptName,'0'));
	except
	end;
end;
procedure SetIntOption(OptName: string; IntVal: integer);
begin
	jScriptOptions.Values[OptName]:=IntToStr(IntVal);
	RegWriteString('JeF_'+OptName,IntToStr(IntVal));
end;

function GetBoolOption(OptName: string): boolean;
begin
	result:=False;
	result:=StrToBool(RegReadString('JeF_'+OptName,'False'));
end;
procedure SetBoolOption(OptName: string; BoolVal: boolean);
begin
	if BoolVal then RegWriteString('JeF_'+OptName,'1')
	else RegWriteString('JeF_'+OptName,'0');
	jScriptOptions.Values[OptName]:=GetStrOption(OptName);
end;

function GetStrThmOption(OptName: string): string;
begin
	result:=ClearExtraSDQuotes(jThemeOptions.Values[OptName]);
end;

// 20090910-JeF
function GetIntThmOption(OptName: string): integer;
begin
	result:=-1;
	Try
		result:=StrToInt(jThemeOptions.Values[OptName]);
	except
	end;
end;

// 20090910-JeF
function GetBoolThmOption(OptName: string): boolean;
begin
	result:=False;
	if (length(jThemeOptions.Values[OptName])>0) 
		then result:=StrToBool(jThemeOptions.Values[OptName]);
end;

{
//20121217
function GetAge(datenow, birthdate: string): integer;
var
  YearsBetweenDate: Integer;
begin
	result := 0;

  YearsBetweenDate := YearsBetween(dateNow, birthdate);
  if (YearsBetweenDate) then
    Result := YearsBetweenDate;
end;
}

function GetAge(Date1, Date2: string): integer;
var
	Age: integer;
	StartDate, EndDate : TDateTime;
begin
	if (Date1<>'') then begin
		if (length(Date1)<=10) 
			then StartDate:=StrToDateTimeDef(FormatDate(Date1),Now)
		else if (length(Date1)=11)
			then StartDate:=DateTimeStrEval('dd-mmm-yyyy',Date1)
		else StartDate:=Now;
	end
	else StartDate:=Now;
	if (Date2<>'') then begin
		if (length(Date2)<=10) 
			then EndDate:=StrToDateTimeDef(FormatDate(Date2),Now)
		else if (length(Date2)=11) 
			then EndDate:=DateTimeStrEval('dd-mmm-yyyy',Date2)
		else EndDate:=Now;
	end
	else EndDate:=Now;
	if (EndDate>=StartDate) then begin
		Age:=YearOf(EndDate)-YearOf(StartDate);
		if (MonthOf(EndDate)*100+DayOf(EndDate)<MonthOf(StartDate)*100+DayOf(StartDate)) 
			then Age:=Age-1;
	end
	else Age:=0;
	result:=Age;
end;


function XFvarName(XFid:integer; VarName: string): string;
var jXFvn: string;
begin
	jXFvn:=IntToStr(XFid);
	while (length(jXFvn)<4) do jXFvn:='0'+jXFvn;
	jXFvn:=jXFvn+'_'+VarName;
	if (jXFvars.IndexOfName(jXFvn)=-1) then jXFvars.add(jXFvn+'=');
	result:=jXFvn;
end;

function GetStrXFvar(XFid:integer; VarName: string): string;
var jXFvn: string;
begin
	jXFvn:=XFvarName(XFid,VarName);
	result:=ClearExtraSDQuotes(jXFvars.Values[jXFvn]);
end;

function GetIntXFvar(XFid:integer; VarName: string): integer;
var jXFvn: string;
begin
	result:=-1;
	jXFvn:=XFvarName(XFid,VarName);
	Try
		result:=StrToInt(jXFvars.Values[jXFvn]);
	except
	end;
end;

function GetBoolXFvar(XFid:integer; VarName: string): boolean;
var jXFvn: string;
begin
	jXFvn:=XFvarName(XFid,VarName);
	result:=StrToBool(jXFvars.Values[jXFvn]);
end;

procedure SetStrXFvar(XFid:integer; VarName, Val: string);
var jXFvn: string;
begin
	jXFvn:=XFvarName(XFid,VarName);
	jXFvars.Values[jXFvn]:=Val;
end;

procedure SetIntXFvar(XFid:integer; VarName: string; Val: integer);
var jXFvn: string;
begin
	jXFvn:=XFvarName(XFid,VarName);
	jXFvars.Values[jXFvn]:=IntToStr(Val);
end;

procedure SetBoolXFvar(XFid:integer; VarName: string; Val:boolean);
var jXFvn: string;
begin
	jXFvn:=XFvarName(XFid,VarName);
	jXFvars.Values[jXFvn]:=BoolToStr(Val);
end;

function GetRscPath(RscName: string): string;
begin
	if (jRscPath.IndexOfName(RscName)<>-1) 
		then result:=jRscPath.Values[RscName]
	else if MyFileExists(jThmCustomPath+RscName) then begin
		jRscPath.Values[RscName]:=jThmCustomPath+RscName;
		result:=jRscPath.Values[RscName];
	end
	else if MyFileExists(jThmPictPath+RscName) then begin
		jRscPath.Values[RscName]:=jThmPictPath+RscName;
		result:=jRscPath.Values[RscName];
	end
	else if MyFileExists(PicturesPath+'Themes\Original\img\'+RscName) then begin
		jRscPath.Values[RscName]:=jOriginalPictPath+RscName;
		result:=jRscPath.Values[RscName];
	end
	else if MyFileExists(PicturesPath+RscName) then begin
		jRscPath.Values[RscName]:=PicturesPath+RscName;
		result:=jRscPath.Values[RscName];
	end
	else result:='NotFound.png';
end;

begin

end.