program jef_program_options;

uses 'jef_program_variables.pas';

procedure ShowOptionsDialog;
var
	i: integer;
	DlgRetVal: string;
	jReloadTheme: boolean;
begin
	LoadDialogs;  // Reset Dialogs
	jReloadTheme:=False;
	DlgRetVal:='General';
	while not InList(UpperCase(DlgRetVal),'OK,CANCEL,ERR,WINCLOSE') 
		do DlgRetVal:=ShowDialog('JeF_Options_'+DlgRetVal);
	if (UpperCase(DlgRetVal)='OK') then begin
		for i:=0 to jScriptOptions.Count-1 do begin
			if (jDialogs.IndexOfName(jScriptOptions.Names[i]+'_value')<>-1) then begin
				if (jDialogs.Values[jScriptOptions.Names[i]+'_value']<>jScriptOptions.Values[jScriptOptions.Names[i]]) then begin
					if (jScriptOptions.Names[i]='Theme') and not InList(jDialogs.Values['Theme_value'],jThemes.CommaText) then continue;
					jScriptOptions.Values[jScriptOptions.Names[i]]:=jDialogs.Values[jScriptOptions.Names[i]+'_value'];
					SetStrOption(jScriptOptions.Names[i],jScriptOptions.Values[jScriptOptions.Names[i]]);
					if (jScriptOptions.Names[i]='Theme') then jReloadTheme:=True;
				end;
			end;
		end;
	end;
	if jReloadTheme then begin
		SetTheme(GetStrOption('Theme'));
		BuildScriptSettingsPage;
	end;
end;

procedure ShowDwnScrOptDialog(site: string);
var
	i, j: integer;
	jPath, DlgRetVal, jDlgName, s: string;
	jSaveOptions, jReloadTheme: boolean;
	jMovieOptions, jMovieOptDesc, jPersonOptions, jPersonOptDesc: TStringList;
begin
	jMovieOptions:=TStringList.Create;
	jMovieOptDesc:=TStringList.Create;
	jPersonOptions:=TStringList.Create;
	jPersonOptDesc:=TStringList.Create;
	jPath:=ScriptsPath+site+'_movie_options.ini';
	if MyFileExists(jPath) then jMovieOptions.LoadFromFile(jPath);
	jPath:=ScriptsPath+site+'_person_options.ini';
	if MyFileExists(jPath) then jPersonOptions.LoadFromFile(jPath);
	jDlgName:=jDialogs.Values['['+site+'_movie]'];
	for i:=0 to jMovieOptions.Count-1 do begin
		j:=pos(';//',jMovieOptions.Values[jMovieOptions.Names[i]]);
		jMovieOptDesc.Add(jMovieOptions.Names[i]+'= ;//');
		if (j>0) then begin
			jMovieOptDesc.Values[jMovieOptions.Names[i]]:=copy(jMovieOptions.Values[jMovieOptions.Names[i]],j,length(jMovieOptions.Values[jMovieOptions.Names[i]])-j+1);
			s:=ClearExtraSDQuotes(ClearExtraBlankCharacters(copy(jMovieOptions.Values[jMovieOptions.Names[i]],1,j-1)));
			if (s='') then s:='''''';
			jMovieOptions.Values[jMovieOptions.Names[i]]:=s;
		end;
		if (jDialogs.IndexOfName(jDlgName+'_'+jMovieOptions.Names[i]+'_value')<>-1)
			then jDialogs.Values[jDlgName+'_'+jMovieOptions.Names[i]+'_value']:=jMovieOptions.Values[jMovieOptions.Names[i]];
	end;
	jDlgName:=jDialogs.Values['['+site+'_person]'];
	for i:=0 to jPersonOptions.Count-1 do begin
		j:=pos(';//',jPersonOptions.Values[jPersonOptions.Names[i]]);
		jPersonOptDesc.Add(jPersonOptions.Names[i]+'= ;//');
		if (j>0) then begin
			jPersonOptDesc.Values[jPersonOptions.Names[i]]:=copy(jPersonOptions.Values[jPersonOptions.Names[i]],j,length(jPersonOptions.Values[jPersonOptions.Names[i]])-j+1);
			s:=ClearExtraSDQuotes(ClearExtraBlankCharacters(copy(jPersonOptions.Values[jPersonOptions.Names[i]],1,j-1)));
			if (s='') then s:='''''';
			jPersonOptions.Values[jPersonOptions.Names[i]]:=s;
		end;
		if (jDialogs.IndexOfName(jDlgName+'_'+jPersonOptions.Names[i]+'_value')<>-1)
			then jDialogs.Values[jDlgName+'_'+jPersonOptions.Names[i]+'_value']:=jPersonOptions.Values[jPersonOptions.Names[i]];
	end;
	jSaveOptions:=False;
	DlgRetVal:='movie';
	while not InList(UpperCase(DlgRetVal),'OK,CANCEL,ERR,WINCLOSE') 
		do DlgRetVal:=ShowDialog(site+'_'+DlgRetVal);
	if (UpperCase(DlgRetVal)='OK') then begin
		jSaveOptions:=False;
		jDlgName:=jDialogs.Values['['+site+'_movie]'];
		for i:=0 to jMovieOptions.Count-1 do begin
			if (jDialogs.IndexOfName(jDlgName+'_'+jMovieOptions.Names[i]+'_value')<>-1) then begin
				if (jDialogs.Values[jDlgName+'_'+jMovieOptions.Names[i]+'_value']<>jMovieOptions.Values[jMovieOptions.Names[i]]) then begin
					jMovieOptions.Values[jMovieOptions.Names[i]]:=jDialogs.Values[jDlgName+'_'+jMovieOptions.Names[i]+'_value'];
					jSaveOptions:=True;
				end;
			end;
			jMovieOptions.Values[jMovieOptions.Names[i]]:=jMovieOptions.Values[jMovieOptions.Names[i]]+'   '+jMovieOptDesc.Values[jMovieOptions.Names[i]];
		end;
		if jSaveOptions then jMovieOptions.SaveToFile(ScriptsPath+site+'_movie_options.ini');
		jSaveOptions:=False;
		jDlgName:=jDialogs.Values['['+site+'_person]'];
		for i:=0 to jPersonOptions.Count-1 do begin
			if (jDialogs.IndexOfName(jDlgName+'_'+jPersonOptions.Names[i]+'_value')<>-1) then begin
				if (jDialogs.Values[jDlgName+'_'+jPersonOptions.Names[i]+'_value']<>jPersonOptions.Values[jPersonOptions.Names[i]]) then begin
					jPersonOptions.Values[jPersonOptions.Names[i]]:=jDialogs.Values[jDlgName+'_'+jPersonOptions.Names[i]+'_value'];
					jSaveOptions:=True;
				end;
			end;
			jPersonOptions.Values[jPersonOptions.Names[i]]:=jPersonOptions.Values[jPersonOptions.Names[i]]+'   '+jPersonOptDesc.Values[jPersonOptions.Names[i]];
		end;
		if jSaveOptions then jPersonOptions.SaveToFile(ScriptsPath+site+'_person_options.ini');
	end;
	jMovieOptions.Free;
	jMovieOptDesc.Free;
	jPersonOptions.Free;
	jPersonOptDesc.Free;
end;

function ShowDialog(DlgId: string):string;
var
	ObjPosX, ObjPosY, ObjWidth, ObjHeight, i, j: integer;
	ObjName, ObjType, ObjCaption, ObjValue, ObjHint, BtnPressed: string;
	ObjParent: TWinControl;
	UpdOptions: boolean;
	DlgDef: TStringList;
	Frm: TForm;
	Btn: TButton;
	Lbl: TLabel;
	Edt: TEdit;
	Mmo: TMemo;
	Chk: TCheckBox;
	Rad: TRadioButton;
	Gpb: TGroupBox;
	Cbx: TComboBox;
	Lbx: TListBox;
	Comp, sl, sl2: TStringList;

procedure ButtonClick(Sender: TButton);
var
	zn: integer;
begin
	BtnPressed:=ReplaceStr(Sender.Name,'_Btn','');
	BtnPressed:=ReplaceStr(BtnPressed,Frm.Name+'_','');
	ExportValues;
	jDialogs.Values[Sender.Name+'_value']:='True';
	Frm.ModalResult := mrOk;
end;

Procedure ExportValues;
Var
	i, j: integer;
	CmpName, CmpUsrType, CmpValue: string;
	sl: TStringList;
begin
	sl:=TStringList.Create;
	for i:=0 to Comp.Count-1 do begin

		CmpName:=Comp.Names[i];
		CmpUsrType:=Comp.Values[Comp.Names[i]];
		CmpValue:='';
		if Comp.Objects[i] is TEdit 
			then CmpValue:=TEdit(Comp.Objects[i]).Text
		else if Comp.Objects[i] is TComboBox then begin
			if (pos('-IDX',UpperCase(CmpUsrType))>0) 
				then CmpValue:=IntToStr(TComboBox(Comp.Objects[i]).ItemIndex)
			else if (DlgDef.IndexOfName(CmpName+'_extvalues')<>-1) then begin
				sl.CommaText:=GetExprValue(DlgDef.Values[CmpName+'_extvalues']);
				j:=TComboBox(Comp.Objects[i]).ItemIndex;
				if (j<sl.count) then CmpValue:=sl[j]
				else CmpValue:=sl[0];
			end
			else CmpValue:=TComboBox(Comp.Objects[i]).Text;
		end
		else if Comp.Objects[i] is TListBox then begin
			sl.Clear;
			if (DlgDef.IndexOfName(CmpName+'_extvalues')<>-1) 
				then sl.CommaText:=GetExprValue(DlgDef.Values[CmpName+'_extvalues']);
			for j:=0 to TListBox(Comp.Objects[i]).Items.Count-1 do begin
				if TListBox(Comp.Objects[i]).Selected[j] then begin
					if (pos('-IDX',UpperCase(CmpUsrType))>0) 
						then CmpValue:=CmpValue+IntToStr(j)+','
					else if (DlgDef.IndexOfName(CmpName+'_extvalues')<>-1) then begin
						if (j<sl.count) then CmpValue:=CmpValue+sl[j]+','
						else CmpValue:=CmpValue+sl[0]+',';
					end
					else CmpValue:=CmpValue+TListBox(Comp.Objects[i]).Items[j]+',';
				end;
			end;
			if (CmpValue>'') then delete(CmpValue,length(CmpValue),1);
		end
		else if Comp.Objects[i] is TCheckBox 
			then CmpValue:=BoolToStr(TCheckBox(Comp.Objects[i]).Checked)
		else if Comp.Objects[i] is TRadioButton 
			then CmpValue:=BoolToStr(TRadioButton(Comp.Objects[i]).Checked)
		else if Comp.Objects[i] is TButton 
			then CmpValue:='False'
		else CmpValue:='?';

		if (CmpValue='') then CmpValue:='''''';
		jDialogs.Values[CmpName+'_value']:=CmpValue;
		
	end;
	sl.Free;
end;

begin
	result:='Err';
	if (jDialogs.Count=0) or (jDialogs.IndexOfName('['+DlgId+']')=-1)
		then exit;
	i:=jDialogs.IndexOfName('['+DlgId+']')+1;
	if (i=0) then exit;
	//Extract dialog to process from dialogs definitions
	BtnPressed:='WinClose';
	Comp:=TStringList.Create;
	DlgDef:=TStringList.Create;
	sl:=TStringList.Create;
	sl2:=TStringList.Create;
	while (i<jDialogs.Count) and (pos('[',jDialogs[i])=0) do begin
		DlgDef.Add(jDialogs[i]);
		i:=i+1;
	end;
	//Build dialog
	Frm:=TForm.Create(nil);
	Frm.BorderStyle:=bsDialog;
	Frm.Position:=poScreenCenter;
	Frm.AutoSize:=false;
	Frm.Name:=jDialogs.Values['['+DlgId+']'];
	for i:=0 to DlgDef.Count-1 do begin
		ObjValue:='';
		ObjHint:='';
		ObjPosX:=0;
		ObjPosY:=0;
		ObjWidth:=0;
		ObjHeight:=0;
		if (pos('_name=',DlgDef[i])=0) then continue;
		ObjName:=DlgDef.Values[DlgDef.Names[i]];
		if (DlgDef.IndexOfName(ObjName+'_type')=-1) then continue;
		ObjType:=DlgDef.Values[ObjName+'_type'];
		ObjCaption:=ObjName;
		ObjParent:=Frm;
		if (DlgDef.IndexOfName(ObjName+'_caption')<>-1) 
			then ObjCaption:=GetExprValue(DlgDef.Values[ObjName+'_caption']);
		if (UpperCase(ObjCaption)='*NONE') then ObjCaption:='';
		if (DlgDef.IndexOfName(ObjName+'_hint')<>-1) 
			then ObjHint:=GetExprValue(DlgDef.Values[ObjName+'_hint']);
		if (DlgDef.IndexOfName(ObjName+'_value')<>-1) 
			then ObjValue:=GetExprValue(DlgDef.Values[ObjName+'_value']);
		if (DlgDef.IndexOfName(ObjName+'_parent')<>-1) then begin
			j:=Comp.IndexOfName(DlgDef.Values[ObjName+'_parent']);
			if (j<>-1) then ObjParent:=TWinControl(Comp.Objects[j]);
		end;
		if (DlgDef.IndexOfName(ObjName+'_bounds')<>-1) then begin
			sl.CommaText:=DlgDef.Values[ObjName+'_bounds'];
			if (sl.Count>0) then ObjPosX:=StrToIntDef(sl[0],ObjPosX);
			if (sl.Count>1) then ObjPosY:=StrToIntDef(sl[1],ObjPosY);
			if (sl.Count>2) then ObjWidth:=StrToIntDef(sl[2],ObjWidth);
			if (sl.Count>3) then ObjHeight:=StrToIntDef(sl[3],ObjHeight);
		end;
		if (DlgDef.IndexOfName(ObjName+'_posx')<>-1) 
			then ObjPosX:=StrToIntDef(DlgDef.Values[ObjName+'_posx'],ObjPosX);
		if (DlgDef.IndexOfName(ObjName+'_posy')<>-1) 
			then ObjPosY:=StrToIntDef(DlgDef.Values[ObjName+'_posy'],ObjPosY);
		if (DlgDef.IndexOfName(ObjName+'_width')<>-1) 
			then ObjWidth:=StrToIntDef(DlgDef.Values[ObjName+'_width'],ObjWidth);
		if (DlgDef.IndexOfName(ObjName+'_height')<>-1) 
			then ObjHeight:=StrToIntDef(DlgDef.Values[ObjName+'_height'],ObjHeight);
//showmessage('ObjName:'+ObjName+#13#10+'ObjValue:'+ObjValue);
		if (UpperCase(ObjType)='TFORM') then begin
			Frm.Caption:=ObjCaption;
			Frm.Width:=ObjWidth;
			Frm.Height:=ObjHeight;
		end
		else if (UpperCase(ObjType)='TBUTTON') then begin
			Btn:=TButton.Create(Frm);
			Btn.Name:=ObjName;
			Btn.Parent:=ObjParent;
			Btn.SetBounds(ObjPosX, ObjPosY, ObjWidth, ObjHeight);
			Btn.Caption:=ObjCaption;
			Btn.Hint:=ObjHint;
			Btn.OnClick:=@ButtonClick;
			Comp.AddObject(ObjName+'='+ObjName,Btn);
		end
		else if (pos('TLABEL',UpperCase(ObjType))>0) then begin
			Lbl:=TLabel.Create(Frm);
			Lbl.Name:=ObjName;
			Lbl.Parent:=ObjParent;
			Lbl.SetBounds(ObjPosX, ObjPosY, ObjWidth, ObjHeight);
			Lbl.Caption:=ObjCaption;
			if (ObjValue>'') then Lbl.Caption:=ObjValue;
			if (pos('-RAL',UpperCase(ObjType))>0) then Lbl.Left:=Lbl.Left+ObjWidth-Lbl.Width;
			Lbl.Hint:=ObjHint;
		end
		else if (UpperCase(ObjType)='TEDIT') then begin
			Edt:=TEdit.Create(Frm);
			Edt.Name:=ObjName;
			Edt.Parent:=ObjParent;
			Edt.SetBounds(ObjPosX, ObjPosY, ObjWidth, ObjHeight);
			Edt.Hint:=ObjHint;
			Edt.Text:=ObjValue;
			Edt.Enabled:=(pos('-DIS',UpperCase(ObjType))=0);
			Comp.AddObject(ObjName+'='+UpperCase(ObjType),Edt);
		end
		else if (pos('TMEMO',UpperCase(ObjType))>0) then begin
			Mmo:=TMemo.Create(Frm);
			Mmo.Name:=ObjName;
			Mmo.Parent:=ObjParent;
			Mmo.SetBounds(ObjPosX, ObjPosY, ObjWidth, ObjHeight);
			Mmo.Hint:=ObjHint;
			Mmo.Lines.Text:=ObjValue;
			if (DlgDef.IndexOfName(ObjName+'_maxlength')<>-1) then begin
				j:=StrToIntDef(DlgDef.Values[ObjName+'_maxlength'],-1);
				if (j>=0) then Mmo.MaxLength:=j;
			end;
			if (pos('-RAL',UpperCase(ObjType))>0) then Mmo.Alignment:=taRightJustify;
			Mmo.Enabled:=(pos('-DIS',UpperCase(ObjType))=0);
			Mmo.WantReturns:=(pos('-NOR',UpperCase(ObjType))=0);
			Mmo.WantTabs:=(pos('-NOT',UpperCase(ObjType))=0);
			Comp.AddObject(ObjName+'='+UpperCase(ObjType),Mmo);
		end
		else if (UpperCase(ObjType)='TCHECKBOX') then begin
			Chk:=TCheckBox.Create(Frm);
			Chk.Name:=ObjName;
			Chk.Parent:=ObjParent;
			Chk.SetBounds(ObjPosX, ObjPosY, ObjWidth, ObjHeight);
			Chk.Caption:=ObjCaption;
			Chk.Hint:=ObjHint;
			Chk.Checked:=StrToBool(ObjValue);
			Comp.AddObject(ObjName+'='+UpperCase(ObjType),Chk);
		end
		else if (UpperCase(ObjType)='TRADIOBUTTON') then begin
			Rad:=TRadioButton.Create(Frm);
			Rad.Name:=ObjName;
			Rad.Parent:=ObjParent;
			Rad.SetBounds(ObjPosX, ObjPosY, ObjWidth, ObjHeight);
			Rad.Caption:=ObjCaption;
			Rad.Hint:=ObjHint;
			Rad.Checked:=StrToBool(ObjValue);
			Comp.AddObject(ObjName+'='+UpperCase(ObjType),Rad);
		end
		else if (UpperCase(ObjType)='TGROUPBOX') then begin
			Gpb:=TGroupBox.Create(Frm);
			Gpb.Name:=ObjName;
			Gpb.Parent:=ObjParent;
			Gpb.SetBounds(ObjPosX, ObjPosY, ObjWidth, ObjHeight);
			Gpb.Caption:=ObjCaption;
			Gpb.Hint:=ObjHint;
			Comp.AddObject(ObjName+'='+UpperCase(ObjType),Gpb);
		end
		else if (pos('TCOMBOBOX',UpperCase(ObjType))>0) then begin
			Cbx:=TComboBox.Create(Frm);
			Cbx.Name:=ObjName;
			Cbx.Parent:=ObjParent;
			Cbx.SetBounds(ObjPosX, ObjPosY, ObjWidth, ObjHeight);
			Cbx.Hint:=ObjHint;
			if (pos('-DDL',UpperCase(ObjType))>0) then Cbx.Style:=CsDropDownlist;
			Cbx.Text:=ObjValue;
			sl.Clear;
			if (DlgDef.IndexOfName(ObjName+'_values')<>-1) then begin
				sl.CommaText:=GetExprValue(DlgDef.Values[ObjName+'_values']);
				sl2.clear;
				if (DlgDef.IndexOfName(ObjName+'_extvalues')<>-1) 
					then sl2.CommaText:=GetExprValue(DlgDef.Values[ObjName+'_extvalues']);
				for j:=1 to sl.Count do begin
					Cbx.Items.Add(sl[j-1]);
					if (sl2.Count>=sl.Count) and (UpperCase(sl2[j-1])=UpperCase(ObjValue))
					or (pos('-DDL',UpperCase(ObjType))>0) and (Cbx.ItemIndex=-1) and (UpperCase(sl[j-1])=UpperCase(ObjValue))
						then Cbx.ItemIndex:=j-1;
				end;
				if (pos('-IDX',UpperCase(ObjType))>0) then begin
					j:=StrToIntDef(ObjValue,-1);
					if (j>=0) and (j<sl.count) then Cbx.ItemIndex:=j;
				end;
			end;
			Comp.AddObject(ObjName+'='+UpperCase(ObjType),Cbx);
		end
		else if (pos('TLISTBOX',UpperCase(ObjType))>0) then begin
			Lbx:=TListBox.Create(Frm);
			Lbx.Name:=ObjName;
			Lbx.Parent:=ObjParent;
			Lbx.SetBounds(ObjPosX, ObjPosY, ObjWidth, ObjHeight);
			Lbx.Hint:=ObjHint;
			Lbx.MultiSelect:=true;
			sl.Clear;
			if (DlgDef.IndexOfName(ObjName+'_values')<>-1) then begin
				sl.CommaText:=GetExprValue(DlgDef.Values[ObjName+'_values']);
				sl2.clear;
				if (DlgDef.IndexOfName(ObjName+'_extvalues')<>-1) 
					then sl2.CommaText:=GetExprValue(DlgDef.Values[ObjName+'_extvalues']);
				for j:=1 to sl.Count do begin
					Lbx.Items.Add(sl[j-1]);
					if (sl2.Count>=sl.Count) and InList(UpperCase(sl2[j-1]),UpperCase(ObjValue))
					or (pos('-IDX',UpperCase(ObjType))>0) and InList(IntToStr(j-1),ObjValue)
					or (pos('-IDX',UpperCase(ObjType))=0) and InList(UpperCase(sl[j-1]),UpperCase(ObjValue))
						then Lbx.Selected[j-1]:=true;
				end;
			end;
			Comp.AddObject(ObjName+'='+UpperCase(ObjType),Lbx);
		end;
	end;

	if (Comp.Count>0) then begin
		Frm.ShowModal;
		result:=BtnPressed;
	end;
	sl.Free;
	sl2.Free;
	Comp.Free;
	Frm.Free;
	DlgDef.Free;
end;


procedure Initialize;
var
	i: integer;
	sl: TStringList;
begin
	jInitialize:=false;
	sl:=TStringList.Create;

	sl.CommaText:='Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday';
	for i:=1 to 7 do jLongDayNames[i]:=sl[i-1];
	sl.CommaText:='January,February,March,April,May,June,July,August,September,October,November,December';
	for i:=1 to 12 do jLongMonthNames[i]:=sl[i-1];

	jHomeCard:=TStringList.Create;
	jMovieCard:=TStringList.Create;
	jPersonCard:=TStringList.Create;
	jListCard:=TStringList.Create;
	jThumbjMovieCard:=TStringList.Create;
	jThumbjMovie3Card:=TStringList.Create;
	jThumbjPersonCard:=TStringList.Create;
	jThumbjPerson3Card:=TStringList.Create;
	jRscPath:=TStringList.Create;
	jScriptOptions:=TStringList.Create;
	jXFvars:=TStringList.Create;
	jSqlStrings:=TStringList.Create;
	jBuildPage:=TStringList.Create;
	jThemes:=TStringList.Create;
	jThemeOptions:=TStringList.Create;
	jThemeOptText:=TStringList.Create;
	jDialogs:=TStringList.Create;
	jOriginalThemePath:=PicturesPath+'Themes\Original\';
	jOriginalPictPath:=PicturesPath+'Themes\Original\img\';

	LoadOptions;
	if (GetStrOption('ThemesList')='') then RegWriteIniOptions;   //Initialize Registry on first use
	LoadSqlStrings;
	CheckFileVersion(0,0,ScriptsPath+ScriptName+'_program.rsc',jOriginalThemePath+ScriptName+'_program.rsc');
	LoadDialogs;

	//jRedir:='http://www.movienizer.com/redirect/';
	jRedir:='';
	jMoviesHomeTag:='<a href="custom030100000:1"><img src="'+GetRscPath('Clap.png')+'" border="0" align="absmiddle"></a>&nbsp;';
	jPersonsHomeTag:='<a href="custom030200000:1"><img src="'+GetRscPath('Persons.png')+'" border="0" align="absmiddle"></a>&nbsp;';
	jHomePageCode:=GetStrOption('HomePageCode');

	SetToolbarVisibility(GetBoolOption('ToolbarVisibility'));
	SetMenuVisibility(GetBoolOption('MenuVisibility'));
	SetNavigationVisibility(GetBoolOption('NavigationVisibility'));

	sl.Free;
end;

// 20090904-JeF
procedure BuildScriptSettingsPage;
var
	ThmPage, s, s2, ThumbsLink: string;
	jMaxPerLine, i, j, k: integer;
begin
	ThumbsLink:='';
	jHomePageCode:='020100000000000000';
	jMaxPerLine:=(GetHTMLWidth-GetIntThmOption('ListOffWidth')) div (150+10);
	j:=1;
	k:=0;
	while (j<=jThemes.Count) do begin;
		s:=PicturesPath+'Themes\'+jThemes[j-1]+'\img\ThemeThumb.jpg';
		if MyFileExists(s) then begin
			s2:='<td valign="bottom"><a href="custom020300000:'+IntToStr(j)+'"><img src = "'+s
				+'" width = "150" border = "1px" alt="'+jThemes[j-1]+'" /></a></td>';
			k:=k+1;
			if (k>jMaxPerLine) and (((k mod jMaxPerLine) = 1) or (jMaxPerLine=1)) then s2:='</tr><tr>'+s2;
			ThumbsLink:=ThumbsLink+s2;
		end;
		j:=j+1;
	end;
	s:='<ol>'
		+'<li><a href="custom020100000:2"><img src="'+GetRscPath('Settings.png')+'" border="0" height="18" align="absmiddle"></a>&nbsp;'
			+'<a href="custom020100000:2">'+GetLangStr(1103)+'</a><br>&nbsp;'
		+'<li><a href="link:'+ScriptsPath+'"><img src="'+GetRscPath('Folder.png')+'" border="0" height="18" align="absmiddle" '
			+'alt="'+ScriptsPath+'"></a>&nbsp;'
			+'<a href="link:'+ScriptsPath+'">'+GetLangStr(1139)+'</a><br>&nbsp;'
{		+'<li><a href="link:'+ScriptsPath+script_name+'_program_options.ini"><img src="'+GetRscPath('ScriptEdit.png')+'" border="0" height="18" align="absmiddle" '
			+'alt="'+ScriptsPath+script_name+'_program_options.ini"></a>&nbsp;'
			+'<a href="link:'+ScriptsPath+script_name+'_program_options.ini">'+GetLangStr(1119)+'</a>'
		+'<li><a href="custom020400000:1"><img src="'+GetRscPath('RefreshGreen.png')+'" border="0" height="18" align="absmiddle"></a>&nbsp;'
			+'<a href="custom020400000:1">'+GetLangStr(1120)+'</a><br>&nbsp;'
}		+'<li><a href="link:'+jThmPath+'Theme_options.ini"><img src="'+GetRscPath('ScriptEdit.png')+'" border="0" height="18" align="absmiddle" '
			+'alt="'+jThmPath+'Theme_options.ini"></a>&nbsp;'
			+'<a href="link:'+jThmPath+'Theme_options.ini">'+GetLangStr(1132)+'</a>'
		+'<li><a href="custom020200000:1"><img src="'+GetRscPath('RefreshGreen.png')+'" border="0" height="18" align="absmiddle"></a>&nbsp;'
			+'<a href="custom020200000:1">'+GetLangStr(1121)+'</a>'
		+'<li><a href="custom020300000:999">'+GetLangStr(1122)+'</a><br>&nbsp;'
			+'<table><tr>'+ThumbsLink+'</tr></table>';
	{s:=s+'</ol><br><br><h1 class="ListTitle">'+GetLangStr(1123)+'</h1><ol>';
	if MyFileExists(ScriptsPath+'imdb_movie_options.ini') 
	or MyFileExists(ScriptsPath+'imdb_person_options.ini') then begin
		s2:='<a href="custom020100000:3"><img src="'+GetRscPath('Settings.png')+'" border="0" height="18" align="absmiddle"></a>&nbsp;'
			+'<a href="custom020100000:3">'+GetLangStr(1683)+'</a>';
		s:=s+'<li>'+s2;
	end;}  // **obsolete**
	{s2:=GetLangStr(1124);
	if MyFileExists(ScriptsPath+'imdb_movie_options.ini') then
		s2:='<a href="link:'+ScriptsPath+'imdb_movie_options.ini"><img src="'+GetRscPath('ScriptEdit.png')+'" border="0" height="18" align="absmiddle" '
			+'alt="'+ScriptsPath+'imdb_movie_options.ini"></a>&nbsp;'
			+'<a href="link:'+ScriptsPath+'imdb_movie_options.ini">'+s2+'</a>';
	s:=s+'<li>'+s2;}   // **obsolete** // 20100907-JeF
	{s2:=GetLangStr(1125);
	if MyFileExists(ScriptsPath+'imdb_person_options.ini') then
		s2:='<a href="link:'+ScriptsPath+'imdb_person_options.ini"><img src="'+GetRscPath('ScriptEdit.png')+'" border="0" height="18" align="absmiddle" '
			+'alt="'+ScriptsPath+'imdb_person_options.ini"></a>&nbsp;'
			+'<a href="link:'+ScriptsPath+'imdb_person_options.ini">'+s2+'</a>';
	s:=s+'<li>'+s2;}  // **obsolete**
	s:=s+'</ol><br>';
	BuildMainNavBar(201,'111111111111');
	jLastPage:=BuildExtraPage(GetLangStr(1126),s);
end;

function GetExprValue(Expr: string): string;
var
	i, j, k: integer;
	Id, Res: string;
begin
	Res:=Expr;
	i:=1;
	while (i>0) do begin
		i:=pos('LANGSTR(',UpperCase(Res));
		if (i>0) then begin
			j:=MyPos('(',Res,i);
			k:=MyPos(')',Res,j);
			j:=StrToIntDef(copy(Res,j+1,k-j-1),0);
			delete(Res,i,k-i+1);
			insert(ReplaceStr(GetLangStr(j),'\t',''),Res,i);
			continue;
		end;
		i:=pos('OPTION(',UpperCase(Res));
		if (i>0) then begin
			j:=MyPos('(',Res,i);
			k:=MyPos(')',Res,j);
			Id:=copy(Res,j+1,k-j-1);
			delete(Res,i,k-i+1);
			insert(GetStrOption(Id),Res,i);
			continue;
		end;
		i:=pos('NOT(',UpperCase(Res));
		if (i>0) then begin
			j:=MyPos('(',Res,i);
			k:=MyPos(')',Res,j);
			Id:=copy(Res,j+1,k-j-1);
			delete(Res,i,k-i+1);
			insert(BoolToStr(NOT(StrToBool(Id))),Res,i);
			continue;
		end;
	end;
	result:=Res;
end;

// Load Options Values
procedure LoadOptions;
var
	i, j: integer;
	OptFile, OptName, OptRegVal, OptTslVal: string;
begin
	jScriptOptions.Clear;
	// Default Values
	jScriptOptions.Add('ShowFeaturedDVD=True');
	jScriptOptions.Add('ShowFileName=True');
	jScriptOptions.Add('CoverInScreenshots=True');
	jScriptOptions.Add('ThumbsHeigh=100');
	jScriptOptions.Add('DescNewLine=True');
	jScriptOptions.Add('ShowPtDateOfDeath=True');
	jScriptOptions.Add('ShowMtAirYears=True');
	jScriptOptions.Add('ShowMtDiskNbr=True');
	jScriptOptions.Add('ThemesList=Original,Indian,IndianGreen,IndianBlue,IndianBlack,Scroll,Scroll2');
	jScriptOptions.Add('Theme=''Indian''');
	jScriptOptions.Add('EnglishMetrics=True');
	jScriptOptions.Add('MovieAwards=False');
	jScriptOptions.Add('LastAddUpdThumbs=True');
	jScriptOptions.Add('TextListMaxItems=40');
	jScriptOptions.Add('RichListMaxItems=20');
	jScriptOptions.Add('ThumbListMaxItems=30');
	jScriptOptions.Add('ListFace=2');
	jScriptOptions.Add('ListThumbsWidth=80');
	jScriptOptions.Add('ToolbarVisibility=True');
	jScriptOptions.Add('MenuVisibility=True');
	jScriptOptions.Add('NavigationVisibility=True');
	jScriptOptions.Add('InCollection=True');
	jScriptOptions.Add('MHomeGListState=1');
	jScriptOptions.Add('AlwaysFullDesc=false');
	jScriptOptions.Add('ByManualsListOrder=0');
	jScriptOptions.Add('HomePageCode=0');
	jScriptOptions.Add('GenreAutoCollapse=0');
	jScriptOptions.Add('NavBarState=1');
	jScriptOptions.Add('IgnoreFileExt=''.txt,.nfo,.asx,.bat,.db''');
	jScriptOptions.Add('PlayButton=all');
	jScriptOptions.Add('FilePathCheckExcept=Type_here_your_exceptions');
	jScriptOptions.Add('LastFileList=Type_here_your_list_filepath');
	jScriptOptions.Add('FileRenBegining=True');
	jScriptOptions.Add('FileRenSrcPattern=%');
	jScriptOptions.Add('FileRenTgtPattern=%');
	jScriptOptions.Add('EdDiscAutoNbrMode=0');
	jScriptOptions.Add('DiscAutoNbrToolGapsMode=1');
	jScriptOptions.Add('TitleTVTag=TV');
	jScriptOptions.Add('TitleTVTagYear=0');
	// Load Registry Values
	for i:=0 to jScriptOptions.Count-1 do begin
		OptName:=jScriptOptions.Names[i];
		OptRegVal:=GetStrOption(OptName);
		OptTslVal:=jScriptOptions.Values[OptName];
		if (OptRegVal>'') 
			then SetStrOption(OptName,OptRegVal)
		else SetStrOption(OptName,OptTslVal);
	end;
	jThemes.CommaText:=GetStrOption('ThemesList');
	if (GetStrOption('Theme')='') then SetStrOption('Theme','Original');
	// Load Theme
	SetTheme(GetStrOption('Theme'));
	jInCollection:=GetBoolOption('InCollection');
	jInCollection:=True;  // TODO
end;

// Load Ini File Options Value into Registry
procedure RegWriteIniOptions;
var
	sl: TStringList;
	i, j: integer;
	OptFile: string;
begin
	// Load Ini File Saved Values
	OptFile:=ScriptsPath+ScriptName+'_program_options.ini';
	if MyFileExists(OptFile) then begin
		sl:=TStringList.Create;
		sl.LoadFromFile(OptFile);
		for i:=0 to sl.Count-1 do begin
			j:=pos(';',sl[i]);
			if (j>0) then sl[i]:=ClearExtraBlankCharacters(copy(sl[i],1,j-1));
			j:=pos('//',sl[i]);
			if (j>0) then sl[i]:=ClearExtraBlankCharacters(copy(sl[i],1,j-1));
		end;
		for i:=0 to sl.Count-1 do begin
			if (jScriptOptions.Values[sl.Names[i]]>'') then begin
				SetStrOption(sl.Names[i],sl.Values[sl.Names[i]]);
			end;
		end;
		sl.free;
	end;
end;

procedure SetDlgValue(CompId, Val: String);
begin
	if (Val='') then Val:='*None';
	if (jDialogs.IndexOfName(CompId+'_value')<>-1) 
		then jDialogs.Values[CompId+'_value']:=Val;
end;

procedure LoadDialogs;
begin
	jDialogs.Clear;
	if MyFileExists(ScriptsPath+ScriptName+'_program.rsc')
		then jDialogs.LoadFromFile(ScriptsPath+ScriptName+'_program.rsc');
	jDialogs.Text:=GetExprValue(jDialogs.Text);
end;

procedure CheckFileVersion(CurVersion, RefVersion: integer; CurPath, RefPath: string);
var
	i,j: integer;
	val: string;
	sl: TStringList;
begin
	sl:=TStringList.Create;
	if (CurVersion<=0) and MyFileExists(CurPath) then begin
		sl.LoadFromFile(CurPath);
		if (sl.IndexOfName('Version')<>-1) then val:=sl.Values['Version']
		else if (sl.IndexOfName('version')<>-1) then val:=sl.Values['version'];
		CurVersion:=StrToIntDef(val,-1);
	end;
	if (CurVersion<RefVersion) or (RefVersion<=0) then begin
		if MyFileExists(RefPath) then begin
			sl.LoadFromFile(RefPath);
			if (sl.IndexOfName('Version')<>-1) then val:=sl.Values['Version']
			else if (sl.IndexOfName('version')<>-1) then val:=sl.Values['version'];
			RefVersion:=StrToIntDef(val,-1);
			if (CurVersion<RefVersion) then sl.SaveToFile(CurPath);
		end;
	end;
	sl.Free;
end;

// 20090830-JeF
function LoadCard(CardName: string): string;
var
	sl: TStringList;
	CardPath, s2, txtpos, jCss: string;
	i,j,k: integer;
begin
	sl:=TStringList.create;
	CardPath:=jThmCustomPath+CardName;
	if MyFileExists(CardPath) then sl.LoadFromFile(CardPath)
	else begin
		CardPath:=jThmPath+CardName;
		if MyFileExists(CardPath) then sl.LoadFromFile(CardPath);
	end;
	i:=Pos('<link rel="stylesheet"',sl.Text);
	if (i>0) then begin
		i:=MyPos('href="',sl.Text,i);
		j:=MyPos('"',sl.Text,i+6);
		s2:=copy(sl.Text,i+6,j-i-6);
		if MyFileExists(jThmCustomPath+s2) 
			then insert(jThmCustomPath,sl.Text,i+6)
		else insert(jThmPath,sl.Text,i+6);
		jCss:='"'+s2+'"';
		if not InList(jCss,jThmCssList)
			then jThmCssList:=jThmCssList+jCss+',';
	end;
	sl.text:=ReplaceStr(sl.text,'src = "','src="');
	sl.text:=ReplaceStr(sl.text,'background = "','background="');
	for j:=1 to 3 do begin
		if (j=1) then txtpos:='src="' 
		else if (j=2) then txtpos:='background="'
		else txtpos:='url(';
		i:=pos(txtpos,sl.text);
		while (i>0) do begin
			i:=i+length(txtpos);
			if (pos('"',txtpos)>0) 
				then j:=MyPos('"',sl.text,i)
			else j:=MyPos(')',sl.text,i);
			s2:=copy(sl.text,i,j-i);
			if InList(copy(s2,length(s2)-3,4),'.gif,.jpg,.png') then begin
				k:=length(s2);
				while (k>0) and (copy(s2,k,1)<>'/') and (copy(s2,k,1)<>'\') do k:=k-1;
				delete(s2,1,k);
				s2:=GetRscPath(s2);
				delete(sl.text,i,j-i);
				insert(s2,sl.text,i);
			end;
			i:=MyPos(txtpos,sl.text,i+1);
		end;
	end;
	if InList('"'+CardName+'"',jThmCssList) and MyFileExists(CardPath) 
		then sl.SaveToFile(CardPath);
	result:=sl.text;
	sl.free;
end;

begin

end.