/****************************************************************
 *                                                              *
 *  Based on CurvyCorners v1.2.9                                *
 *  Copyright (c) 2006 Cameron Cooke                            *
 *  By: Cameron Cooke and Tim Hutchison.                        *
 *                                                              *
 *  Website: http://www.curvycorners.net                        *
 *  Email:   info@totalinfinity.com                             *
 *  Forum:   http://www.curvycorners.net/forum/                 *
 *                                                              *
 *  Modified by: Dominique Archambault                          *
 *                                                              *
 *  This library is free software; you can redistribute         *
 *  it and/or modify it under the terms of the GNU              *
 *  Lesser General Public License as published by the           *
 *  Free Software Foundation; either version 2.1 of the         *
 *  License, or (at your option) any later version.             *
 *                                                              *
 *  This library is distributed in the hope that it will        *
 *  be useful, but WITHOUT ANY WARRANTY; without even the       *
 *  implied warranty of MERCHANTABILITY or FITNESS FOR A        *
 *  PARTICULAR PURPOSE. See the GNU Lesser General Public       *
 *  License for more details.                                   *
 *                                                              *
 *  You should have received a copy of the GNU Lesser           *
 *  General Public License along with this library;             *
 *  Inc., 59 Temple Place, Suite 330, Boston,                   *
 *  MA 02111-1307 USA                                           *
 *                                                              *
 ****************************************************************/


/****************************************************************
 *                        Base Functions                        *
 ****************************************************************/

var cCorners = new CurvyCorners();

function applyCornersToObject(obj,settings) {
   if (hasClass(obj,'inFloat')) {
      settings.doBottomMargin = false;
      settings.doTopMargin = false;
   }
   cCorners.register(obj,settings);
}

function RoundedTop(obj,rad) {
   var settings = {tl:{radius:rad},tr:{radius:rad},bl:{radius:0},br:{radius:0},doTopMargin:true,doBottomMargin:true,'cornerScriptUrl':cornerScriptUrl}
   applyCornersToObject(obj,settings);
}

function RoundedBottom(obj,rad) {
   var settings = {tl:{radius:0},tr:{radius:0},bl:{radius:rad},br:{radius:rad},doTopMargin:true,doBottomMargin:true,'cornerScriptUrl':cornerScriptUrl}
   applyCornersToObject(obj,settings);
}

function RoundedLeft(obj,rad) {
   var settings = {tl:{radius:rad},tr:{radius:0},bl:{radius:rad},br:{radius:0},doTopMargin:true,doBottomMargin:true,'cornerScriptUrl':cornerScriptUrl}
   applyCornersToObject(obj,settings);
}

function RoundedRight(obj,rad) {
   var settings = {tl:{radius:0},tr:{radius:rad},bl:{radius:0},br:{radius:rad},doTopMargin:true,doBottomMargin:true,'cornerScriptUrl':cornerScriptUrl}
   applyCornersToObject(obj,settings);
}

function RoundedBox(obj,rad, doTopMargin, doBottomMargin) {
   if (typeof(doTopMargin) == 'undefined') {
      doTopMargin = true;
   }
   if (typeof(doBottomMargin) == 'undefined') {
      doBottomMargin = true;
   }
   var settings = {tl:{radius:rad},tr:{radius:rad},bl:{radius:rad},br:{radius:rad},'doTopMargin':doTopMargin,'doBottomMargin':doBottomMargin,'cornerScriptUrl':cornerScriptUrl}
   applyCornersToObject(obj,settings);
}

function RoundedBoxRecursive(obj,outerRad) {
   // Apply corners to outer object
   var innerRad = (outerRad - 3);

   applyCornersToObject(obj,{tl:{radius:outerRad},tr:{radius:outerRad},bl:{radius:outerRad},br:{radius:outerRad},doTopMargin:true,doBottomMargin:true,'cornerScriptUrl':cornerScriptUrl});

   var i = 0;
   var padDiv = null;
   while (i < obj.childNodes.length && !padDiv) {
      if (obj.childNodes[i].tagName == 'DIV' && hasClass(obj.childNodes[i],'padDiv')) {
         padDiv = obj.childNodes[i];
      }
      i++;
   }

   if (padDiv) {
      for (var i = 0; i < padDiv.childNodes.length; i++) {
         var isRoundTop = hasClass(padDiv.childNodes[i],'roundTop');
         var isRoundBottom = hasClass(padDiv.childNodes[i],'roundBottom');
         var isRound = (hasClass(padDiv.childNodes[i],'round') || isRoundTop || isRoundBottom);
         if (padDiv.childNodes[i].tagName == 'DIV' && isRound) {
            var settingsInner = {tl:{radius:innerRad},tr:{radius:innerRad},bl:{radius:innerRad},br:{radius:innerRad},doTopMargin:false,doBottomMargin:false,'cornerScriptUrl':cornerScriptUrl};
            if (hasClass(padDiv.childNodes[i],'top')) {
               settingsInner.doBottomMargin = true;
            }
            if (hasClass(padDiv.childNodes[i],'bottom')) {
               settingsInner.doTopMargin = true;
            }
            if (isRoundTop) {
               settingsInner.bl.radius = 0;
               settingsInner.br.radius = 0;
            }
            if (isRoundBottom) {
               settingsInner.tl.radius = 0;
               settingsInner.tr.radius = 0;
            }
            applyCornersToObject(padDiv.childNodes[i],settingsInner);
         }
      }
   }
}


/****************************************************************
 *                         CurvyCorners                         *
 ****************************************************************/

function CurvyCorners() {
   this.startTS = new Date().getTime();
   this.aObjects = [];
}

CurvyCorners.prototype.register = function(obj,settings) {
   if (typeof(obj) != 'object' && typeof(obj) != 'string') {
      throw newCurvyError('first parameter of CurvyCorners().register must be an object, a class name or an id.');
   }
   if (typeof(settings) != 'object') {
      throw newCurvyError('Second parameter of CurvyCorners().register must be an object.');
   }
   if (settings.validTags) {
      var validElements = settings.validTags;
   } else {
      var validElements = ['div','li','form']; // Default
   }

   if (typeof(obj) == 'string') {
      if (obj.substr(0,1) == '.') {
         obj = getElementsByClassName(obj.substr(1));
      } else if (obj.substr(0,1) == '#') {
         obj = [document.getElementById(obj.substr(1))];
      } else {
         obj = [document.getElementById(obj)];
      }
   } else {
      obj = [obj];
   }
   var styles;
   for (var i=0; i<obj.length; i++) {
      if (inArray(validElements,obj[i].tagName.toLowerCase()) !== false) {
         this.aObjects.push({'obj':obj[i],'settings':settings,'styles':this._fetchStyles(obj[i])});
      }
   }
}

CurvyCorners.prototype._fetchStyles = function(obj) {
   var props = [
                  ['borderTopWidth', 'border-top-width'],
                  ['borderTopColor', 'border-top-color'],
                  ['borderTopStyle', 'border-top-style'],
                  ['borderBottomWidth', 'border-bottom-width'],
                  ['borderBottomColor', 'border-bottom-color'],
                  ['borderBottomStyle', 'border-bottom-style'],
                  ['borderLeftWidth', 'border-left-width'],
                  ['borderLeftColor', 'border-left-color'],
                  ['borderLeftStyle', 'border-left-style'],
                  ['borderRightWidth', 'border-right-width'],
                  ['borderRightColor', 'border-right-color'],
                  ['borderRightStyle', 'border-right-style'],
                  ['backgroundColor', 'background-color'],
                  ['backgroundImage', 'background-image'],
                  ['backgroundRepeat', 'background-repeat'],
                  ['paddingTop', 'padding-top'],
                  ['paddingBottom', 'padding-bottom'],
                  ['paddingLeft', 'padding-left'],
                  ['paddingRight', 'padding-right'],
                  ['marginTop', 'margin-top'],
                  ['marginBottom', 'margin-bottom'],
                  ['width', 'width'],
                  ['height', 'height'],
                  ['position', 'position'],
                  ['float', 'float'],
                  ['styleFloat', 'style-float']
               ];
   var styles = {};
   for (var i=0; i<props.length; i++) {
      styles[props[i][0]] = get_style(obj, props[i][0], props[i][1]);
   }
   return styles;
}

CurvyCorners.prototype.applyToRegistered = function() {
   for (var i=0; i<this.aObjects.length; i++) {
      new CurvyObject(this.aObjects[i].settings,this.aObjects[i].obj,this.aObjects[i].styles).doCorners();
   }
   //this.showTime();
}

CurvyCorners.prototype.apply = function(obj,settings) {
   if (typeof(obj) != 'object' && typeof(obj) != 'string') {
      throw newCurvyError('first parameter of CurvyCorners().register must be an object, a class name or an id.');
   }
   if (typeof(settings) != 'object') {
      throw newCurvyError('Second parameter of CurvyCorners().register must be an object.');
   }
   if (settings.validTags) {
      var validElements = settings.validTags;
   } else {
      var validElements = ['div','li','form']; // Default
   }

   if (typeof(obj) == 'string') {
      if (obj.substr(0,1) == '.') {
         obj = getElementsByClassName(obj.substr(1));
      } else if (obj.substr(0,1) == '#') {
         obj = [document.getElementById(obj.substr(1))];
      } else {
         obj = [document.getElementById(obj)];
      }
   } else {
      obj = [obj];
   }
   var styles;
   for (var i=0; i<obj.length; i++) {
      if (inArray(validElements,obj[i].tagName.toLowerCase()) !== false) {
         new CurvyObject(settings,obj[i],this._fetchStyles(obj[i])).doCorners();
      }
   }
}

CurvyCorners.prototype.showTime = function() {
   var totCurvyTime = (new Date().getTime() - this.startTS) / 1000;
   document.body.innerHTML += 'CurvyCorners execution time: ' + totCurvyTime + ' seconds<br />';
}


/****************************************************************
 *                         CurvyObject                          *
 ****************************************************************/

function CurvyObject(settings,obj,styles) {
   // Setup Globals
   this.box              = obj;
   this.settings         = settings;
   this.styles           = styles;
   this.cornerScriptUrl  = settings.cornerScriptUrl;

   this.topContainer     = null;
   this.bottomContainer  = null;
   this.contentDIV       = null;

   var sides = ['top','bottom','left','right'];
   var borders = {};
   var boxPadding = {};
   var sideName;
   // Get box formatting details
   for (var i=0; i<sides.length; i++) {
      sideName = sides[i].ucFirst();
      borders[sides[i]]          = {};
      borders[sides[i]].width    = convertToPX(this._getStyle('border'+sideName+'Width'),this.box);
      borders[sides[i]].color    = this._getStyle('border'+sideName+'Color');
      borders[sides[i]].style    = this._getStyle('border'+sideName+'Style');

      // if no border are defined, Opera sets a borderWidth of 3 and borderStyle of none, but will still display the border
      // Also IE will set a borderWidth of 'medium' by default if nothing is defined
      if (borders[sides[i]].style == 'none') {
         borders[sides[i]].width = '0';
      }

      borders[sides[i]].string = borders[sides[i]].width + 'px' + ' ' + borders[sides[i]].style + ' ' + borders[sides[i]].color;

      boxPadding[sides[i]] = convertToPX(this._getStyle('padding'+sideName),this.box);
   }

   var boxColor            = this._getStyle('backgroundColor');
   var backgroundImage     = this._getStyle('backgroundImage');
   var backgroundRepeat    = this._getStyle('backgroundRepeat');
   var boxPosition         = this._getStyle('position');

   // Set formatting propertes
   this.boxHeight          = this._computeHeight();
   this.boxWidth           = this._computeWidth();

   var corners = ['tl', 'tr', 'bl', 'br'];
   for (var i=0; i<corners.length; i++) {
      var cc = corners[i];
      if (typeof(this.settings[cc]) == 'undefined') {
         this.settings[cc] = {radius:0};
      }
   }

   if (this.settings.tl.radius || this.settings.tr.radius) {
      this.borderWidth     = borders.top.width;
      this.borderColor     = borders.top.color;
      this.borderStyle     = borders.top.style;
      this.borderString    = borders.top.string;
   } else {
      this.borderWidth     = borders.bottom.width;
      this.borderColor     = borders.bottom.color;
      this.borderStyle     = borders.bottom.style;
      this.borderString    = borders.bottom.string;
   }

   this.boxColor           = boxColor;
   this.boxPadding         = boxPadding;
   this.boxPosition        = boxPosition;
   this.backgroundImage    = ((backgroundImage != 'none')? backgroundImage : '');
   this.backgroundRepeat   = ((backgroundRepeat != 'none')? backgroundRepeat : '');
   this.boxContent         = this.box.innerHTML;

   this.boxColorOut        = this._colorOut(this.boxColor);
   this.borderColorOut     = this._colorOut(this.borderColor);

   this._topMaxRadius      = Math.max(this.settings.tl.radius, this.settings.tr.radius);
   this._botMaxRadius      = Math.max(this.settings.bl.radius, this.settings.br.radius);
   
   this._initBox();
}

CurvyObject.prototype.doCorners = function() {
   // Build containers
   if (this.settings.tl.radius || this.settings.tr.radius) {
      // Only build top bar if a top corner is to be draw
      this.topContainer = this._buildContainer('top');
      this.box.style.borderTopWidth = '0px';
   }

   if (this.settings.bl.radius || this.settings.br.radius) {
      // Only build bottom bar if a bottom corner is to be draw
      this.bottomContainer = this._buildContainer('bottom');
      this.box.style.borderBottomWidth = '0px';
   }

   // Build corners
   var corners = ['tl', 'tr', 'bl', 'br'];
   var corner;
   for (var j=0; j<corners.length; j++) {
      cc = corners[j];

      // Has the user requested the currentCorner be round?
      if (this.settings[cc].radius) {
         // Yes
         corner = this._buildRoundCorner(cc);

         // Position corner (absolute to container)
         switch (cc) {
            case 'tl':
               corner.style.left    = '0px';
               corner.style.top     = '0px';
               break;
            case 'tr':
               corner.style.right   = (isIE && verIE <= 6 && this.boxWidth.isOdd() ? '-1' : '0') + 'px';
               corner.style.top     = '0px';
               break;
            case 'bl':
               corner.style.left    = '0px';
               //corner.style.bottom  = (isIE && verIE <= 6 && !this.boxHeight.isOdd() ? '-1' : '0') + 'px';
               corner.style.bottom  = '0px';
               break;
            case 'br':
               corner.style.right   = (isIE && verIE <= 6 && this.boxWidth.isOdd() ? '-1' : '0') + 'px';
               //corner.style.bottom  = (isIE && verIE <= 6 && !this.boxHeight.isOdd() ? '-1' : '0') + 'px';
               corner.style.bottom  = '0px';
               break;
         }
      } else {
         corner = null;
      }

      if (cc == 'tl' || cc == 'tr') {
         if (this.topContainer && corner) {
            this.topContainer.appendChild(corner);
         }
      } else {
         if (this.bottomContainer && corner) {
            this.bottomContainer.appendChild(corner);
         }
      }
   }

   /*
   We only need to create a filler DIVs when two corners have
   different radiuses in either the top or bottom container.
   */

   // Find out which corner has the bigger radius and get the difference amount
   topRadiusDiff = (this.settings.tl.radius - this.settings.tr.radius)
   botRadiusDiff = (this.settings.bl.radius - this.settings.br.radius);

   if (topRadiusDiff != 0 && Math.abs(topRadiusDiff) != this.settings.tl.radius && Math.abs(topRadiusDiff) != this.settings.tr.radius) {
      // Create and attach top diff filler bar
      var topDiffFiller = this._buildDiffFiller('top', topRadiusDiff);
      this.topContainer.appendChild(topDiffFiller);
   }
   if (botRadiusDiff != 0 && Math.abs(botRadiusDiff) != this.settings.bl.radius && Math.abs(topRadiusDiff) != this.settings.br.radius) {
      // Create and attach bottom diff filler bar
      var botDiffFiller = this._buildDiffFiller('bottom', botRadiusDiff);
      this.bottomContainer.appendChild(botDiffFiller);
   }

   // Create and attach top filler bar
   if (this.topContainer) {
      var topFiller = this._buildFiller('top');
      this.topContainer.appendChild(topFiller);
   }

   // Create and attach bottom filler bar
   if (this.bottomContainer) {
      var botFiller = this._buildFiller('bottom');
      this.bottomContainer.appendChild(botFiller);
   }

   // Attach containers to DOM
   if (this.bottomContainer) {
      this.box.insertBefore(this.bottomContainer, this.box.firstChild);
   }
   if (this.topContainer) {
      this.box.insertBefore(this.topContainer, this.box.firstChild);
   }

   // Transfer padding from rounded box to inner box
   this._transferPadding();

   if (this.settings.doTopMargin) {
      this._transferTopMargin();
   }

   if (this.settings.doBottomMargin) {
      this._transferBottomMargin();
   }
}

CurvyObject.prototype._initBox = function() {
   // Make box relative if not already absolute and remove any padding
   if (this.boxPosition != 'absolute' && this.boxPosition != 'relative') {
      this.box.style.position = 'relative';
   }

   if (this.boxPadding.top || this.boxPadding.bottom || this.boxPadding.left || this.boxPadding.right) {
      this.box.style.padding = '0px';
   }

   // If IE make sure we have hasLayout so that we get positioning
   if (isIE && this._getStyle('width') == 'auto' && this._getStyle('height') == 'auto') {
      this.box.style.zoom = '1';
   }
}

CurvyObject.prototype._getStyle = function(styleProp) {
   if (typeof(this.styles[styleProp]) == 'undefined') {
      throw newCurvyError('attempting to read a style property not initialized ('+styleProp+')');
   }
   return this.styles[styleProp];
}

CurvyObject.prototype._computeWidth = function() {
   var boxWidth = this._getStyle('width'), res;
   if (isIE || boxWidth == 'auto' && boxWidth == '' && boxWidth.charAt(boxWidth.length - 1) == '%') {
      res = this.box.scrollWidth;
   } else {
      res = convertToPX(boxWidth, this.box);
   }
   return res;
}

CurvyObject.prototype._computeHeight = function() {
   var boxHeight = this._getStyle('height'), res;
   if (boxHeight == 'auto' || boxHeight == '' || boxHeight.charAt(boxHeight.length - 1) == '%') {
      res = this.box.scrollHeight;
   } else {
      res = convertToPX(boxHeight, this.box);
   }
   return res;
}

CurvyObject.prototype._buildContainer = function(type) {
   var container = document.createElement('DIV');
   container.style.fontSize = '1px';
   container.style.overflow = 'hidden';
   container.style.position = 'absolute';
   container.style.width = '100%';
   container.style.paddingLeft = this.borderWidth + 'px';
   container.style.paddingRight = this.borderWidth + 'px';
   container.style.left = (0 - this.borderWidth) + 'px';

   switch (type) {
      case 'top':
         container.className = 'rcContainerTop';
         container.style.top = (0 - this._topMaxRadius) + 'px';
         container.style.height = this._topMaxRadius + 'px';
         break;
      case 'bottom':
         container.className = 'rcContainerBottom';
         container.style.bottom = (0 - this._botMaxRadius) + 'px';
         container.style.height = this._botMaxRadius + 'px';
         break;
   }

   return container;
}

CurvyObject.prototype._colorOut = function(color) {
   if (color != 'transparent') {
      color = format_color(color).toLowerCase();
      if (color.charAt(0) == '#') {
         color = color.substr(1);
      }
   }
   return color;
}

CurvyObject.prototype._buildRoundCorner = function(cc) {
   var radius = this.settings[cc].radius;

   var corner = document.createElement('DIV');
   corner.className        = 'rcCorner'+cc.toUpperCase();
   corner.style.fontSize   = '1px';
   corner.style.overflow   = 'hidden';
   corner.style.position   = 'absolute';
   corner.style.width      = radius + 'px';
   corner.style.height     = radius + 'px';
   corner.style.backgroundImage = 'url("'+ this.cornerScriptUrl + '?pos=' + cc + '&radius=' + radius + '&bgColor=transparent&fgColor=' + this.boxColorOut + '&boWidth=' + this.borderWidth + '&boColor=' + this.borderColorOut +'")';

   return corner;
}

CurvyObject.prototype._buildFiller = function(type) {
   // Create the bar to fill the gap between each corner horizontally
   var newFillerBar = document.createElement('DIV');
   newFillerBar.className              = 'rcHorizontalFiller';
   newFillerBar.style.position         = 'relative';
   newFillerBar.style.fontSize         = '1px';
   newFillerBar.style.overflow         = 'hidden';
   newFillerBar.style.backgroundColor  = this.boxColor;
   newFillerBar.style.backgroundImage  = this.backgroundImage;
   newFillerBar.style.backgroundRepeat = this.backgroundRepeat;

   switch (type) {
      case 'top':
         if (this.topContainer) {
            if (this.settings.tl.radius || this.settings.tr.radius) {
               newFillerBar.style.height      = (this._topMaxRadius - this.borderWidth) + 'px';
               newFillerBar.style.marginLeft  = (this.settings.tl.radius - this.borderWidth) + 'px';
               newFillerBar.style.marginRight = (this.settings.tr.radius - this.borderWidth) + 'px';
               newFillerBar.style.borderTop   = this.borderString;

               if (this.backgroundImage != '') {
                  newFillerBar.style.backgroundPosition  = (0 - this._topMaxRadius + this.borderWidth) + 'px 0px';
               }
            }
         }
         break;

      case 'bottom':
         if(this.bottomContainer) {
            if (this.settings.bl.radius || this.settings.br.radius) {
               newFillerBar.style.height       = (this._botMaxRadius - this.borderWidth) + 'px';
               newFillerBar.style.marginLeft   = (this.settings.bl.radius - this.borderWidth) + 'px';
               newFillerBar.style.marginRight  = (this.settings.br.radius - this.borderWidth) + 'px';
               newFillerBar.style.borderBottom = this.borderString;

               if (this.backgroundImage != '') {
                  newFillerBar.style.backgroundPosition  = (0 - this._botMaxRadius + this.borderWidth) + 'px ' + (0 - this.boxHeight + this._topMaxRadius + this.borderWidth) + 'px';
               }
            }
         }
         break;
   }

   return newFillerBar;
}

CurvyObject.prototype._buildDiffFiller = function(type, radiusDiff) {
   // Get the type of corner that is the smaller one
   var smallerCornerType = (radiusDiff < 0 ? type.charAt(0) + 'l' : type.charAt(0) + 'r');

   // First we need to create a DIV for the space under the smaller corner
   var newFiller = document.createElement('DIV');
   newFiller.className        = 'rcDiffFiller';
   newFiller.style.height     = Math.abs(radiusDiff) + 'px';
   newFiller.style.width      = this.settings[smallerCornerType].radius + 'px';
   newFiller.style.position   = 'absolute';
   newFiller.style.fontSize   = '1px';
   newFiller.style.overflow   = 'hidden';
   newFiller.style.backgroundColor = this.boxColor;

   // Position filler
   switch (smallerCornerType) {
      case 'tl':
         newFiller.style.bottom        = '0px';
         newFiller.style.left          = '0px';
         newFiller.style.borderLeft    = this.borderString;
         break;

      case 'tr':
         newFiller.style.bottom        = '0px';
         newFiller.style.right         = '0px';
         newFiller.style.borderRight   = this.borderString;
         break;

      case 'bl':
         newFiller.style.top           = '0px';
         newFiller.style.left          = '0px';
         newFiller.style.borderLeft    = this.borderString;
         break;

      case 'br':
         newFiller.style.top           = '0px';
         newFiller.style.right         = '0px';
         newFiller.style.borderRight   = this.borderString;
         break;
   }

   return newFiller;
}

CurvyObject.prototype._buildStraightCorner = function(cc) {
   var corner;

   if (((cc == 'tr' || cc == 'tl') && this.topContainer) || ((cc == 'br' || cc == 'bl') && this.bottomContainer)) {
      // We need to create a filler div to fill the space upto the next horizontal corner.
      corner = document.createElement('DIV');
      corner.className      = 'rcNoCorner' + cc.toUpperCase();
      corner.style.position = 'relative';
      corner.style.fontSize = '1px';
      corner.style.overflow = 'hidden';

      if (this.backgroundImage == '' || this.backgroundImage == 'none') {
         corner.style.backgroundColor = this.boxColor;
      } else {
         corner.style.backgroundImage = this.backgroundImage;
         corner.style.backgroundRepeat = this.backgroundRepeat;
      }

      switch (cc) {
         case 'tl':
            corner.style.height      = (this._topMaxRadius - this.borderWidth) + 'px';
            corner.style.marginRight = (this.settings.tr.radius - (this.borderWidth * 2)) + 'px';
            corner.style.borderLeft  = this.borderString;
            corner.style.borderTop   = this.borderString;
            corner.style.left        = (0 - this.borderWidth) + 'px';
            break;

         case 'tr':
            corner.style.height      = (this._topMaxRadius - this.borderWidth) + 'px';
            corner.style.marginRight = (this.settings.tl.radius - (this.borderWidth * 2)) + 'px';
            corner.style.borderRight = this.borderString;
            corner.style.borderTop   = this.borderString;
            corner.style.left        = this.borderWidth + 'px';
            corner.style.backgroundPosition = (0 - this._topMaxRadius + this.borderWidth) + 'px 0px';
            break;

         case 'bl':
            corner.style.height       = (this._botMaxRadius - this.borderWidth) + 'px';
            corner.style.marginRight  = (this.settings.br.radius - (this.borderWidth * 2)) + 'px';
            corner.style.borderLeft   = this.borderString;
            corner.style.borderBottom = this.borderString;
            corner.style.left         = (0 - this.borderWidth) + 'px';
            corner.style.backgroundPosition = (0 - this.borderWidth) + 'px ' + (0 - this.boxHeight + this._botMaxRadius + this.borderWidth) + 'px';
            break;

         case 'br':
            corner.style.height       = (this._botMaxRadius - this.borderWidth) + 'px';
            corner.style.marginLeft   = (this.settings.bl.radius - (this.borderWidth * 2)) + 'px';
            corner.style.borderRight  = this.borderString;
            corner.style.borderBottom = this.borderString;
            corner.style.left         = this.borderWidth + 'px';
            corner.style.backgroundPosition = (0 - this._botMaxRadius + this.borderWidth) + 'px ' + (0 - this.boxHeight + this._botMaxRadius + this.borderWidth) + 'px';
            break;
      }
   }

   return corner;
}

CurvyObject.prototype._transferPadding = function() {
   // Transfer padding from rounded box to inner box
   if (this.boxPadding.top || this.boxPadding.bottom || this.boxPadding.left || this.boxPadding.right) {
      if(this.settings.autoPad) {
         // Remove content
         this.box.innerHTML = '';

         // Create content container
         var contentContainer = document.createElement('DIV');
         contentContainer.className = 'padDiv';
      } else {
         var i = 0;
         var contentContainer = null;
         while (i < this.box.childNodes.length && !contentContainer) {
            if (this.box.childNodes[i].className == 'padDiv') {
               contentContainer = this.box.childNodes[i];
            }
            i++;
         }
      }

      if (contentContainer) {
         // Set contentContainer's properties
         contentContainer.style.position = 'relative';
         contentContainer.style.backgroundColor = this.boxColor;

         if (this.settings.autoPad) {
             contentContainer.innerHTML = this.boxContent;
         }

         // Get padding amounts
         var topPadding = Math.abs(this._topMaxRadius - this.boxPadding.top);
         var botPadding = Math.abs(this._botMaxRadius - this.boxPadding.bottom);

         // Apply top padding
         if (this._topMaxRadius < this.boxPadding.top) {
            contentContainer.style.paddingTop = topPadding + 'px';
         }

         // Apply Bottom padding
         if (this._botMaxRadius < this.boxPadding.bottom)
            contentContainer.style.paddingBottom = this._botMaxRadius + 'px';

         // Apply left and right padding
         contentContainer.style.paddingLeft = this.boxPadding.left + 'px';
         contentContainer.style.paddingRight = this.boxPadding.right + 'px';

         if (this.settings.autoPad) {
            // Append contentContainer
            this.contentDIV = this.box.appendChild(contentContainer);
         }
      }
   }
}

CurvyObject.prototype._transferBottomMargin = function() {
   if (this._botMaxRadius) {
      if (!isSafari) {
      // Add a magic div under the rounded div to create a "enforceable" bottom margin
         var boxMarginBottom = this._getStyle('marginBottom');
         boxMarginBottom = (boxMarginBottom != 'auto' && boxMarginBottom != '' ? parseInt(convertToPX(boxMarginBottom,this.box)) : 0);

         var boxFloat = (isIE ? this._getStyle('styleFloat') : this._getStyle('float'));

         if (boxFloat != 'left' && boxFloat != 'right') {
            var newBotMagic = document.createElement('DIV');
            newBotMagic.className      = 'rcBottomMargin';
            newBotMagic.style.height   = (isIE ? (this._botMaxRadius - 1) : this._botMaxRadius) + 'px';
            newBotMagic.style.width    = this.boxWidth + 'px';
            newBotMagic.style.fontSize = '1px';
            newBotMagic.style.overflow = 'hidden';
            if (isOpera) {
               newBotMagic.style.marginBottom = (boxMarginBottom + this._botMaxRadius) + 'px';
            }
            insertAfter(this.box.parentNode, newBotMagic, this.box)
         } else {
            //this.box.style.marginBottom = (boxMarginBottom + (this._botMaxRadius * 2)) + 'px';
            this.box.style.marginBottom = (boxMarginBottom + this._botMaxRadius) + 'px';
         }
      } else {
         //TODO: find a way to transfer bottom margin in Safari
      }
   }
}

CurvyObject.prototype._transferTopMargin = function() {
   // Add appropriate top margin
   if (this._topMaxRadius) {
      var boxMarginTop = this._getStyle('marginTop');
      boxMarginTop = (boxMarginTop != 'auto' && boxMarginTop != '' ? parseInt(convertToPX(boxMarginTop,this.box)) : 0);

      this.box.style.marginTop = (boxMarginTop + this._topMaxRadius) + 'px';
   }
}

// Displays error message
function newCurvyError(errorMessage)
{
    return new Error("CurvyCorners Error:\n" + errorMessage)
}

