// Requires mootools.
var FeaturedController = {
  currentFeature: 0,
  featureElements: null,
  nFeatures: null,
  stageElement: null,
  pagingElements: null,
  timer: null,
  playEl: null,
  pauseEl: null,
  animSpeed: 4000,
  
  initialize: function(element, animSpeed) {
    if (typeof element == 'string') element = $(element);
    this.featureElements = element.getElementsBySelector('div.featured-item');
    this.nFeatures = this.featureElements.length;
    this.stageElement = element.getElementsBySelector('div.featured-content-box')[0];
    this.pagingElements = element.getElementsBySelector('div.featured-pages a');
    this._show(0, false);
    this.playEl = $('featured_playing');
    this.pauseEl = $('featured_paused');
    this.animSpeed = animSpeed;
    this.play();
  },
  show: function(featureIndex) { this._show(featureIndex, true); },
  _show: function(featureIndex, pause) { 
    if (pause) {this.pause();} 
    this._unselectCurrent(); 
    this.currentFeature = featureIndex; 
    this._featureCurrent(); 
  },
  prev: function() { this._relativeShow(-1, true); },
  next: function() { this._relativeShow(1, true); },
  _nextNoPause: function() { this._relativeShow(1, false); },
  play: function() {
	  this.pauseEl.setStyle('display','none');
    this.playEl.setStyle('display','inline');
    this.timer = (function(){FeaturedController._nextNoPause();}).periodical(this.animSpeed);
  },
  pause: function() {
    this.playEl.setStyle('display','none');
		this.pauseEl.setStyle('display','inline');
		$clear(this.timer);
  },
  _unselectCurrent: function() { this.pagingElements[this.currentFeature].removeClass('currently-featured'); },
  _featureCurrent: function() { 
	  fel = new Fx.Scroll(this.stageElement);
		fel.toElement(this.featureElements[this.currentFeature]);
		this.pagingElements[this.currentFeature].addClass('currently-featured');
	},
	_relativeShow: function(addIndex, pause) {
	  f = this.currentFeature + addIndex; 
	  if (f >= this.nFeatures) { f = 0; }
	  else if (f < 0) { f = this.nFeatures - 1; }
	  this._show(f, pause);
	}
}