/**
 * @author bbloodgood
 */

var BannerSlideshow = Class.create();

BannerSlideshow.prototype =
{
  container_id: 'banner',
  content_id: 'slideshow_content',
  nav_id: 'slideshow_nav',
  transition_timer: null,
  transition_delay: 5000,
  slides: [],
  active_slide: 0,
  
  initialize: function()
  {
    this.content = $(this.content_id);
    if ( ! this.content ) return;
    this.currentLink = this.content.down('a');
    this.currentImage = this.content.down('.active');
    this.nextImage = this.content.down('.next');
    this.nav = $(this.nav_id);
    this.nextLink = this.nav.down('.next');
    this.prevLink = this.nav.down('.prev');
    
    // INIT SLIDES
    this.slides = $$('#' + this.nav_id + ' .slide').collect(function(slide){
      return {href: slide.down('.url').href, slide: slide};
    }).uniq();
    
    while (this.slides[this.active_slide].href != this.currentLink.href) { this.active_slide++; }
    this.updateActiveSlide();
    
    this.initNavigation();
    

    // start transitions
    var self = this;
    this.transition_timer = setInterval(function() { self.performTransitionForward() }, this.transition_delay);
    
    
  },
  initNavigation: function()
  {
    $$('#' + this.nav_id + ' .slide').each((function(slide){
      slide.hover(
        function() { slide.addClassName('hover');    },
        function() { slide.removeClassName('hover'); }
      );
      slide.observe('click', (function(ev){

        index = this.getIndexForSlide(slide);
        this.performTransition(index);
        clearInterval(this.transition_timer);
        
      }).bind(this));
    }).bind(this)) 

    this.nextLink.observe('click', (function(){
      this.performTransitionForward();
      clearInterval(this.transition_timer);
    }).bind(this));    

    this.prevLink.observe('click', (function(){
      this.performTransitionBackward();
      clearInterval(this.transition_timer);
    }).bind(this));    
    
  },
  
  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;
    info = this.slides[index]; 
    src = info.slide.down('.large').src;
    href = info.href;
    
    this.nextImage.src = src;
    this.currentLink.href = href;
    new Effect.Appear(this.nextImage);
    new Effect.Fade(this.currentImage, {
      afterFinish: (function()
      {
        this.currentImage.src = this.nextImage.src;
        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.slide.removeClassName('active');
    });
    this.activeSlide().slide.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(){ new BannerSlideshow; });


