How to automatically wrap the LI when it reaches the browser's right most end?

How to automatically wrap the LI when it reaches the browser's right most end?

I have a dynamic multi-level dropdown menu. When the rightmost level reaches, I would prefer having it expand on the left hand side instead of extending the browser's scrollbar.  Is this achievable through CSS or Jquery?

This is the Javascript which I am using:

function mainmenu(){
$(" #nav ul ").css({display: "none"}); // Opera Fix
$(" #nav li").hover(function(){
$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(400);
},function(){
$(this).find('ul:first').css({visibility: "hidden"});
});
}

 
 
 $(document).ready(function(){
mainmenu();
});







body{
font-size:0.85em;
font-family:Verdana, Arial, Helvetica, sans-serif;
}

#nav, #nav ul{
margin:0;
padding:0;
list-style-type:none;
list-style-position:outside;
position:relative;
line-height:1.5em; 
}

#nav a{
display:block;
padding:0px 5px;
border:1px solid #333;
color:#fff;
text-decoration:none;
background-color:#333;
}

#nav a:hover{
background-color:#fff;
color:#333;
}

#nav li{
float:left;
position:relative;
}

#nav ul {
position:absolute;
display:none;
width:12em;
top:1.5em;
}

#nav li ul a{
width:12em;
height:auto;
float:left;
}

#nav ul ul{
top:auto;
}

#nav li ul ul {
left:12em;
margin:0px 0 0 10px;
}

#nav li:hover ul ul, #nav li:hover ul ul ul, #nav li:hover ul ul ul ul{
display:none;
}
#nav li:hover ul, #nav li li:hover ul, #nav li li li:hover ul, #nav li li li li:hover ul{
display:block;
}



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
<script type='text/javascript' src='jquery-1.2.3.min.js'></script>
<script type='text/javascript' src='menu.js'></script>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />

</head>

<body><ul id="nav">
    <li><a href="#">1 HTML</a></li>
    <li><a href="#">2 CSS</a></li>
<li><a href="#">2 CSS</a></li>
<li><a href="#">2 CSS</a></li>
<li><a href="#">2 CSS</a></li>
<li><a href="#">2 CSS</a></li>
<li><a href="#">2 CSS</a></li>
<li><a href="#">2 CSS</a></li>
<li><a href="#">2 CSS</a></li>
<li><a href="#">2 CSS</a></li>
<li><a href="#">2 CSS</a></li>
<li><a href="#">2 CSS</a></li>
<li><a href="#">2 CSS</a></li>
<li><a href="#">2 CSS</a></li>
<li><a href="#">2 CSS</a></li>
<li><a href="#">2 CSS</a></li>
    <li><a href="#">3 Javascript </a>
      <ul>
            <li><a href="#">3.1 jQuery</a>
              <ul>
                    <li><a href="#">3.1.1 Download</a>
<ul>
                    <li><a href="#">3.1.1 Download</a>
         <ul>
                    <li><a href="#">3.1.1 Download</a>
         <ul>
                    <li><a href="#">3.1.1 Download</a></li>
                    <li><a href="#">3.1.2 Tutorial</a></li>
              </ul>
</li>
                    <li><a href="#">3.1.2 Tutorial</a></li>
              </ul>
</li>
                    <li><a href="#">3.1.2 Tutorial</a></li>
              </ul>
</li>
                    <li><a href="#">3.1.2 Tutorial</a></li>
              </ul>
            </li>
            <li><a href="#">3.2 Mootools</a></li>
            <li><a href="#">3.3 Prototype</a></li>
      </ul>
    </li>
</ul>
</body>
</html>