/* 
usweb homepage-only 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. 
*/

// fonts
function fontSetup() {
  iterateElementsByTagAndClassName( "a", null, $("SiteNav"), function( elements, i ) { 
    if ( hasElementClass( elements[i].parentNode, "active" ) ) {
      addElementClass( elements[i], "AGBold");
    }
    else {
      addElementClass( elements[i], "AGMedium");
    }
  } );
  Cufon.replace( getElementsByTagAndClassName( null, "AGMedium"), { "fontFamily": "AGMedium", "hover": true });
  Cufon.replace( getElementsByTagAndClassName( null, "AGBold"), { "fontFamily": "AGBold", "hover": true });
  log("Fonts replaced");
}
connect( window, "ondomload", window, "fontSetup" );


// special
var special = { "version": "1.0", "enabled": false }

special.open = function() {
  $("Special").style.opacity = 0;
  $("Special").style.display = "block";
  slideDown( $("Special"), { "duration": 0.8, "beforeUpdate": function() { $("Special").style.opacity = 1; } } );
  //$("Special").style.opacity = 1.0;
  log("Opened Special");
}

special.setCookie = function() {
  
}

special.close = function() {
  var exdate=new Date();
  exdate.setTime( exdate.getTime() + (4*60*60*1000)); 
  setCookieValue( "donated", "1", "expires="+exdate.toUTCString() );
  slideUp( $("Special"), { "duration": 0.5 } );
  special.enabled = false;
}

special.donate = function() {
  var exdate=new Date();
  exdate.setTime( exdate.getTime() + (30*24*60*60*1000)); 
  setCookieValue( "donated", "1", "expires="+exdate.toUTCString() );
  window.location = "https://dnbweb1.blackbaud.com/OPXDONATE/AddDonor.asp?cguid=5334E533-420F-4BAF-9DF0-80895891FA39&sTarget=https%3A%2F%2Fdnbweb1.blackbaud.com%2FOPXDONATE%2Fdonate.asp%3Fcguid%3D5334E533%252D420F%252D4BAF%252D9DF0%252D80895891FA39%26dpid%3D13117&sid=525A4B2E-EF21-4D07-BF58-53F892AA11F6";
}

special.init = function() {
  if ( $("Special") ) {
    var donated = getCookieValue( "donated" );
    //var donated = false;
    if ( !donated ) {
      special.enabled = true;
      window.setTimeout( special.open, 500 );
    }
  }
}

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


// homeshow object
var homeshow = { "version": "1.0", "slides": [], "sl": 0, "activeslide": null, "nextslide": null }

// set timeout based on activeslide duration (this is called once any effects are complete)
homeshow.settimeout = function() {
  if ( special.enabled ) {
    window.setTimeout( homeshow.settimeout, 500 );
  }
  else {
    var dur = getNodeAttribute( homeshow.activeslide, "duration" );
    var mdur = 1000 * dur;
    window.setTimeout( homeshow.next, mdur );
    log( "Next transition in",mdur,"milliseconds");
  }
}

// sets up transition, calls appropriate effect
homeshow.next = function() {
  // find next slide
  for( var i=0; i<homeshow.sl; i++ ) {
    if ( homeshow.slides[ i ].id == homeshow.activeslide.id ) {
      if ( i+1 < homeshow.sl ) {
        homeshow.nextslide = homeshow.slides[ i + 1 ];
      }
      else {
        homeshow.nextslide = homeshow.slides[ 0 ];
      }
      break;
    }
  }
  log( "i is", i, "activeslide is", homeshow.activeslide.id, "nextslide is", homeshow.nextslide.id );
  if ( i%2==0 ) {
    homeshow.fade();
  }
  else {
    homeshow.swipe();
  }
}

// fades between activeslide and next slide
homeshow.fade = function() {
  this.activeslide.style.zindex = 1;
  this.nextslide.style.zindex = 2;
  this.nextslide.style.opacity = 0;
  appear( this.nextslide, { "duration": 1, "afterFinish": homeshow.resetfade } );
}
// resets after fade, calls settimeout
homeshow.resetfade = function() {
  homeshow.activeslide.style.display = "none";
  homeshow.activeslide = homeshow.nextslide;
  homeshow.nextslide = false;
  homeshow.settimeout();
}

// swipes between activeslide and next slide
homeshow.swipe = function() {
  this.activeslide.style.zindex = 1;
  this.nextslide.style.left = "705px";
  this.nextslide.style.display = "block";
  Move( this.nextslide, { "x": -705, "y": 0, "duration": 1 } );
  Move( this.activeslide, { "x": -705, "y": 0, "duration": 1, "afterFinish": homeshow.resetswipe } );
}
// resets after fade, calls settimeout
homeshow.resetswipe = function() {
  homeshow.activeslide.style.display = "none";
  homeshow.activeslide.style.left = "0px";
  homeshow.activeslide = homeshow.nextslide;
  homeshow.nextslide = false;
  homeshow.settimeout();
}

homeshow.init = function() {
  if ( !$('HomeSlides') ) {
    // not the homepage
    return;
  }
  iterateElementsByTagAndClassName ( "div", "slide", $('HomeSlides'), function( eles, i ) {
    eles[i].index = i+1;
    homeshow.slides.push( eles[i] );
  });
  this.sl = this.slides.length;
  log( "Homeshow slides", this.slides, "length", this.sl );
  var found = false;
  if ( window.location.hash ) {
    var hash = window.location.hash.substr( 1 );
    for ( var i=0; i<this.slides.length; i++ ) {
      if ( this.slides[i].id==hash ) {
        found = i;
        removeElementClass( this.slides[0], "first" );
        addElementClass( this.slides[i], "first" );
        this.activeslide = this.slides[i];
        log("Hash slide found at",found );
        break;
      }
    }
    if ( !found ) {
      this.activeslide = this.slides[0];
      log( "Hash present, but slide not found.");
    }
  }
  else {
    this.activeslide = this.slides[0];
    log("No hash");
  }
  log("First slide is", this.activeslide.id );
  if ( !found ) {
    this.settimeout();
  }
  else {
    window.setTimeout( homeshow.settimeout, 3000 );
    log( "Delayed start by 3 secs" );
  }
}

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

// sidebar enabler
var sidebars = {};

sidebars.init = function() {
  iterateElementsByTagAndClassName ( "div", "sideitem", $("CanvasInner"), function( eles, i ) {
    var href = getNodeAttribute( eles[i], "href" );
    if ( href ) {
      connect( eles[i], "onclick", function( e ) { window.location=getNodeAttribute( e.src(), "href" ); } );
      addElementClass( eles[i], "linked" );
    }
  } );
  log( "Sidebars init");
}

connect( window, "onload", sidebars, "init" );

// EOF

