
var BestBuyController = Class.create({
  container: null,
  frames: null,
  executer: null,
  currentIndex: 0,
  initialize:function(){
    this.container = $('bestbuy-search-results');
    this.frames = $$('#bestbuy-search-results .frame');

    this.showItem(this.currentIndex);
    
    this.executer = new PeriodicalExecuter(this.moveTab.bind(this), 5);
  },
  showItem: function(index) {
    var itemToShow = this.frames[index];
    Position.prepare();
    var container_y = this.container.cumulativeOffset()[1];
    var element_y = itemToShow.cumulativeOffset()[1];
    new Effect.Scroll(this.container, {x:0, y:(element_y-container_y), queue: { position: 'end', scope: 'bestbuys' } });
    this.currentIndex = index;
    return false;
  },
  moveTab: function() {
    pos = this.nextTabIndex();

    this.showItem(pos);
  },
  nextTabIndex : function()
  {
    var frames = $$('#bestbuy-search-results .frame');
    if((this.currentIndex + 1) >= frames.length){
      return 0;
    }

    return this.currentIndex + 1;
  }
});

var bestBuyController = null;
document.observe('dom:loaded', function() {
  bestBuyController = new BestBuyController();
});

