Choose image URL from large array, insert into random DIV on page, cycle
<p>Bit of an odd request. I have ~300 image URLs and 27 <code>div</code>s. Each <code>div</code> has an <code>id="img1"</code>, where <code>1</code> is a number between 1 and (you guessed it) 27, and a <code>class="imageblock"</code> (for general styling).</p>

<p>Into each of these <code>div</code>s, I'd like to insert an <code>&lt;img src="http://foo.bar/img.jpg" width="640"&gt;</code>, where the URL is chosen from a list of about 300 randomly, and inserted into one of the <code>div</code>s randomly. Each of the 27 <code>div</code>s should have its own image chosen from the 300 options. </p>

<p>Furthermore, these images should cycle. Every 5 seconds, I'd like to have one random image URL on the page replaced with another random one.</p>

<p>This is the (terrible) code I have attempted to cobble together with a little help, but I'm useless with javascript and can't make this work:</p>

<pre><code>&lt;script type="text/javascript"&gt;     
    $('document').ready(function() {        
                    var divs = $('div');

                        $("div").each(function() {
                          $(this).html("&lt;img src='http://foo.bar/img1.jpg'alt='"+$(this).attr("id")+"'/&gt;");         
                        });  

                        var imageChanger = setInterval(function() {switchImage()}, 500);


                        function switchImage() {
                         var new_image_url = [ 'http://foo.bar/anotherimg.jpg', 'http://bar.foo/image.jpg', 'http://foo.bar/imgremote.jpg'];

                            var random_div = Math.floor(Math.random() * $('div').length);
                            var random_img = Math.floor(Math.random() * $(new_image_url.length);

                          $("div:nth-child("+random_div+")").html('&lt;img src="('+random_img+')" width="640"&gt;)');            

                        }
    });
    &lt;/script&gt;
</code></pre>

<p>Note that the remote images do not follow any common pattern, thereby rendering my <code>random_img</code> variable unworking.</p>

<p>Any help would be appreciated.</p>
