/**
 * Peace Is Loud javascript include
 * Copyright 2010 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
 */


var alertballoons = { "version": "1.0", "alerts": {}, "active": 0, "timer": 6000 }

alertballoons.init = function() {
  iterateElementsByTagAndClassName( "table", "alertballoon", $("top"), function( elements, i ) { 
    alertballoons.alerts[i] = elements[i];
    elements[i].hold = false;
    connect( elements[i], "onmouseover", alertballoons, "mouseover" );
    connect( elements[i], "onmouseout", alertballoons, "mouseout" );
    log("Found alert", elements[i].id,"stored at",i );
  } );
  if ( this.alerts[0] ) {
    appear( this.alerts[0] );
    window.setTimeout( "alertballoons.showNext()", this.timer+1000 );
  }
}

alertballoons.showNext = function() {
  var next = this.active + 1;
  if ( this.alerts[ next ] ) {
    log("Will display next alert",this.alerts[next].id );
  }
  else if ( next > 1 ) {
    log( "At end. Will display first alert" );
    next = 0;
  }
  else {
    log( "Only one alert, no change" );
    return;
  }
  var current = this.alerts[this.active];
  if ( current.hold==false ) {
    this.active = next;
    fade( current );
    appear( this.alerts[next], { "queue": "end" } );
  }
  else {
    log("On hold for mouseover, try again later.")
  }
  window.setTimeout( "alertballoons.showNext()", this.timer );
}

alertballoons.mouseover = function( e ) {
  var bal = e.src();
  bal.hold = true;
}

alertballoons.mouseout = function( e ) {
  var bal = e.src();
  bal.hold = false;
}

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