/* 
WebRight Local Javascript 
Copyright 2007, 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. 
*/

if ( typeof gebid == 'undefined' ) {
  function gebid( id ) {
    return document.getElementById( id );
  }
}

// signal ondomload when body element has loaded
// based on http://brothercake.com/site/resources/scripts/domready/
var ondomloadTries = 0;
var ondomloadInterval = setInterval( function() {
  ondomloadTries++;
  bodies = getElementsByTagAndClassName('body', null);
  log("Looking for body",ondomloadTries,bodies);
  if ( bodies.length > 0 ) {
    log("signal ondomload!");
    signal(window,'ondomload')
    clearInterval( ondomloadInterval ); 
  }
  if (ondomloadTries >= 8) {
    clearInterval( ondomloadInterval ); 
  }
}, 1500);


var wr = { "version":"1.1" }

// popover code
wr.activePopover = false;
wr.popover = function( id, offset ) {
  this.id = id;
  wr.offset = 9;
  if ( offset ) wr.offset = offset;
  //alert(this.offset);
  this.element = $(id);
  this.element.style.display = 'none';
  addElementClass( this.element, 'popover' );
  this.show = function( e ) {
    wr.showPopover( this.id );
    e.stop();
  }
  this.showNoStop = function() {
    wr.showPopover( this.id );
  }
  this.fixlinks = function() {
    //log("Fixlinks for",this.id);
    var links = getElementsByTagAndClassName( 'a', null, $('Content') );
    forEach( links, bind( function(e){ 
      if ( e.hash=='#'+this.id ) {
        log("Connecting link",e.hash); 
        connect( e, 'onclick', this, 'show' );
        connect( this.element, 'show', bind( function() { addElementClass( e, 'active' ); log("Activating",e); }, e ) );
        connect( this.element, 'hide', bind( function() { removeElementClass( e, 'active' ); log("Deactivating",e); }, e ) );
      } else { 
        //log("Ignoring",e.hash);
      } 
    }, this ) );
  }
  connect( window, 'ondomload', this, 'fixlinks' );
  if ( window.location.hash == '#'+this.id ) {
    log("Will open popover",this.id);
    connect( window, 'ondomload', this, 'showNoStop' );
  }
  var innerHeight = window.innerHeight
                          || document.documentElement.clientHeight
                          || document.body.clientHeight
                          || 0;
  wr.popoverHeight = innerHeight - 63;
}
wr.showPopover = function( id ) {
  if ( wr.activePopover ) {
    wr.hidePopover( wr.activePopover );
  }
  signal( $(id), 'show' );
  wr.activePopover = id;
  thetop =  window.pageYOffset
            || document.documentElement.scrollTop
            || document.body.scrollTop
            || 0;
  if ( thetop < wr.offset ) thetop = wr.offset;
  thetop = thetop + this.offset;
  $(id).style.top = thetop + 'px';
  inners = getElementsByTagAndClassName('div','inner',$(id));
  inners[0].style.maxHeight = wr.popoverHeight + 'px';
  $(id).style.display = 'block';
  var abovetop = thetop - wr.offset;
  window.scrollTo(0, abovetop);
}
wr.hidePopover = function( id ) {
  $(id).style.display = 'none';
  signal( $(id), 'hide' );
  wr.activePopover = false;
}

// slideshow code
wr.activeSlide = false;
wr.slideshow = function( id ) {
  this.id = id;
  this.element = $(this.id);
  // store slides as ids
  this.slides = [];
  slides = getElementsByTagAndClassName( "div", "slide", this.element );
  for ( i=0; i<slides.length; i++ ) {
    this.slides.push( slides[i].id );
  }
  log("Found slides",this.slides);
  // store controls as elements
  this.controls = { 'back': getElementsByTagAndClassName( "a", "back", this.element ), 'next': getElementsByTagAndClassName( "a", "next", this.element ) }
  controls = getElementsByTagAndClassName( "div", "controls", this.element );
  this.controls.element = controls[0];
  
  // init
  this.init = function() {
    log("Slideshow",this.id,"init");
    var hashval = false;
    if ( window.location.hash ) {
      hashval = window.location.hash;
      if ( hashval.substr( 0, 1 )=="#" ) {
        hashval = hashval.substr( 1 );
      }
    }
    if ( hashval ) {
      this.showSlide( hashval );
    }
    else {
      this.showSlide();
    }
    this.controls.element.style.display = "block";
  }
  connect( window, 'ondomload', this, 'init' );
}
wr.slideshow.prototype.stripShow = function( id ) {
  if ( !id ) return "";
  var showstring = this.id + "_";
  return id.substr( showstring.length );
}
wr.slideshow.prototype.controlClick = function( e ) {
  log("Control click",e.target().hash);
  e.stop();
  var slidename = e.target().hash.substr(1);
  window.location.hash = e.target().hash;
  this.showSlide( slidename );
}
wr.slideshow.prototype.showSlide = function( slidename ) {
  if ( this.activeSlide ) {
    $(this.activeSlide).style.display = "none";
  }
  var slideid = this.slides[0];
  var found = 0;
  if ( !slidename ) {
    // do nothing
  }
  else {
    var realname = this.id + "_" + slidename;
    for ( i=0; i<this.slides.length; i++ ) {
      if ( this.slides[i]==realname ) {
        found = i;
        break;
      }
    }
    if ( found ) {
      slideid = this.slides[ found ];
    }
  }
  log( "Showing slide", slideid );
  this.activeSlide = slideid;
  $(slideid).style.display = "block";
  
  // set controls
  for( i=0; i<this.controls.back.length; i++ ) {
    var control = this.controls.back[i];
    var bid = false;
    disconnectAll( control );
    if ( found < 1 ) {
      bid = this.stripShow( this.slides[0] );
      control.style.visibility = 'hidden';
    }
    else {
      bid = this.stripShow( this.slides[ found - 1 ] );
      control.style.visibility = 'visible';
    }
    control.href = "#" + bid;
    connect( control, "onclick", this, "controlClick" );
  }
  for( i=0; i<this.controls.next.length; i++ ) {
    var control = this.controls.next[i];
    var nid = false;
    disconnectAll( control );
    if ( found == this.slides.length-1 ) {
      nid = this.stripShow( this.slides[ found ] );
      control.style.visibility = 'hidden';
    }
    else {
      nid = this.stripShow( this.slides[ found + 1 ] );
      control.style.visibility = 'visible';
    }
    control.href = "#" + nid;
    connect( control, "onclick", this, "controlClick" );
  }
}