/* 
212auction 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. 
*/

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

slides.activeslide = false;

slides.showslide = function ( id ) {
  var slide = $(id);
  if ( !slide ) return;
  if ( this.activeslide ) {
    this.activeslide.style.display = 'none';
    signal( this.activeslide, "deactivate" );
  }
  this.activeslide = slide;
  this.activeslide.style.display = 'block';
  var videoid = getNodeAttribute( slide, "videoid" );
  if ( videoid ) {
    log("Playing",videoid);
    window.setTimeout( "$('flowplayer"+videoid+"').DoPlay()", 1000 );
  }
  signal( this.activeslide, "activate" );
}

slides.clickhandler = function ( e ) {
  e.stop();
  e.src().blur();
  var id = e.src().hash.substr(1);
  this.showslide( id );
}

slides.init = function() {
  // find slidenav links
  iterateElementsByTagAndClassName ( "a", "slidenav", $('Contents'), function( navs, i ) {
    connect( navs[i], "onclick", slides, "clickhandler" );
    var slideid = navs[i].hash.substr(1);
    var slide = $(slideid);
    if ( slide ) {
      var imgObj = getFirstElementByTagAndClassName('IMG', null, navs[i]);
      if( imgObj != null ) {
        connect( slide, "activate", bind(function() { addElementClass( this, "active" ); }, imgObj) );
        connect( slide, "deactivate", bind(function() { removeElementClass( this, "active" ); }, imgObj) );
      }
    }
  });
  // set activeslide to cover
  var covers = getElementsByTagAndClassName( "div", "cover", $("Object") );
  if (covers.length) {
    this.activeslide = covers[0];
  }
  // show a different slide if requested
  if ( window.location.hash ) {
    var id = window.location.hash.substr(1);
    if ( !$(id) ) {
      log("Will turn to allslides to find id not present on this page",id,allslides);
      for( i=0; i<allslides.length; i++ ) {
        if ( allslides[i].name==id ) {
          log( "Slide found on page with cstart",allslides[i].cstart );
          window.location = '?cstart=' + allslides[i].cstart + '#' + id;
        }
      }
    }
    else {
      log("Showing slide on this page",id);
      this.showslide( id );
    }
  }
}

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