%% I got most of this code from 
%% http://tex.stackexchange.com/questions/15951/hyperlink-name-with-biblatex-authoryear-biblatex-1-4b
%% http://tex.stackexchange.com/questions/1687/hyperlink-name-with-biblatex-authoryear?rq=1


\NeedsTeXFormat{LaTeX2e}[1994/06/01]

\ProvidesFile{apahref.sty}
\ProvidesPackage{apahref}

\RequirePackage{biblatex}
\RequirePackage{xpatch,kvoptions}
\RequirePackage[american]{babel}

\DeclareBoolOption[true]{hyper}
\DeclareBoolOption[true]{apachange}

\ProcessKeyvalOptions*

\DeclareLanguageMapping{american}{american-apa}

\@ifpackageloaded{hyperref}{}{\PackageError{apahref}{ApaHref requires hyperref}{}\endinput}

% : for pages
\DeclareFieldFormat{postnote}{: #1}
\renewcommand{\postnotedelim}{}

% citeposs
\newcommand{\citeposs}[2][]{\citeauthor{#2}'s \parentext{\citeyear[#1]{#2}}}
\newcommand{\citealposs}[2][]{\citeauthor{#2}'s \citeyear[#1]{#2}}
\newcommand{\Citeposs}[2][]{\Citeauthor{#2}'s \parentext{\citeyear[#1]{#2}}}

%% HYPERREF %%
%% authoryear citestyle

\newbool{authoryear}
\setbool{authoryear}{false}

\@ifpackagewith{biblatex}{citestyle=authoryear-comp}{\setbool{authoryear}{true}}{}
\@ifpackagewith{biblatex}{citestyle=authoryear-icomp}{\setbool{authoryear}{true}}{}
\@ifpackagewith{biblatex}{citestyle=authoryear}{\setbool{authoryear}{true}}{}
% Combine label and labelyear links

\DeclareCiteCommand*{\citeyear}
    {}
    {\printtext[bibhyperref]{\printfield{year}\printfield{extrayear}}}
    {\multicitedelim}
    {\usebibmacro{postnote}}

\DeclareCiteCommand{\citeyear}
    {}
    {\printtext[bibhyperref]{\printfield{year}}}
    {\multicitedelim}
    {\usebibmacro{postnote}}

    \DeclareCiteCommand{\citeauthor}
  {\boolfalse{citetracker}%
   \boolfalse{pagetracker}%
   \usebibmacro{prenote}}
  {\ifciteindex
     {\indexnames{labelname}}
     {}%
   \printtext[bibhyperref]{\printnames{labelname}}}
  {\multicitedelim}
  {\usebibmacro{postnote}}

 \xpatchbibmacro{textcite}
   {\printnames{labelname}}
   {\printtext[bibhyperref]{\printnames{labelname}}}{}{}

    \xpatchbibmacro{cite}
   {\printnames{labelname}}
   {\printtext[bibhyperref]{\printnames{labelname}}}{}{}

       \xpatchbibmacro{cite:author}
   {\printnames{labelname}}
   {\printtext[bibhyperref]{\printnames{labelname}}}{}{}

}{%    apa's own citestyle
\newbibmacro*{cite}{%
  \iffieldequals{fullhash}{\cbx@lasthash}
% Multiple cites in one command
   {\setunit{\compcitedelim}%
    \usebibmacro{cite:plabelyear+extrayear}}%
% Single cite
   {\ifthenelse{\ifnameundef{labelname}\OR\iffieldequalstr{entrytype}{patent}}
% No author/editor
     {\usebibmacro{cite:noname}%
       \setunit{\nameyeardelim}%
       \usebibmacro{cite:plabelyear+extrayear}%
       \savefield{fullhash}{\cbx@lasthash}}
% Normal cite
     {\printtext[bibhyperref]{\ifnameundef{shortauthor}
        {\printnames[labelname][-\value{listtotal}]{labelname}}%
        {\cbx@apa@ifnamesaved
          {\printnames{shortauthor}}
          {\printnames[labelname][-\value{listtotal}]{author}\addspace\printnames[sabrackets]{shortauthor}}}%
      \setunit{\nameyeardelim}%
      \usebibmacro{cite:plabelyear+extrayear}%
      \savefield{fullhash}{\cbx@lasthash}}}}%
   \setunit{\multicitedelim}}

\newbibmacro*{textcite}{%
  \iffieldequals{fullhash}{\cbx@lasthash}
% Compact cite - more than one thing for same author
    {\setunit{\compcitedelim}%
     \usebibmacro{cite:plabelyear+extrayear}}
% New cite
    {%
    \ifbool{cbx:parens}
      {\bibcloseparen\global\boolfalse{cbx:parens}}
      {}%
      \setunit{\compcitedelim}%
      \ifthenelse{\ifnameundef{labelname}\OR\iffieldequalstr{entrytype}{patent}}
  % No author/editor or patent
       {\iffieldundef{shorthand}%
    % Cite using title
         {\usebibmacro{cite:noname}%
          \setunit{\ifbool{cbx:np}%
                   {\nameyeardelim}%
                   {\global\booltrue{cbx:parens}\addspace\bibopenparen}}%
          \usebibmacro{cite:plabelyear+extrayear}}
    % Cite using shorthand
         {\usebibmacro{cite:shorthand}}}
  % Normal cite with author/editor
  % Normal full cite
       {\ifnameundef{shortauthor}%
    % Normal full cite
         {\printtext[bibhyperref]{\printnames[labelname][-\value{listtotal}]{labelname}}}
    % Cite using short author
         {\cbx@apa@ifnamesaved
           {\printnames{shortauthor}}
           {\printnames[labelname][-\value{listtotal}]{author}}}%
  % Year
        \setunit{\ifbool{cbx:np}
                  {\nameyeardelim}
                  {\global\booltrue{cbx:parens}\addspace\bibopenparen}}%
  % Put the shortauthor inside the year brackets if necessary
        \ifnameundef{shortauthor}
         {}
         {\cbx@apa@ifnamesaved
           {}
           {\printnames{shortauthor}\setunit{\nameyeardelim}}}%
  % Actual year printing
        \usebibmacro{cite:plabelyear+extrayear}%
  % Save name hash for checks later
        \savefield{fullhash}{\cbx@lasthash}}}}

\DeclareCiteCommand{\citeauthor}
  {\usebibmacro{cite:init}%
   \usebibmacro{prenote}}
  {\usebibmacro{citeindex}%
   \printtext[bibhyperref]{\usebibmacro{cite:author}}}
  {}
  {\usebibmacro{postnote}%
   \usebibmacro{cite:post}}
}

%%%% CHANGES TO THE APA STYLE 

\ifapahref@apachange

\DeclareFieldFormat{volumeof}{\bibstring{volume}~#1~\bibstring{ofseries}}% 0.7.1

% Adapter from apa style file:
  
% New incollection
\DeclareBibliographyDriver{incollection}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/translator+others}%
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{title}%
  \newunit
  \printlist{language}%
  \newunit\newblock
  \usebibmacro{byauthor}%
  \newunit\newblock
%  \usebibmacro{in:}
\newblock
   \usebibmacro{editor+trans}%
   \setunit{\addcomma\addspace}
  \usebibmacro{maintitle+booktitle}%
%  \newunit\newblock
%  \usebibmacro{byeditor+others}%
  \newunit\newblock
  \printfield{edition}%
  \newunit
    \usebibmacro{chapter+pages} 
  \iffieldundef{maintitle}
    {\printfield[volumeof]{volume}%
     \printfield{part}}
    {}%
 % \setunit{\space}
  %\printfield[volumeof]{volume} 
  \newunit\newblock
  \printfield{note}%
  \newblock
%
   \setunit{\addspace}
  \usebibmacro{series+number}
  \usebibmacro{location+publisher}%
  \newunit\newblock
%  \usebibmacro{chapter+pages}%
  \newunit\newblock
  \iftoggle{bbx:isbn}
    {\printfield{isbn}}
    {}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \usebibmacro{addendum+pubstate}%
  \newunit\newblock
  \usebibmacro{related}%
  \usebibmacro{finentry}}

% no parentheses in thesis
\renewbibmacro*{type+institution}{%
  %\setunit{\addspace}%
  \ifthenelse{\iffieldundef{type}\AND\iffieldundef{institution}}
    {}
    {\printtext{%
       \iflistundef{institution}
         {\setunit*{\addspace}}
         {\setunit*{\addcolon\space}}%
       \printfield{type}%
       \setunit*{\addcomma\space}%
       \printlist{institution}%
       \setunit*{\addcomma\space}%
       \printlist{location}}%
     \newunit}}

% no capitalization of Eds., replace bibcpstring by bibstring
\renewbibmacro*{apaeditorstrg}[1]{%
  \iffieldundef{#1type}
    {\ifthenelse{\value{#1}>1\OR\ifandothers{#1}}
       {\bibstring{editors}}
       {\bibstring{editor}}}
    {\ifthenelse{\value{#1}>1\OR\ifandothers{#1}}
         {\bibstring{type\thefield{#1type}s}}
         {\bibstring{type\thefield{#1type}}}}}

\DeclareFieldFormat[incollection]{series}{\mkbibemph{#1}}

\AtEveryCitekey{\renewcommand*{\finalnamedelim}{%
    \ifthenelse{\value{listcount}>\maxprtauth}
      {}
      {\ifthenelse{\value{liststop}>2}
         {\finalandcomma\addspace\bibstring{and}\space}
         {\addspace\bibstring{and}\space}}}}

\AtBeginBibliography{\renewcommand*{\finalnamedelim}{%
    \ifthenelse{\value{listcount}>\maxprtauth}
      {}
      {\ifthenelse{\value{liststop}>2}
         {\finalandcomma\addspace\bibstring{and}\space}
         {\addspace\bibstring{and}\space}}}}

%keep capitalization
\DeclareFieldFormat{apacase}{#1}

\fi
\endinput