var Buildings = Class.create({

  initialize: function(div, links, left, right) {
    this.div   = $(div);
    this.left  = $(left)
    this.right = $(right);
    this.links = $$(links);
    this.years = [];
    this.years[1995] = -200;
    this.years[1998] = -600;
    this.years[1999] = -1000;
    this.years[2000] = -1600;
    this.years[2003] = -2000;
    this.years[2004] = -2500;
    this.years[2005] = -3300;
    this.years[2006] = -3700;
    this.years[2007] = -4400;
    this.years[2012] = -4700;
    this.effect;
    this.activeIndex = 2;
    this.active      = this.links[this.activeIndex];
    this.links.each(
      (function(s, i){
        s.observe('click', (function(event){
          event.stop();
          this.move(s, i);
        }).bind(this));
      }).bind(this)
    );
    this.left.observe('click', (function(event){
      var prevIndex = this.activeIndex-1;
      var prev      = this.links[prevIndex];
      if (prev) {
        this.move(prev, prevIndex);
      }
    }).bind(this));
    this.right.observe('click', (function(event){
      var nextIndex = this.activeIndex+1;
      var next      = this.links[nextIndex];
      if (next) {
        this.move(next, nextIndex);
      }
    }).bind(this));
  },

  move: function(link, index) {
    year = parseInt(link.innerHTML);
    if (this.active == link) {
      return;
    }
    this.links.each(function(link){
      link.addClassName('white-dash');
    });
    this.active      = link;
    this.activeIndex = index;
    position = this.div.getStyle('backgroundPosition');
    if (position) {
      position = position.split(' ');
      left = parseFloat(position[0]);
    } else {
      left = 0;
    }
    if (this.effect) {
      this.effect.cancel();
    }
    this.effect = new Effect.BackgroundMove(this.div, { x: this.years[year] - left, afterFinish: (function(){this.active.removeClassName('white-dash');}).bind(this) });
  }

});

var site, logo;
var img_path = '/themes/site/images/';
document.observe('dom:loaded', function(event) {
  new Buildings('buildings', '#navigation-years a', 'building-left', 'building-right');
  site = $('site');
  logo = $('logo').down();
});

function switch_night()
{
  site.addClassName('night');
  logo.src = img_path + 'logo-n.png';
}

function switch_day()
{
  site.removeClassName('night');
  logo.src = img_path + 'logo.png';
}