// http://blog.yosle.com/2007/09/20/css-round-corners/, generated entirely in javascript

function RoundBorder(selector, options) {
  $$(selector).each(function(element) {
    new RoundBorder.Builder(element, options);
  });
}

RoundBorder.Builder = Class.create({
  initialize: function(element, options) {
    options = options || {};
    
    var backgroundColor, borderColor;
    
    // todo: move to element storage?
    if (options.backgroundColor) {
      backgroundColor = options.backgroundColor;
    } else if (backgroundColor = element.readAttribute('round-border-background-color')) {
      // nothing
    } else {
      backgroundColor = element.getStyle('background-color') || '#fff';
      element.writeAttribute('round-border-background-color', backgroundColor);
    }
    
    if (!options.borderless) {
      if (options.borderColor) {
        borderColor = options.borderColor;
      } else if (borderColor = element.readAttribute('round-border-border-color')) {
        // nothing
      } else {
        borderColor = element.getStyle('border-left-color') || '#000';
        element.writeAttribute('round-border-border-color', borderColor);
      }
    }
    
    if (element.tagName.toLowerCase() == 'li') {
      options.extraWrap = true;
    }
    
    this.element = element;
    this.options = options;
    this.backgroundColor = backgroundColor;
    if (!options.borderless) {
      this.borderColor = borderColor;
    }
    
    this.setup();
  },
  
  setup: function() {
    var commonStyle = {fontSize: '1px', overflow: 'hidden', display: 'block'};
    var b1, b2, b3, b4;
    
    var insertElement = this.element;
    if (this.options.extraWrap) {
      var wrapper = new Element('div');
      $A(this.element.childNodes).each(function(node) {
        insertElement.removeChild(node);
        wrapper.appendChild(node);
      });
      this.element.appendChild(wrapper);
      insertElement = wrapper;
      this.element.setStyle({borderLeft: 'none', background: 'none'});
    }
    
    if (this.options.only != 'bottom') {
      if (!this.options.borderless) {
        b1 = new Element('b');
        b1.setStyle(commonStyle);
        b1.setStyle({height: '1px', background: this.borderColor, margin: '0 5px'});
        insertElement.insert({before: b1});
      }
      
      b2 = new Element('b');
      b2.setStyle(commonStyle);
      b2.setStyle(this.computeStyle(1, 2, 3));
      insertElement.insert({before: b2});
      
      b3 = new Element('b');
      b3.setStyle(commonStyle);
      b3.setStyle(this.computeStyle(1, 1, 2));
      insertElement.insert({before: b3});
      
      b4 = new Element('b');
      b4.setStyle(commonStyle);
      b4.setStyle(this.computeStyle(2, 1, 1));
      insertElement.insert({before: b4});
    }
    
    var contentWrapper = new Element('div');
    contentWrapper.setStyle({background: this.backgroundColor});
    if (!this.options.borderless) {
      contentWrapper.setStyle({borderRight: '1px solid '+this.borderColor, borderLeft: '1px solid '+this.borderColor, padding: '0px'});
    }
    insertElement.insert({before: contentWrapper});
    insertElement.parentNode.removeChild(insertElement);
    contentWrapper.appendChild(insertElement);
    
    if (this.options.only != 'top') {
      b4 = new Element('b');
      b4.setStyle(commonStyle);
      b4.setStyle(this.computeStyle(2, 1, 1));
      contentWrapper.insert({after: b4});
      
      b3 = new Element('b');
      b3.setStyle(commonStyle);
      b3.setStyle(this.computeStyle(1, 1, 2));
      b4.insert({after: b3});
      
      b2 = new Element('b');
      b2.setStyle(commonStyle);
      b2.setStyle(this.computeStyle(1, 2, 3));
      b3.insert({after: b2});
      
      if (!this.options.borderless) {
        b1 = new Element('b');
        b1.setStyle(commonStyle);
        b1.setStyle({height: '1px', background: this.borderColor, margin: '0 5px'});
        b2.insert({after: b1});
      }
    }
  },
  
  computeStyle: function(height, borderWidth, marginWidth) {
    var style = {height: height+'px', background: this.backgroundColor};
    if (this.borderless) {
      marginWidth += borderWidth;
    } else {
      style.borderLeft = borderWidth+'px solid '+this.borderColor;
      style.borderRight = borderWidth+'px solid '+this.borderColor;
    }
    style.margin = '0 '+marginWidth+'px';
    return style;
  }
});
