%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Page styles
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{fancyhdr}

\makeatletter

\newif\ifnormal
\normaltrue

%\def\version{\relax}
%\def\bookNAME{\relax}
%\def\coursename{\relax}
%\def\undefined{\relax}

\fancyhf{}
%\usepackage{fancyhdr}
\def\sectionmark#1{%
  \markboth {{%
	\ifnum \c@secnumdepth >\z@
	  \thesection\quad
	\fi
	#1}}{}}%
	
\fancyhead{}
%\fancyhead[LO]{\S\ChapterNumber.\normalsize\scshape\leftmark}
%\fancyhead[LO]{\S\ChapterNumber.\normalsize\scshape\botmark}
%%%%%\fancyhead[LO]{\ifnormal\S\ChapterNumber.\thesection\fi\normalsize\scshape\SectionName}
%\fancyhead[LO]{\llap{\S\ChapterNumber\qquad}.\leftmark}
\fancyhead[RO]{\thepage}
%\fancyhead[RE]{\small Chapter \ChapterNumber: \ChapterName}
%%%%%%%\fancyhead[RE]{\normalsize\scshape \ChapterName}
\fancyhead[LE]{\thepage}
\fancyfoot{}
\addtolength{\headheight}{\baselineskip}

%% normal
\fancypagestyle{normal}{%
	\normaltrue
	\fancyhf{} % clear all header and footer fields
	\fancyhead[LO]{%
%		\ifnormal\S\ChapterNumber.\thesection\quad\fi
		\ifnormal\S\thesection\quad\fi
		\normalsize\scshape\SectionName
	}%
	\fancyhead[RO]{\thepage}
	\fancyhead[LE]{\thepage}
	\fancyhead[RE]{\scshape \normalsize\ChapterName}
	\fancyfoot{}
	\renewcommand{\headrulewidth}{0.5pt}
	\renewcommand{\footrulewidth}{0pt}%
}

%% draft
\fancypagestyle{draft}{%
	\normaltrue
	\fancyhf{} % clear all header and footer fields
	\fancyhead[LO]{%
%		\ifnormal\S\ChapterNumber.\thesection\quad\fi
		\ifnormal\S\thesection\quad\fi
		\normalsize\scshape\SectionName
	}%
	\fancyhead[RO]{\thepage}
	\fancyhead[LE]{\thepage}
	\fancyhead[RE]{\scshape \normalsize\ChapterName}
	\fancyfoot[L]{{\setlength{\fboxsep}{1.5mm}	
		\fbox{\itshape Draft: \version\ \copyright David Pollard}%
	}}
	\renewcommand{\headrulewidth}{0.5pt}
	\renewcommand{\footrulewidth}{0pt}%
}

%% handout
\fancypagestyle{handout}{%
	\normaltrue
	\fancyhf{} % clear all header and footer fields
	\fancyhead[LO]{%
		\coursename
%		\ifnormal\thesection\quad\fi
%		\normalsize\scshape\SectionName
	}%
	\fancyhead[RO]{\thepage}
	\fancyhead[LE]{\thepage}
	%\fancyhead[RE]{\scshape \normalsize\ChapterName}
	\fancyfoot{}
	\renewcommand{\headrulewidth}{0.5pt}
	\renewcommand{\footrulewidth}{0pt}%
}


%% memo
\fancypagestyle{memo}{%
	\normaltrue
	\fancyhf{} % clear all header and footer fields
	\fancyhead[LO]{%
		\coursename
%		\ifnormal\thesection\quad\fi
%		\normalsize\scshape\SectionName
	}%
%	\fancyhead[RO]{\thepage}
%	\fancyhead[LE]{\thepage}
	%\fancyhead[RE]{\scshape \normalsize\ChapterName}
	%\fancyfoot[L]{\vtop{\hbox{\scshape David Pollard}\hbox{\today}}}
	% changed 26 March 2016
	\fancyfoot[L]{{\setlength{\fboxsep}{1.5mm}	
		\fbox{\itshape Draft: \version\ \copyright David Pollard}%
	}}	\fancyfoot[R]{\thepage}
	\renewcommand{\headrulewidth}{0pt}
%	\renewcommand{\footrulewidth}{0.5pt}%
	\renewcommand{\footrulewidth}{0pt}%
}


%% YPNG
\def\YPNGdate{YPNG \today}% default
\fancypagestyle{YPNG}{%
	\normaltrue
	\fancyhf{} % clear all header and footer fields
	\fancyhead[L]{%
		\YPNGdate
%		\ifnormal\thesection\quad\fi
%		\normalsize\scshape\SectionName
	}%
%	\fancyhead[RO]{\thepage}
%	\fancyhead[LE]{\thepage}
	%\fancyhead[RE]{\scshape \normalsize\ChapterName}
	%\fancyfoot[L]{\vtop{\hbox{\scshape David Pollard}\hbox{\today}}}
	% changed 26 March 2016
%  \fancyfoot[L]{{\setlength{\fboxsep}{1.5mm}	
%	  \fbox{\itshape Draft: \version\ \copyright David Pollard}%
%  }}	\fancyfoot[R]{\thepage}
	\renewcommand{\headrulewidth}{0pt}
%	\renewcommand{\footrulewidth}{0.5pt}%
	\renewcommand{\footrulewidth}{0pt}%
}

\newcommand{\makeYPNG}{% 8 June 2016
	\pagestyle{YPNG}%
%	\thispagestyle{startchap}% 
	\renewcommand{\thesection}{\@arabic\c@section}%
}

%% blank
\fancypagestyle{blank}{%
	\normaltrue
	\fancyhf{} % clear all header and footer fields
%	\fancyhead[LO]{%
%		\coursename
%		\ifnormal\thesection\quad\fi
%		\normalsize\scshape\SectionName
%	}%
%	\fancyhead[RO]{\thepage}
%	\fancyhead[LE]{\thepage}
	%\fancyhead[RE]{\scshape \normalsize\ChapterName}
%	\fancyfoot{}
	\renewcommand{\headrulewidth}{0pt}%
	\renewcommand{\footrulewidth}{0pt}%
}


\pagestyle{normal}

%\def\bookNAME{\relax} % causes trouble with empty line
\def\bookNAME{{\scshape unknown bookNAME}}
\def\SectionName{\relax}
\def\AUTHOR{David Pollard}

\fancypagestyle{startchap}{%
	\fancyhf{} % clear all header and footer fields
	\fancyfoot[L]{%
		\vtop to0pt{%
			\small\itshape 
			version: \version\\
			printed: \today%
			\vss
		}}
	%\fancyfoot[C]{\thepage}
	\fancyfoot[R]{%
		\vtop to0pt{%
			\small\itshape 
			\bookNAME\\%
			\copyright \AUTHOR%
			\vss 
		}}%
	\renewcommand{\headrulewidth}{0pt}
	\renewcommand{\footrulewidth}{0.5pt}%
}

%\newcommand{\ps@references}{}
\fancypagestyle{references}{%
	\fancyhf{}
	\fancyhead[LO]{\scshape
	\ChapTitle/ \ChapterNumber\ references}
	%\fancyhead[LO]{\llap{\S\ChapterNumber\qquad}.\leftmark}
	\fancyhead[RO]{\thepage}%
	\fancyhead[RE]{\scshape \ChapterName}%
	\fancyhead[LE]{\thepage}%
}

%% problems
\fancypagestyle{problems}{%
	\fancyhf{}
	\fancyhead[LO]{\scshape
	\ChapTitle/ \ChapterNumber\ problems}
	%\fancyhead[LO]{\llap{\S\ChapterNumber\qquad}.\leftmark}
	\fancyhead[RO]{\thepage}%
	\fancyhead[RE]{\scshape \ChapterName}%
	\fancyhead[LE]{\thepage}%
}

\fancypagestyle{notes}{%
	\fancyhf{}
	\fancyhead[LO]{\scshape\normalsize Chapter \ChapterNumber\ notes}
	%\fancyhead[LO]{\llap{\S\ChapterNumber\qquad}.\leftmark}
	\fancyhead[RO]{\thepage}%
	\fancyhead[RE]{\scshape \ChapterName}%
	\fancyhead[LE]{\thepage}%
}

\fancypagestyle{roman}{%
	\fancyhf{}
	\fancyhead[RO]{\roman{page}}
	\fancyhead[LE]{\roman{page}}
	\renewcommand{\headrulewidth}{0pt}
}

\fancypagestyle{HWnumbered}{%
	\fancyhf{}
	\fancyhead[RO]{\thepage}
	\fancyhead[LE]{\thepage}
	\renewcommand{\headrulewidth}{0pt}
}

\fancypagestyle{preface}{%
%	\normaltrue
	\fancyhf{} % clear all header and footer fields
%	\fancyhead[LO]{%
%%		\ifnormal\S\ChapterNumber.\thesection\quad\fi
%		\ifnormal\S\thesection\quad\fi
%		\normalsize\scshape\SectionName
%	}%
	\fancyhead[RO]{\thepage}
	\fancyhead[LE]{\thepage}
%	\fancyhead[RE]{\scshape \normalsize\ChapterName}
	\fancyfoot{}
	\renewcommand{\headrulewidth}{0pt}
	\renewcommand{\footrulewidth}{0pt}%
}


\makeatother
