/* 
cphs 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. 
*/

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

// site nav object
var cphs = {};
cphs.sectionConnect = function( elements, i ) {  
  var link = elements[i];
  addElementClass( link, "vagbold")
  var par = link.parentNode;
  while( par.tagName != "DIV" ) {
    par = par.parentNode;
  }
  connect( par, "onclick", function(){ window.location=link.href; } );
  par.style.cursor = "pointer";
}

cphs.navAndFontSetup = function() {
  if ( $('SiteNav') ) {
    iterateElementsByTagAndClassName( "a", null, $("SiteNav"), cphs.sectionConnect );
    
    iterateElementsByTagAndClassName( "h1", null, $("Main"), function( elements, i ) { addElementClass( elements[i], "vagbold") } );
    iterateElementsByTagAndClassName( "h2", null, $("Main"), function( elements, i ) { addElementClass( elements[i], "vagbold") } );
    iterateElementsByTagAndClassName( "h3", null, $("Main"), function( elements, i ) { if ( !hasElementClass( elements[i].parentNode, "day" ) ) addElementClass( elements[i], "vagbold") } );
    iterateElementsByTagAndClassName( "h4", null, $("Main"), function( elements, i ) { addElementClass( elements[i], "vagbold") } );
    iterateElementsByTagAndClassName( "h4", null, $("Navigation"), function( elements, i ) { addElementClass( elements[i], "vagbold") } );
    iterateElementsByTagAndClassName( "p", null, $("Navigation"), function( elements, i ) {
      var ele = elements[i];
      if ( hasElementClass( ele, "editor") ) return;
      var link = ele.firstChild;
      if ( hasElementClass( link, "active") ) {
        addElementClass( ele, "vagbold");
      }
      else {
        addElementClass( ele, "vaglight");
      }
    } );
  }
  if ( $('HomeNav') ) {
    iterateElementsByTagAndClassName( "a", null, $("HomeNav"), cphs.sectionConnect );
    
    iterateElementsByTagAndClassName( "h1", null, $("HomeContent"), function( elements, i ) { addElementClass( elements[i], "vagbold") } );
    iterateElementsByTagAndClassName( "h2", null, $("HomeContent"), function( elements, i ) { addElementClass( elements[i], "vagbold") } );
    iterateElementsByTagAndClassName( "h3", null, $("HomeContent"), function( elements, i ) { addElementClass( elements[i], "vagbold") } );
    iterateElementsByTagAndClassName( "h4", null, $("HomeContent"), function( elements, i ) { addElementClass( elements[i], "vagbold") } );
  }
  
  Cufon.replace( getElementsByTagAndClassName( null, "vagbold"), { "fontFamily": "vag-rounded-bold" });
  Cufon.replace( getElementsByTagAndClassName( null, "vaglight"), { "fontFamily": "vag-rounded-light" });
}

connect( window, "ondomload", cphs, "navAndFontSetup" );

// calendar popout helper
connect( window, "ondomload", function() { 
  if ( window.location.hash ) {
    var id = window.location.hash.substr(1);
    var isslide = $(id);
    if ( isslide && hasElementClass( isslide, "popout" ) ) {
      log("Showing popout on this page",id);
      isslide.style.display = "block";
    }
  }
});

