For my phd dissertation I would like to have a new environment for figures that gives me the possibility to either place the caption sideways if there is enough space (say the figure width is < 0.6 column width) otherwise places the caption below the figure. The sideways caption should switch left and right based on odd even page too, and it can stretch the line width limit in that part. An example for the sideways captions: say the figure width is 0.5\columnwidth, then the caption can start from 0.6\columnwidth to 1.2\columnwidth. Could someone give me the direction on how to realize this in latex?
For my thesis I am using xelatex - memoir. This is the type of sideways figures that I am looking for:

EDIT: By the helpful comment of Ipsen and some tweaking I am managed to reach to the thing that I want although not perfect yet.
Code:
\documentclass{memoir}
\usepackage{graphicx}
\usepackage[english]{babel}
\usepackage{lipsum}
\usepackage{calc}
\renewcommand*{\sidecapstyle}{%
\captionnamefont{\bfseries}%
\slshape%
}
\renewcommand*{\sidecapfloatwidth}{0.5\linewidth}
\setsidecaps{0.1\textwidth}{.55\textwidth}
\sidecapmargin{outer}
\setsidecappos{b}
\strictpagecheck
\sidecapraise 0.05\textheight
\begin{document}
\chapter{Captions}
\begin{figure}[tb]
\raggedright
\begin{sidecaption}{This is a subcaption just for illustration purposes. This is a subcaption just for illustration purposes.
This is a subcaption just for illustration purposes. Page number is \LARGE\textbf{\thepage}}[fig:test]
\includegraphics[width=\linewidth]{Picture}
\end{sidecaption}
\end{figure}
\lipsum[2-2]
\begin{figure}[tb]
\raggedleft
\begin{sidecaption}{This is a subcaption just for illustration purposes. This is a subcaption just for illustration purposes.
This is a subcaption just for illustration purposes - Page number is \LARGE\textbf{\thepage}}[fig:test]
\includegraphics[width=\linewidth]{Picture}
\end{sidecaption}
\end{figure}
\lipsum[1]
\end{document}
Page 1 of output:

Page 2 of output:

As you see page number in float is not correct, thus, I have problem automatically parsing \raggedleft and \raggedright after \begin{figure}. How does sidecaption find which page it is now?