(function($) {

	$.fn.radius = function(styleRadius){
	  
		var key = false;
		if (document.body.style.borderRadius != undefined) {
			key = 'w3c';
		} else if (document.body.style.MozBorderRadius !== undefined) {
			key = 'moz';
		} else if (document.body.style.webkitBorderRadius !== undefined) {
			key = 'wkit';
		}
	
		var props = {
			'w3c': {
				'border-radius':'border-radius',
				'border-bottom-left-radius':'border-bottom-left-radius',
				'border-bottom-right-radius':'border-bottom-right-radius',
				'border-top-left-radius':'border-top-left-radius',
				'border-top-right-radius':'border-top-right-radius'
			},
			'moz': {
				'border-radius':'-moz-border-radius',
				'border-bottom-left-radius':'-moz-border-radius-bottomleft',
				'border-bottom-right-radius':'-moz-border-radius-bottomright',
				'border-top-left-radius':'-moz-border-radius-topleft',
				'border-top-right-radius':'-moz-border-radius-topright'
			},
			'wkit': {
				'border-radius':'-webkit-border-radius',
				'border-bottom-left-radius':'-webkit-border-bottom-left-radius',
				'border-bottom-right-radius':'-webkit-border-bottom-right-radius',
				'border-top-left-radius':'-webkit-border-top-left-radius',
				'border-top-right-radius':'-webkit-border-top-right-radius'
			}
		};
		
		var round = function(e, arcsize) {
			
			var o = $(e);
			var pos = o.offset();
			
			var fillColor = e.currentStyle.backgroundColor;
			var fillSrc = e.currentStyle.backgroundImage.replace(/^url\("(.+)"\)$/, '$1');
			var stroked = true;
			var opacity = e.currentStyle.opacity;
			var strokeWeight = parseInt(e.currentStyle.borderWidth);
			var strokeColor = e.currentStyle.borderColor;
			if (isNaN(strokeWeight)) { strokeWeight = 0; strokeColor = fillColor; stroked = false; }
			var width = e.offsetWidth;
			var height = e.offsetHeight;

			var img = new Image();
			img.src = fillSrc;
			var imgW = img.width+'px';
			var imgH = img.height+'px';

			var max = Math.min(width, height);
			var per = (1/max) * arcsize;
			e.style.background = 'transparent';
			e.style.borderColor = 'transparent';

			var rr = document.createElement('v:roundrect');
			rr.arcsize = per;
			rr.stroked = stroked;
			rr.style.display = 'block';
			rr.style.position = 'absolute';
			rr.style.top = pos.top+'px';
			rr.style.left = pos.left+'px';
			rr.style.width = width+'px';
			rr.style.height = height+'px';
			rr.style.antialias = true;
			rr.style.zIndex = -1;

			//if (!/png/.test(fillSrc)) { rr.style.filter = 'alpha(opacity='+(100)+')'; }
			
			var stroke = document.createElement('v:stroke');
			stroke.color = strokeColor;
			stroke.weight = strokeWeight +'px';
			stroke.joinstyle = 'mitter';
			stroke.miterlimit = 8;

			var fill = document.createElement('v:fill');
			fill.color = fillColor;
			fill.src = fillSrc;
			fill.type = (o.css('backgroundRepeat') == 'no-repeat') ? 'frame' : 'tile';
			
			rr.appendChild(fill);
			rr.appendChild(stroke);
			e.appendChild(rr);
			
			var initH = o.outerHeight();
			var initW = o.outerWidth();
			$(window).bind('resize.css3', function(){ update(rr, o); });

		}
		
		var update = function(e, o) {
			var pos = o.offset();
			e.style.top = pos.top+'px';
			e.style.left = pos.left+'px';
		}
		
		if ($.browser.msie && jQuery.browser.version < 9 && !document.namespaces.v) {
			var css = document.createStyleSheet();
			css.addRule("v\\:roundrect", "behavior:url(#default#VML);");
			css.addRule("v\\:fill", "behavior:url(#default#VML);");
			css.addRule("v\\:stroke", "behavior:url(#default#VML);");
			document.namespaces.add("v", "urn:schemas-microsoft-com:vml");
		}
		
		this.each(function(index, obj) {
			if (key == false && styleRadius['border-radius']) {
				round(obj, styleRadius['border-radius']);
			} else {
				$.each(styleRadius, function(prop, value){
					$(obj).css(props[key][prop], value);
				});
			}
		});
	  
	};

})(jQuery);
