/**
 * discover queens javascript include
 * Copyright 2009 Fund for the City of New York
 * 
 * This is free software. Use and distribution are subject to the terms
 * of the FCNY Open Source Software License. See license.txt, or
 * http://fred.fcny.org/fcny-ossl.txt
 */

function toggleGMapFrame() {
  var gmf = $("GMapFrame");
  if ( !gmf.open ) {
    gmf.width = 605;
    gmf.open = true;
    $("GMapLink").style.display = "block";
    $("GMapExpand").style.display = "none";
    $("GMapCollapse").style.display = "inline";
  }
  else {
    $("GMapLink").style.display = "none";
    $("GMapExpand").style.display = "inline";
    $("GMapCollapse").style.display = "none";
    gmf.width = 150;
    gmf.open = false;
  }
}

function toggleShareThis() {
  var st = $("ShareThis");
  if ( !st.open ) {
    st.open = true;
    st.style.display = "block";
  }
  else {
    st.style.display = "none";
    st.open = false;
  }
}


// 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", $('Canvas'), function( navs, i ) {
    connect( navs[i], "onclick", slides, "clickhandler" );
    var slideid = navs[i].hash.substr(1);
    var slide = $(slideid);
    if ( slide ) {
      connect( slide, "activate", function() { addElementClass( navs[i].firstChild, "active" ) } );
      connect( slide, "deactivate", function() { removeElementClass( navs[i].firstChild, "active" ) } );
    }
  });
  // set activeslide to cover
  var covers = getElementsByTagAndClassName( "div", "cover", $("Canvas") );
  if (covers.length) {
    this.activeslide = covers[0];
  }
  // show a different slide if requested
  if ( window.location.hash ) {
    var id = window.location.hash.substr(1);
    var isslide = $(id);
    if ( hasElementClass( isslide, "slide" ) ) {
      log("Showing slide on this page",id);
      this.showslide( id );
    }
  }
  log("Slides init");
}
connect( window, "ondomload", slides, "init" );

// events object
var events = { "version":"1.0" }
events.activeday = false;
events.prev = false;
events.next = false;
events.na = "";

events.showday = function( day ) {
  if ( events.activeday ) {
    removeElementClass( $("grid"+events.activeday), "active" );
    removeElementClass( $("cell"+events.activeday), "active" );
    removeElementClass( $("day"+events.activeday), "active" );
  }
  events.activeday = day;
  addElementClass( $("grid"+events.activeday), "active" );
  addElementClass( $("cell"+events.activeday), "active" );
  addElementClass( $("day"+events.activeday), "active" );
}

events.daycellclick = function( e ) {
  var daycell = e.src();
  events.showday( daycell.id.substr(4) );
}

events.gridclick = function( e ) {
  var gridsrc = e.target();
  while( gridsrc.tagName!='TABLE' && gridsrc.tagName!='TD' ) {
    gridsrc = gridsrc.parentNode;
  }
  if ( gridsrc.tagName=='TD' ) {
    e.stop();
    // does the gridsrc have an id?
    if ( gridsrc.id ) {
      // is it part of the active week?
      if ( hasElementClass( gridsrc, "activeweek" ) ) {
        events.showday( gridsrc.id.substr(4) );
      }
      else {
        // redirect to week
        window.location = "?date="+gridsrc.id.substr(4) + events.na;
      }
    }
    else {
      if ( hasElementClass(gridsrc, "prev") ) {
        // go to previous month
        if ( events.prev ) {
          window.location = "?date=" + events.prev + events.na;
        }
      }
      else if ( hasElementClass(gridsrc, "next") ) {
        // go to next month
        if ( events.next ) {
          window.location = "?date=" + events.next + events.na;
        }
      }
    }
  }
}

events.init = function() {
  // find daycells
  iterateElementsByTagAndClassName ( "td", "daycell", $('Week'), function( daycells, i ) {
    connect( daycells[i], "onclick", events, "daycellclick" );
    if ( hasElementClass( daycells[i], "active") ) {
      events.activeday = daycells[i].id.substr( 4 );
    }
  });
  // clickhandler for grid
  connect( $("Grid"), "onclick", events, "gridclick" );
  // daycell handler for ongoing
  iterateElementsByTagAndClassName ( "div", "ongoing", $('Sidebar'), function( daycells, i ) {
    connect( daycells[i], "onclick", events, "daycellclick" );
  });
  // use query string to set active day
  var getargs = parseQueryString( window.location.search );
  if ( getargs.date ) {
    events.showday( getargs.date );
  }
  log("Events init, activeday is",events.activeday);
}
// init occurs on calendar view


// homepage autoshow object
var autoshow = { "version":"1.0", "play": true, "activeslide": false, "nextindex": 0, "autoslides": [], "autoslidesLength": 0 };
autoshow.pause = 5000;
autoshow.fade = 1.0;
autoshow.clickhandler = function( e ) {
  if ( !this.play ) {
    this.play = true;
    this.timeout = window.setTimeout( "autoshow.timehandler()", 300 );
    log( "Autoshow resumed." );
  }
  else {
    this.play = false;
    window.clearTimeout( this.timeout );
    log( "Autoshow paused." );
  }
}
autoshow.timehandler = function ( ) {
  // advance to next slide
  var activeindex = this.nextindex;
  this.nextindex++;
  if ( this.nextindex+1 > this.autoslidesLength ) {
    this.nextindex = 0;
  }
  var nextslide = this.autoslides[ this.nextindex ];
  // turn on next slide
  this.activeslide.style.zIndex = 1;
  nextslide.style.zIndex = 3;
  nextslide.style.opacity = 0;
  nextslide.style.display = 'block';
  appear( nextslide, { "duration": this.fade } );
  this.activeslide = nextslide;
  // trigger video
  var videoid = getNodeAttribute( this.activeslide, "videoid" );
  if ( videoid && !paused ) {
    log("Playing",videoid);
    window.setTimeout( "$('flowplayer"+videoid+"').DoPlay()", 1000 );
  }
  // set timeouts
  window.setTimeout( "autoshow.resetSlide("+activeindex+")", (this.pause - 500) );
  this.timeout = window.setTimeout( "autoshow.timehandler()", this.pause );
}
autoshow.resetSlide = function( index ) {
  this.autoslides[ index ].style.zIndex = 1;
  this.autoslides[ index ].style.display = 'none';
}
autoshow.init = function() {
  var hasBanner = $("BannerPhoto");
  if ( !hasBanner ) return;
  
  // connect play / pause to bannerphoto
  connect( $("BannerPhoto"), "onclick", autoshow, "clickhandler" );
  // autoslides
  this.autoslides = getElementsByTagAndClassName( "div", "autoslide", $("Canvas") );
  this.autoslidesLength = this.autoslides.length;
  if ( this.autoslidesLength  ) {
    this.nextindex = 0;
  }
  // show a different slide if requested
  if ( window.location.hash ) {
    var id = window.location.hash.substr(1);
    for ( var i=0; i < this.autoslidesLength; i++ ) {
      if ( this.autoslides[i].id == id ) {
        this.nextindex = i;
      }
    }
  }
  // show first slide
  if ( this.autoslidesLength  ) {
    this.activeslide = this.autoslides[ this.nextindex ];
    if ( this.nextindex > 0 ) {
      this.activeslide.style.display = 'block';
      this.activeslide.style.zIndex = 3;
      this.autoslides[ 0 ].style.display = 'none';
    }
    if ( this.play ) {
      this.timeout = window.setTimeout( "autoshow.timehandler()", this.pause );
      log( "Starting autoshow pause:",this.pause,"slides:",this.autoslidesLength );
    }
    else {
      log( "Autoshow ready (not playing) pause:",this.pause,"slides:",this.autoslidesLength );
    }
  }
}
connect( window, "ondomload", autoshow, "init" );


