Conditional "if a package is available"

Use \IfFileExists. Here's a code snippet from my idxlayout package that tests for the availability of ragged2e.

\AtBeginDocument{%
  \IfFileExists{ragged2e.sty}{%
    \RequirePackage{ragged2e}%
    \renewcommand*{\ila@RaggedRight}{\RaggedRight}%
  }{%
    \PackageWarning{idxlayout}{%
      Package ragged2e not available, therefore\MessageBreak
      substituting command raggedright for RaggedRight\MessageBreak
    }%
    \renewcommand*{\ila@RaggedRight}{\raggedright}%
  }%
}

\documentclass{article}

\IfFileExists{mdframed.sty}
  {\usepackage{mdframed}
   %\newenvironment{framedbox}
   \let\framed\mdframed
   \let\endframed\endmdframed
  }
  {\usepackage{framed}
   %\newenvironment{framedbox}... 
  }

\begin{document}

\begin{framed}
foo
\end{framed}

\end{document}