var timer;
window.addEvent('domready',function() {
  var boxLastIndex = -1;
  var boxIndex = -1;
  var boxes = $$('.rotorBox');
  $('prevbutton').addEvent('click',function(){
    boxShift('prev');
    return false;
  });
  $('nextbutton').addEvent('click',function(){
    boxShift('next');
    return false;
  })
  boxes.each(function(box,index) {
    box.set('opacity','0');
    box.set('tween', {duration: 'long'});
  });

  function boxShift(dir) {
    clearTimeout(timer);
    boxLastIndex = boxIndex;

    if ( dir == 'prev' ) {
      if ( --boxIndex < 0 ) boxIndex = boxes.length-1;
    } else {
      if ( ++boxIndex >= boxes.length ) {
        boxIndex = 0;
      }
    }

    if ( boxLastIndex >= 0 ) boxes[boxLastIndex].tween('opacity',0);
    boxes[boxIndex].tween('opacity',1);
    timer = setTimeout(function(){boxShift();},8000);
  }

  boxShift(0);

});
