
var _SPOTLIGHT = new Object();

var SpotlightController = function(containerID) {
  if(!this.isSupported())
    return false;

  this.container = $(containerID);
  this.active = 0;
  this.SpotlightPromos = [];
  this.btnPrev = document.getElementsByClassName('spotlight_nav_back', containerID)[0];
  this.btnNext = document.getElementsByClassName('spotlight_nav_forward', containerID)[0];
  this.interval = new Object();
  this.duration = 700; //milliseconds
  this.delay = 5000; //milliseconds
  this.isAnimating = false;

  if(!this.prepare()) return false;
  this.bindEvents();
  
  document.getElementById("spotlightloading").style.height = '0px';
//  document.getElementById("spotlightloading_caption").style.display = 'none';
  document.getElementById("spotlight_main").style.height = '338px';

  this.play();
};

SpotlightController.prototype.isSupported = function() {
  return (document.getElementById && document.getElementsByTagName) ? true : false;
};

SpotlightController.prototype.prepare = function() {
  var nav = $('spotlight_nav');
  var btns = nav.getElementsByTagName('a');
  var captions = $('spotlight_caption').getElementsByTagName('a');
  var sliders = $('spotlight_slider').getElementsByTagName('img');

  this.btnPrev.onmouseover = this.btnNext.onmouseover = function() { this.className += '_on'; };
  this.btnPrev.onmouseout = this.btnNext.onmouseout = function() { this.className = this.className.replace('_on', ''); };
  nav.style.left = ((this.container.offsetWidth - nav.offsetWidth) / 2) + 'px';
  nav.style.visibility = 'visible';

  if((captions.length!=sliders.length) || (captions.length!=btns.length))
    return false;

  for(var i=0; i<captions.length; i++) {
    this.SpotlightPromos.push(new SpotlightPromo(i, captions[i], sliders[i], btns[i]));
    new Fx.Style(sliders[i], 'opacity').set(0);
    new Fx.Style(captions[i], 'opacity').set(0);
  }
  new Fx.Style(sliders[0], 'opacity').set(1);
  new Fx.Style(captions[0], 'opacity').set(1);
  
  return true;
};

SpotlightController.prototype.bindEvents = function() {
  var self = this;
  var list = self.SpotlightPromos;

  for(var i=0; i<list.length; i++) {
    list[i].btn.id = i;
    list[i].btn.onclick = function() {
      self.stop();
      self.selectItem(parseInt(this.id));
      return false;
    };
  }

  self.btnPrev.onclick = function() {
    self.stop();
    self.prev();
    return false;
  };
  self.btnNext.onclick = function() {
    self.stop();
    self.next();
    return false;
  };
};

SpotlightController.prototype.selectItem = function(index) {
  if(this.active==index || this.isAnimating)
    return;

  var list = this.SpotlightPromos;
  var prev = this.SpotlightPromos[this.active];
  var next = this.SpotlightPromos[index];

  prev.btn.className = '';
  next.btn.className = 'active';

  this.animate(prev, next);
  this.active = index;
};

SpotlightController.prototype.animate = function(p, n) {
  var self = this;
  var tween = new Fx.Style(p.slider, 'opacity', {
    duration: self.duration,
    onStart: function() {
      self.isAnimating = true;
      new Fx.Style(p.caption, 'opacity', {duration: self.duration}).custom(1, 0);
      $('spotlight_nav').style.display = 'none';
    },
    onComplete: function() { self.animateIn(p, n); }
	}).custom(1, 0);
};

SpotlightController.prototype.animateIn = function(p, n) {
  var self = this;

  p.slider.style.display = 'none';
  p.caption.style.display = 'none';
  var tween = new Fx.Style(n.slider, 'opacity', {
    duration: self.duration,
    onStart: function() {
      n.slider.style.display = 'block';
      n.caption.style.display = 'block';
      new Fx.Style(n.caption, 'opacity', {duration: self.duration}).custom(0, 1);
      $('spotlight_nav').style.display = 'block';
    },
    onComplete: function() { self.isAnimating = false; }
  }).custom(0, 1);
};

SpotlightController.prototype.prev = function() {
  var list  = this.SpotlightPromos;
  var index = (this.active==0) ? list.length-1 : this.active-1;

  this.selectItem(index);
};

SpotlightController.prototype.next = function() {
  var list  = this.SpotlightPromos;
  var index = (this.active==(list.length-1)) ? 0 : this.active+1;

  this.selectItem(index);
};

SpotlightController.prototype.play = function() {
  var self = this;
  self.interval = window.setInterval(function(){ self.next(); }, self.delay);
};

SpotlightController.prototype.stop = function() {
  window.clearInterval(this.interval);
};

var SpotlightPromo = function(i, c, s, b) {
  this.index = i;
  this.caption = c;
  this.slider = s;
  this.btn = b;
}

var _spotlightLoader = function() {
//  document.getElementById("spotlightloading").style.height = '0px';
//  document.getElementById("spotlightloading_caption").style.display = 'none';
//  document.getElementById("spotlight_main").style.height = '338px';
  _SPOTLIGHT = new SpotlightController('spotlight_main');
};

if(window.addEventListener)
  window.addEventListener("load", _spotlightLoader, false);
else if(window.attachEvent)
  window.attachEvent("onload", _spotlightLoader);
