Rotate through DIVs via Prev & Next Button; unknown # of divs
<p>This question is very similar to a few other questions on here, but with a twist.  The number of divs can be anything between none and a dozen, and the named DIV is the same for each object to be rotated.  Code below:</p>

<p>PHP:</p>

<pre><code>if($job-&gt;getPictureGalleries() != null){
$gallerys = $job-&gt;getPictureGalleries();
foreach($gallerys as $gallery){
    $gallery = Doctrine_Core::getTable('Gallery')-&gt;find($gallery);
    echo "&lt;div class=\"galleries\"&gt;";
    include_partial('gallery/record', array('gallery' =&gt; $gallery));
    echo "&lt;/div&gt;";
}
if(count($gallerys) &gt; 1){
    $buttons = &lt;&lt;&lt;EOD
&lt;table style="box-shadow: 0.2em 0.2em 0.3em grey; width: 60%; margin-left: 20%; margin-top: 15px; background-color: whitesmoke; border: 1px solid grey; border-radius: 15px; height: 20px"&gt;
&lt;tr style="width: 100%; "&gt;
 &lt;td style="width: 33%; text-align: right"&gt;
    &lt;a href="#" id="previous_link"&gt;&lt;img src="/web/images/previous.png" height="35" width="35" /&gt;&lt;/a&gt;
 &lt;/td&gt;
 &lt;td style="
     width: 33%;
     color: #444444; 
     font-weight: bold;
     text-align: center;
     text-shadow: 0.1em 0.1em 0.3em grey;
 "&gt;
    More Galleries
 &lt;/td&gt;
 &lt;td style="width: 33%"&gt;
    &lt;a href="#" id="show_next_gallery" &gt;&lt;img src="/web/images/next_1.png" height="35" width="35" /&gt;&lt;/a&gt;&lt;/div&gt;
 &lt;/td&gt;
 &lt;/tr&gt;
 &lt;/table&gt;
 EOD;
    echo $buttons;
}
};
</code></pre>

<p>jQuery:</p>

<pre><code>$(document).ready(function() {
if($('.galleries').size() &gt; 1){
   $('.galleries').hide();
   $('.galleries').first().show();

   $("#show_next_gallery").click(function() {
       $('.galleries').first().hide();
       $('.galleries').nextAll().show();
        return false;
      });

   $("#previous_link").click(function() {
       $('.galleries').hide();
       $('.galleries').prevAll().show();
        return false;
      });
}

});
</code></pre>

<p>The above code works great when only two objects are retrieved, but anything more than that and everything breaks down when the next button is clicked - resulting in multiple divs being shown.</p>
