var Site = {

  
  // initialize page and all its many wonderful functions
  start: function(){
    if ($$('.home')[1]) {
      Site.setAccordians();
      //Site.prepSlideshow();
      //window.addEvent('load', Site.setSlideshow);
    }
  },
  
  processHash: function(link) {
    href = link.hash.split("#")[1];
    link.removeAttribute('href');
    return href;
  },
  
  setAccordians: function() {
    $$('.read-handle').each( function(e) {
      var link = Site.processHash(e);
      var end = $('about-info').getStyle('height');
      $('about-info').setStyle('height', '0');
      e.addEvent('click', function(e) {
        Site.toggleHeight($(link), end);
      }.bind(end));
    });
  },
  
  toggleHeight: function(e, end) {
    var myFx = new Fx.Tween(e);
    if (e.getStyle('height').toInt() > 0) {
      var end = 0;
    }
    myFx.start('height', end+'px');
  },
  
  setSlideshow: function(){
    var mySlideShow = new simpleSlide($$('#image li'));
    mySlideShow.displayImage.periodical(5000,mySlideShow);
  },

  prepSlideshow: function(){
    var imgs = $$('#image img');
    for (var i=0; i < imgs.length; i++) {
      li = imgs[i].getParent();
      li.setStyle('z-index', 100 - i);
      alt = imgs[i].alt;
      if ($$('.about, .home')[1] && alt) {
        var caption = new Element('span', {'class': 'caption', 'html': alt});
        caption.inject($$('#image li')[i]);
      }
    };
  }
  
}

var simpleSlide = new Class({
  initialize: function(imageArray) {
    if ($type(imageArray)!='array') return;
    this.images = imageArray;
    this.max = this.images.length;
    this.active = 0;
  },

  displayImage: function() { 
    // Set next image or the first
    if (this.active < this.max - 1)  {
      this.last = this.active;
      this.active++;
    }
    else {
      this.active = 0;
      this.last = this.max - 1;
    }

    // don't fade in IE
    if(window.ie) {
      var dur = 1;
    }
    
    else {
      var dur = 1000;
    }
     
    // Hide image
    this.images[this.last].effect('opacity', {
      duration: dur,
      'link': 'chain'}).start(1,0);
    
    // Show image
    this.images[this.active].effect('opacity', {
      duration: dur,
      'link': 'chain'}).start(0,1);
    
  }
});


window.addEvent('domready', Site.start);
