/* document.getElementsBySelector(selector)
   - returns an array of element objects from the current document
     matching the CSS selector. Selectors can contain element names,
     class names and ids and can be nested. For example:

       elements = document.getElementsBySelect('div#main p a.external')

     Will return an array of all 'a' elements with 'external' in their
     class attribute that are contained inside 'p' elements that are
     contained inside the 'div' element which has id="main"

   New in version 0.4: Support for CSS2 and CSS3 attribute selectors:
   See http://www.w3.org/TR/css3-selectors/#attribute-selectors

   Version 0.4 - Simon Willison, March 25th 2003
   -- Works in Phoenix 0.5, Mozilla 1.3, Opera 7, Internet Explorer 6, Internet Explorer 5 on Windows
   -- Opera 7 fails
*/

function getAllChildren(e) {
  // Returns all children of element. Workaround required for IE5/Windows. Ugh.
  return e.all ? e.all : e.getElementsByTagName('*');
}

document.getElementsBySelector = function(selector) {
  var attrName;
  var attrOperator;
  var attrValue;
  var bits;
  var checkFunction;	// This function will be used to filter the elements
  var currentContext;
  var currentContextIndex;
  var element;
  var elements;
  var found;
  var foundCount;
  var h;
  var i;
  var id;
  var j;
  var k;
  var tagName;
  var tokens;

  // Attempt to fail gracefully in lesser browsers
  if ( !document.getElementsByTagName ) {
    return [];
  }

  // Split selector in to tokens
  tokens = selector.split(' ');
  currentContext = [document];

  for ( i = 0; i < tokens.length; i++ ) {
    token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');

    if ( token.indexOf('#') > -1 ) {
      // Token is an ID selector
      bits = token.split('#');
      tagName = bits[0];
      id = bits[1];
      element = document.getElementById(id);

      if ( !element || tagName && element.nodeName.toLowerCase() != tagName) {
        // tag with that ID not found, return false
        return [];
      }

      // Set currentContext to contain just this element
      currentContext = [element];
      continue; // Skip to next token
    }

    if ( token.indexOf('.') > -1 ) {
      // Token contains a class selector
      bits = token.split('.');
      tagName = bits[0];
      className = bits[1];

      if ( !tagName ) {
        tagName = '*';
      }

      // Get elements matching tag, filter them for class selector
      found = [];
      foundCount = 0;

      for ( h = 0; h < currentContext.length; h++ ) {

        if ( tagName == '*' ) {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }

        for ( j = 0; j < elements.length; j++ ) {
          found[foundCount++] = elements[j];
        }
      }

      currentContext = [];
      currentContextIndex = 0;

      for ( k = 0; k < found.length; k++ ) {

        if ( found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b')) ) {
          currentContext[currentContextIndex++] = found[k];
        }
      }

      continue; // Skip to next token
    }

    // Code to deal with attribute selectors
    if ( token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/) ) {
      tagName = RegExp.$1;
      attrName = RegExp.$2;
      attrOperator = RegExp.$3;
      attrValue = RegExp.$4;

      if (!tagName) {
        tagName = '*';
      }

      // Grab all of the tagName elements within current context
      found = [];
      foundCount = 0;

      for ( h = 0; h < currentContext.length; h++ ) {

        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for ( j = 0; j < elements.length; j++ ) {
          found[foundCount++] = elements[j];
        }
      }

      currentContext = [];
      currentContextIndex = 0;

      switch ( attrOperator ) {
        case '=': // Equality
          checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); };
          break;

        case '~': // Match one of space seperated words
          checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };
          break;

        case '|': // Match start with value followed by optional hyphen
          checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };
          break;

        case '^': // Match starts with value
          checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) === 0); };
          break;

        case '$': // Match ends with value - fails with "Warning" in Opera 7
          checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };
          break;

        case '*': // Match ends with value
          checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); };
          break;

        default :
          // Just test for existence of attribute
          checkFunction = function(e) { return e.getAttribute(attrName); };
      }

      currentContext = [];
      currentContextIndex = 0;

      for ( k = 0; k < found.length; k++ ) {

        if ( checkFunction(found[k]) ) {
          currentContext[currentContextIndex++] = found[k];
        }
      }

      // alert('Attribute Selector: '+tagName+' '+attrName+' '+attrOperator+' '+attrValue);
      continue; // Skip to next token
    }

    // If we get here, token is JUST an element (not a class or ID selector)
    tagName = token;
    found = [];
    foundCount = 0;

    for ( h = 0; h < currentContext.length; h++ ) {
      elements = currentContext[h].getElementsByTagName(tagName);

      for ( j = 0; j < elements.length; j++ ) {
        found[foundCount++] = elements[j];
      }
    }

    currentContext = found;
  }

  return currentContext;
};

/* That revolting regular expression explained
/^(\w+)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/
  \---/  \---/\-------------/    \-------/
    |      |         |               |
    |      |         |           The value
    |      |    ~,|,^,$,* or =
    |   Attribute
   Tag
*/
