/**
 * @author bbloodgood
 */

var Slideshow = Class.create();

Slideshow.prototype =
{
//  container_id: 'banner_slideshow',
//  content_id: 'banner_slideshow_content',
//  nav_id: 'banner_slideshow_nav',
  transition_timer: null,
  transition_delay: 4000,
  slides: [],
  active_slide: 0,
  width: 400,
  height: 300,
  
  initialize: function(id)
  {
    this.content = $(id);
    if ( ! this.content ) return;
    this.currentImage = this.content.down('.active');
    this.nextImage = this.content.down('.next');
    this.slides_container = this.content.down('.slides')
    
    // INIT SLIDES
    this.slides = this.slides_container.select('img').collect(function(img){
      return img;
    }).uniq();

    this.updateActiveSlide();
    
    // start transitions
    var self = this;
    this.transition_timer = setInterval(function() { self.performTransitionForward() }, this.transition_delay);
    
    
  },
  
  performTransitionForward: function()
  {
    this.performTransition(this.nextSlideIndex());
  },

  performTransitionBackward: function()
  {
    this.performTransition(this.prevSlideIndex());
  },
  
  
  performTransition: function(index)
  {
    if (this.transitioning) return;
    this.transitioning = true;

    this.active_slide = index;

    img = this.slides[index];
    src = img.src;
    offset_left = img.style.left;
    offset_top = img.style.top;
    

    this.nextImage.src = src;
    this.nextImage.style.left = offset_left;
    this.nextImage.style.top = offset_top;
    

    new Effect.Appear(this.nextImage);
    new Effect.Fade(this.currentImage, {
      afterFinish: (function()
      {
        this.currentImage.src = this.nextImage.src;
        this.currentImage.style.left = this.nextImage.style.left;
        this.currentImage.style.top = this.nextImage.style.top;

        this.currentImage.show();
        this.nextImage.hide();
        this.updateActiveSlide();
        this.transitioning = false;
      }).bind(this)
    });
  },
  
  nextSlideIndex: function()
  {
    
    if (this.active_slide < (this.slides.length - 1) ) return this.active_slide + 1;
    return 0;
  },
  
  nextSlide: function()
  {
    return this.slides[this.nextSlideIndex()];
  },

  prevSlideIndex: function()
  {
    
    if (this.active_slide ==  0 ) return this.slides.length - 1;
    return this.active_slide - 1;
  },

  prevSlide: function()
  {
    return this.slides[this.prevSlideIndex()];
  },

  activeSlide: function()
  {
    //alert(this.active_slide);
    return this.slides[this.active_slide];
  },
  updateActiveSlide: function()
  {
    this.slides.each(function(slide){
      slide.removeClassName('active');
    });
    this.activeSlide().addClassName('active');
  },


  getIndexForSlide: function(slide)
  {
    var index = 0;
    var href = slide.down('.url').href;
    while (this.slides[index].href != href) { index++; }
    return index;
    
  }


}
Event.observe(window, 'load', function(){
  $$('.slideshow').each(function(el) {
    new Slideshow(el.id);
  });
});


