/**
 *  Spotlights
 *
**/
function spotlight() {

  this.shifting = false;

  this.ie_mode  = document.all ? true : false;
  this.ie_skip  = false;

  this.content  = featured.getElementsByTagName('div');
  this.count    = 0;

  this.slides   = new Object();

  for(n = 0; n < this.content.length; n++) {

    if( this.content[n].className == 'content' ) {

      this.count++;
      this.slides[this.count] = this.content[n];

    }

  }

  this.current  = 1;
  this.previous = false;
  this.dots     = false;
  this.dot      = false; // used to switch the active dot

  this.active   = fader(this.slides[this.current], default_speed);

  this.slides[1].style.zIndex = this.count;


  this.update = function() {

    this.dots   = document.getElementById('featured-dots').getElementsByTagName('img');

    for(n = 0; n < this.count; n++) {

      this.dots[n].src = this.current == (n + 1) ? '/images/featured_dot_active.png' : '/images/featured_dot.png';

    }

    this.slides[this.current].style.zIndex  = this.count;
    this.slides[this.previous].style.zIndex = 0;

    if( this.ie_mode && this.ie_skip == false  ) {

      this.ie_skip    = true;

      ie_fadein       = fader(this.slides[this.current], default_speed);
      ie_fadein.fade_set(0);
      ie_fadein.fade(10, this.update);

      return;

    } else if( this.ie_mode ) {

      this.active   = fader(this.slides[this.previous], default_speed);
      this.ie_skip  = false;

    }

    spotlight_media(this.current);

    this.active.fade_set(10);

    this.active = fader(this.slides[this.current], default_speed);

    // Fix IEs bug of not removingt he fade-node
    if( this.ie_mode ) {

      var fade_nodes  = featured.getElementsByTagName('div');

      for(var n in fade_nodes) {
        if( fade_nodes[n].className == 'ie-fade' ) {
          fade_nodes[n].removeNode(true);
        }
      }

    } // ! IE bug fix

    this.shifting = false;

  } // ! update()


  this.back = function() {

    if( this.shifting ) { return false; }

    this.shifting = true;

    this.previous = this.current;
    this.current  = this.current - 1 >= 1 ? this.current - 1 : this.count;

    this.slides[this.current].style.zIndex = this.count - 1;

    spotlight_media(this.previous, true);

    this.active.fade(0, this.update);

  } // ! back()


  this.next = function() {

    if( this.shifting ) { return false; }

    this.shifting = true;

    this.previous = this.current;
    this.current  = this.current + 1 <= this.count ? this.current + 1 : 1;

    this.slides[this.current].style.zIndex = this.count - 1;

    spotlight_media(this.previous, true);

    this.active.fade(0, this.update);

  } // ! next()



  // Add the dynamic elements to the spotlight
  if( this.dots == false && this.count > 1 ) {

    this.dots = document.createElement('div');
    this.dots.setAttribute('id', 'featured-dots');

    this.dots = featured.parentNode.insertBefore(dots, featured.nextSibling);

    for(n = 1; n <= this.count; n++) {

      this.dot = document.createElement('img');
      this.dot.src = n == 1 ? '/images/featured_dot_active.png' : '/images/featured_dot.png';
      this.dot.setAttribute('alt', 'Dot');

      dots.appendChild(this.dot);

    }

    // Add the arrows
    var a_back  = document.createElement('img');
        a_back.setAttribute('id', 'featured-back');
        a_back.setAttribute('alt', 'Back');
        a_back.className = 'arrow';
        a_back.src = '/images/featured_arrow_back.png';

        a_back  = featured.parentNode.insertBefore(a_back, dots.nextSibling);

    var a_next  = document.createElement('img');
        a_next.setAttribute('id', 'featured-next');
        a_next.setAttribute('alt', 'Next');
        a_next.className = 'arrow';
        a_next.src = '/images/featured_arrow_next.png';

        a_next  = featured.parentNode.insertBefore(a_next, a_back.nextSibling);

  }


  return this;

} // ! spotlight()


function spotlight_media(slot, remove) {
  if( remove == true ) {

    document.getElementById('flash-video-' + slot).innerHTML = '';

  } else {
    flash(
      '/flash/video_player.swf',
      414, 314,
      'flash-video-' + slot,
      spotlights[slot]
    );
  }
}

