<%
var
	aTopicList: THndTopicsInfoArray;
	nCurTopic, nCurParent, nTopicKind, nHeaderKind, nFooterKind: integer;
	sCharset, sTopicHeader, sTopicFooter, sRelativeTopic: string;
	aBreadCrumb: array of String;

begin
	// UTF-8 or project charset ?
	if HndGeneratorInfo.GetCustomSettingValue('UseCharsetForTopics') then
	begin
		// Use project charset
		HndGeneratorInfo.BOMOutput := False;
		HndGeneratorInfo.ForceOutputEncoding := True;
		sCharset := HndProjects.GetProjectCharsetAsHtml();
	end
	else begin
		// Need BOM for UTF8 files
		HndGeneratorInfo.BOMOutput := True;
		sCharset := 'UTF-8';
	end;

	// Output global CSS content
	HndGeneratorInfo.CurrentFile := 'css\hnd.css';
	print(HndProjects.GetProjectCssContent());
	
	// Each individual topics...
	aTopicList := HndTopics.GetTopicListVisible(False);
	for nCurTopic := 0 to length(aTopicList) - 1 do
	begin
		// Notify about the topic being generated
		HndGeneratorInfo.CurrentTopic := aTopicList[nCurTopic].id;
		// Topic kind
		nTopicKind := aTopicList[nCurTopic].Kind;
		if (nTopicKind = 1) then continue;  // Empty topic: do not generate anything
		// Setup the file name
		HndGeneratorInfo.CurrentFile := HndTopics.GetTopicHelpId(HndGeneratorInfo.CurrentTopic) + '.htm';
		// Topic header
		nHeaderKind := HndTopics.GetTopicHeaderKind(HndGeneratorInfo.CurrentTopic);
		sTopicHeader := HndTopics.GetTopicHeaderTextCalculated(HndGeneratorInfo.CurrentTopic);
		// Topic footer
		nFooterKind := HndTopics.GetTopicFooterKind(HndGeneratorInfo.CurrentTopic);
		sTopicFooter := HndTopics.GetTopicFooterTextCalculated(HndGeneratorInfo.CurrentTopic);
		// Create the breadcrumb
		SetLength(aBreadCrumb, 0);
		if HndGeneratorInfo.GetCustomSettingValue('ShowBreadCrumbs') then
		begin
			sRelativeTopic := HndTopics.GetTopicParent(HndGeneratorInfo.CurrentTopic);
			while (sRelativeTopic <> '') and (sRelativeTopic <> HndTopics.GetProjectTopic()) do
			begin
				SetLength(aBreadCrumb, Length(aBreadCrumb) + 1);
				aBreadCrumb[Length(aBreadCrumb) - 1] := sRelativeTopic;
				sRelativeTopic := HndTopics.GetTopicParent(sRelativeTopic);
			end;
		end;

%>

<html>
	
<head>
	<title><% print(HTMLEncode(HndTopics.GetTopicCaption(HndGeneratorInfo.CurrentTopic))); %></title>
	<meta http-equiv="Content-Type" content="text/html; charset=<% print(sCharset); %>">
    <meta name="generator" content="<% print(HTMLEncode(HndGeneratorInfo.HelpNDocVersion)); %>">
	
	<%
		// Redirect for URL and Files topic
		if (nTopicKind = 2) then
		begin
		  printf('<meta http-equiv="refresh" content="0;URL=%s">', [HndTopics.GetTopicUrlLink(HndGeneratorInfo.CurrentTopic)]);
		end
		else begin
	%>
	
	<link type="text/css" rel="stylesheet" media="all" href="css/reset.css" />
	<link type="text/css" rel="stylesheet" media="all" href="css/base.css" />
	<link type="text/css" rel="stylesheet" media="all" href="css/hnd.css" />
	<!--[if lte IE 8]>
		<link type="text/css" rel="stylesheet" media="all" href="css/ielte8.css" />
	<![endif]-->
	<style type="text/css">
		#topic_header
		{
			background-color: #<% print(TColorToHex(HndGeneratorInfo.GetCustomSettingValue('BaseColor'))); %>;
		}
	</style>
	<script type="text/javascript" src="js/chmRelative.js"></script>
	
	<%
		end;
	%>
</head>

<body>

<%
	// Redirect for URL and Files topic
	if (nTopicKind = 2) then
	begin
	  printf('<a href="%s">Redirecting... click here if nothing happens</a>', [HndTopics.GetTopicUrlLink(HndGeneratorInfo.CurrentTopic)]);
	end
	else begin
%>

	<% if nHeaderKind <> 2 then begin %>
		<div id="topic_header">
			<div id="topic_header_content">
				<h1><% print(HTMLEncode(sTopicHeader)); %></h1>
				
				<%
					if Length(aBreadCrumb) > 0 then
					begin
				%>
				<div id="topic_breadcrumb">
					<%
						for nCurParent := Length(aBreadCrumb) - 1 downto 0 do
							printf('<a href="%s.htm">%s</a> &rsaquo;&rsaquo; ', [HndTopics.GetTopicHelpId(aBreadCrumb[nCurParent]), HTMLEncode(HndTopics.GetTopicCaption(aBreadCrumb[nCurParent]))]);
					%>
				</div>
				<%
					end;
				%>
			</div>
			<%
				if HndGeneratorInfo.GetCustomSettingValue('ShowNavigation') then
				begin
			%>
			<div id="topic_header_nav">
				<%
					sRelativeTopic := HndTopics.GetTopicParent(HndGeneratorInfo.CurrentTopic);
					if (sRelativeTopic <> '') and (sRelativeTopic <> HndTopics.GetProjectTopic()) then
					begin
				%>
				
				<a href="<% printf('%s.htm', [HndTopics.GetTopicHelpId(sRelativeTopic)]); %>"><img src="img/arrow_up.png" alt="Parent"/></a>
				
				<%
					end;
					sRelativeTopic := HndTopics.GetTopicPreviousVisible(HndGeneratorInfo.CurrentTopic);
					if (sRelativeTopic <> '') and (sRelativeTopic <> HndTopics.GetProjectTopic()) then
					begin
				%>
				
				<a href="<% printf('%s.htm', [HndTopics.GetTopicHelpId(sRelativeTopic)]); %>"><img src="img/arrow_left.png" alt="Previous"/></a>
				
				<%
					end;
					sRelativeTopic := HndTopics.GetTopicNextVisible(HndGeneratorInfo.CurrentTopic);
					if (sRelativeTopic <> '') and (sRelativeTopic <> HndTopics.GetProjectTopic()) then
					begin
				%>
				
				<a href="<% printf('%s.htm', [HndTopics.GetTopicHelpId(sRelativeTopic)]); %>"><img src="img/arrow_right.png" alt="Next"/></a>
				
				<%
					end;
				%>
			</div>
			<%
				end;
			%>
			<div class="clear"></div>
		</div>
	<% end; %>
	
	<div id="topic_content">
		<% print(HndTopics.GetTopicContentAsHtml(HndGeneratorInfo.CurrentTopic)); %>
	</div>
	
	<% if nFooterKind <> 2 then begin %>
		<div id="topic_footer">

			<div id="topic_footer_content">
				<% print(HTMLEncode(sTopicFooter)); %>
			</div>
		</div>
	<% end; %>
	
<%
	end;
%>
	
</body>
	
</html>

<%
	end;  // for nCurTopic
	
end.
%>