// border radius
eCSStender.register(
  { 'fragment': 'radius',
    'test':     function()
    {
      return ( ! eCSStender.isSupported( 'property', 'border-top-left-radius: 3px' ) &&
               ( eCSStender.isSupported( 'property', '-moz-border-radius-topleft: 3px' ) ||
                 eCSStender.isSupported( 'property', '-webkit-border-bottom-left-radius: 3px' ) ||
                 eCSStender.isSupported( 'property', '-khtml-border-bottom-left-radius: 3px' ) ) );
    }
  },
  false,
  function( selector, properties, medium ){
    var style_block = selector + ' { ';
    
    // mozilla currently messes up the individual assignment, so we need to manage that
    var transpose = false;
    if ( eCSStender.isSupported( 'property', '-moz-border-radius-topleft: 3px' ) )
    {
      transpose = true;
    }
    
    // shorthand
    if ( typeof( properties['border-radius'] ) != 'undefined' )
    {
      // handle radii or just plain old corners
      var
      radii = properties['border-radius'].split('/'),
      corners = [];
      if ( radii.length > 1 )
      {
        var horiz = eCSStender.trim( radii[0] ).split(' ');
        var vert  = eCSStender.trim( radii[1] ).split(' ');
        // handle mirroring
        if ( horiz.length < 4 )
        {
          if ( typeof( horiz[1] ) == 'undefined' ){ horiz[1] = horiz[0]; }
          if ( typeof( horiz[2] ) == 'undefined' ){ horiz[2] = horiz[0]; }
          if ( typeof( horiz[3] ) == 'undefined' ){ horiz[3] = horiz[1]; }
        }
        if ( vert.length < 4 )
        {
          if ( typeof( vert[1] ) == 'undefined' ){ vert[1] = vert[0]; }
          if ( typeof( vert[2] ) == 'undefined' ){ vert[2] = vert[0]; }
          if ( typeof( vert[3] ) == 'undefined' ){ vert[3] = vert[1]; }
        }
        // some browsers can't handle compund radii yet
        var compound = true;
        if ( ! eCSStender.isSupported( 'property', '-webkit-border-bottom-left-radius: 3px 3px' ) &&
             ! eCSStender.isSupported( 'property', '-moz-border-radius-bottomleft: 3px 3px' ) &&
             ! eCSStender.isSupported( 'property', '-khtml-border-bottom-left-radius: 3px 3px' ) )
        {
          compound = false;
        }
        for ( var i=0; i<4; i++ )
        {
          corners[i] = compound ? horiz[i] + ' ' + vert[i] : horiz[i];
        }
      }
      else
      {
        corners = properties['border-radius'].split(' ');
      }
      if ( corners.length > 1 )
      {
        // webkit/konquerer is a little funky with multiple-assignment
        if ( eCSStender.isSupported( 'property', '-webkit-border-bottom-left-radius: 3px' ) ||
             eCSStender.isSupported( 'property', '-khtml-border-bottom-left-radius: 3px' ) )
        {
          style_block += '-webkit-border-top-left-radius: ' + corners[0] + '; ' +
                         '-webkit-border-top-right-radius: ' + corners[1] + '; ' +
                         '-webkit-border-bottom-right-radius: ' + corners[2] + '; ' +
                         '-webkit-border-bottom-left-radius: ' + corners[3] + '; ' +
                         '-khtml-border-top-left-radius: ' + corners[0] + '; ' +
                         '-khtml-border-top-right-radius: ' + corners[1] + '; ' +
                         '-khtml-border-bottom-right-radius: ' + corners[2] + '; ' +
                         '-khtml-border-bottom-left-radius: ' + corners[3] + '; ';
        }
        else
        {
          style_block += '-moz-border-radius-topleft: ' + corners[0] + '; ' +
                         '-moz-border-radius-topright: ' + corners[1] + '; ' +
                         '-moz-border-radius-bottomright: ' + corners[2] + '; ' +
                         '-moz-border-radius-bottomleft: ' + corners[3] + '; ';
        }
      }
      else
      {
        style_block += '-moz-border-radius: ' + properties['border-radius'] + '; ' +
                       '-webkit-border-radius: ' + properties['border-radius'] + '; ' +
                       '-khtml-border-radius: ' + properties['border-radius'] + '; ';
      }
      properties['border-radius'] = null;
    }
    
    for ( var prop in properties )
    {
      if ( eCSStender.isInheritedProperty( properties, prop ) ) { continue; };
      if ( transpose )
      {
        style_block += prop.replace( /border-(top|bottom)-(left|right)-radius/, '-moz-border-radius-$1$2' ) +
                       ': ' + properties[prop] + '; ';
      }
      else
      {
        style_block += prop + ': ' + properties[prop] + '; ';
      }
    }
    style_block += '} ';
    eCSStender.embedCSS( style_block, medium );
  }
);
// nth child
eCSStender.register(
  { 'selector': /:nth-child\(\s*(?:even|odd|[+-]?\d*?|[+-]?\d*?n(?:\s*[+-]\s*\d*?)?)\s*\)/,
    'test':     function(){
      // the markup
      var
      div = document.createElement('div'),
      p   = document.createElement('p');
      div.appendChild( p );
      // the test
      return ( ! eCSStender.isSupported( 'selector', 'div p:nth-child( odd )', div, p ) );
    }
  },
  '*',
  function( selector, properties, medium, specificity ){
    selector = selector.replace( /(child\()\s*/g, '$1' ).replace( /\s*(\+)\s*/g, '$1' ).replace( /\s*(\))/g, '$1' );
    // secondary test to see if the browser just doesn't like spaces in the parentheses
    // the markup
    var
    style_block = selector + ' {',
    prop, els, i,
    div   = document.createElement('div'),
    p     = document.createElement('p');
    div.appendChild( p );
    // embedding is the way to go
    if ( ( eCSStender.isSupported( 'selector', 'p:nth-child(odd)', div, p ) &&
           ! eCSStender.isSupported( 'selector', 'p:nth-child(2n+1)', div, p ) &&
           selector.match( /:nth-child\(\s*(?:even|odd)\s*\)/ ) != null ) ||
         eCSStender.isSupported( 'selector', 'p:nth-child(2n+1)', div, p ) )
    {
      for ( prop in properties )
      {
        if ( eCSStender.isInheritedProperty( properties, prop ) ) { continue; };
        style_block += prop + ': ' + properties[prop] + '; ';
      }
      style_block += '} ';
      eCSStender.embedCSS( style_block, medium );
    }
    // no nth-child support natively, so inline is only option
    else
    {
      els = $( selector ).each(function(){
        eCSStender.applyWeightedStyle( this, properties, specificity );
      });
    }
  }
);

