/* 
b-ya interface 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. 
*/

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

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

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

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

// init handler
b_ya.init = function() {
  // find slides
  this.slides = getElementsByTagAndClassName( "div", "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",b_ya,"init");