/* 
Scholarship Plus 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.5" }

slides.activeslides = {};
slides.slides = {};
slides.shows = {};
slides.autoplay = {};
slides.nextindex = {};
slides.timeouts = {};
slides.pause = 5000;
slides.fade = 1.0;

slides.showslide = function ( id, first ) {
  var slide = $(id);
  if ( !slide ) return;
  // which show?
  var showid = this.slides[ id ];
  if ( !showid ) {
    log("Got slide but no registered show for it!");
    return;
  }
  
  // hide cover slide
  if( this.cover_slide ) {
    this.cover_slide.style.display = 'none';
  }
  
  var aslide = false;
  if ( this.activeslides[ showid ] ) {
    //this.activeslides[ showid ].style.display = 'none';
    aslide = this.activeslides[ showid ];
    /*
    aslide.style.zIndex = 1;
    aslide.style.position = "absolute";
    */
    aslide.style.display = "none";
    signal( this.activeslides[ showid ], "inactive" );
  }
  this.activeslides[ showid ] = slide;
  slide.style.display = "block";
  /*
  slide.style.zIndex = 3;
  slide.style.opacity = 0;
  slide.style.display = 'block';
  appear( slide, { "duration": this.fade, "afterFinish": function() { 
    if (aslide) {
      aslide.style.display = "none";
      aslide.style.position = "static";
    }
  } } );
  */
  var videoid = getNodeAttribute( slide, "videoid" );
  if ( videoid && !first ) {
    log("Playing",videoid);
    window.setTimeout( "$('flowplayer"+videoid+"').DoPlay()", 1000 );
    if ( this.autoplay[ showid ] ) {
      this.stopauto( id );
    }
  }
  signal( this.activeslides[ showid ], "active" );
  
  // update nextindex
  var mylength = this.shows[ showid ].length;
  for( var i=0; i<mylength; i++ ) {
    if ( this.shows[ showid ][i].id == this.activeslides[ showid ].id ) {
      this.nextindex[ showid ] = i+1;
      if ( i+1 >= mylength ) {
        this.nextindex[ showid ] = 0;
      }
      i = mylength;
      log("Found next slide", this.nextindex[ showid ] );
    }
  }
}

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.stopauto( id );
    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.stopauto( id );
  this.showslide( id );
}

slides.timehandler = function ( showid ) {
  var next = this.shows[ showid ][ this.nextindex[ showid ] ];
  this.showslide( next.id );
  if ( this.autoplay[ showid ] ) {
    this.timeouts[ showid ] = window.setTimeout( "slides.timehandler('"+showid+"')", this.pause );
  }
}

slides.stopauto = function ( slideid ) {
  var showid = this.slides[ slideid ];
  if ( this.autoplay[ showid ] ) {
    this.autoplay[ showid ] = 0;
    window.clearTimeout( this.timeouts[ showid ] );
    log ( "Autoplay stopped for", showid );
  }
}

slides.init = function( slideshow ) {
  log("Init for slideshow",slideshow.id,"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[ slideshow.id ] = false;
  // map slides to this slideshow
  var allslides = getElementsByTagAndClassName( "div", "slide", slideshow );
  var mylength = allslides.length;
  if ( mylength ) {
    for ( var i=0; i < mylength; i++ ) {
      this.slides[ allslides[i].id ] = slideshow.id;
      if ( hasElementClass( allslides[i], "cover") ) {
        this.activeslides[ slideshow.id ] = allslides[i];
      }
    }
  }
  // track show by show
  this.shows[ slideshow.id ] = allslides;
  this.nextindex[ slideshow.id ] = 0;
  log("Found",mylength,"slides, showing first.");
  if( mylength && this.cover_slide == null ) {
    this.showslide( allslides[0].id, true );
  }
  // autoplay
  if ( hasElementClass( slideshow, "autoplay" ) ) {
    this.autoplay[ slideshow.id ] = 1;
    this.timeouts[ slideshow.id ] = window.setTimeout( "slides.timehandler('"+slideshow.id+"')", this.pause );
  }
  else {
    this.autoplay[ slideshow.id ] = 0;
  }
}

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" );
