% TODO major cleanup necessary!

%    timetable.sty - The LaTeX timetable stylesheet V1.4
%    http://www.planetk.de/index.php?title=Stundenplan
%
%    Copyright (C) 2007-2009 Pascal Gwosdek
%    Modifications (C) 2008 Daniel Bader
%
%    This program is free software; you can redistribute it and/or modify it
%    under the terms of the GNU General Public License as published by the
%    Free Software Foundation; either version 3 of the License, or (at your
%    option) any later version.
%
%    This program is distributed in the hope that it will be useful, but
%    WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
%    Public License for more details.
%
%    You should have received a copy of the GNU General Public License along
%    with this program; if not, see <http://www.gnu.org/licenses/>.
%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{timetable}
\RequirePackage{ifthen, calc, color}

% swtich case helper
\newcommand\addcase[3]{\expandafter\def\csname\string#1@case@#2\endcsname{#3}}
\newcommand\makeswitch[2][]{%
  \newcommand#2[1]{%
    \ifcsname\string#2@case@##1\endcsname\csname\string#2@case@##1\endcsname\else#1\fi%
  }%
}
\makeswitch[nada]\dothis


% Preset the colors
\definecolor{ttframecol1}{rgb}{0.95,0.95,0.95}
\definecolor{ttframecol2}{rgb}{0.9,0.9,0.9}
\definecolor{ttfontcolor}{rgb}{0.25,0.25,0.25}
\definecolor{ttlinecol1}{rgb}{0.0,0.0,0.0}
\definecolor{ttlinecol2}{rgb}{0.5,0.0,0.0}

% Simple for loop construct as proposed by N. Setzer
\newcommand{\@ttforloop}[5][1]
{
  \setcounter{#2}{#3}
  \ifthenelse{#4}
  {
    #5
    \addtocounter{#2}{#1}
    \@ttforloop[#1]{#2}{\value{#2}}{#4}{#5}
  }
  {}
 }

% SansSerif font
\newcommand{\timetablefont}{\sffamily}
\newcommand{\@ttfont}{\timetablefont\selectfont}

% Dimensions
\newlength{\@rowheight}
\newlength{\@rowhght}
\newlength{\@colwidth}
\newlength{\@colwdth}
\newlength{\@offset}
\newcounter{@rowcount}
\newcounter{@colcount}
\newlength{\@textframe}
\newlength{\@bottomspace}
\newlength{\@totalheight}
\newlength{\@totalwidth}
\newlength{\@toprowheight}

% Arbitrary stuff
\newcounter{@timemarks}
\newcounter{@daymarks}
\newcounter{@topheight}
\newcounter{@printtimestamps}
\newlength{\@cornerradius}
\newcounter{@frametype}
\newcounter{@framevalign}

% Predefines
\setlength{\@cornerradius}{3pt}
\setcounter{@frametype}2
\setcounter{@framevalign}2
\setlength{\@rowheight}{1.2cm}
\setlength{\@colwidth}{2.8cm}
\setlength{\@textframe}{0.8mm}
\setlength{\@bottomspace}{8pt}
\setcounter{@rowcount}9
\setcounter{@colcount}5
\setcounter{@topheight}{2}
\setcounter{@printtimestamps}{0}
\newcommand{\@bottomstyle}{\scriptsize}
\newcommand{\@timestyle}{\tiny}

% Event declarations
\newlength{\@startposx}
\newlength{\@startposy}
\newlength{\@eventheight}
\newlength{\@newcolwidth}
\newlength{\@newrowheight}
\newlength{\@newcolleft}
\newlength{\@newrowtop}

% Helpers
\newcounter{@hourslot}
\newcounter{@durationslots}
\newcounter{@minutesperslot}
\newcounter{@timetablehelp}

\newcounter{@day}
\newcounter{@column}
\newcounter{@row}
\newcounter{@hour}
\newcounter{@minute}
\newcounter{@minuteStep}
\newcounter{@startHour}
\newcounter{@startMinute}
\newcounter{@endHour}
\newcounter{@endMinute}
\newcounter{@hoursset}
\setcounter{@hoursset}{0}
\newcounter{@hourzero}

\newcommand{\seteventcornerradius}[1]
{
  \setlength{\@cornerradius}{#1}
  \ifthenelse{\strip@pt\@cornerradius > 7}
  {
    \setlength{\@cornerradius}{7pt}
  }
  {
    \ifthenelse{\strip@pt\@cornerradius < 0}
    {
      \setlength{\@cornerradius}{0pt}
    }
    {}
  }
}

\newcommand{\settopheight}[1]
{
    \addtocounter{@rowcount}{#1-\value{@topheight}}
    \setcounter{@topheight}{#1}
}

\newcommand{\setframetype}[2][c]
{
  % Set frame type
  \ifthenelse{#2 < 1}
  {
    % Lines
    \setcounter{@frametype}{1}
  }
  {
    \ifthenelse{#2 > 2}
    {
      % Checkerboard
      \setcounter{@frametype}{2}
    }
    {
      \setcounter{@frametype}{#2}
    }
  }

  % Caption vertical alignment
  \ifthenelse{\equal{#1}{b}}
  {\setcounter{@framevalign}{1}}
    {\ifthenelse{\equal{#1}{t}}
    {\setcounter{@framevalign}{0}}
    {\setcounter{@framevalign}{2}}
  }
}

\newcommand{\setprinttimestamps}[1]
{
    \setcounter{@printtimestamps}{#1}
}

\newcommand{\setbottomstyle}[1]
{
    \renewcommand{\@bottomstyle}{#1}
}

\newcommand{\settimestyle}[1]
{
    \renewcommand{\@timestyle}{#1}
}

\newcommand{\setbottomspace}[1]
{
    \setlength{\@bottomspace}{#1}
}

\newcommand{\@fmtTime}[2]
{
    % Arguments: {hour}{minute}
    \ifthenelse{\value{#1} < 10}{0}{}\arabic{#1}:\ifthenelse{\value{#2} < 10}{0}{}\arabic{#2}
}

\newcommand{\slotevent}[9]
{
    % Arguments: {x}{y}{duration}{title}{lecturer}{place}{type}{textcolor}
    \renewcommand{\baselinestretch}{0.75}\normalsize

    \setlength{\@startposx}{#1\@colwidth-0.5pt}
    \setlength{\@startposy}{-#2\@rowheight-1.0pt}
    \setlength{\@eventheight}{#3\@rowheight-0pt}
    \addtolength{\@startposy}{-\@eventheight}

    \setlength{\@colwdth}{\@colwidth*\real{0.5}}
    \addtolength{\@startposx}{-\@colwdth}
    \setlength{\@offset}{-2pt}

    % \addcase\dothis{0}{\setlength{\@colwdth}{\@colwidth}}
    \addcase\dothis{0}{\addtolength{\@colwdth}{\@colwidth*\real{0.5}}}
    \addcase\dothis{6}{
      \addtolength{\@colwdth}{\@colwidth*\real{0.5}}
    }
    \addcase\dothis{1}{}
    \addcase\dothis{7}{}
    \addcase\dothis{2}{\addtolength{\@startposx}{\@colwdth}\addtolength{\@offset}{\@colwdth}}
    \addcase\dothis{3}{
      \addtolength{\@startposx}{\@colwidth-10pt}
      % \addtolength{\@offset}{60pt}
      \setlength{\@colwdth}{10pt}
      % \addtolength{\@offset}{-\@colwidth-0.5pt}
      % \addtolength{\@offset}{\@colwdth}
    }
    % \addcase\dothis{4}{\addtolength{\@colwdth}{\@colwidth*\real{0.5}}}
    \addcase\dothis{4}{
      % \addtolength{\@startposx}{\@colwidth-10pt}
      % \addtolength{\@offset}{60pt}
      \setlength{\@colwdth}{10pt}
      % \addtolength{\@colwdth}{-10pt}
      % \addtolength{\@offset}{-\@colwidth-0.5pt}
      % \addtolength{\@offset}{\@colwdth}
    }
    \addcase\dothis{5}{
      \newlength{\@manuOffset}
      \setlength{\@manuOffset}{7pt}
      \addtolength{\@colwdth}  {-\@manuOffset*\real{2}+\@colwidth*\real{0.5}}
      \setlength{\@eventheight}{-\@manuOffset*\real{2}+#3\@rowheight}
      \addtolength{\@startposx}{\@manuOffset}
      \addtolength{\@startposy}{\@manuOffset}
      % % \addtolength{\@offset}{60pt}
      % \setlength{\@colwdth}{10pt}
      % % \addtolength{\@offset}{-\@colwidth-0.5pt}
      % % \addtolength{\@offset}{\@colwdth}
    }

    \dothis{#9}

    \addtolength{\@startposx}{\@cornerradius}
    \put(\strip@pt\@startposx,\strip@pt\@startposy){\textcolor{@timetable#8block}
      {\rule[-\dp\strutbox]{\@colwdth-2\@cornerradius}{\@eventheight}}}
    \addtolength{\@startposx}{-\@cornerradius}
    \addtolength{\@startposy}{\@cornerradius}
    \put(\strip@pt\@startposx,\strip@pt\@startposy){\textcolor{@timetable#8block}
      {\rule[-\dp\strutbox]{\@colwdth}{\@eventheight-2\@cornerradius}}}
    \addtolength{\@startposy}{-\@cornerradius}

    % Event borders
    \addtolength{\@startposy}{-0.25pt}
    % \addtolength{\@startposy}{+0.5pt}
    \put(\strip@pt\@startposx,\strip@pt\@startposy){\textcolor{ttlinecol1}{\rule[-\dp\strutbox]{\@colwdth}{0.5pt}}}
    % \addtolength{\@startposy}{+0.5pt}
    % \addtolength{\@startposy}{+0.5pt}
    \addtolength{\@startposy}{\@eventheight}
    \put(\strip@pt\@startposx,\strip@pt\@startposy){\textcolor{ttlinecol1}{\rule[-\dp\strutbox]{\@colwdth+0.5pt}{0.5pt}}}
    % \addtolength{\@startposy}{-0.5pt}
    % \addtolength{\@eventheight}{-0.5pt}
    \addtolength{\@startposy}{-\@eventheight}
    \put(\strip@pt\@startposx,\strip@pt\@startposy){\textcolor{ttlinecol1}{\rule[-\dp\strutbox]{0.5pt}{\@eventheight}}}
    \addtolength{\@startposx}{\@colwdth}
    \put(\strip@pt\@startposx,\strip@pt\@startposy){\textcolor{ttlinecol1}{\rule[-\dp\strutbox]{0.5pt}{\@eventheight}}}

    \setlength{\@cornerradius}{2\@cornerradius}

    \ifthenelse{\strip@pt\@cornerradius > 0}
    {
      % Left rounded corners
      \addtolength{\@startposx}{.5\@cornerradius}
      \addtolength{\@startposy}{.5\@cornerradius-\dp\strutbox}
      \put(\strip@pt\@startposx,\strip@pt\@startposy){\textcolor{@timetable#8block}{\circle*{\strip@pt\@cornerradius}}}
      \addtolength{\@startposy}{-.5\@cornerradius+\dp\strutbox}
      \addtolength{\@startposy}{-.5\@cornerradius-\dp\strutbox+\@eventheight}
      \put(\strip@pt\@startposx,\strip@pt\@startposy){\textcolor{@timetable#8block}{\circle*{\strip@pt\@cornerradius}}}
      \addtolength{\@startposy}{.5\@cornerradius+\dp\strutbox-\@eventheight}
      \addtolength{\@startposx}{-.5\@cornerradius}

      % Right rounded corners
      \addtolength{\@startposx}{-.5\@cornerradius+\@colwidth-1pt}
      \addtolength{\@startposy}{.5\@cornerradius-\dp\strutbox}
      \put(\strip@pt\@startposx,\strip@pt\@startposy){\textcolor{@timetable#8block}{\circle*{\strip@pt\@cornerradius}}}
      \addtolength{\@startposy}{-.5\@cornerradius+\dp\strutbox}
      \addtolength{\@startposy}{-.5\@cornerradius-\dp\strutbox+\@eventheight}
      \put(\strip@pt\@startposx,\strip@pt\@startposy){\textcolor{@timetable#8block}{\circle*{\strip@pt\@cornerradius}}}
      \addtolength{\@startposy}{.5\@cornerradius+\dp\strutbox-\@eventheight}
      \addtolength{\@startposx}{.5\@cornerradius-\@colwidth-1pt}
    }{}

    \setlength{\@cornerradius}{0.5\@cornerradius}

    \setlength{\@newrowheight}{\@rowheight}
    \addtolength{\@newrowheight}{-2\@textframe-1pt}
    \setlength{\@newcolwidth}{\@colwidth}
    \addtolength{\@newcolwidth}{-2\@textframe-1pt}

    \setlength{\@newrowtop}{-#2\@rowheight}
    \addtolength{\@newrowtop}{-2\@textframe}
    \setlength{\@newcolleft}{#1\@colwidth+.5pt}
    \addtolength{\@newcolleft}{\@textframe}
    \ifthenelse{#9 = 0 \OR #9 = 5 \OR #9 = 6}{
      \addtolength{\@newcolleft}{-\@colwidth*\real{0.5}}
      \ifthenelse{#9 = 5}{
        \addtolength{\@newcolleft}{ \@manuOffset}
        \addtolength{\@newrowtop} {-\@manuOffset}
      }{}
      \ifthenelse{#9 = 6}{
        \addtolength{\@newcolleft}{10pt}
      }{}
    }{
      \addtolength{\@newcolleft}{-\@colwdth}
    }

    \ifthenelse{\value{@hoursset} = 0}{\setcounter{@printtimestamps}{0}}{}

    % Title
    \ifthenelse{#9 = 7}{
      \addtolength{\@newcolleft}{10pt}
    }{}
    \ifthenelse{#9 = 3}{
      \addtolength{\@newcolwidth}{-\@colwidth*\real{0.5}}
      \addtolength{\@newcolwidth}{11pt}
      \addtolength{\@newrowtop}{-10pt}
    } {}
    \ifthenelse{#9 = 4}{
      % \addtolength{\@newcolwidth}{-\@colwidth*\real{0.5}}
      % \setlength{\@colwdth}{\@colwidth*\real{0.5}}
      \addtolength{\@newcolwidth}{11pt}
      \addtolength{\@newrowtop}{-\@eventheight*\real{0.33}}
      \addtolength{\@newcolleft}{-\@colwidth}
      \addtolength{\@newcolleft}{9.25pt}
    } {}
    \ifthenelse{#9 = 5}{
      % \addtolength{\@newcolwidth}{-\@colwidth*\real{0.5}}
      \addtolength{\@newcolwidth}{-\@manuOffset}
    } {}
    \addtolength{\@newrowtop}{-10pt}
    \put(\strip@pt\@newcolleft,\strip@pt\@newrowtop)
        {\begin{minipage}[t]{\@newcolwidth}%
          \hspace*{\@offset}
          \@ttfont\raggedright\color{@timetable#8text}%
            \ifthenelse{\value{@printtimestamps} > 0}%
              {{\@timestyle \@fmtTime{@startHour}{@startMinute}}%
                \ifthenelse{\value{@printtimestamps} = 2}%
                  {{\@timestyle -- \@fmtTime{@endHour}{@endMinute}}}%
                  {}%
              \\}%
              {}%
              \ifthenelse{#9 = 0 \OR #9 = 6 \OR #9 = 5}{%
                \textbf{#4}%
                \ifthenelse{\equal{#4}{Abschlussplenum}}{
                  \\\transparent{.7}#5 %
                }{
                  \ifthenelse{\equal{#4}{Nach der Anreise}}{
                    \\#5%
                   }{
                    \hfill\transparent{.7}#5 \hspace*{10pt}%
                    \ifthenelse{#9 = 6}{
                      \hspace*{10pt}%
                    }{}
                  }
                }
              }{}%
              \ifthenelse{#9 = 1 \OR #9 = 7}{%
                {\textbf{#4} \\ \transparent{.7}#5 }%
              }{}%
              \ifthenelse{#9 = 2}{%
                {\textbf{#4} \\ \hspace*{\@offset} \transparent{.7}#5 }%
              }{}%
              \ifthenelse{#9 = 3 \OR #9 = 4}{%
                {\centering \textbf{#4}}%
              }{}%
         \end{minipage}}
    \addtolength{\@newrowtop}{1pt}

    \renewcommand{\baselinestretch}{0.5}\normalsize
    \setlength{\@newrowtop}{-#2\@rowheight-#3\@rowheight}
    \addtolength{\@newrowtop}{2\@textframe + \@bottomspace}

    \addtolength{\@newrowtop}{-13pt}
    % Lecturer
    \put(\strip@pt\@newcolleft,\strip@pt\@newrowtop)
        {\begin{minipage}[t]{1.0\@newcolwidth}%
          \hspace*{\@offset}
	  \@ttfont\raggedright\color{@timetable#8text}{\@bottomstyle #6%
            \vphantom{A}}
            \ifthenelse{\value{@printtimestamps} = 1}%
              {{\@timestyle \@fmtTime{@endHour}{@endMinute}}}%
              {}%
          \end{minipage}}

    \setlength{\@newcolleft}{#1\@colwidth + \@colwidth}
    \addtolength{\@newcolleft}{-\@textframe-0.4\@newcolwidth}

    % Location
    % \put(\strip@pt\@newcolleft,\strip@pt\@newrowtop)
    %   {\begin{minipage}[t]{0.4\@newcolwidth}%
    %     \@ttfont\raggedleft\color{@timetable#7text}{\@bottomstyle #6%
    %       \vphantom{A}}%
    %   \end{minipage}}

    \renewcommand{\baselinestretch}{1.0}\normalsize
  }

\newcommand{\event}[9]
  {
    % Arguments: {@day}{@start}{@end}{title}{lecturer}{place}{type}

    % Use the time format, if hours are defined, and a slot number otherwise:
    \ifthenelse{\value{@hoursset} > 0}
    {
      \setcounter{@startHour}{#2 / 100}
      \setcounter{@startMinute}{#2 - #2 / 100 * 100}

      \setcounter{@endHour}{#3 / 100}
      \setcounter{@endMinute}{#3 - #3 / 100 * 100}
	
      \setcounter{@hourslot}{((#2 - \value{@hourzero}) / 100 * (60 / \value{@minutesperslot})) + 2 + ((#2 - #2 / 100 * 100) / \value{@minutesperslot})}
      \setcounter{@durationslots}{((#3 - \value{@hourzero}) / 100 * (60 / \value{@minutesperslot})) + 2 + ((#3 - #3 / 100 * 100) / \value{@minutesperslot}) - \value{@hourslot}}

      % Compensate for 24h wrap
      \ifthenelse{\value{@hourslot} < 0}{\addtocounter{@hourslot}{(24 * (60 / \value{@minutesperslot}))}}{}

      \addtocounter{@hourslot}{\value{@topheight}-2}
    }
    {
      \setcounter{@hourslot}{#2}
      \setcounter{@durationslots}{#3-#2}
    }

    \slotevent{#1}{\value{@hourslot}}{\value{@durationslots}}{#4}{#5}{#6}{#7}{#8}{#9}
  }

\newcommand{\legend}[3]
  {
    % Arguments: <horizontal> <vertical> <text>
    % Lets you manually set slot contents (use this really rarely and
    % SEE: \daymark, \timemark )

    \setlength{\@newrowtop}{\@rowheight*(-#2+1)-\dp\strutbox}
    \setlength{\@newcolleft}{#1\@colwidth-\@colwidth}
    \ifthenelse{#2 = 1}{
      \addtolength{\@newcolleft}{-\@colwidth*\real{0.5}}
    } {
    }

    \ifthenelse{\value{@hoursset} = 0}
      {\setcounter{@minutesperslot}{60}}
      {}

    % Select frame type
    \ifthenelse{\value{@frametype} = 1}
    { % Lines
      % \ifthenelse{#1 = 1}{\put(\strip@pt\@newcolleft,\strip@pt\@newrowtop){\textcolor{black}{\rule{\@colwidth}{1pt}}}}{}
      % TODO change here 
      \addtolength{\@newrowtop}{-\value{@topheight}\@rowheight}

      \addtolength{\@newrowtop}{-2.5pt-\@rowheight*(\value{@rowcount} -4)-\dp\strutbox}
      % \addtolength{\@newrowtop}{-50pt}

      \addtolength{\@newcolleft}{-0.5pt}
      % \ifthenelse{#2 = 1}{\put(\strip@pt\@newcolleft,\strip@pt\@newrowtop){\textcolor{black}{\rule{0.5pt}{\value{@topheight}\@rowheight}}}}{}
      \ifthenelse{#2 = 1}{
        % \ifthenelse{\equal{#3}{Mittwoch}}{}{
          \put(\strip@pt\@newcolleft,\strip@pt\@newrowtop){\textcolor{black}{\rule{0.5pt}{\@rowheight*(\value{@rowcount})-\dp\strutbox}}}
         % }
      }{}
      \addtolength{\@newcolleft}{+0.5pt}
      \addtolength{\@newrowtop}{+2.5pt+\@rowheight*(\value{@rowcount} -4)+\dp\strutbox}
      % \addtolength{\@newcolleft}{1pt}
      \addtolength{\@newrowtop}{\value{@topheight}\@rowheight}
    }
    { % Checkerboard
      % Horizontal stripe
      \ifthenelse{#2 = 1}
        {\setcounter{@timetablehelp}{#1 * \real{0.5} * \real{2.0}}
          \ifthenelse{\value{@timetablehelp} = #1}
            {\addtolength{\@newrowtop}{-\value{@topheight}\@rowheight}
             \put(\strip@pt\@newcolleft,\strip@pt\@newrowtop)
               {\textcolor{ttframecol2}{\rule{\@colwidth}{\value{@topheight}\@rowheight}}}
             \addtolength{\@newrowtop}{\value{@topheight}\@rowheight}
            }
            {}
        }
        {}
      % Vertical stripe
      \ifthenelse{#1 = 1}
        {\setcounter{@timetablehelp}{(((#2-\value{@topheight}-1) * \value{@minutesperslot} / 120) * 2) - ((#2-\value{@topheight}-1) * \value{@minutesperslot} / 60)}
          \ifthenelse{\value{@timetablehelp} = 0}
            {\setcounter{@timetablehelp}{60 / \value{@minutesperslot}}
             \addtolength{\@newrowtop}{-\value{@timetablehelp}\@rowheight}
             \put(\strip@pt\@newcolleft,\strip@pt\@newrowtop)
              {\textcolor{ttframecol2}{\rule{\@colwidth}{\value{@timetablehelp}\@rowheight}}}
             \addtolength{\@newrowtop}{\value{@timetablehelp}\@rowheight}
            }
            {}
        }
        {}
    }

    % Print text
    % \ifthenelse{#1 = 1}{
    %   \addtolength{\@newcolleft}{\@textframe-100pt}
    %   }{
    %   \addtolength{\@newcolleft}{\@textframe}
    % }
    \addtolength{\@newcolleft}{\@textframe}
    \addtolength{\@newrowtop}{-\@textframe-\heightof{\large #3}}

    % Optionally move caption to middle or bottom
    \ifthenelse{\value{@framevalign} > 0}
    {
    \addtolength{\@newrowtop}{(- \value{@topheight}\@rowheight + 2\@text    frame + \heightof{\Large #3})/\value{@framevalign}}  
    }

    \ifthenelse{#2 = 1}{
      \setlength{\@newcolleft}{#1\@colwidth-\@colwidth}
      \addtolength{\@newcolleft}{-\@colwidth*\real{0.25}}
      \put(\strip@pt\@newcolleft,\strip@pt\@newrowtop){
        \makebox[\@colwidth*\real{0.5}]{\textcolor{ttfontcolor}
        {\@ttfont\large \textbf{#3}}}}
    } {
      \put(\strip@pt\@newcolleft,\strip@pt\@newrowtop){
        \makebox[\@colwidth*\real{0.37}]{\textcolor{ttfontcolor}
        {\@ttfont\large \hfill \textbf{#3}}}}
    }
  }

\newcommand{\timemark}[1]
  {
    \stepcounter{@timemarks}
    \legend 1 {\value{@timemarks}} {#1}
  }

\newcommand{\daymark}[1]
  {
    \stepcounter{@daymarks}
    \legend {\value{@daymarks}} 1 {#1}
  }

\newcommand{\defineevent}[7]
  {
    \definecolor{@timetable#1block}{rgb}{#2, #3, #4}
    \definecolor{@timetable#1text} {rgb}{#5, #6, #7}
  }

\newcommand{\setslotsize}[2]
  {
    % Arguments: <width> <height>
    % Defines the slot size

    \setlength{\@rowheight}{#2}
    \setlength{\@colwidth}{#1}
  }

\newcommand{\setslotcount}[2]
  {
    % Arguments: <days> <hours per day>
    % Defines the number of slots in both dimensions

    \setcounter{@colcount}{#1}
    \setcounter{@rowcount}{#2+\value{@topheight}}

    \stepcounter{@colcount}
  }

\newcommand{\settextframe}[1]
  {
    % Arguments: <width>
    % Defines the width of the border of each entry (i.e. the
    % distance between outer border and text)

    \setlength{\@textframe}{#1}
  }

\newcommand{\hours}[3]
  {
    % Arguments: <start hour> <minute stepping> <print end time?>
    % Automatically sets the time slots to full hours, beginning with <start hour>

    \setcounter{@hoursset}{1}
    \setcounter{@hourzero}{(#1 * 100)}

    \setcounter{@hour}{#1}
    \setcounter{@minute}{0}
    \setcounter{@minuteStep}{#2}
    \setcounter{@minutesperslot}{\value{@minuteStep}}

    \@ttforloop{@row}{\value{@topheight}}{\value{@row} < \value{@rowcount}}
    {
      \ifthenelse{\value{@minute} = 0}{
	  	\timemark{\@fmtTime{@hour}{@minute}\ifthenelse{#3 > 0}{-\setcounter{@hour}{\value{@hour}+1}\@fmtTime{@hour}{@minute}\setcounter{@hour}{\value{@hour}-1}}{}}
	  }{\stepcounter{@timemarks}}
	  \addtocounter{@minute}{#2}
	  \ifthenelse{\value{@minute} = 60}{\stepcounter{@hour}\setcounter{@minute}{0}}{}	
      \ifthenelse{\value{@hour} > 23}{\addtocounter{@hour}{-24}}{}
    }
  }

\newcommand{\germandays}[1]
  {
    % Arguments: <number of start day>
    % Macro for the english week days

    \setcounter{@day}{#1}

    \@ttforloop{@column}{1}{\value{@column} < \value{@colcount}}
    {
      \ifthenelse{\equal{\value{@day}}1}{\daymark{Montag}}{}
      \ifthenelse{\equal{\value{@day}}2}{\daymark{Dienstag}}{}
      \ifthenelse{\equal{\value{@day}}3}{\daymark{Mittwoch}}{}
      \ifthenelse{\equal{\value{@day}}4}{\daymark{Donnerstag}}{}
      \ifthenelse{\equal{\value{@day}}5}{\daymark{Freitag}}{}
      \ifthenelse{\equal{\value{@day}}6}{\daymark{Samstag}}{}
      % \ifthenelse{\equal{\value{@day}}5}{\daymark{Sonntag}}{}

      \stepcounter{@day}
      \ifthenelse{\value{@day} > 7}{\addtocounter{@day}{-7}}{}
    }
  }

\newenvironment{timetable}%
  {\noindent%
    % Arguments: None
    % Starts a new timetable
    %
    % Set the new layout setting
    \setlength{\@totalheight}{-\value{@rowcount}\@rowheight}%
    \setlength{\@totalwidth}{\value{@colcount}\@colwidth}%
    \addtolength{\@totalwidth}{-\@colwidth*\real{0.5}}%
    %
    % Reset the counters
    \setcounter{@timemarks}{\value{@topheight}}%
    \setcounter{@daymarks}{1}%
    %
    % Minipage to correct bounding box
    \begin{minipage}[t][-\@totalheight]{\@totalwidth}
    %
    % Begin. Note the offset hack of the picture, this is to make it consistent to the old pstricks package. 
    \begin{picture}(\strip@pt\@totalwidth,\strip@pt\@totalheight)(0,-\strip@pt\baselineskip)
      % Draw the grey frame
      \put(0,\strip@pt\@totalheight){\textcolor{ttframecol1}{\rule[-\dp\strutbox]{\@totalwidth}{-\@rowheight}}}
      \setlength{\@toprowheight}{\value{@topheight}\@rowheight}
      \put(0,-\strip@pt\@toprowheight){\textcolor{white}{\rule[-\dp\strutbox]{\@totalwidth}{\@toprowheight}}}

      % Test
      % \put(0,\strip@pt\@rowheight){\textcolor{ttframecol1}{\rule[-\dp\strutbox]{\@totalwidth}{-\@rowheight}}}
      \setlength{\@toprowheight}{\value{@topheight}\@rowheight}
      \setlength{\@rowhght}{\@rowheight}
      \addtolength{\@rowhght}{\@rowheight}
      \addtolength{\@rowhght}{\@rowheight}
      \addtolength{\@rowhght}{\@rowheight}
      \newcounter{@x}
      \setcounter{@x} 5
    \@ttforloop{@row}{\value{@topheight}}{\value{@row} < \value{@x}}
    {
           \addtolength{\@toprowheight}{\@rowhght}
      \put(0,-\strip@pt\@toprowheight){\textcolor{ttframecol1}{\rule[-\dp\strutbox]{\@totalwidth}{\@rowhght}}}
      \addtolength{\@toprowheight}{\@rowhght}
      \put(0,-\strip@pt\@toprowheight){\textcolor{ttframecol2}{\rule[-\dp\strutbox]{\@totalwidth}{\@rowhght}}}
      \addtolength{\@toprowheight}{\@rowhght}
      \put(0,-\strip@pt\@toprowheight){\textcolor{ttframecol1}{\rule[-\dp\strutbox]{\@totalwidth}{\@rowhght}}}
     \addtolength{\@toprowheight}{\@rowhght}
      \put(0,-\strip@pt\@toprowheight){\textcolor{ttframecol2}{\rule[-\dp\strutbox]{\@totalwidth}{\@rowhght}}}
    }
 
        \addtolength{\@toprowheight}{\@rowhght}
      \put(0,-\strip@pt\@toprowheight){\textcolor{ttframecol1}{\rule[-\dp\strutbox]{\@totalwidth}{\@rowhght}}}

      }
  {
      % % Draw the contours
      % % Inner
      % \addtolength{\@colwidth}{-1pt}
      % \put(\strip@pt\@colwidth,-\strip@pt\@toprowheight){\textcolor{ttlinecol2}{\rule[-\dp\strutbox]{\@totalwidth-\@colwidth+1pt}{1pt}}}
      % \put(\strip@pt\@colwidth,\strip@pt\@totalheight){\textcolor{ttlinecol2}{\rule[-\dp\strutbox]{1pt}{-\@totalheight-\@toprowheight}}}
      % \addtolength{\@colwidth}{1pt}

      % % Outer
      % \put(0,0){\textcolor{ttlinecol1}{\rule[-\dp\strutbox]{\@totalwidth}{1pt}}}
      % \put(0,\strip@pt\@totalheight){\textcolor{ttlinecol1}{\rule[-\dp\strutbox]{1pt}{-\@totalheight}}}
      % \put(\strip@pt\@totalwidth,\strip@pt\@totalheight){\textcolor{ttlinecol1}{\rule[-\dp\strutbox]{1pt}{-\@totalheight+1pt}}}
      % \addtolength{\@totalheight}{-0.5pt}
      % \put(0,\strip@pt\@totalheight){\textcolor{ttlinecol1}{\rule[-\dp\strutbox]{\@totalwidth}{1pt}}}
      % \addtolength{\@totalheight}{0.5pt}
    \end{picture}
    \end{minipage}
  }
