/* 
For A Better Bronx Slideshow Javascript 
Copyright 2008, Fund for the City of New York
All rights reserved.

This source file is distributable subject to the terms of the
FCNY Open Source License. 
*/

// fabb object
var fabb = { "version":"1.0" }

// no active slide to start
fabb.slides = [];
fabb.activeslide = false;

// show a slide, hide others
fabb.showSlide = function ( id ) {
  if ( this.activeSlide ) {
    this.activeSlide.style.display = 'none';
    this.activeSlide = false;
  }
  if ( id ) {
    this.activeSlide = $( id );
  }
  if ( !this.activeSlide ) {
    this.activeSlide = this.slides[0];
  }
  this.activeSlide.style.display = 'block';
}

// click handler
fabb.controlClick = function ( e ) {
  e.stop();
  var slideid = e.src().hash.substr(1);
  window.location.hash = e.src().hash;
  this.showSlide( slideid );
}

// init handler
fabb.init = function() {
  // find slides
  this.slides = getElementsByTagAndClassName( "table", "slide" );
  log("Found slides",this.slides)
  // modifiy all slidenav links to use the controlClick handler
  var slidecontrols = getElementsByTagAndClassName( "a", "slidenav" );
  for ( i=0; i<slidecontrols.length; i++ ) {
    connect( slidecontrols[i], "onclick", this, "controlClick" );
  }
  log("Captured",slidecontrols.length,"slide navigation links");
  // check hash and display slide, otherwise display first slide
  var hashval = false;
  if ( window.location.hash ) {
    hashval = window.location.hash;
    if ( hashval.substr( 0, 1 )=="#" ) {
      hashval = hashval.substr( 1 );
    }
  }
  if ( hashval ) {
    log("Showing slide",hashval);
    this.showSlide( hashval );
  }
  else {
    log("Showing first slide");
    this.showSlide();
  }
}

connect(window,"ondomload",fabb,"init");
