var menu = document.getElementById('menu');
var anchors = menu.getElementsByTagName('a');
var locationHref = window.location.href.toString().replace( /.*\//, "/" );

for (i = anchors.length - 1; i >= 0; i--) {
  a = anchors[i];
  aHref = a.href.replace( /.*\//, "/" );

  // Does this menu item link to the current page?
  if (locationHref.indexOf(aHref) != -1 || (locationHref != '/' && aHref.indexOf(locationHref) != -1)) {
    a.parentNode.className = 'active';
    break;
  }
}
