% Copyright 2015 by Marei Peischl
%
% This file may be distributed and/or modified
% under the LaTeX Project Public License
%

\def\fileversion{1.0}
\def\filedate{2015/01/28}
\ProvidesPackage{beamerouterthemeUR}[\filedate\space\fileversion\space
beamer-outertheme im Corporate Design der Universität Regensburg]

%Booleans
%22 Booleans zum aktivieren/deaktivieren der einzelnen Abteilungsfarben
\newcounter{UR@runner}
\RequirePackage{ifthen}
%Create Booleans UR@Dep@\the\value{UR@runner}
\def\newdep#1{\expandafter\@ifdefinable\csname ifUR@dep@#1\endcsname{%
		\expandafter\newif\csname ifUR@dep@#1\endcsname}}
\whiledo{\value{UR@runner}<23}{\stepcounter{UR@runner}\newdep{\the\value{UR@runner}}}
\newboolean{UR@coloredboxes}
\newboolean{UR@draft}
\newboolean{fsimphy}
\newboolean{UR@externalize}

%Counter-Definitionen
\newcounter{UR@dep}
\newcounter{UR@temp} %temporärer counter zum Abtasten
\newcounter{UR@color}
%Optionen zum aktivieren einzelner Farben
\DeclareOption{lov}{\setboolean{UR@dep@1}{true}\stepcounter{UR@dep}}
\DeclareOption{ffg}{\setboolean{UR@dep@2}{true}\stepcounter{UR@dep}}
\DeclareOption{asz}{\setboolean{UR@dep@3}{true}\stepcounter{UR@dep}}
\DeclareOption{rw}{\setboolean{UR@dep@4}{true}\stepcounter{UR@dep}}
\DeclareOption{ww}{\setboolean{UR@dep@5}{true}\stepcounter{UR@dep}}
\DeclareOption{kt}{\setboolean{UR@dep@6}{true}\stepcounter{UR@dep}}
\DeclareOption{pkgg}{\setboolean{UR@dep@7}{true}\stepcounter{UR@dep}}
\DeclareOption{pps}{\setboolean{UR@dep@8}{true}\stepcounter{UR@dep}}
\DeclareOption{slk}{\setboolean{UR@dep@9}{true}\stepcounter{UR@dep}}
\DeclareOption{bvm}{\setboolean{UR@dep@10}{true}\stepcounter{UR@dep}}
\DeclareOption{mat}{\setboolean{UR@dep@11}{true}\stepcounter{UR@dep}}
\DeclareOption{ph}{\setboolean{UR@dep@12}{true}\stepcounter{UR@dep}}
\DeclareOption{chp}{\setboolean{UR@dep@13}{true}\stepcounter{UR@dep}}
\DeclareOption{med}{\setboolean{UR@dep@14}{true}\stepcounter{UR@dep}}
\DeclareOption{ub}{\setboolean{UR@dep@15}{true}\stepcounter{UR@dep}}
\DeclareOption{zsk}{\setboolean{UR@dep@16}{true}\stepcounter{UR@dep}}
\DeclareOption{eur}{\setboolean{UR@dep@17}{true}\stepcounter{UR@dep}}
\DeclareOption{zhw}{\setboolean{UR@dep@18}{true}\stepcounter{UR@dep}}
\DeclareOption{rul}{\setboolean{UR@dep@19}{true}\stepcounter{UR@dep}}
\DeclareOption{zfw}{\setboolean{UR@dep@20}{true}\stepcounter{UR@dep}}
\DeclareOption{spo}{\setboolean{UR@dep@21}{true}\stepcounter{UR@dep}}
\DeclareOption{rz}{\setboolean{UR@dep@22}{true}\stepcounter{UR@dep}}
\DeclareOption{fsimphy}{\setboolean{UR@dep@23}{true}\stepcounter{UR@dep}\setboolean{fsimphy}{true}}
\DeclareOption{externalize}{\setboolean{UR@externalize}{true}}
\DeclareOption{coloredboxes}{\setboolean{UR@coloredboxes}{true}}
\DeclareOption{all}{\setcounter{UR@temp}{1}\whiledo{\value{UR@temp}<23}{\setboolean{UR@dep@\arabic{UR@temp}}{true}\stepcounter{UR@dep}\stepcounter{UR@temp}}}
\DeclareOption{faculties}{\setcounter{UR@temp}{4}\whiledo{\value{UR@temp}<15}{\setboolean{UR@dep@\arabic{UR@temp}}{true}\stepcounter{UR@dep}\stepcounter{UR@temp}}}
\ProcessOptions\relax
%Notwendige Pakete
\RequirePackage{calc}
\RequirePackage{tikz,pgf}
\usetikzlibrary{calc}
\RequirePackage[RGB]{URcolors}


\setcounter{UR@temp}{1}
\setcounter{UR@color}{0}
\ifthenelse{\boolean{UR@coloredboxes}}{
	\ifnum\value{UR@dep}=1
	\else
	\ClassError{urbeamer}{You selected more than one department}{coloredboxes are not available if more than one color has been choosen}
	\fi}{}

\ifnum\value{UR@dep}=1
\whiledo{\value{UR@temp}<23}{
	\ifthenelse{\boolean{UR@dep@\value{UR@temp}}}{\colorlet{UR@color@basic}{UR@color@\arabic{UR@temp}}
	}
	{}
	\stepcounter{UR@temp}
}\setcounter{UR@temp}{1}
\hypersetup{allcolors=UR@color@basic}
\else
\definecolor{UR@color@basic}{RGB}{142,142,141}
\fi

\ifnum\value{UR@dep}=1
\whiledo{\value{UR@temp}<24}{
	\ifthenelse{\boolean{UR@dep@\value{UR@temp}}}{\colorlet{UR@color@alert}{-UR@color@\arabic{UR@temp}}\colorlet{UR@color@example}{UR@color@\arabic{UR@temp}}}
	{}
	\stepcounter{UR@temp}
}
\setcounter{UR@temp}{1}
\else
\colorlet{UR@color@alert}{red}
\colorlet{UR@color@example}{green}
\fi

\mode<presentation>
%Notwendige Definitionen
\newlength{\x}%halber Radius der Kugel
\setlength{\x}{0.43cm}
\setlength{\unitlength}{\x}
%%    Breite der Farbstreifen festlegen
\newlength{\streifen}	
\setlength{\streifen}{\paperwidth-10\x}
%    \ifthenelse{\boolean{fsimphy}}{
%    \pgfdeclareimage[width=12\x]{titlepagelogo}{FSIlogotext}
%    }{
%    \pgfdeclareimage[width=12\x]{titlepagelogo}{urlogotext}} % placed on the title page
\newlength{\y} %halber radius kleine kugel in kopfzeile
\setlength{\y}{0.5\x}
%  	\ifthenelse{\boolean{fsimphy}}{\pgfdeclareimage[height=4\y]{headlinelogo}{FSIlogo}}{
%  	\pgfdeclareimage[height=4\y]{headlinelogo}{urlogo.pdf}}
\newlength{\hstreifen}
\setlength{\hstreifen}{0.5\paperwidth}
\addtolength{\hstreifen}{-3.75\y}
\pgfdeclareimage[height=4\y]{headlinelogo}{urlogo}
\pgfdeclareimage[width=12\x]{titlepagelogo}{urlogotext}
%\logo{\pgfuseimage{headlinelogo}}
\titlegraphic{\pgfuseimage{titlepagelogo}}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

%   	
%Option für tikzexternalize 
\ifthenelse{\boolean{UR@externalize}}{\usetikzlibrary{external}}{\newcommand\tikzexternalenable\relax\newcommand\tikzexternaldisable\relax} 

%
%Headline & Footline

\defbeamertemplate*{headline}{URtheme}{\tikzexternaldisable%
	\vspace*{-1mm}
	\begin{tikzpicture}[remember picture,line width=0pt]
	\node at (0,0) {};
	\node[anchor=north west] at (0.5\y,-0.5\y) {\pgfuseimage{headlinelogo}};
	\coordinate (NWHB) at ($(0.5\y,0)+7*(\y,0)$);
	\coordinate (SMHB) at ($0.5*(\paperwidth,-5\y)+3.5*(\y,0)$);
	\coordinate (NEHB) at (\paperwidth,0);
	\node[anchor=north west] at ($(SMHB)$){\hspace*{-.5\fboxsep}\parbox{0.4\paperwidth}{
			{\fontsize{6}{6.6}\selectfont\bfseries\insertshortauthor}\vspace*{1pt}
			
			{\fontsize{6}{6.6}\selectfont\insertshortinstitute}
		}};
		\end{tikzpicture}
		\ifthenelse{\boolean{UR@draft}}{
			\begin{tikzpicture}[overlay,remember picture,line width=0pt]
			\draw (NWHB)rectangle(SMHB)rectangle(NEHB);
			\end{tikzpicture}
		}{
		\begin{tikzpicture}[overlay,remember picture,line width=0pt]
		\filldraw[UR@reingrau] (NWHB)rectangle(SMHB);
		\end{tikzpicture}
		\ifthenelse{\boolean{UR@draft}}{}{
			\setcounter{UR@temp}{1}
			\setcounter{UR@color}{0}
			\whiledo{\value{UR@temp}<24}{
				\ifthenelse{\boolean{UR@dep@\value{UR@temp}}}{
					\begin{tikzpicture}[overlay,remember picture,line width=0pt]
					\filldraw[UR@color@\arabic{UR@temp}]($(SMHB)+\arabic{UR@color}*(\hstreifen/\arabic{UR@dep},0)$)rectangle ($(SMHB)+\arabic{UR@color}*(\hstreifen/\arabic{UR@dep},0)+(0,5\y)+(\streifen/\arabic{UR@dep},0)$);
					\end{tikzpicture}
					\stepcounter{UR@color}}
				{}
				\stepcounter{UR@temp}
			}
		}}\tikzexternalenable} 
	
\setbeamertemplate*{footline}{}
% Sidebar

\setbeamertemplate*{sidebar left}{}
\setbeamertemplate*{sidebar right}{}
\setbeamertemplate*{sidebar canvas left}{}
\setbeamertemplate*{sidebar canvas right}{}

% Frame title
\defbeamertemplate*{frametitle}{UR}{\parbox{\textwidth}{\usebeamerfont*{frametitle}\usebeamercolor[fg]{frametitle}\insertframetitle}\hfill}

%Title Page
\defbeamertemplate*{title page}{URtheme}{
	\tikzexternaldisable%
	\begin{tikzpicture}[shift={(-1,3.7)},remember picture,overlay]  %($(a) + 1/3*(1cm,0)$)
	\coordinate (NWC) at (0,0); % North West Corner of the slide
	\coordinate (NEC) at (\paperwidth,0); %North East Corner of the slide
	\coordinate (LC) at (0,-\paperheight); %lower left corner of the slide
	\coordinate (NWB) at ($10*(\x,0)-2/3*(0,\paperheight)$); %north west corner of the bar
	\coordinate (NEB) at ($(\paperwidth,0)-2/3*(0,\paperheight)$);
	\coordinate (SWB) at (10\x,-2/3\paperheight-3\x);
	\coordinate (SEB) at ($(\paperwidth,0)-2/3*(0,\paperheight)-(0,3\x)$);
	\coordinate (NWL) at (3\x,-2/3*\paperheight-\x);%north west corner of the logo
	\coordinate (NWT) at ($10*(\x,0)-1/3*(0,\paperheight)$); %north west of the title box
	\coordinate (SWA) at ($10*(\x,0)-2/3*(0,\paperheight)$); %south west of the author box
	\fill[UR@hellgrau](NWC)rectangle(NEB);
	\node[anchor=north west] at ($3*(\x,0)-2/3*(0,\paperheight)-(0,\x)$) {\inserttitlegraphic};
	\end{tikzpicture}
	\ifthenelse{\boolean{UR@draft}}{}{
		%Taste nun alle Einrichtungen ab
		\setcounter{UR@temp}{1}
		\setcounter{UR@color}{0}
		\whiledo{\value{UR@temp}<24}{
			\ifthenelse{\boolean{UR@dep@\value{UR@temp}}}{
				\begin{tikzpicture}[overlay,remember picture,line width=0pt]
				\filldraw[UR@color@\arabic{UR@temp}]($(NWB)+\arabic{UR@color}*(\streifen/\arabic{UR@dep},0)$)rectangle ($(NWB)+\arabic{UR@color}*(\streifen/\arabic{UR@dep},0)-(0,3\x)+(\streifen/\arabic{UR@dep},0)$);
				\end{tikzpicture}
				\stepcounter{UR@color}}
			{}
			\stepcounter{UR@temp}
		}
	}
	\vspace*{-1.3mm}\par
	\begin{tikzpicture}[overlay,remember picture]
	\node[anchor=north west] at ($2/12*(0,\paperheight)+10*(\x,0)-1/12*(\paperwidth,0)-(0.5mm,0)$) {%
		\begin{minipage}[t]{.65\paperwidth}
		{\usebeamerfont{title}\usebeamercolor[fg]{title}\inserttitle}\vspace*{1pt}
		
		{\usebeamerfont{subtitle}\usebeamercolor[fg]{subtitle}\insertsubtitle}
		\end{minipage}
	};
	\node[anchor=south west] at ($(SWA)+(-\fboxsep,0)$)
	{\begin{minipage}[b]{.65\paperwidth}
		{\usebeamerfont{date}\usebeamercolor[fg]{date}\insertdate}
		
		{\usebeamerfont{author}\usebeamercolor[fg]{author}\insertauthor}
		
		{\usebeamerfont{institute}\usebeamercolor[fg]{institute}\fontsize{9}{9.6}\selectfont\scshape\bfseries\insertinstitute}
		\end{minipage}
	};
	\end{tikzpicture}\tikzexternalenable
}%


\endinput