YUI({
  combine: false,
  filter: 'raw',
  modules : {
        'plugin-slide-toggle' : {
            fullpath : '/scripts/plugin-slide-toggle.js',
            requires :  ['base-build','plugin','node','event', 'anim']
        }
    }
}).use('gallery-slideshow-animated', 'plugin-slide-toggle', 'gallery-text-resizer', function(Y) {
  Y.SlideshowAnimated.prototype.show_slide = function(slide_number) {
    if(this.timer && this.timer.hasOwnProperty('cancel')) {
      this.timer.cancel();
    }
    if (this._anim_running()) {
      Y.later(this.get('pause_time')/20, this, 'show_slide', slide_number);
    } else {
      if (slide_number != this.get('current_slide')) {
        this.set('current_slide', slide_number);
        this.syncUI();
      }
    }
  };

  Y.Text.Resizer.prototype._update = function() {
    Y.all('#bd, #callouts').setStyle('fontSize', this.get('currentSize') + this.get('unit'));
  }

  Y.all("dl.slide-toggle").plug(Y.Plugin.SlideToggle);
  Y.all(".text-resize").plug(Y.Text.Resizer, {
    smallest : 11,
    largest: 15,
    baseSize: 13,
    currentSize: 13
  });

  if (Y.one(".slideshows")) {
    var s1, s2, s3, sm, next, interval;

    s1 = new Y.SlideshowAnimated({
      contentBox: '.s1',
      slides: Y.all(".s1 img"),
      activate_play_pause_buttons: false,
      activate_next_prev_buttons: false,
      activate_slide_buttons: false,
      auto_advance: false,
      'animation_in.easing': Y.Easing.easeOut,
      'animation_out.easing': Y.Easing.easeIn
    });

    s2 = new Y.SlideshowAnimated({
      contentBox: '.s2',
      slides: Y.all(".s2 img"),
      activate_play_pause_buttons: false,
      activate_next_prev_buttons: false,
      activate_slide_buttons: false,
      auto_advance: false,
      'animation_in.easing': Y.Easing.easeOut,
      'animation_out.easing': Y.Easing.easeIn
    });

    s3 = new Y.SlideshowAnimated({
      contentBox: '.s3',
      slides: Y.all(".s3 img"),
      activate_play_pause_buttons: false,
      activate_next_prev_buttons: false,
      activate_slide_buttons: false,
      auto_advance: false,
      'animation_in.easing': Y.Easing.easeOut,
      'animation_out.easing': Y.Easing.easeIn
    });

    sm = new slideManager({
      shows: 3,
      interval: {
        min: 6,
        max: 12
      }
    });

    s1.on("slideshow:slide-displayed", function(slide, index) {
      var next, interval;
      next = sm.nextSlide();
      interval = sm.nextInterval();
      Y.log("(next slide: " + (next+1) + ", interval: " + interval + ")");
      advanceShow(next, interval);
    });

    s2.on("slideshow:slide-displayed", function(slide, index) {
      var next, interval;
      next = sm.nextSlide();
      interval = sm.nextInterval();
      Y.log("(next slide: " + (next+1) + ", interval: " + interval + ")");
      advanceShow(next, interval);
    });

    s3.on("slideshow:slide-displayed", function(slide, index) {
      var next, interval;
      next = sm.nextSlide();
      interval = sm.nextInterval();
      Y.log("(next slide: " + (next+1) + ", interval: " + interval + ")");
      advanceShow(next, interval);
    });

    s1.render();
    s2.render();
    s3.render();

    next = sm.nextSlide();
    interval = sm.nextInterval();
    Y.log("(next slide: " + (next+1) + ", interval: " + interval + ")");
    advanceShow(next, interval);
  }

  function advanceShow(index, timeout) {
    switch(index) {
      case 0:
        setTimeout(advanceS1, timeout);
        break;
      case 1:
        setTimeout(advanceS2, timeout);
        break;
      case 2:
        setTimeout(advanceS3, timeout);
        break;
      default:

    }
  }

  function advanceS1() {
    Y.log("Advancing s1", "info");
    s1.advance();
  }

  function advanceS2() {
    Y.log("Advancing s2", "info");
    s2.advance();
  }

  function advanceS3() {
    Y.log("Advancing s3", "info");
    s3.advance();
  }

  function slideManager(opts) {
    var self = {},
      shows = opts.shows,
      min,
      max,
      usage = [],
      history = [];

    if (opts.interval) {
      min = (opts.interval.min) ? opts.interval.min : 0;
      max = (opts.interval.max) ? opts.interval.max : 10;
    } else {
      min = 0;
      max = 10;
    }

    function _init() {
      Y.log("_init()", "info");

      for (var i = 0; i < shows; i++) {
        usage[i] = i;
      }

      if(history.length > 0) {    // don't run on the very first iteration
        var last = history.pop();
        history = [usage[last]];
        usage.splice(last, 1);
      }
    }

    function _nextSlide() {    // goal was for each iteration, make sure each slideshow advances one time, in a random order
      var index, next;    // from one iteration to the next, the last to advance couldn't be the first to advance the subsequent iteration

      if (usage.length == 0) {  // start of new iteration
        _init();        // reinitialize and remove the last show temporarily
        index = Math.floor(Math.random() * usage.length);
        next = usage[index];
        usage.push(history.pop());    // add the last show back in, now that the new next has been calculated
        history.push(next);
        usage.splice(index, 1);
      } else {          // in the midst of the current iteration
        index = Math.floor(Math.random() * usage.length);
        next = usage[index];
        history.push(next);
        usage.splice(index, 1);
      }

      return next;
    }

    function _nextInterval() {
      return Math.floor(Math.random() * (max - min) + min + 1) * 1000;
    }

    _init();

    self.nextSlide = _nextSlide;
    self.nextInterval = _nextInterval;
    return self;
  }
});
