\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{wmletterhead}[2014/12/15 provides LaTeX WM letter head class]

% Created by Eugeniy E. Mikhailov 
% v1.0 2014/12/15
% Note logo files belong to W&M
% 'wmletterhead' class accepts all options of the 'letter' class and the following
% additional options which specify the header logo
% 	prior2014dec - old logo prior 2014
% 	fullcolor - full color logo with yellowish WM cypher
% 	black - all black logo
% 	green - all green logo
% 	white - all white logo (quite use less on white background)
% DEFAULT options: [fullcolor]
% Recomended use:
%	\documentclass[12pt,fullcolor]{wmletterhead}

%-------------- define WM header logo file ---------------------------------
\providecommand{\wmheaderlogo}{}% nologo

% this is prior December 2014 logo with Wren building
\DeclareOption{prior2014dec}{%
	\renewcommand{\wmheaderlogo}%
		{wmlogos/wmheader2012}%
}

% full colored logo starting from December 2014
\DeclareOption{fullcolor}{%
	\renewcommand{\wmheaderlogo}%
		{wmlogos/wm_horizontal_single_line_full_color}%
}

% green logo starting from December 2014
\DeclareOption{green}{%
	\renewcommand{\wmheaderlogo}%
		{wmlogos/wm_horizontal_single_line_green}%
}

% black logo starting from December 2014
\DeclareOption{black}{%
	\renewcommand{\wmheaderlogo}%
		{wmlogos/wm_horizontal_single_line_black}%
}

% black logo starting from December 2014
\DeclareOption{white}{%
	\renewcommand{\wmheaderlogo}%
		{wmlogos/wm_horizontal_single_line_white}%
}
%-------------- end of define WM header logo file ------------------------------


% by default all unknown options are passed to 'letter' class
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{letter}}

% DEFAULT options
\ExecuteOptions{fullcolor}
\ProcessOptions\relax

\LoadClass{letter}

\newlength{\wmlogoheight}
\newlength{\wmlogowidth}
\newlength{\wmlogohpos}
\newlength{\wmlogovpos}

% below are measured in the Word template
\setlength{\wmlogoheight}{0.65in} 
\setlength{\wmlogowidth}{3.64in}
\setlength{\wmlogohpos}{0.76in}%
\setlength{\wmlogovpos}{0.75in}%

% --------------------------------------------------
% Required by WM
\newlength{\wmlogoseparation}
\setlength{\wmlogoseparation}{0.5in}

\newlength{\wmleftmargin}
\setlength{\wmleftmargin}{1.64in}

\newlength{\wmrightmargin}
\setlength{\wmrightmargin}{1.0in}

\newlength{\wmbottommargin}
\setlength{\wmbottommargin}{1.0in} % guessed
% --------------------------------------------------

% --------------------------------------------------
% Let's calculate top margin
\newlength{\wmtopmargin}
\setlength{\wmtopmargin}{\wmlogoheight}%
\addtolength{\wmtopmargin}{\wmlogovpos}%
\addtolength{\wmtopmargin}{\wmlogoseparation}%
% --------------------------------------------------

\RequirePackage{graphicx}
\RequirePackage[top=\wmtopmargin, left=\wmleftmargin, right=\wmrightmargin, bottom=\wmbottommargin, letterpaper]{geometry}
\RequirePackage[absolute]{textpos}
\RequirePackage{fancyhdr}


%% WM fancy letter head definition
\newcommand{\wmheader}{
	\fancyhf{}%
	\fancyhead[L]{%
		\begin{textblock*}{\wmlogowidth}(\wmlogohpos{},\wmlogovpos{})%
			\includegraphics[height=\wmlogoheight{},keepaspectratio=true]{\wmheaderlogo}%
		\end{textblock*}%
	}%
	\renewcommand{\headrulewidth}{0pt}%
	\fancyfoot[C]{\thepage}%
}

\newcommand{\wmaddress}{
	{\raggedleft\begin{tabular}{l@{}}\ignorespaces
		\fromaddress \\*[2\parskip]%
			\@date \end{tabular}}%
}

\fancypagestyle{firstpage}{% looks like this has no effect for the letter class
	\wmheader{}%
}
\fancypagestyle{plain}{% this takes care of the 2nd and the following pages
	\wmheader{}%
}
\fancypagestyle{empty}{% some how this is the very first page style
	\wmheader{}%
	\settoheight{\headheight}{\hbox{\wmaddress}}%
	\newlength{\wmaddresswidth}
	\settowidth{\wmaddresswidth}{\hbox{\wmaddress}}%
	\newlength{\wmaddresshpos}
	\setlength{\wmaddresshpos}{\paperwidth}
	\addtolength{\wmaddresshpos}{-\wmrightmargin}
	\addtolength{\wmaddresshpos}{-\wmaddresswidth}
	\fancyhead[R]{%
		\begin{textblock*}{\wmaddresswidth}(\wmaddresshpos{},\wmlogovpos{})%
			\wmaddress{}%
		\end{textblock*}%
	}%
}
\pagestyle{plain}% important! if switched to fancy pages 2, 3, ... do not have logo

% -------------------------------------------------------------------
% Now let redefine opening so address goes to the right of logo
\renewcommand*{\opening}[1]{\ifx\@empty\fromaddress
  \thispagestyle{firstpage}%
    {\raggedleft\@date\par}%
  \else  % home address
    \thispagestyle{empty}%
  \fi
  {\raggedright \toname \\ \toaddress \par}%
  \vspace{2\parskip}%
  #1\par\nobreak}
% -------------------------------------------------------------------
