%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A simple CV style. Written by Max Shawabkeh. MIT License.                    %
% Required packages:                                                           %
%   microtype                                                                  %
%   shortlst                                                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{cv}[2011/05/21 Simple CV Format, v1.0]

% Inherit from the article class with hardcoded options.
\LoadClass[a4paper,onecolumn]{article}

% Get rid of .aux files since we don't use them.
\nofiles

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                               Import Packages                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Paragraphs are separated by vertical space and no indentation is used.
\usepackage{parskip}
\setlength{\parindent}{0pt}

% We want to use some basic colors.
\usepackage[usenames,dvipsnames]{color}

% Some of our commands take named arguments.
\usepackage{keyval}

% We use some logical control flow.
\usepackage{ifthen}

% We want to manually control the width of columns in tabular layouts.
\usepackage{tabularx}

% We have multi-column lists of small bullet points for skills.
\usepackage{shortlst}

% For width calculations.
\usepackage{calc}

% Use microtype to increase letter spacing in small caps.
\usepackage[tracking=smallcaps]{microtype}

% Use hyperref to better control links and PDF output.
\definecolor{@linkcolor}{RGB}{0,0,127}
\usepackage[bookmarks=false,
            pdfstartview=FitBH,
            colorlinks=true,
            urlcolor=@linkcolor]{hyperref}
% Make URLs use normal fonts.
\renewcommand{\url}[1]{\urlstyle{same}\href{#1}{\nolinkurl{#1}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                               Helper Utilities                               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Shortcut for Palatino small caps font, used for titles.
\newcommand{\cvtitlefont}[1]{{\fontfamily{ppl}\fontshape{sc}\selectfont#1}}

% Shortcut for Default bold font.
\newcommand{\cvheadfont}[1]{{\fontseries{b}\selectfont#1}}

% A shortcut for creating a vertical space that separates subsections of the CV.
\newcommand{\cvbreak}{\vspace{0.7em}\par}

% A shortcut for subsection headers.
\newcommand{\cvhead}[1]{
  \cvheadfont{#1}
  \vspace{5pt}
  \par
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                    Margins                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Use smaller margins.
\setlength{\hoffset}{-20pt}
\setlength{\voffset}{-40pt}
\setlength{\oddsidemargin}{0pt}
\setlength{\topmargin}{-8pt}
\setlength{\footskip}{0pt}
\setlength{\headsep}{20pt}
\setlength{\textheight}{701pt}
\setlength{\textwidth}{490pt}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                    Headers                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Commands to define header matter.
\newcommand{\@name}{}
\newcommand{\name}[1]{\renewcommand{\@name}{#1}}
\newcommand{\@address}{}
\newcommand{\address}[1]{\renewcommand{\@address}{#1}}
\newcommand{\@phone}{}
\newcommand{\phone}[1]{\renewcommand{\@phone}{#1}}
\newcommand{\@email}{}
\newcommand{\email}[1]{\renewcommand{\@email}{#1}}
\newcommand{\@site}{}
\newcommand{\site}[1]{\renewcommand{\@site}{#1}}

% 3-line fancy header containing the name and contact details.
\setlength{\headheight}{50pt}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\fancyhead[C]{
  \cvtitlefont{
    \huge{\@name} \\
    \vspace{5pt}
    \small{\@address} \\
    \small{
      \hypersetup{urlcolor=black}
      \@phone
      \hspace{1em} \textbullet \hspace{1em}
      \href{mailto:\@email}{\@email}
      \hspace{1em} \textbullet \hspace{1em}
      \href{http://\@site}{\@site}
      \hypersetup{urlcolor=@linkcolor}
    }
  }
}

% Redefine \maketitle to adjust the PDF title.
\renewcommand{\maketitle}{
  \hypersetup{
    pdfauthor = {Max Shawabkeh},
    pdftitle = {Max Shawabkeh, Curriculum Vitae}
  }
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                             Content Item Commands                            %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Inserts an experience subsection. Takes named arguments date, position,
% organization and location. Location is optional.
\newcommand{\@exp@date}{}
\define@key{experience}{date}{\renewcommand{\@exp@date}{#1}}
\newcommand{\@exp@position}{}
\define@key{experience}{position}{\renewcommand{\@exp@position}{#1}}
\newcommand{\@exp@organization}{}
\define@key{experience}{organization}{\renewcommand{\@exp@organization}{#1}}
\newcommand{\@exp@location}{}
\define@key{experience}{location}{\renewcommand{\@exp@location}{#1}}

\newcommand{\experience}[1]{
  % Clear old args.
  \renewcommand{\@exp@date}{}
  \renewcommand{\@exp@position}{}
  \renewcommand{\@exp@organization}{}
  \renewcommand{\@exp@location}{}
  % Set new args.
  \setkeys{experience}{#1}
  % Format and write the output.
  \begin{tabularx}{\linewidth}{@{}>{\setlength\hsize{115pt}}X X X}
    % First column.
    \cvheadfont{\@exp@date}
    % Second column.
    & \cvheadfont{\@exp@position}
    \\ & \cvheadfont{\@exp@organization}
    %& \cvheadfont{\@exp@position}
    %\ifthenelse{\equal{\@exp@location}{}}{}{
    %  \\ & \cvheadfont{\@exp@location}
    %}
  \end{tabularx}
  \par
}

% Inserts a reference item. Takes named arguments name, position and email.
\newcommand{\@cvref@name}{}
\define@key{cvref}{name}{\renewcommand{\@cvref@name}{#1}}
\newcommand{\@cvref@position}{}
\define@key{cvref}{position}{\renewcommand{\@cvref@position}{#1}}
\newcommand{\@cvref@email}{}
\define@key{cvref}{email}{\renewcommand{\@cvref@email}{#1}}

\newcommand{\referenceitem}[1]{
  % Clear old args.
  \renewcommand{\@cvref@name}{}
  \renewcommand{\@cvref@position}{}
  \renewcommand{\@cvref@email}{}
  % Set new args.
  \setkeys{cvref}{#1}
  % Write the name and position then float the email to the right.
  \item
    \begin{tabularx}{\linewidth+1.45em}{@{}X r@{}}
      \cvheadfont{\@cvref@name}, \@cvref@position
      &
      \href{mailto:\@cvref@email}{\@cvref@email}
    \end{tabularx}
    % Remove the extra vertical spacing added by the tabular environment.
    \vspace{-1em}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                           Main Section Environment                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% An environment for main CV sections - takes a title and indents its contents.
\newenvironment{cvsection}[1]{
  % Write the section title in small caps and draw a rule under it.
  \cvtitlefont{#1}
  \vspace{3pt}
  \textcolor{Gray}{\hrule}
  \vspace{5pt}

  % Indent the contents.
  \begin{list}{}{\setlength{\leftmargin}{70pt}}
  \item[]
}{
  % Finish intentation and add some vertical padding.
  \end{list}
  \vspace{11pt}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                           Bullet List Environments                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% An environment for CV bullet points. Simply an itemize list with no margins.
\newenvironment{cvbullets}{
  % Move the whole list to the left to align with the normal margin.
  \hspace{-17.5pt}
  \begin{minipage}[l]{\linewidth}
  % Start a normal item list.
  \begin{itemize}
  \setlength{\itemsep}{2pt}
}{
  \end{itemize}
  \end{minipage}
}

% An environment for multi-column CV bullet points with no margins. Takes the
% number of columns as a parameter.
\newenvironment{cvmultibullets}[1]{
  % Move the whole list to the left to align with the normal margin.
  \hspace{-17.5pt}
  \begin{minipage}[l]{\linewidth}
  % Start a short item list.
  \begin{shortitemize}
  % Resize the item to fit the specified number of columns.
  \setlength{\shortitemwidth}{1pt * \ratio{\linewidth}{#1pt} - \labelwidth - \labelsep}
  % Add some more space before lines of items.
  \setlength{\baselineskip}{16pt}
}{
  \end{shortitemize}
  \end{minipage}
}
