ns('MagicWand.ImageSwitcher.Manager');

/**
 * Image switch manager
 * 
 * @version $Id$
 */
MagicWand.ImageSwitcher.Manager.Rotate = function(config) {
  // configurable {
  this.switchers = []; // required
  this.collection = null; // required
  this.delay = 5000;
  this.auto_start = false;
  // }
  this._timer = null;
  
  $.extend(this, config);
  
  if(this.auto_start)
    this.start();
}

MagicWand.ImageSwitcher.Manager.Rotate.prototype = {
  start: function() {
    var _this = this;
    this._timer = setTimeout(function() {
      _this.rotate();  
    }, this.delay);
  },
  
  rotate: function() {
    for(var i = 0, l = this.switchers.length; i < l; i ++) {
      if(i < l - 1) {
        var $next_image = this.switchers[i + 1].getSecondaryImageEl();
        this.switchers[i].switchTo($next_image.attr('src'), $next_image.attr('title'));
      } else {
        var next_image = this._getNextImage();
        this.switchers[i].switchTo(
          next_image.image.src, 
          next_image.alt,
          this.start.createDelegate(this)
        );  
      }
    }
  },
  
  _getNextImage: function() {
    var current_image = this.collection.current();
    var next_image = this.collection.next();
    if(!next_image) 
      this.collection.rewind();

    return current_image;
  }
}

