(function( $ ){

  $.fn.roundBoxes = function(options) {
  
    var settings = {
      'radius' : 10,
      'width' : 0,
      'barColour' : '#ffa255',
      'borderColour' : '#ffa255',
      'footerHeight' : 20,
      'headerHeight' : 20,
      'footer' : true,
      'header' : true,
      'borderwidth' : 1,
      'radiusOffset' : 3,
      'padding' : '10px',
      'margin' : '10px',
      'colour' : 'no',
      'mainBackColour' : '#FFFFFF'
      }
 
 // prevent the plugin from creating a box when the style sheet does not display the box
 if ($(this).css('display') == 'none')
  return;
 
  count = 0;   
  return this.each(function(){

      if ( options ) { 
        $.extend( settings, options );
      }
      
      count++;
      
      var $this = $(this);
      var mainPadding = settings.padding;
      $this.css({'padding':mainPadding});
      
      var radius = settings.radius;
      var setRadius = radius ;
      var setRadiusB = radius - settings.radiusOffset;
  
      // common variables for header and footer
      var marginLeft    = '-' + $this.css('padding-left');
      var marginRight   = '-' + $this.css('padding-right');
      
      if (settings.width == 0)
        var divWidth = $this.css('width');
      else  
        var divWidth = settings.width;
       
      var divBorder     = settings.borderwidth + "px";
      
      // set the colour scheme of the header and footer
      if (settings.colour != 'no')
        {
          var footerColour  = settings.colour;
          var borderColour  = settings.colour;
        }
      else
        {
          var footerColour  = settings.barColour;
          var borderColour  = settings.borderColour;
        }  

      // set the margins
/*      if (settings.margin == 'no')
        var mainMargin = 'inherit';
      else if (settings.margin != '')  
*/        var mainMargin = settings.margin;
 
      var mainBackColour = settings.mainBackColour;  
      
      // setting the bottom variables
      var footerHeight        = settings.footerHeight;
      var marginFooterBottom  = '-' + $this.css('padding-bottom');
      var marginFooterTop     = $this.css('padding-bottom');
      var footerDivName       = $this.attr("class") + 'botDiv' + count;
      var footerDiv           = '.' + footerDivName;

      // setting the header variables
      var headerHeight      = settings.headerHeight;
      var marginHeadBottom  = $this.css('padding-top');
      var marginHeadTop     = '-' + $this.css('padding-top');
      var headerDivName     = $this.attr("class") + 'topDiv' + count;
      var headerDiv         = '.' + headerDivName;


      // setting up the two additional divs at the top and the bottom
      var appendDiv = "<div class='" + footerDivName + "' style='height:" + footerHeight + "px; background-color:" + footerColour
                   + "; margin:" + marginFooterTop + " " + marginRight + " " + marginFooterBottom + " " + marginLeft + ";'></div>";

      var prependDiv = "<div class='" + headerDivName + "' style='height:" + headerHeight + "px; background-color:" + footerColour
                   + "; margin:" + marginHeadTop + " " + marginRight + " " + marginHeadBottom + " " + marginLeft + ";'></div>";
  
      // standar settings for the main, header and footer sections
      var settingsMain = {
              tl: { radius: setRadius },
              tr: { radius: setRadius },
              bl: { radius: setRadius },
              br: { radius: setRadius },
              antiAlias: true,
              autoPad: false,
              validTags: ["div"]
          }
    
      var settingsFooter = {
              tl: { radius: 0 },
              tr: { radius: 0 },
              bl: { radius: setRadiusB },
              br: { radius: setRadiusB },
              antiAlias: true,
              autoPad: false,
              validTags: ["div"]
          }
    
      var settingsHeader = {
              tl: { radius: setRadiusB },
              tr: { radius: setRadiusB },
              bl: { radius: 0 },
              br: { radius: 0 },
              antiAlias: true,
              autoPad: false,
              validTags: ["div"]
          }
          
      
        $this.css({'margin':mainMargin, 'width':divWidth,'border-width':divBorder, 'border-color':borderColour, 'background-color':mainBackColour });
        if (settings.header)  
          {
            $this.prepend(prependDiv);
            $(headerDiv).corner(settingsHeader);
          }
  
        if (settings.footer)  
          {
            $this.append(appendDiv);
            $(footerDiv).corner(settingsFooter);
          }
  
       $this.corner(settingsMain);

     }); // end each
  };

})( jQuery );


