// border-radius
// Jonah Fox
// MIT Licensed
// Use like : $(".myClass").borderRadius() will attempt to apply curved corners as per the elements -moz-border-radius attribute
// Good:
//   - supports textured forgrounds and backgrounds
//   - maintains layouts
//   - very easy to use
//   - IE6 and IE7
// Bad:
//   - not fluid. Reapply if the dimensions change
//   - only supports rounding all corners
//   - no hover
//   - no Opera

;(function($){

	if ($.browser.msie && document.namespaces["v"] == null) {
		document.namespaces.add("v", "urn:schemas-microsoft-com:vml");
		var ss = document.createStyleSheet().owningElement;
		ss.styleSheet.cssText = "v\\:*{behavior:url(#default#VML);}";
	}

	function RR(o) {
		var html = '<div class="ie_border_radius" style="position: absolute; left: 0px; top: 0px; z-index: -1; width:' + (o.width) + "px;height:" + (o.height) + 'px;">'
		html += '<v:roundrect arcsize="' + o.arcSize + '" style="behavior: url(#default#VML); position:absolute;  antialias: true; width:' + (o.width) + "px;height:" + (o.height) + 'px;' + '" fillcolor="' + o.fillColor + '" ';
		if (o.strokeWeight)
			html += 'strokecolor="' + o.strokeColor + '" strokeweight="' + o.strokeWeight + '" ';
		else
			html += 'stroked="false" ';
		html += ' />';
		html += "</div>";

		return html;
	}

	$.fn.borderRadius = !$.browser.msie ? function() {} : function(options) {

		var options = options || {};
		
		return this.each(function() {

			var $this = $(this);
			
			var opts = {};
			var wrapper = null;

			if (this._border_radius_opts) {
				alert("borderRadius() can't currently be applied more than once");
				return;
				// opts = this._border_radius_opts;
				// wrapper = $this.parent();
				// wrapper.find(".ie_border_radius").remove();
			} else {
				if (this.currentStyle.borderStyle == 'none' || this.currentStyle.borderWidth == '0px') {
					opts.strokeColor = this.currentStyle.backgroundColor;
					opts.strokeWeight = '1px';
				} else {
					opts.strokeColor = this.currentStyle.borderColor;
					opts.strokeWeight = this.currentStyle.borderWidth;
				}

				opts.fillColor = this.currentStyle.backgroundColor;
				opts.fillSrc = this.currentStyle.backgroundImage.replace(/^url\("(.+)"\)$/, '$1');

				this.style.border = 'none'; // perhaps add onto padding?
				this.style.background = 'transparent';

				this._border_radius_opts = opts;
			}
			

			opts.width = $this.outerWidth() - 1;
			opts.height = $this.outerHeight() - 1;

			var r = options.radius || parseInt( this.currentStyle['-ie-border-radius'] || this.currentStyle['-moz-border-radius'] || this.currentStyle['moz-border-radius'] );
			opts.arcSize = Math.min( r / Math.min(opts.width, opts.height), 1);
			
			var target = $this;
			
			// need to wrap table elements because they can't have a direct inner div element
			if (this.tagName == 'TABLE') {
				target = $this.wrap('<div></div>').parent();
				if ($this.css('position') == 'absolute') {
					target.css('position', 'absolute');
					target.css('top', $this.css('top'));
					target.css('right', $this.css('right'));
					target.css('bottom', $this.css('bottom'));
					target.css('left', $this.css('left'));
					target.css('z-index', $this.css('z-index'));
					$this.css('top', 'auto');
					$this.css('right', 'auto');
					$this.css('bottom', 'auto');
					$this.css('left', 'auto');
				}
			}
			
			target.get(0).innerHTML += RR(opts);
			
			if (target.css('position') != 'absolute')
				target.css('position', 'relative');
			
			target.css('zoom', 1); // give it a layout
		});
	};
})(jQuery);

