(function() {
  if( document.querySelectorAll )
  {
    var hashtag = window.location.hash.substr(1);
    var blockelements = document.querySelectorAll('article p, article div, article h1, article h2, article h3, article h4, article h5, article h6, article ul, article dl, article blockquote, article table');
    
    for(var i = 0; i < blockelements.length; i++)
    {
      var el = blockelements[i];
      
      if( el.id && !document.querySelector('li #'+el.id+', .footnotes #'+el.id+', #'+el.id+'.footnotes') )
      {
        var a = document.createElement('a');
        a.className = hashtag == el.id ? 'hashlink curhashlink' : 'hashlink';
        a.href = '#' + el.id;
        if( el.firstChild )
        {
          el.insertBefore(a,el.firstChild);
        }
        else
        {
          el.appendChild(a);
        }
      }
    }
  }
})();
