// 
//  FpSS.js
//  FoodConnectV2
//  
//  Created by Rich Conaway on 2009-03-01.
//  Copyright 2009 FoodConnect Inc. All rights reserved.
// 
//  Runs the front page slide show
// 

var FpSS = Class.create(
{
  /*
    Constructor
  */
  initialize: function(timeout){
    this.timeout = timeout;
    this.panes = $$('.fpSSPane');
    this.cnt = 0;
    this.timer = null;
    
    // add navigation links
    for(var i = 0; i < this.panes.length; i++)
    {
      var ele = new Element('a', {
        href: 'javascript:slide_show.show(' + i + ');',
        'class': (i == 0 ? 'active' : '')
      }).update(i + 1);
      
      $('fpSSNav').insert(ele);
    }
    
    // fade up nav bar
    Effect.Appear($('fpSSNav'), {duration: 1, from: 0.0, to: 0.75});
    
    // start timer
    this.timer = setTimeout(this.run.bind(this), this.timeout);
  },
  
  /*
    Runs the slide show
  */
  run: function(){
    this.switchPane(-1);
  },
  
  /*
    show a requested pane
  */
  show: function(index){
    // stop timer
    clearTimeout(this.timer);
    
    this.switchPane(index);
  },
  
  /*
    handles the actual pane switching
  */
  switchPane: function(new_pane){
    // fade out current pane
    Effect.Fade($(this.panes[this.cnt]).identify(), {
			duration: 1,
			from: 1.0,
			to: 0.0
		});
		
    // remove active class from nav
    $$('#fpSSNav a')[this.cnt].removeClassName('active');
		
		if(new_pane == -1)
		{
		  // inc counter
  		this.cnt++;
		}
		else
		{
		  this.cnt = new_pane;
		}
		
		// reset counter if needed
		if(this.cnt == this.panes.length)
		{
			this.cnt = 0;
		}
		
		// fade in new pane
		Effect.Appear($(this.panes[this.cnt]).identify(), {
			duration: 1,
			from: 0.0,
			to: 1.0
		});
		
    // add active class to nav
    $$('#fpSSNav a')[this.cnt].addClassName('active');
		
    this.timer = setTimeout(this.run.bind(this), this.timeout);
  }
});

var slide_show = null;

Event.observe(window, 'load', function(){
  slide_show = new FpSS(10000);
});


