\RequirePackage{ifpdf}
\RequirePackage{xkeyval}
\RequirePackage{ifthen}
\RequirePackage{calc}
\RequirePackage{graphicx}
\RequirePackage{color}
\RequirePackage{colortbl}
\RequirePackage{fancybox}

\newif\if@vartwo\@vartwofalse % for variant two column layout

%%%%%%%%%%%%%%%%%%%%%%% CAPTION %%%%%%%%%%%%%%%%%%%%%%%%%%
%\newcommand\capfont{\rmfamily\small\bfseries}
%\captionnamefont{\capfont}
%\captiontitlefont{\capfont}
%\captiondelim{:~}

\setlength\abovecaptionskip{.25ex plus .125ex minus .125ex}
\setlength\belowcaptionskip{.25ex plus .125ex minus .125ex}

\newlength\nulllength
\setlength\nulllength{0pt}

\newfixedcaption{\tabcaption}{table}
\newcommand{\tabcaptionstyle}{}
\newcommand{\settabcaptionstyle}[1]{\renewcommand\tabcaptionstyle{\captionstyle{#1}}}

\newfixedcaption{\figcaption}{figure}
\newcommand{\figcaptionstyle}{}
\newcommand{\setfigcaptionstyle}[1]{\renewcommand\figcaptionstyle{\captionstyle{#1}}}

%%%%%%%%%%%%%%%%%%%%%%% ILLUSTRATION %%%%%%%%%%%%%%%%%%%%%%%
%\define@boolkey{style}{floating}[true]{}

\newcommand{\figscale}{1}
\newcommand\setfigscale[1]{\renewcommand\figscale{#1}}

\newcommand\orgscale{}
\newcommand\restorefigscale{\let\figscale\orgscale}
\newenvironment{FigScale}[1]{\setfigscale{#1}}{\restorefigscale}

\newlength\figprotrude

\newlength{\leftboxwidth}
\newlength{\rightboxwidth}
\newlength{\interwidth}
\setlength\interwidth{.75em}
\newlength{\illustwidth}

\newlength\fixedwidth

%\newcommand\exlcudegraphics{%
%	\renewcommand\includegraphics[2][\@empty]{\LARGE FIGURE HERE}
%}

% PF stands for placefigure %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newsavebox{\PFbox}
\newlength{\PFboxwidth}
\newlength{\PFboxheight}

\newlength\PFtopspace
\newlength\PFbottomspace
\setlength\PFtopspace{2.5\parskip}
\setlength\PFbottomspace{2.5\parskip}
\newcommand\PFtopskip{\vspace{\PFtopspace}\par\noindent}
\newcommand\PFbottomskip{\vspace{\PFbottomspace}\par}

\newcommand\PFcaption{}
\newcommand\PFcaptionfont{}
\newcommand\PFcaptionstyle{}
\newcommand\PFlabel{}
\newcommand\PFjustify{}

\newif\ifPF@float\PF@floatfalse
\define@key{placefigure}{float}[true]{%
	\ifthenelse{\equal{#1}{true}}{%
		\PF@floatrue%
	}{%
		\PF@floatfalse%
	}%
}

\newif\ifPF@caption\PF@captionfalse
\define@key{placefigure}{caption}{%
	\PF@captiontrue%
	\renewcommand\PFcaption{#1}%
}
\define@key{placefigure}{captionfont}{%
	\renewcommand\PFcaptionfont{#1}%
}
\define@key{placefigure}{captionstyle}{%
	\renewcommand\PFcaptionstyle{#1}%
}

\newif\ifPF@label\PF@labelfalse
\define@key{placefigure}{label}{%
	\PF@labeltrue%
	\renewcommand\PFlabel{#1}%
}

\define@key{placefigure}{figurejustify}{%
	\renewcommand\PFjustify{#1}%
}

\define@key{placefigure}{topspace}{%
	\setlength\PFtopspace{#1}
}
\define@key{placefigure}{bottomspace}{%
	\setlength\PFbottomspace{#1}
}

\newcommand\setplacefigure[1]{%
	\presetkeys{placefigure}{#1}{}
}

\setplacefigure{%
	float=false,
	captionfont=\rmfamily\small\bfseries,
	captionstyle=\figcaptionstyle
}

\newcommand\resetplacefigure{%
	\PF@captionfalse%
	\PF@labelfalse%
}

\newcommand\placefigure[2][]{%
\resetplacefigure
\setkeys{placefigure}{#1}%
\ifPF@label\else%
	\renewcommand\PFlabel{#2}%
\fi%
\sbox{\PFbox}{\includegraphics[scale=\figscale]{#2}}%
\ifPF@float%
	\begin{figure}[!hbt]%
		\putPF%
	\end{figure}%
\else%
	\PFtopskip%
	\begin{minipage}{\linewidth}%
		\putPF%
	\end{minipage}%
	\PFbottomskip% 
\fi%
}

\newcommand\putPF{%
\if@vartwo%
	\settowidth{\PFboxwidth}{\usebox{\PFbox}}%
	\ifdim \PFboxwidth > \linewidth%
		\addtolength{\PFboxwidth}{-\linewidth}%
		\hspace*{-1\PFboxwidth}%
	\fi%
\fi%
\PFjustify\usebox{\PFbox}%
\ifPF@caption%
	\PFcaptionstyle%
	\ifPF@float%
		\vspace{1.25ex}\PFcaptionfont\caption{\PFcaption}\label{\PFlabel}%
	\else%
		\PFcaptionfont\figcaption{\PFcaption}\label{\PFlabel}%
	\fi%
\fi%
}

% IF stands for illustfigure %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% to align a figure with its text
\newlength\figalignment
\setlength\figalignment{-.5\baselineskip}
\newcommand{\figalign}{\mbox{}\\[\figalignment]}

\newlength\IFtopspace
\newlength\IFbottomspace
\setlength\IFtopspace{1.25\parskip}
\setlength\IFbottomspace{1.25\parskip}
\newcommand\IFtopskip{\vspace{\IFtopspace}\par\noindent}
\newcommand\IFbottomskip{\vspace{\IFbottomspace}\par}

\newsavebox{\IFbox}
\newlength{\IFboxwidth}
\newlength{\IFboxheight}

\newcommand\IFcaption{}
\newcommand\IFcaptionfont{}
\newcommand\IFcaptionstyle{}
\newcommand\IFlabel{}
\newcommand\IFstyle{}
\newcommand\IFjustify{\raggedleft}
\newcommand\IFalign{t}
\newlength\IFfigurewidth
\newlength\IFprotrude
\newlength\IFframerule
\newlength\IFframesep
\setlength\IFframerule{.25pt}
\setlength\IFframesep{2.5pt}

\newif\ifIF@caption\IF@captionfalse
\define@key{illustfigure}{caption}{%
	\IF@captiontrue%
	\renewcommand\IFcaption{#1}%
}
\define@key{illustfigure}{captionfont}{%
	\renewcommand\IFcaptionfont{#1}%
}
\define@key{illustfigure}{captionstyle}{
	\renewcommand\IFcaptionstyle{#1}%
}
\newif\ifIF@label\IF@labelfalse
\define@key{illustfigure}{label}{%
	\IF@labeltrue
	\renewcommand\IFlabel{#1}%
}

\define@key{illustfigure}{textstyle}{%
	\renewcommand\IFstyle{#1}%
}

\newif\ifIF@left\IF@leftfalse
\define@key{illustfigure}{figureposition}{%
	\ifthenelse{\equal{#1}{left}}{%
		\IF@lefttrue%
	}{%
		\IF@leftfalse%
	}%
}

\newif\ifIF@fixed\IF@fixedfalse % if to use a fixed width for figure
\define@key{illustfigure}{figurewidth}{%	
	\setlength\IFfigurewidth{#1}%
	\ifdim \IFfigurewidth > \nulllength%
		\IF@fixedtrue%				
	\else%
		\IF@fixedfalse%
	\fi%
}

\newif\ifIF@protrude\IF@protrudefalse % to disable protruding for the vartwo layout
\define@key{illustfigure}{protrude}{%
	\ifthenelse{\equal{#1}{true}}{%
		\IF@protrudetrue%
		\setlength\IFprotrude{\figprotrude}%		
	}{%
		\IF@protrudefalse%
		\setlength\IFprotrude{0pt}%
	}%
}

\newif\ifIF@frame\IF@framefalse
\define@key{illustfigure}{frame}{%
	\ifthenelse{\equal{#1}{true}}{%
		\IF@frametrue%
	}{%
		\IF@framefalse%
	}%
}
\define@key{illustfigure}{framerule}{
	\setlength\IFframerule{#1}
}
\define@key{illustfigure}{framesep}{
	\setlength\IFframesep{#1}
}


\define@key{illustfigure}{figurejustify}{%
	\renewcommand\IFjustify{#1}%
}

\define@key{illustfigure}{align}{
	\ifthenelse{\equal{#1}{top}}{\renewcommand\IFalign{t}}{}
	\ifthenelse{\equal{#1}{middle}}{\renewcommand\IFalign{m}}{}
	\ifthenelse{\equal{#1}{bottom}}{\renewcommand\IFalign{b}}{}
}

\define@key{illustfigure}{topspace}{%
	\setlength\IFtopspace{#1}
}
\define@key{illustfigure}{bottomspace}{%
	\setlength\IFbottomspace{#1}
}

\newcommand\setillustfigure[1]{%
	\presetkeys{illustfigure}{#1}{}%
}

\newcommand\resetillustfigure{%
	\IF@captionfalse%
	\IF@labelfalse%	
}

\setillustfigure{%
	captionfont=\rmfamily\small\bfseries,
	captionstyle=\figcaptionstyle,
	textstyle=\sloppy,
	protrude=true,
	figureposition=left,
	figurewidth=0pt,
	frame=false
}

\newcommand\illustfigure[3][]{%
\resetillustfigure%
\setkeys{illustfigure}{#1}%
\ifIF@label\else%
	\renewcommand\IFlabel{#2}%
\fi%
\ifthenelse{\not\equal{#2}{}}{%
	\ifIF@frame
		\setlength\fboxrule{\IFframerule}
		\setlength\fboxsep{\IFframesep}
		\sbox{\IFbox}{\fbox{\includegraphics[scale=\figscale]{#2}}}%
	\else
		\sbox{\IFbox}{\includegraphics[scale=\figscale]{#2}}%
	\fi
}{%
	\sbox{\IFbox}{}%
}%
\putIF{#3}%
}

\newcommand\putIF[1]{%
\ifIF@fixed
	\setlength{\IFboxwidth}{\IFfigurewidth}
\else
	\settowidth{\IFboxwidth}{\usebox{\IFbox}}%
\fi
\ifIF@left%
	\setlength\leftboxwidth{\IFboxwidth}%
	\setlength{\rightboxwidth}{\illustwidth-\leftboxwidth-\interwidth}%
\else%
	\setlength\rightboxwidth{\IFboxwidth}%
	\setlength{\leftboxwidth}{\illustwidth-\rightboxwidth-\interwidth}%
\fi%
\if@vartwo
	\ifIF@left%
		\addtolength\rightboxwidth{\IFprotrude}%
	\else%
		\addtolength\leftboxwidth{\IFprotrude}%
	\fi%
\fi%
\IFtopskip%
\begin{minipage}[t]{\illustwidth}%
	\hspace*{-\IFprotrude}%
	\begin{minipage}[\IFalign]{\leftboxwidth}%
		\putIFleft{#1}%
	\end{minipage}%
		\hspace{\interwidth}%
	\begin{minipage}[\IFalign]{\rightboxwidth}%
		\putIFright{#1}%
	\end{minipage}%
\end{minipage}%
\IFbottomskip%
}

\newcommand\putIFleft[1]{%
\ifIF@left%
	\figalign\IFjustify\usebox{\IFbox}\putIFcaption%
\else%
	\IFstyle#1%
\fi%
}

\newcommand\putIFright[1]{%
\ifIF@left%
	\IFstyle#1%
\else%	
	\figalign\IFjustify\usebox{\IFbox}\putIFcaption%
\fi%
}

\newcommand\putIFcaption{%
\ifIF@caption%
	\IFcaptionstyle\IFcaptionfont\figcaption{\IFcaption}\label{\IFlabel}%
\fi%
}

\newcommand\fixedillustfigure[3][]{%
\illustfigure[figurewidth=\fixedwidth, #1]{#2}{#3}%
}

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

\newcommand\linefigscale{}%
\let\linefigscale\figscale%
\newcommand\setlinefigscale[1]{\renewcommand\linefigscale{#1}}%

\newcommand\linefig[2][\@empty]{%
\ifx #1\@empty \raisebox{-.35ex}{\includegraphics[scale=\linefigscale]{#2}}% 
\else \raisebox{#1}{\includegraphics[scale=\linefigscale]{#2}}% 
\fi% 
}

\newcommand\figname{}
\newcommand\setfigname[1]{\renewcommand\figname{#1}}

%%%%%%%%%%%%%%%%%%%%%%% TABULAR %%%%%%%%%%%%%%%%%%%%%%%%%
%\renewcommand\arraystretch{1}

\definecolor{textgray}{gray}{0.4}
\definecolor{backgray}{gray}{0.9}
\definecolor{buttongray}{gray}{0.2}
\definecolor{watermarkgray}{gray}{0.95}
\gdef\RowColor{backgray}

\newcounter{rowcnt}
\newcommand\startgrayrow{\setcounter{rowcnt}{0}}
\newcommand\startwhiterow{\setcounter{rowcnt}{-1}}
\newcommand\grayrow{}
\newcommand\grayrowoff{\renewcommand\grayrow{}}
\newcommand\grayrowon{
\renewcommand\grayrow{%         
        \rowcolor{\RowColor}%    
        \ifodd\therowcnt% 
                \gdef\RowColor{white}% 
        \else%                 
                \gdef\RowColor{backgray}% 
        \fi%   
        \stepcounter{rowcnt}%       
}}

\newcommand\grayheadrow{\rowcolor{backgray}}

\setlength\heavyrulewidth{.75pt}
\setlength\lightrulewidth{.25pt}
\newcommand\tabfont{\sffamily\small}

% PT stands for placetable %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newsavebox{\PTbox}
\newlength{\PTboxwidth}
\newlength{\PTboxheight}

\newlength\PTtopspace
\newlength\PTbottomspace
\setlength\PTtopspace{1.25\parskip}
\setlength\PTbottomspace{1.25\parskip}
\newcommand\PTtopskip{\vspace{\PFtopspace}\par\noindent}
\newcommand\PTbottomskip{\vspace{\PFbottomspace}\par}

\newcommand\PTcaption{}
\newcommand\PTcaptionfont{}
\newcommand\PTcaptionstyle{}
\newcommand\PTlabel{}
\newcommand\PTfont{}
\newcommand\PTstyle{}

\newif\ifPT@float\PT@floatfalse
\define@boolkey{placetable}{float}[true]{%
	\ifthenelse{\equal{#1}{true}}{%
		\PT@floattrue
	}{%
		\PT@floatfalse
	}%
}

\newif\ifPT@caption\PT@captionfalse
\define@key{placetable}{caption}{%
	\PT@captiontrue%
	\renewcommand\PTcaption{#1}%
}
\define@key{placetable}{captionfont}{%
	\renewcommand\PTcaptionfont{#1}%
}
\define@key{placetable}{captionstyle}{%
	\renewcommand\PTcaptionstyle{#1}%
}

\newif\ifPT@label\PT@labelfalse
\define@key{placetable}{label}{%
	\PT@labeltrue%
	\renewcommand\PTlabel{#1}%
}

\define@key{placetable}{tabularfont}{%
	\renewcommand\PTfont{#1}%
}
\define@key{placetable}{tabularstyle}{%
	\renewcommand\PTstyle{#1}%
}

\define@key{placetable}{topspace}{%
	\setlength\PTtopspace{#1}
}
\define@key{placetable}{bottomspace}{%
	\setlength\PTbottomspace{#1}
}

\newcommand\setplacetable[1]{%
	\presetkeys{placetable}{#1}{}%
}

\newcommand\resetplacetable{%
	\PT@captionfalse%
	\PT@labelfalse%
}

\setplacetable{
	float=false,
	captionfont=\rmfamily\small\bfseries,
	captionstyle=\tabcaptionstyle,
	tabularfont=\sffamily\small,
	tabularstyle=\tabcaptionstyle
}


\newcommand\placetable[2][]{%
\resetplacetable
\setkeys{placetable}{#1}%
\sbox{\PTbox}{\PTfont#2}%
\ifPT@float%
	\begin{table}[!hbt]%
		\putPT%
	\end{table}%
\else%
	\PTtopskip%
	\begin{minipage}{\linewidth}%
		\putPT%
	\end{minipage}%
	\PTbottomskip%
\fi%
}

\newcommand\putPT{%
\if@vartwo%
	\settowidth{\PTboxwidth}{\usebox{\PTbox}}%
	\ifdim \PTboxwidth > \linewidth%
		\addtolength{\PTboxwidth}{-\linewidth}%
		\begin{adjustwidth}{-\PTboxwidth}{0pt}%
		\putPTx%
		\end{adjustwidth}%
	\else%
		\putPTx%
	\fi%
\else%
	\putPTx%
\fi%
}

\newcommand\putPTx{%
\ifPT@caption%
	\PTcaptionstyle%
	\ifPT@float%
		\PTcaptionfont\caption{\PTcaption}\ifPT@label\label{\PTlabel}\fi\vspace{1.25ex}%
	\else%
		\PTcaptionfont\tabcaption{\PTcaption}\ifPT@label\label{\PTlabel}\fi%
	\fi%
\fi%
\PTstyle\usebox{\PTbox}%
}

% IT stands for illusttable %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% to align a tabular with its text
\newlength\tabalignment
\setlength\tabalignment{-.5\baselineskip}
\newcommand{\tabalign}{\mbox{}\\[\tabalignment]}

\newlength\ITtopspace
\newlength\ITbottomspace
\setlength\ITtopspace{1.25\parskip}
\setlength\ITbottomspace{1.25\parskip}
\newcommand\ITtopskip{\vspace{\ITtopspace}\par\noindent}
\newcommand\ITbottomskip{\vspace{\ITbottomspace}\par}

\newsavebox{\ITbox}
\newlength{\ITboxwidth}
\newlength{\ITboxheight}

\newcommand\ITcaption{}
\newcommand\ITcaptionfont{}
\newcommand\ITcaptionstyle{}
\newcommand\ITlabel{}
\newcommand\ITfont{}
\newcommand\ITstyle{}
\newcommand\ITjustify{\raggedleft}
\newcommand\ITalign{t}
\newlength\ITtablewidth
\newlength\ITprotrude

\newif\ifIT@caption\IT@captionfalse
\define@key{illusttable}{caption}{%
	\IT@captiontrue%
	\renewcommand\ITcaption{#1}%
}
\define@key{illusttable}{captionfont}{%
	\renewcommand\ITcaptionfont{#1}%
}
\define@key{illusttable}{captionstyle}{
	\renewcommand\ITcaptionstyle{#1}%
}
\newif\ifIT@label\IT@labelfalse
\define@key{illusttable}{label}{%
	\IT@labeltrue
	\renewcommand\ITlabel{#1}%
}

\define@key{illusttable}{tabularfont}{%
	\renewcommand\ITfont{#1}%
}

\define@key{illusttable}{textstyle}{%
	\renewcommand\ITstyle{#1}%
}

\newif\ifIT@left\IT@leftfalse
\define@key{illusttable}{tableposition}{%
	\ifthenelse{\equal{#1}{left}}{%
		\IT@lefttrue
	}{%
		\IT@leftfalse
	}%
}

\newif\ifIT@fixed\IT@fixedfalse
\define@key{illusttable}{tablewidth}{%
	\setlength\ITtablewidth{#1}%
	\ifdim \ITtablewidth > \nulllength%
		\IT@fixedtrue%		
	\else%
		\IT@fixedfalse%
	\fi%
}

\newif\ifIT@protrude\IT@protrudefalse % to disable protruding for the vartwo layout
\define@key{illusttable}{protrude}[true]{
	\ifthenelse{\equal{#1}{true}}{
		\IT@protrudetrue
		\setlength\ITprotrude{\figprotrude}%		
	}{
		\IT@protrudefalse
		\setlength\ITprotrude{0pt}%
	}
}

\define@key{illusttable}{tablejustify}{%
	\renewcommand\ITjustify{#1}%
}

\define@key{illusttable}{align}{
	\ifthenelse{\equal{#1}{top}}{\renewcommand\ITalign{t}}{}
	\ifthenelse{\equal{#1}{middle}}{\renewcommand\ITalign{m}}{}
	\ifthenelse{\equal{#1}{bottom}}{\renewcommand\ITalign{b}}{}
}

\define@key{illusttable}{topspace}{%
	\setlength\ITtopspace{#1}
}
\define@key{illusttable}{bottomspace}{%
	\setlength\ITbottomspace{#1}
}

\newcommand\setillusttable[1]{%
	\presetkeys{illusttable}{#1}{}%
}

\newcommand\resetillusttable{%
	\IT@captionfalse%
	\IT@labelfalse%
}

%\hangcaption
\setillusttable{
	captionfont=\rmfamily\small\bfseries,
	captionstyle=\tabcaptionstyle,
	tabularfont=\sffamily\small,
	textstyle=\sloppy,
	protrude=true,
	tableposition=left,
	tablewidth=0em
}

\newcommand\illusttable[3][]{%
\resetillusttable%
\setkeys{illusttable}{#1}%
\sbox{\ITbox}{\ITfont#2}%
\putIT{#3}%
}

\newcommand\putIT[1]{%
\ifIT@fixed
	\setlength{\ITboxwidth}{\ITtablewidth}
\else
	\settowidth{\ITboxwidth}{\usebox{\ITbox}}%
\fi
\ifIT@left%
	\setlength\leftboxwidth{\ITboxwidth}%
	\setlength{\rightboxwidth}{\illustwidth-\leftboxwidth-\interwidth}%
\else%
	\setlength\rightboxwidth{\ITboxwidth}%
	\setlength{\leftboxwidth}{\illustwidth-\rightboxwidth-\interwidth}%
\fi%
\if@vartwo
	\ifIT@left%
		\addtolength\rightboxwidth{\ITprotrude}%
	\else%
		\addtolength\leftboxwidth{\ITprotrude}%
	\fi%
\fi%
\ITtopskip%
\begin{minipage}[t]{\illustwidth}%
	\hspace*{-\ITprotrude}%
	\begin{minipage}[\ITalign]{\leftboxwidth}%
		\putITleft{#1}%
	\end{minipage}%
		\hspace{\interwidth}%
	\begin{minipage}[\ITalign]{\rightboxwidth}%
		\putITright{#1}%
	\end{minipage}%
\end{minipage}%
\ITbottomskip%
}

\newcommand\putITleft[1]{%
\ifIT@left%
	\tabalign\ITjustify\usebox{\ITbox}\putITcaption%
\else%
	\ITstyle#1%
\fi%
}

\newcommand\putITright[1]{%
\ifIT@left%
	\ITstyle#1%
\else%	
	\tabalign\ITjustify\usebox{\ITbox}\putITcaption%
\fi%
}

\newcommand\putITcaption{%
\ifIT@caption%
	\ITcaptionstyle%
	\ITcaptionfont\tabcaption{\ITcaption}\label{\ITlabel}%
\fi%
}

\AtBeginDocument{

\let\orgscale\figscale
\setlength\illustwidth{\linewidth}
\ifthenelse{\boolean{@vartwo}}{
	\setlength\figprotrude{\marginparwidth}
}{
	\setlength\figprotrude{0pt}
}
\setlength\fixedwidth{\figprotrude}
%\let\illustfig\illustfigure
%\let\placefig\placefigure
\setfigscale{.9}
\setlinefigscale{.925}
}