var PromoRotator = Class.create({
    defaultOptions: {
      groupSelector: 'div.promoGroup',
      itemSelector: 'div.promoItem',
      startDelay: 1000,
      groupDelay : 2500,
      fadeTime: 1500
    },
    setOptions: function(options) {
        this.options = Object.clone(this.defaultOptions);
        Object.extend(this.options, options || {});
        return this.options;
    },
    initialize: function(options) {
        this.setOptions.bind(this)(options);

        
        this.groups = $$(this.options.groupSelector);
        for(var g = 0; g<this.groups.length; g++)
        {
           var group = this.groups[g];
           this.groups[g].items = group.select(this.options.itemSelector);
           this.groups[g].currentItem = 0;
        }
        window.setTimeout(this.start.bind(this),this.options.startDelay);
    },
    start: function(){
      this.currentGroup = 0;
      this.step.bind(this)();
    },
    step: function(){
      var currentGroup = this.groups[this.currentGroup];
      var currentItem = currentGroup.items[currentGroup.currentItem];
      currentGroup.currentItem++;
      if(currentGroup.items.length == 1)
      {
          this.currentGroup++;
          if(this.currentGroup >= this.groups.length)
          {
             this.currentGroup = 0;
          }
          window.setTimeout(this.step.bind(this), 0);
          return;
      }
      new Effect.Fade(currentItem, { duration: this.options.fadeTime  / 1000 });
      var nextItem = currentGroup.items[currentGroup.currentItem];
      if(!nextItem)
      {
        currentGroup.currentItem = 0;  
        nextItem = currentGroup.items[currentGroup.currentItem];        
      }
      new Effect.Appear(nextItem, { duration: this.options.fadeTime / 1000 });
      this.currentGroup++;
      if(this.currentGroup >= this.groups.length)
      {
         this.currentGroup = 0;
      }
      
      window.setTimeout(this.step.bind(this), this.options.groupDelay);
    }
});



