var sections = new Array();
var section_names = new Array(
  'description',
  'ingredients',
  'nutrients'
);

function Section(link) {
  this.link     = link;
  this.name     = this.link.hash.substr(1);
  this.content  = document.getElementById(this.name);
  this.cssClass = this.link.parentNode.className;

  this.show = function() {
    this.link.parentNode.className = 'active';
    this.content.style.display = 'block';
  }
  this.hide = function() {
    this.link.parentNode.className = this.cssClass;
    this.content.style.display = 'none';
  }

  this.link.Section = this;
  this.link.onclick = function() {
    this.blur();
    toggleSections(this.Section.name);
    return false;
  }
}

function toggleSections(section) {
  for (var i = 0, n = sections.length; i < n; i++) {
    if (section == sections[i].name) {
      sections[i].show();
    } else {
      sections[i].hide();
    }
  }
}


window.onload = function() {
  var section = (location.hash.substr(1)) ? location.hash.substr(1) : section_names[0];

  if (document.getElementById('menu')) {
    var links = document.getElementById('menu').getElementsByTagName('a');

    for (var i = 0, n = links.length; i < n; i++) {
      sections[i] = new Section(links[i]);
    }

    toggleSections(section);
  }
}
