/**
 *   @fileoverview  Javascripts for www.micronomics.com.
 *   @author  jeremydw@gmail.com (Jeremy Weinstein)
 */

var micron = micron || {};

micron.initBackground = function(divId) {
  var maxNum = Math.ceil(Math.random() * 4);

  this.divEl = document.getElementById(divId);
  this.divEl.style.backgroundImage = "url('images/banners/0" + maxNum + ".jpg')";
  document.getElementById('caption-0' + maxNum).style.display = 'block';
};

micron.preso = function(listId) {
  this.listElements = {};
  this.selectedEl = this.getElementFromName('default');
  this.selectedListEl = '';

  var tags = document.getElementById(listId).getElementsByTagName('li');
  var links = document.getElementById(listId).getElementsByTagName('a');
  for (var i in links) {
    if (links[i].href) {
      links[i].parentNode.onclick = this.swapHandler(this, links[i].parentNode);
      this.listElements[links[i].href.split('#')[1]] = links[i].parentNode;
      links[i].parentNode.id = 'li-' + links[i].href.split('#')[1];
    }
  }

  var hash = window.location.hash;
  if (hash) {
    this.swap(this.listElements[hash.replace('#', '')]);
  }

};

micron.preso.prototype.swapByName = function(name) {
  this.swap(document.getElementById('li-' + name));
}

micron.preso.prototype.getSideLinkFromName = function(name) {

}

micron.preso.prototype.getElementFromName = function(name) {
  return document.getElementById('item-' + name);
};

micron.preso.prototype.swapHandler = function(me, aEl) {
  return function() {
    me.swap(aEl);
  }
};

micron.preso.prototype.swap = function(aEl) {
  var name = aEl.getElementsByTagName('a')[0].href.split("#")[1];

  this.selectedListEl.className = '';
  this.selectedListEl = aEl;
  this.selectedListEl.className = 'selected';

  this.selectedEl.style.display = 'none';
  this.selectedEl = this.getElementFromName(name);
  this.selectedEl.style.display = 'block';
};
