var currTab = '';

window.addEvent('domready', function() {
  if (window.location.hash) {
    chooseTab(window.location.hash.substr(1));
  } else {
    var firstTab = $('tabs').getElement('a.first');
    chooseTab(firstTab.get('href').match(/#(.+)$/)[1]);
  }
  $('tabs').getElements('a').each(function(tab) {
    tab.addEvent('click', function() {
      chooseTab(tab.get('href').match(/#(.+)$/)[1]);
    });
  });
  (function() {
    if (window.location.hash && currTab != window.location.hash.substr(1)) {
      chooseTab(window.location.hash.substr(1));
    }
  }).periodical(500);
});

function chooseTab(id) {
  currTab = id;
  var selected = $('tabs').getElement('.selected');
  if (selected) {
    selected.removeClass('selected');
  }
  $('tab' + id).addClass('selected');;
  $(document.body).set('class', 'section' + id);
  $(document.body).scrollTo(0, 0);
}

