/* 
arthurashe interface Javascript 
Copyright 2011, 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. 
*/

// albums object - used for viewing videos attached to an object
var albums = { "version":"1.0" }

albums.activeslide = false;

albums.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" );
}

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

albums.init = function() {
  // find slidenav links
  iterateElementsByTagAndClassName ( "a", "slidenav", $('Contents'), function( navs, i ) {
    connect( navs[i], "onclick", albums, "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", albums, "init" );


// 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 ] ) {
    this.activeslides[ showindex ].style.display = 'none';
    signal( this.activeslides[ showindex ], "inactive" );
  }
  this.activeslides[ showindex ] = slide;
  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" )
}

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.clickhandler = function ( e ) {
  if ( e.mouse().button["left"]==true ) {
//    e.stop();
    var target = e.src();
    if ( target.tagName!="A" ) return;
    target.blur();
    var id = target.hash.substr(1);
    this.showslide( id );
  }
}

slides.navhandler = function ( e ) {
  var target = e.target();
  if ( target.tagName=="IMG" ) {
    target = target.parentNode;
  }
  if ( target.tagName!="A" ) return;
  if ( hasElementClass( target, "pagenav") ) 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", "slide", slideshow, function( navs, i ) {
    logDebug( "Found slidenav",navs[i], slides.activeslides );
    connect( navs[i], "onclick", slides, "navhandler" );
  });
  // find slideholder links
  iterateElementsByTagAndClassName ( "a", "slidenav", slideshow, function( navs, i ) {
    logDebug( "Found slidenav",navs[i], slides.activeslides );
    connect( navs[i], "onclick", slides, "clickhandler" );
  });
  
  // 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] );
  }
  var medias = getElementsByTagAndClassName( "div", "media", $("Canvas") );
  var mediaslength = medias.length;
  logDebug("medias:", medias);
  for ( var s=0; s < mediaslength; s++ ) {
    slides.init( medias[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" );

