/* 
ci interface Javascript 
Copyright 2008, 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. 
*/

// ci object
var ci = { "version":"1.0" }

ci.sections = [];
ci.hiding = {};
ci.activeSection = false;
ci.activeMenu = false;
ci.slidenavs = [];
ci.activeSlide = false;

// show a section, hide others
ci.showSection = function ( id ) {
  if ( this.hiding[ id ] ) {
    window.clearTimeout( this.hiding[ id ] );
    this.hiding[ id ] = false;
  }
  var menuid =  "menu_" + id.substr( 8 );
  if ( !menuid || !$(menuid) ) {
    //log( "Menu not found for", id, "looking for", menuid, $(menuid) );
    return;
  }
  if ( this.activeMenu.id == menuid ) {
    return;
  }
  if ( this.activeSection ) {
    this.activeSection.style.borderRightStyle = 'solid';
    this.activeSection = false;
  }
  if ( this.activeMenu ) {
    this.activeMenu.style.display = 'none';
    this.activeMenu = false;
  }
  this.activeMenu = $( menuid );
  this.activeSection = $( id ).childNodes[1];
  var canvasPos = elementPosition( $('Canvas') );
  var pos = elementPosition( $( id ) );
  var dim = elementDimensions( $( id ) );
  log( id, "Position", pos, " Dimensions", dim, " Cavas position", canvasPos );
  var menuOffsetTop = pos.y - canvasPos.y - 1;
  var menuOffsetLeft = pos.x - canvasPos.x + dim.w - 1;
  this.activeMenu.style.top = menuOffsetTop + 'px';
  this.activeMenu.style.left = menuOffsetLeft + 'px';
  this.activeMenu.style.display = 'block';
  this.activeSection.style.borderRightStyle = "none";
}

// hide a section
ci.hideSection = function( id ) {
  if ( this.hiding[ id ] ) {
    return this.hiding[ id ];
  }
  else {
    this.hiding[ id ] = window.setTimeout( "ci.hideSectionNow( '"+id+"' )", 500 );
  }
  return this.hiding[ id ];
}

ci.hideSectionNow = function ( id ) {
  this.hiding[ id ] = false;
  var menuid =  "menu_" + id.substr( 8 );
  //log("Hiding menu",menuid);
  if ( $(menuid) ) {
    $(menuid).style.display = 'none';
  }
  if ( this.activeMenu.id == menuid ) {
    this.activeMenu = false;
  }
  if ( $(id).childNodes[1] ) {
    $(id).childNodes[1].style.borderRightStyle = 'solid';
  }
  if ( this.activeSection.id == id ) {
    this.activeSection = false;
  }
}

// show a slide
ci.showSlide = function ( id ) {
  if ( this.activeSlide ) {
    this.activeSlide.style.display = "none";
  }
  this.activeSlide = $(id);
  this.activeSlide.style.display = "block";
}

// event handlers
ci.mouseover = function ( e ) {
  e.stop();
  var sectionid = e.src().id;
  this.showSection( sectionid );
}

ci.mouseout = function ( e ) {
  e.stop();
  var sectionid = e.src().id;
  this.hideSection( sectionid );
}

ci.slidenavClick = function ( e ) {
  e.stop();
  if ( !e.target().hash ) return;
  var slideid = e.target().hash.substr(1);
  window.location.hash = e.target().hash;
  this.showSlide( slideid );
}

// init handler
ci.init = function() {
  // find sections
  this.sections = getElementsByTagAndClassName( "div", "section", $("Navigation") );
  log("Found sections",this.sections);
  for ( var i=0; i < this.sections.length; i++ ) {
    connect( this.sections[i], "onmouseover", this, "mouseover" );
    connect( this.sections[i], "onmouseout", this, "mouseout" );
  }
  var actives = getElementsByTagAndClassName( "div", "active", $("Navigation") );
  if ( actives.length ) {
    for ( var i=0; i < actives.length; i++ ) {
      var sectionid = "section_" + actives[i].id.substr( 5 );
      actives[i].style.position = "absolute";
      this.showSection( sectionid );
      this.hiding[ sectionid ] = window.setTimeout( "ci.hideSectionNow( '"+sectionid+"' )", 900 );
    }
  }
  // find slides and slidenav links
  this.slidenavs = iterateElementsByTagAndClassName( "p", "slidenav", $("Object"), function( elements, i ) { connect( elements[i], "onclick", ci, "slidenavClick" ) } );
  // display first or active slide
  var hashval = false;
  if ( window.location.hash ) {
    hashval = window.location.hash;
    if ( hashval.substr( 0, 1 )=="#" ) {
      hashval = hashval.substr( 1 );
    }
  }
  if ( this.slidenavs.length ) {
    if ( hashval ) {
      log("Showing slide",hashval);
      this.showSlide( hashval );
    }
    else {
      var firstslides = getElementsByTagAndClassName( "div", "firstslide", $("Object") );
      if ( firstslides.length ) {
        log("Showing first slide");
        this.showSlide( firstslides[0].id );
      }
    }
  }
}

connect(window,"ondomload",ci,"init");