/*
  emblem.js
  Object Oriented JavaScript Application
  Copyright (C) 2010 JDAllen.net
*/
var EmblemApp = new Class({
  initialize: function(options) {
    this.options = options;
    this.doubleDelay = this.options.animateFrameDelay * 2;
    this.numFrames = this.options.data.length;
    this.currentFrame = 0;
    this.emblemFx = [];
    this.createEmblem();
  },
  debug: function(obj) {
    if (window.console && window.console.debug) {
      console.debug(obj);
    }
  },
  createEmblem: function() {
    var frame = this.options.data[this.currentFrame];
    this.frameHeight = frame.length;
    for (var y=0; y<this.frameHeight; y++) {
      var line = frame[y];
      this.lineWidth = line.length;
      for (var x=0; x<this.lineWidth; x++) {
        var datum = line[x];
        if (datum > 0) {
          var a = new Element('a', {
            'class': 'emblem-a',
            'href': this.options.links[datum],
            'target': this.options.targets[datum],
            'html': this.options.types[datum],
            'styles': {
              'left': this.getX(x),
              'top': this.getY(y),
              'color': this.getRandomHexColor()
            }
          });
          this.emblemFx[datum] = new Fx.Morph(a, {
            duration: this.options.animateFrameDelay, 
            transition: Fx.Transitions.Sine.easeOut
          });
          this.options.container.appendChild(a);
        }
      }
    }
    this.timeoutID = this.nextFrame.delay(1, this);
  },
  nextFrame: function() {
    if (++this.currentFrame == this.numFrames) {
      this.currentFrame = 0;
    }
    var frame = this.options.data[this.currentFrame];
    for (var y=0; y<this.frameHeight; y++) {
      var line = frame[y];
      for (var x=0; x<this.lineWidth; x++) {
        var datum = line[x];
        if (datum > 0) {
          this.emblemFx[datum].start({
            'left': this.getX(x),
            'top': this.getY(y),
            'color': this.getRandomHexColor()
          });
        }
      }
    }
    this.timeoutID = this.nextFrame.delay(this.doubleDelay, this);
  },
  getRandomHexColor: function() {
    return this.getHexColor(
      $random(0, this.options.colors.length - 1)
    );
  },
  getHexColor: function(index) {
    return '#' + this.options.colors[index];
  },
  getX: function(x) {
    return x * this.options.xOffset + this.options.baseX;
  },
  getY: function(y) {
    return y * this.options.yOffset + this.options.baseY;
  }
});

var emblemAppInstance;
Window.onDomReady = window.addEvent('domready', function() {
  startEmblemAppInstance.delay(100);
});
function startEmblemAppInstance() {
  emblemAppInstance = new EmblemApp({
    'container': document.id('emblem'),
    'links': [
      'not used', /* indexes start at one */
      'http://en.wikipedia.org/wiki/John_Horton_Conway',
      'http://en.wikipedia.org/wiki/Martin_Gardner',
      'http://en.wikipedia.org/wiki/Bill_Gosper',
      'http://en.wikipedia.org/wiki/Richard_K._Guy',
      'http://en.wikipedia.org/wiki/Paul_Erd%C5%91s'
    ],
    'targets': [
      'not used', /* indexes start at one */
      'emblem1',
      'emblem2',
      'emblem3',
      'emblem4',
      'emblem5'
    ],
    'animateFrameDelay': 1000,
    'baseX': 35,
    'baseY': 15,
    'xOffset': 30,
    'yOffset': 30,
    'data': [
      [
        [0,1,0],
        [0,0,2],
        [3,4,5]
      ],
      [
        [1,0,2],
        [0,3,5],
        [0,4,0]
      ],
      [
        [0,0,2],
        [1,0,5],
        [0,3,4]
      ],
      [
        [1,0,0],
        [0,5,2],
        [3,4,0]
      ]
    ],
    'colors': [
      '868488',
      '919292',
      'b8988a',
      'a1a49a',
      'bba38f',
      'adafb2',
      'bbae9c'
    ],
    'types': {
      0: '&nbsp;',
      1: '&bull;',
      2: '&bull;',
      3: '&bull;',
      4: '&bull;',
      5: '&bull;'
    }
  });
}

