<!-- UpcomingEvents
/*

v.1.1

loop: doAnimation -> startAnimation -> play -> startFade -> fade -> restartAnimation

*/
var UpcomingEvents = Class.create({

  initialize: function() {
    this.ue = $('Upcoming_Events');
    this.strAry = [];
    this.frame_delay = 0.1;
    this.blank_delay = 0.2;
    this.loop_delay = 2.5;
    this.idx = 0;
    this.opacity = 1.0;
  },
  
  init: function() {
    if (!this.ue) return;
    this.prepareStrings();
    this.doAnimation();
  },

  prepareStrings: function() {
    var chars = 'Upcoming Events'.split('');
    var str = '';
    var e;
    for (var i = 0, len = chars.length; i < len; ++i) {
      str += chars[i];
      e = document.createElement('span')
      e.innerHTML = str
      this.strAry.push(e);
    }
  },
  
  doAnimation: function() {
    this.ue.childElements().invoke('remove');
    var e = document.createElement('span');
    e.innerHTML = '&nbsp;';
    this.ue.appendChild(e);
    this.ue.removeClassName('Upcoming_Events');
    this.ue.addClassName('box');
    this.ue.setStyle({'color': 'red'});
    this.startAnimation();
  },
  
  startAnimation: function() {
    new PeriodicalExecuter(this.play.bind(this), this.frame_delay);
  },
      
  play: function(pe) {
    this.ue.replaceChild(this.strAry[this.idx], this.ue.firstDescendant());
    this.idx++;
    if (this.idx >= this.strAry.length) {
      this.idx = 0;
      pe.stop();
      this.startFade.bind(this).delay(this.loop_delay);
    }
  },
  
  startFade: function() {
    new PeriodicalExecuter(this.fade.bind(this), this.frame_delay / 3);
  },
  
  fade: function(pe) {
    if (this.opacity <= 0) {
      this.opacity = 1.0;
      pe.stop();
      this.ue.firstDescendant().setOpacity(this.opacity);
      this.restartAnimation();
      return;
    }
    this.opacity -= 0.1;
    this.ue.firstDescendant().setOpacity(this.opacity);
  },
  
  restartAnimation: function() {
    var e = document.createElement('span');
    e.innerHTML = '&nbsp;';
    this.ue.replaceChild(e, this.ue.firstDescendant());
    this.startAnimation.bind(this).delay(this.blank_delay);
  }

});

new UpcomingEvents().init();

//-->

