// Image slideshow
// Author: Douglas Greiman
// Written: July 28, 2009
// Released to the public domain.
// 
// To get a random slideshow, add code like this to your page:
//
// <head>
//   ...
//   <script src="slideshow.js"></script>
//   <script>
//     window.onload=function() {
//       duggelz.slideshow.startlist(8,[
//         'http://www.sourisseauacademy.org/images/ecs1612.jpg',
//         'http://www.sourisseauacademy.org/images/ecs1614.jpg',
//         ...
//       ]);
//     };
//   </script>
//   ...
// </head>
// <body>
//   ...
//   <img name="_slideshow_img_tag" src="...">
//   ...
//
//
// Other examples:
// 8 seconds per image, 5 images chosen, from entire Waterhouse collection
//   duggelz.slideshow.start(8, 5, duggelz.slideshow.waterhouse_urls())
//
// 8 seconds per image, hand picked list
//   duggelz.slideshow.startlist(8, [ "http://...", "url..", "url.."])

if (!this.duggelz) {

var duggelz = {};

if (!this.duggelz.slideshow) {

duggelz.slideshow = function() {
    var urls;
    var cur_idx = 0;
    var images;
    
    // Preload images
    function _preload() {
        images = new Array(urls.length);
        for (var i = 0; i < urls.length; i++) {
            images[i] = new Image();
            images[i].src = urls[i];
        }
    }
    
    // Advance to next slideshow image
    function _next_image() {
        cur_idx = (cur_idx + 1) % urls.length;
        if (images[cur_idx].width) {
            var slideshow = document.getElementById("slideshow_img_tag");
            slideshow.src = urls[cur_idx];
        }
    }

    // Advance to random slideshow index
    function _random_image() {
        cur_idx = _randn(urls.length);
        if (images[cur_idx].width) {
            var slideshow = document.getElementById("slideshow_img_tag");
            slideshow.src = urls[cur_idx];
        }
    }

    // Choose random number between 0 and maxn
    function _randn(maxn) {
        return Math.floor(Math.random()*maxn);
    }

    return {
      // Public entry point
      start : function(interval_in_s, num_images, all_urls) {
            urls = new Array(num_images);
            for (var i = 0; i < num_images; i++) {
                urls[i] = all_urls[_randn(all_urls.length)];
            }
            _preload();
            window.setInterval(_next_image, interval_in_s * 1000);
        },

      // Public entry point
      startlist : function(interval_in_s, all_urls) {
            // Copy array
            urls = all_urls.slice();
            _preload();
            window.setInterval(_next_image, interval_in_s * 1000);
        },

      // Return an array of urls to all Waterhouse photos
      waterhouse_urls : function()  {
            // Compute url for photo number i
            function u(i) {
                var ones = i % 10;
                var tens = Math.floor(i/10) % 10;
                var hundreds = Math.floor(i/100) % 10;
                var thousands = Math.floor(i/1000) % 10;
                return ("http://www.sourisseauacademy.org/" +
                        "Edith_Smith_Collections/Images/" + 
                        thousands + "000/" 
                        + thousands + hundreds + "00/JPGs/" +
                        "ecs" + thousands + hundreds + tens + ones + ".jpg");
            }
            var all_urls = new Array(305);
            for (var i=1; i<306; i++) {
                all_urls[i-1] = u(i);
            }
            return all_urls;
        }
    }
} ();

} // if (!this.duggelz.slideshow)
} // if (!this.duggelz)
