(function($) {
	
	// constructor
	$.fn.roundCournersCache = new Array();
	$.fn.roundCournersCache_b = {};
	$.fn.roundCournersCacheBorder = new Array();
	$.fn.roundCournersFire = false;
	
	$.fn.makeStar = function (conf)
	{
		$('#canvasadDSASDASd').attr("width",conf.radius*2+conf.borderWidth*2);
		$('#canvasadDSASDASd').attr("height",conf.radius*2+conf.borderWidth*2);
		var canvas = document.getElementById('canvasadDSASDASd');
		var ctx = canvas.getContext('2d');
		ctx.fillStyle = conf.color;
		if (conf.borderWidth>0)
		{
			ctx.strokeStyle = conf.borderColor;
			if (conf.borderWidth>1) ctx.lineWidth=conf.borderWidth*2;
			else
			{
				ctx.lineWidth=conf.borderWidth*2;
			}
		}
		r=conf.radius;
		zX=0;
		zY=r*2;
		
		var radius=r; //+conf.borderWidth/2;
		
		var x=0;
		var y=0;

		ctx.moveTo(x+radius,y);
		
		ctx.arc(x, y, radius, 0, (Math.PI/180)*90 ,false);
		ctx.arc(x, y+radius*2, radius, (Math.PI/180)*270, (Math.PI/180)*360 ,false);
		ctx.arc(x+radius*2, y+radius*2, radius, (Math.PI/180)*180, (Math.PI/180)*270 ,false);
		ctx.arc(x+radius*2, y, radius, (Math.PI/180)*90, (Math.PI/180)*180 ,false);
		
		if (conf.borderWidth>0) ctx.stroke();
		ctx.fill();
		return canvas.toDataURL();
	}
	
	
	
	$.fn.rCorners = function(userConf)
	{
		var conf = {
			radius : 10,
			color : "#ffffff",
			topLeft : true,
			topRight: true,
			bottomRight: true,
			bottomLeft: true,
			borderWidth: 0,
			borderColor: "",
			ieBorderOut: false,
			canvas: "auto"
		};
		$.extend(conf, userConf);	
//		if (conf.topLeft && conf.topRight && conf.bottomRight && conf.bottomLeft)
		var canv=false;
		if ($.browser.opera || ($.browser.mozilla && jQuery.browser.version.substr(0,3)!="1.9"))
		{
			canv=true;
		}
		if (conf.canvas=="always" && !$.browser.msie) canv=true; 
		if (canv)
		{
			var rCournersExt=
			{
				topLeft : false,
				topRight: false,
				bottomRight: false,
				bottomLeft: false
			};
			if (!$.fn.roundCournersFire) $('body').append("<div style='display: none; border:1px solid red; float: left;'><canvas id='canvasadDSASDASd'></canvas></div>");
			
			var bo=parseInt(this.eq(0).css("border-left-width"));
			
			
			eval("var cache=$.fn.roundCournersCache_b.cache"+conf.radius+"_"+bo+";");
			if (cache == undefined)
			{
				conf.borderWidth=bo;
				conf.borderColor=this.eq(0).css("border-left-color");
				var current=$.fn.makeStar(conf);
				eval("$.extend($.fn.roundCournersCache_b,{ cache"+conf.radius+"_"+bo+":current });");
			}
			else
			{
				var current=cache;
			}
		
			var r=conf.radius; //+ conf.borderWidth/2;
			
			
			this.css({ position:'relative'});
			var forAppend="";
			
			if (conf.topLeft) { forAppend+='<div style="background: url('+current+') no-repeat -'+r+'px -'+r+'px; width:'+r+'px; height:'+r+'px; position: absolute; left: '+(-bo)+'px; top: '+(-bo)+'px;"></div>'; rCournersExt.topLeft=true; };
			if (conf.topRight) { forAppend+='<div style="background: url('+current+') no-repeat 0px -'+r+'px; width:'+r+'px; height:'+r+'px; position: absolute; right: '+(-bo)+'px; top: '+(-bo)+'px;"></div>'; rCournersExt.topRight =true; };
			if (conf.bottomRight) { forAppend+='<div style="background: url('+current+') no-repeat 0px 0px; width:'+r+'px; height:'+r+'px; position: absolute; right: '+(-bo)+'px; bottom: '+(-bo)+'px;"></div>'; rCournersExt.bottomRight=true; };
			if (conf.bottomLeft) { forAppend+='<div style="background: url('+current+') no-repeat -'+r+'px 0px; width:'+r+'px; height:'+r+'px; position: absolute; left: '+(-bo)+'px; bottom: '+(-bo)+'px;"></div>'; rCournersExt.bottomLeft=true; };
			var curOb=$(this);
//			$.each(curOb,function(i,o){
//				alert(i);
//				curOb.eq(i).append(forAppend);
//				$.extend($(this),rCournersExt);
//			});
			$(this).append(forAppend);
//			this.append(forAppend);
			
//			this.append('<div style="background: url('+$.fn.roundCournersCache[conf.radius]+') no-repeat -'+r+'px -'+r+'px; width:'+r+'px; height:'+r+'px; position: absolute; left: 0px; top: 0px;"></div>'+
//					'<div style="background: url('+$.fn.roundCournersCache[conf.radius]+') no-repeat 0px -'+r+'px; width:'+r+'px; height:'+r+'px; position: absolute; right: 0px; top: 0px;"></div>'+
//					'<div style="background: url('+$.fn.roundCournersCache[conf.radius]+') no-repeat 0px 0px; width:'+r+'px; height:'+r+'px; position: absolute; right: 0px; bottom: 0px;"></div>'+
//					'<div style="background: url('+$.fn.roundCournersCache[conf.radius]+') no-repeat -'+r+'px 0px; width:'+r+'px; height:'+r+'px; position: absolute; left: 0px; bottom: 0px;"></div>');
		}
		else
		{
			if ($.browser.mozilla)
			{
				if (conf.topLeft && conf.topRight && conf.bottomRight && conf.bottomLeft) this.css("-moz-border-radius",conf.radius);
				else
				{
					//-moz-border-radius-topleft, -moz-border-radius-topright, -moz-border-radius-bottomright and -moz-border-radius-bottomleft
					if (conf.topLeft) this.css("-moz-border-radius-topleft",conf.radius);
					if (conf.topRight) this.css("-moz-border-radius-topright",conf.radius);
					if (conf.bottomRight) this.css("-moz-border-radius-bottomright",conf.radius);
					if (conf.bottomLeft) this.css("-moz-border-radius-bottomleft",conf.radius);
				}
			}
			if ($.browser.safari)
			{
				if (conf.topLeft && conf.topRight && conf.bottomRight && conf.bottomLeft) this.css("-webkit-border-radius",conf.radius);
				else
				{
					if (conf.topLeft) this.css("-webkit-border-top-left-radius",conf.radius);
					if (conf.topRight) this.css("-webkit-border-top-right-radius",conf.radius);
					if (conf.bottomRight) this.css("-webkit-border-bottom-right-radius",conf.radius);
					if (conf.bottomLeft) this.css("-webkit-border-bottom-left-radius",conf.radius);
				}
			}
			if ($.browser.msie)
			{
				
				if (!$.fn.roundCournersFire)
				{
					$("html").attr("xmlns:v","urn:schemas-microsoft-com:vml");
					$("head").append('<style>'+
							'v\\:* { behavior: url(#default#VML) }'+
					'</style>');
				}
				if ($.fn.roundCournersCache[conf.radius] == undefined)
				{
					$.fn.roundCournersCache[conf.radius]=true;
					$("body").prepend('<v:shapetype id="starASD'+conf.radius+'"'+
						  'strokecolor="'+conf.color+'" strokeweight="px" fillcolor="'+conf.color+'"'+
						  'coordorigin="0 0" coordsize="50 50"'+
						'path="m 0,0 wa 0,0,100,100, 0,50,50,0 l 0,00"></v:shapetype>');
				}
				
				var bo=parseInt(this.eq(0).css("border-width"));
				var l1=""; var l2="";	var l3="";	var l4="";
				var r=conf.radius;
				var forAppend="";
				
				if (bo>0)
				{
					
					if (conf.ieBorderOut)
					{
						this.width(this.eq(0).width()+bo*4);
						this.height(this.eq(0).height()+bo*4);
					}
					var boc=this.eq(0).css("border-color");
					
					//'+boc+'
					
					var w=-1*bo;
					r=r+w;
					            

					$("body").prepend('<v:shapetype id="_starASD'+conf.radius+'" coordorigin="0 0" coordsize="'+r+' '+r+'">'+
							'<v:fill color="'+boc+'"></v:fill>'+
							'<v:path  v="m 0,'+r+' wa 0,0,'+(r*2)+','+(r*2)+', 0,'+r+','+r+',0 l '+r+','+w+' m 0,'+r+' wa '+w+','+w+','+(r*2-w)+','+(r*2-w)+', 0,'+r+','+r+',0 ns"></v:path>'+
						'</v:shapetype>');
					
					l1='<v:shape style="overflow: hidden; position: absolute; top: -1px; left: -1px; width: '+r+'; height: '+r+';" type="#_starASD'+conf.radius+'"></v:shape>';
					l2='<v:shape style="overflow: hidden; position: absolute; top: -1px; right: 0px; width: '+r+'; height: '+r+'; flip: x;" type="#_starASD'+conf.radius+'"></v:shape>';
					l3='<v:shape style="overflow: hidden; position: absolute; bottom: '+(-bo)+'px; right: 0px; width: '+r+'; height: '+r+';  flip: x y;" type="#_starASD'+conf.radius+'"></v:shape>';
					l4='<v:shape style="overflow: hidden; position: absolute; left: -1px; bottom: '+(-bo)+'px; width: '+r+'; height: '+r+'; flip: y;" type="#_starASD'+conf.radius+'"></v:shape>';
					r=r-w;

				} else bo=0;
				
				this.css({ position:'relative' });
	
				if (conf.topLeft) forAppend+=l1+'<v:shape style="overflow: hidden; position: absolute; top: '+(-1-bo)+'px; left: '+(-1-bo)+'px; width: '+r+'; height: '+r+';" type="#starASD'+conf.radius+'"></v:shape>';
				if (conf.topRight) forAppend+=l2+'<v:shape style="overflow: hidden; position: absolute; top: '+(-1-bo)+'px; right: '+(-bo)+'px; width: '+r+'; height: '+r+'; flip: x;" type="#starASD'+conf.radius+'"></v:shape>';
				if (conf.bottomRight) forAppend+=l3+'<v:shape style="overflow: hidden; position: absolute; bottom: '+(-1-bo)+'px; right: '+(-bo)+'px; width: '+r+'; height: '+r+';  flip: x y;" type="#starASD'+conf.radius+'"></v:shape>';
				if (conf.bottomLeft) forAppend+=l4+'<v:shape style="overflow: hidden; position: absolute; left: '+(-1-bo)+'px; bottom: '+(-1-bo)+'px; width: '+r+'; height: '+r+'; flip: y;" type="#starASD'+conf.radius+'"></v:shape>';
				
				this.append(forAppend);
			}
		}
		$.fn.roundCournersFire=true;
		return this;
	};
})(jQuery);