%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- Mode: Latex -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% aaletter.sty -- A letter style for personal letter based on brev.cls
%% by Asgeir Nilsen <ambolt@stud.ntnu.no>.
%% 
%% Author          : Anders Andersen
%% Created On      : Thu Aug  1 11:36:15 1996
%% Last Modified By: Anders Andersen
%% Last Modified On: Thu Jan  2 17:13:10 1997
%% Status          : Unknown, Use with caution!
%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Original header:

% A little letter class for personal letters.  It is called ``brev''
% because that is the Norwegian name for letter, and the class
% ``letter'' is the standard LaTeX letter class. 

% This version originally requires LaTeX 2e version 1995/12/01 or
% later.  

% The special characteristics of this style is the addresses field at
% the top of the first page. This one is placed within a 7cm by 7cm
% picture element adjusted to the paper edge at the top and 1in from
% the paper edge at the left.  The recipient address should be placed
% within the window of window envelopes, and the sender address above
% the window.
% Two folding lines is also provided for folding the letter correctly
% both in E5 og E65 envelopes.  These are lines placed 10.5cm and
% 14.8cm from the top edge of the paper. 
% The class is made and tested with A4 paper and 10pt font, but should
% be portable to other dimensions as well. 

% The envelope used for defining this class is a E65 window envelope
% with the following characteristics:
% +------------------------------+
% |                   |          |
% | +-----------+ -   A          |
% | |           | B   |          |
% | +-----------+ -   -          |
% |                              |
% +------------------------------+
% Dimension A (from top edge of env. to bot. edge of win): 7cm
% Dimension B (height of window)                         : 3cm

% The class is distributed for free use and modifications by anyone,
% private, educational or commercial.  But I would like to hear about
% any improvements!

% Written by Asgeir Nilsen <ambolt@stud.ntnu.no>
% With thanks to 
%   Dag Langmyhr <dag@ifi.uio.no>
%   Svend Tollak Munkejord <stm@stud.unit.no>

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --------------------------------------%--------------------------------------
% ---------------------------- Identification part ----------------------------
% --------------------------------------%--------------------------------------

\RequirePackage{rcs}
\RCS$Date: 2002/07/09 23:45:45 $
\RCS$RCSfile: aaletter.sty,v $
\RCS$Revision: 1.1 $
\RCS$State: Exp $
\RCS$Author: aa $

\typeout{Package: `aaletter' v\RCSRevision\space <\RCSRawDate> (AA)}
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{aaletter}[\RCSRawDate]


% --------------------------------------%--------------------------------------
% -------------------------------- Start code ---------------------------------
% --------------------------------------%--------------------------------------

\RequirePackage{ifthen}
\newboolean{winenv}
\newlength{\widthadjust}


% --------------------------------------%--------------------------------------
% -------------------------- Declaration of options ---------------------------
% --------------------------------------%--------------------------------------

\DeclareOption{winenv}{\setboolean{winenv}{true}}
\DeclareOption{nowinenv}{\setboolean{winenv}{false}}
\DeclareOption{leftjust}{\setlength{\widthadjust}{\oddsidemargin}}
\DeclareOption{noleftjust}{\setlength{\widthadjust}{0pt}}
\DeclareOption{norsk}{%
  \def\yourrefname{Deres~ref}
  \def\ourrefname{V{\aa}r~ref}
  \def\yourdatename{Deres~dato}
  \def\ourdatename{V{\aa}r~dato}
  \def\fromlocationname{Postadresse}
  \def\telephonenumname{Telefon}
  \def\fromemailname{Epost}
  \def\fromvisitname{Besøksadresse}}
\DeclareOption{english}{%
  \def\yourrefname{Your~ref}
  \def\ourrefname{Our~ref}
  \def\yourdatename{Your~date}
  \def\ourdatename{Our~date}
  \def\fromlocationname{Postal Address}
  \def\telephonenumname{Phone}
  \def\fromemailname{Email}
  \def\fromvisitname{Visiting Address}}


% --------------------------------------%--------------------------------------
% --------------------------- Execution of options ----------------------------
% --------------------------------------%--------------------------------------

\ExecuteOptions{leftjust,norsk,nowinenv}
\ProcessOptions


% --------------------------------------%--------------------------------------
% ------------------------------ Package loading ------------------------------
% --------------------------------------%--------------------------------------

\RequirePackage{calc}


% --------------------------------------%--------------------------------------
% --------------------------------- Main code ---------------------------------
% --------------------------------------%--------------------------------------

% Be aware that a long unitlength makes the counter versions of
% lengths not verry useful.
\setlength{\unitlength}{1sp}

% The width of the top and foot
\newlength{\topwidth}
\setlength{\topwidth}{\textwidth+2\widthadjust}
\newcounter{@topwidth}
\setcounter{@topwidth}{\topwidth/\unitlength}

% The positions and length of the fold lines
\newlength{\xposfoldlines}
\setlength{\xposfoldlines}{\widthadjust-\oddsidemargin-2cm}
\newcounter{@xposfoldlines}
\setcounter{@xposfoldlines}{\xposfoldlines/\unitlength}
\newlength{\yposfoldline@one}
\setlength{\yposfoldline@one}{-2.5cm}
\newcounter{@yposfoldline@one}
\setcounter{@yposfoldline@one}{\yposfoldline@one/\unitlength}
\newlength{\yposfoldline@two}
\setlength{\yposfoldline@two}{-6.8cm}
\newcounter{@yposfoldline@two}
\setcounter{@yposfoldline@two}{\yposfoldline@two/\unitlength}
\newlength{\lenfoldlines}
\setlength{\lenfoldlines}{1cm}
\newcounter{@lenfoldlines}
\setcounter{@lenfoldlines}{\lenfoldlines/\unitlength}

% The height of the top
\newlength{\topheight}
\setlength{\topheight}{8cm}
\newcounter{@topheight}
\setcounter{@topheight}{\topheight/\unitlength}

% The ypos of the recivers address
\newlength{\yposto}
\setlength{\yposto}{2.75cm}
\newcounter{@yposto}
\setcounter{@yposto}{\yposto/\unitlength}

% The ypos of the senders address
\newlength{\yposfrom}
\setlength{\yposfrom}{5cm}
\newcounter{@yposfrom}
\setcounter{@yposfrom}{\yposfrom/\unitlength}

% The starting point of the picture environment (the top)
\newlength{\topadjust}
\setlength{\topadjust}{\topmargin+1in+\baselineskip-\topheight}


\newcommand*{\yourref}[1]{\def\toref{#1}}
\newcommand*{\yourdate}[1]{\def\todate{#1}}
\newcommand*{\ourref}[1]{\def\fromref{#1}}
\newlength{\tmpwidth}
\newcommand{\mkotherinfo}[2]{%
  \setlength{\tmpwidth}{0.2\topwidth}%
  \parbox[t]{\tmpwidth}{{\footnotesize\textsf{#1:}\\#2}}}
\newcommand{\otherinfo}[2]{%
  \ifx\@empty\info@one
  \def\info@one{\mkotherinfo{#1}{#2}}
  \else
  \ifx\@empty\info@two
  \def\info@two{\mkotherinfo{#1}{#2}}
  \else
  \ifx\@empty\info@three
  \def\info@three{\mkotherinfo{#1}{#2}}
  \else
  \ifx\@empty\info@four
  \def\info@four{\mkotherinfo{#1}{#2}}
  \else
  \ifx\@empty\info@five
  \def\info@five{\mkotherinfo{#1}{#2}}
  \else
  \fi\fi\fi\fi\fi}
\renewcommand{\location}[1]{\def\fromlocation{#1}%
  \otherinfo{\fromlocationname}{\fromlocation}}
\renewcommand{\telephone}[1]{\def\telephonenum{#1}%
  \otherinfo{\telephonenumname}{\telephonenum}}
\newcommand{\email}[1]{\def\fromemail{#1}%
  \otherinfo{\fromemailname}{\fromemail}}
\newcommand{\visit}[1]{\def\fromvisit{#1}%
  \otherinfo{\fromvisitname}{\fromvisit}}

\yourref{}
\yourdate{}
\ourref{}
\def\info@one{}
\def\info@two{}
\def\info@three{}
\def\info@four{}
\def\info@five{}

% Window envelopes first page 
\def\ps@winpage{%
  \def\@oddhead{%
    % Moves the starting point to the lower left corner of the picture
    % environment
    \raisebox{\topadjust}[0pt][0pt]{%
      % Makes us belive that this doesn't need any space
      \makebox[0pt][l]{%
        % Moves the starting point a little bit further left
        \hspace*{-\widthadjust}%
        % This picture environment makes the top of the letter
        \begin{picture}(\value{@topwidth},\value{@topheight})
          % Put the recivers name and address in the envelope window
          \put(0,\value{@yposto}){\makebox(0,0)[cl]{%
              \begin{minipage}{7cm}
                \toname\\
                \toaddress
              \end{minipage}}}
          % If we have a from address, put it on the top
          \put(0,\value{@yposfrom}){\makebox(0,0)[bl]{%
              \ifx\@empty\fromaddress
              \else
              \begin{minipage}{7cm}
                \ifx\@empty\fromname % In case you don't use \name
                \else
                \fromname\\
                \fi
                \fromaddress
              \end{minipage}%
              \fi}}
          % If we don't have a location, put the date up in the corner
          \put(\value{@topwidth},\value{@yposfrom}){\makebox(0,0)[br]{%
              \ifx\@empty\fromlocation\@date\fi}}
          % If we have a location, put references and date on a line
          % between addresses and text
          \put(0,0){\makebox(\value{@topwidth},0)[b]{%
              \ifx\@empty\fromlocation \else
              {\footnotesize
                \ifx\@empty\toref \else
                \textsf{\yourrefname:}~\toref
                \fi
                \ifx\@empty\todate \else
                \hfill \textsf{\yourdatename:}~\todate
                \fi
                \ifx\@empty\fromref \else
                \hfill \textsf{\ourrefname:}~\fromref
                \fi
                \ifx\@empty\@date \else
                \hfill \textsf{\ourdatename:}~\@date
                \fi}%
              \fi}}
          % These two folding lines are placed at the left edge of the
          % paper
          \put(\value{@xposfoldlines},\value{@yposfoldline@one}){%
            \line(-1,0){\value{@lenfoldlines}}}
          \put(\value{@xposfoldlines},\value{@yposfoldline@two}){%
            \line(-1,0){\value{@lenfoldlines}}}
        \end{picture}}}\hfil}%
  \def\@oddfoot{\raisebox{\baselineskip}[0pt][0pt]{%
      \hspace*{-\widthadjust}\makebox[0pt][l]{%
        \makebox[\topwidth][l]{%
          \ifx\@empty\fromlocation\else
          \makebox[0pt][l]{\rule[2.5ex]{\topwidth}{0.4pt}}%
          \ifx\@empty\info@one\else
          \info@one
          \fi
          \ifx\@empty\info@two\else
          \hfill\info@two
          \fi
          \ifx\@empty\info@three\else
          \hfill\info@three
          \fi
          \ifx\@empty\info@four\else
          \hfill\info@four
          \fi
          \ifx\@empty\info@five\else
          \hfill\info@five
          \fi
          \fi}}}\hfil}}

% First page without window envelopes
\def\ps@firstpage{%
     \def\@oddhead{}
     \def\@oddfoot{{\footnotesize \fromlocation \hfill \telephonenum}\hss}}

% Modify \opening:
\renewcommand{\opening}[1]{%
  \ifthenelse{\boolean{winenv}}{%
    \thispagestyle{winpage}
    \ifx\@empty\fromlocation\else
    \enlargethispage{-4\baselineskip}
    \vspace*{2.5cm}
    \fi
    \vspace*{2cm}
    }{%
    \ifx\@empty\fromaddress
    \thispagestyle{firstpage}%
    {\raggedleft\@date\par}%
    \else  % home address
    \thispagestyle{empty}
    {\raggedright 
      \ifx\@empty\fromname % In case you don't use \name
      \else
      \fromname \\
      \fi
      \fromaddress%
      \hfill\@date \par}
    \fi
    \vspace{2\parskip}%
    {\raggedright \toname \\ \toaddress \par}%
    \vspace{2\parskip}}%
  #1\par\nobreak}

% Modify closing
\renewcommand{\closing}[1]{\par\nobreak\vspace{\parskip}%
  \stopbreaks
  \noindent
%  \ifx\@empty\fromaddress\else
%  \hspace*{\longindentation}\fi
  \parbox{\indentedwidth}{\raggedright
       \ignorespaces #1\\[6\medskipamount]%
       \ifx\@empty\fromsig
           \fromname
       \else \fromsig \fi\strut}%
   \par}

\pagestyle{plain}
