
function $(selector, rootnode) {
  var tagname;
  var attr = {};

  // match [tagname]#idname
  // match [tagname].classname
  // match tagname

  var by_id = /^(\w*)#(\w+)$/;
  var by_class = /^(\w*)\.(\w+)$/;

  if (by_id.test(selector)) {
    var matches = selector.match(by_id);
    tagname = matches[1];
    attr['id'] = matches[2];
  }
  else if (by_class.test(selector)) {
    var matches = selector.match(by_class);
    tagname = matches[1];
    attr['className'] = matches[2];
  }
  else {
    tagname = selector;
  }

  // by default, start search at <html> element
  // by default, match all tagnames

  if (rootnode == null)
    rootnode = document.documentElement;

  if (tagname == '' || tagname == null)
    tagname = '*';

  // search for matching elements

  var nodes = rootnode.getElementsByTagName(tagname);
  var matches = [];

  for (var i=0; i<nodes.length; i++) {
    var node = nodes[i];
    var ismatch = true;

    for (var k in attr) {
      if (node[k] != attr[k]) {
        ismatch = false;
        break;
      }
    }

    if (ismatch)
      matches.push(node);
  }

  return matches;
}

