/* 
reelworks interface javascript 
Copyright 2010, 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":"2.0" }

slides.showindex = 0;
slides.activeslides = {};
slides.slides = {};

slides.showslide = function ( id, first ) {
  var slide = $(id);
  if ( !slide ) return;
  // which show?
  var showindex = this.slides[ id ];
  if ( !showindex ) {
    log("Got slide but no registered show for it!");
    return;
  }
  
  // hide cover slide
  if( this.cover_slide ) {
    this.cover_slide.style.display = 'none';
  }
  
  if ( this.activeslides[ showindex ] ) {
    navid = "nav_" + this.activeslides[ showindex ].id;
    this.activeslides[ showindex ].style.display = 'none';
    signal( this.activeslides[ showindex ], "inactive" );
    if ( $(navid) ) {
      removeElementClass( $(navid), "active" );
    }
  }
  this.activeslides[ showindex ] = slide;
  navid = "nav_" + this.activeslides[ showindex ].id;
  this.activeslides[ showindex ].style.display = 'block';
  var videoid = getNodeAttribute( slide, "videoid" );
  if ( videoid && !first ) {
    log("Playing",videoid);
    window.setTimeout( "$('flowplayer"+videoid+"').DoPlay()", 1000 );
  }
  signal( this.activeslides[ showindex ], "active" );
  if ( $(navid) ) {
    addElementClass( $(navid), "active" );
  }
}

slides.activeIcon = function ( e ) {
  var src = e.src();
  var icon = $( "t_"+src.id )
  addElementClass( icon, "active" );
  //log("Activated",src.id,"icon",icon.id);
}

slides.inactiveIcon = function ( e ) {
  var src = e.src();
  var icon = $( "t_"+src.id )
  removeElementClass( icon, "active" );
  //log("Deactivated",src.id);
}

slides.navhandler = function ( e ) {
  var target = e.target();
  log("Navhandler", target.tagName);
  if ( target.tagName=="IMG" || target.tagName=="SPAN" ) {
    target = target.parentNode;
  }
  if ( target.tagName!="A" ) return;
  e.stop();
  target.blur();
  var id = target.hash.substr(1);
  this.showslide( id );
}

slides.init = function( slideshow ) {
  this.showindex++;
  log("Init for slideshow",this.showindex,"with slideshow",slideshow);
  // find slidenav links
  iterateElementsByTagAndClassName ( "div", "slidenav", slideshow, function( navs, i ) {
    logDebug( "Found normal slidenav",navs[i], slides.activeslides );
    connect( navs[i], "onclick", slides, "navhandler" );
  });
  if ( $("FilmTable") ) {
    // find slidenav links
    iterateElementsByTagAndClassName ( "td", "slidenav", $("FilmTable"), function( navs, i ) {
      logDebug( "Found FilmTable slidenav",navs[i], slides.activeslides );
      connect( navs[i], "onclick", slides, "navhandler" );
    });
  }
  
  // find cover
  this.cover_slide = getFirstElementByTagAndClassName("div", "cover", slideshow);

  // set activeslide
  this.activeslides[ this.showindex ] = false;
  // map slides to this slideshow
  var allslides = getElementsByTagAndClassName( "div", "slide", slideshow );
  var mylength = allslides.length;
  for ( var i=0; i < mylength; i++ ) {
    this.slides[ allslides[i].id ] = this.showindex;
    if ( hasElementClass( allslides[i], "cover") ) {
      this.activeslides[ this.showindex ] = allslides[i];
    }
  }
  log("Found",mylength,"slides, showing first.");
  if( this.cover_slide == null ) {
    this.showslide( allslides[0].id, true );
  }
}

slides.factory = function() {
  var shows = getElementsByTagAndClassName( "div", "show", $("Canvas") );
  var showslength = shows.length;
  logDebug("shows:", shows);
  for ( var s=0; s < showslength; s++ ) {
    slides.init( shows[s] );
  }
  // show a different slide if requested
  if ( window.location.hash ) {
    var id = window.location.hash.substr(1);
    var isslide = $(id);
    if ( isslide && hasElementClass( isslide, "slide" ) ) {
      log("Showing slide on this page",id);
      this.showslide( id, true );
    }
  }
}

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

connect( window, "ondomload", function() {
  if ( window.location.hash ) {
    var hash = window.location.hash.substr( 1 );
    if ( $(hash) && hasElementClass( $(hash), "popout") ) {
      $(hash).style.display = "block";
    }
  }
})
