Background
The following screenshot shows a Problem-Solution environment that is symmetric for twoside papers.
NOTE: The problem heading is on the right side for odd-numbered pages. For even-numbered pages, the heading is on the left side. Orphan-widow issues must also be taken into account such that there is no page break at the heading.

I already created this environment but it looks so complicated. Is there a package to do this out of the box?
Maximal Code Snippet (I cannot minimize my code anymore)
\documentclass[cmyk,10pt]{article}
\usepackage[a6paper,hmargin=1cm,vmargin=2cm]{geometry}
\usepackage{ifthen}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage{calc}
\newcounter{example}[section]
\renewcommand{\theexample}{\thesection.\arabic{example}}
\newenvironment{Example}[2][]%
{%
\refstepcounter{example}%
%------------------- Example Head -------------------
\par\nointerlineskip\vspace{\baselineskip}\noindent%
\label{example:\theexample}\color{blue}%
\ifthenelse{\isodd{\pageref{example:\theexample}}}%
{%
{\color{blue}\hrulefill}%
\raisebox{\depth}{%
\colorbox{blue}{%
\color{white}Example \theexample\ifthenelse{\equal{#1}{}}{}{\ (#1)}%
}%
}%
}
{%
\raisebox{\depth}{%
\colorbox{blue}{%
\color{white}Example \theexample\ifthenelse{\equal{#1}{}}{}{\ (#1)}%
}%
}%
{\color{blue}\hrulefill}%
}%
%------------------- Example Body -------------------
\par\nopagebreak\nointerlineskip\vspace{0.5\baselineskip}\noindent{\footnotesize#2}%
%------------------- Solution Head -------------------
\par\nointerlineskip\vspace{0.5\baselineskip}\noindent%
\colorbox{red}{\makebox[\linewidth-2\fboxsep]{\color{white}Solution}}%
%------------------- Solution Body -------------------
\par\nopagebreak\nointerlineskip\vspace{0.5\baselineskip}\noindent%
\color{red}\footnotesize\ignorespaces%
}
{%
%------------------- Solution Foot -------------------
\par\nopagebreak\nointerlineskip\vspace{0.5\baselineskip}\noindent{\color{red}\hrulefill}%
\par\nointerlineskip\vspace{\baselineskip}\noindent\ignorespacesafterend%
}
\usepackage{multido}
\begin{document}
\section{Without Title}
\begin{Example}
{%
\lipsum[1]
}%
\lipsum[2]
\end{Example}
\section{With Title}
\begin{Example}[With Title]
{%
\lipsum[1]
}%
\lipsum[2]
\end{Example}
\section{Looping}
\multido{\ix=1+1}{10}{%
\begin{Example}[\ix]
{%
\lipsum[\ix]
}%
Einstein's formula
\begin{equation}
E\not=mc^2
\end{equation}
\lipsum[\ix]
\end{Example}\lipsum[\ix]}
\end{document}
Or if there is no package, could you give me a suggestion to do it in a better way?