// make proper email addresses
(function() {
  var a = ['r'].reverse().join('') + '@' + window.location.hostname, b = document.createElement('a'), c = document.getElementById('e');
  if (c) {
    b.appendChild(document.createTextNode(a));
    b.href = ['o','t','l','i','a','m'].reverse().join('') + ':' + a;
    c.replaceChild(b, c.firstChild);
  }
})();


// Create navigation from H2 elements on page
(function() {
  var h2 = document.getElementsByTagName('h2'),
      ul = document.createElement('ul'),
      hl = document.getElementById('homelink'),
      h3 = hl.appendChild(document.createElement('h3')),
      i = 0, j = h2.length, li, a, txt, s, hrefbits;
  for (; i < j; i++) {
    li = ul.appendChild(document.createElement('li'));
    a = li.appendChild(document.createElement('a'));
    txt = h2[i].firstChild.nodeValue;
    a.appendChild(document.createTextNode(txt));
    txt = txt.split(' ')[0];
    a.href = '#' + txt.toLowerCase();
    h2[i].id = txt.toLowerCase();
  }
  ul.className = 'homelink';
  h3.appendChild(document.createTextNode('On this page:'));
  hl.appendChild(ul);
  
  // Flattr stuff
  s = document.createElement('script');
  s.type = 'text/javascript';
  s.async = true;
  s.src = 'http://api.flattr.com/js/0.6/load.js?mode=auto';
  a = document.createElement('a');
  a.className = 'FlattrButton';
  a.style.display = 'none';
  a.style.outline = '1px solid red';
  a.rev = 'flattr;button:compact;';
  hrefbits = location.href.split('/');
  a.href = 'http://ratherodd.com/' + hrefbits[hrefbits.length-2] + '/';
  h3 = h3.cloneNode(true);
  h3.firstChild.nodeValue = 'If you wish to donate:';
  hl.appendChild(h3);
  hl.appendChild(a);
  hl.appendChild(s);
  h3 = document.createElement('p');
  h3.appendChild(document.createTextNode('or PayPal (contact email)'));
  hl.appendChild(h3);
})();

