jQuery.fn.rotate = function(minwidth,r,mh,angle,whence) {
	var p = this.get(0);

	// we store the angle inside the image tag for persistence
	if (!whence) {
		p.angle = ((p.angle==undefined?0:p.angle) + angle) % 360;
	} else {
		p.angle = angle;
	}

	if (p.angle >= 0) {
		var rotation = Math.PI * p.angle / 180;
	} else {
		var rotation = Math.PI * (360+p.angle) / 180;
	}
	var costheta = Math.cos(rotation);
	var sintheta = Math.sin(rotation);

	if (document.all && !window.opera) {
		var canvas = document.createElement('img');

		canvas.src = p.src;
		canvas.height = p.height;
		canvas.width = p.width;

		canvas.style.filter = "progid:DXImageTransform.Microsoft.Matrix(M11="+costheta+",M12="+(-sintheta)+",M21="+sintheta+",M22="+costheta+",SizingMethod='auto expand')";
	} else {
		var canvas = document.createElement('canvas');
		if (!p.oImage) {
			canvas.oImage = new Image();
			canvas.oImage.src = p.src;
		} else {
			canvas.oImage = p.oImage;
		}
		var context
		if (rotation == 0) 
		{
			canvas.width = canvas.oImage.width
			canvas.height = canvas.oImage.height
			if(canvas.width < minwidth) {canvas.width = minwidth}
			context = canvas.getContext('2d')

			context.translate(canvas.width/2-canvas.oImage.width/2,0)
			radio = r
		}
		else if (rotation == Math.PI/2) 
		{	
			if(canvas.oImage.width > mh){
				canvas.height = mh
				radio1 = mh/canvas.oImage.width
				canvas.width = Math.round(canvas.oImage.height * radio1)
				radio = radio1 * r
			}else{
				radio1 = 1
				radio = r
				canvas.width = canvas.oImage.height
				canvas.height = canvas.oImage.width
				
			}
			context = canvas.getContext('2d')
		
			if(canvas.width < minwidth) {canvas.width = minwidth}
			context.translate(canvas.width/2+canvas.oImage.height*radio1/2,0)
		
		} 
		else if (rotation == Math.PI) 
		{
			canvas.width = canvas.oImage.width
			canvas.height = canvas.oImage.height
			text = canvas.getContext('2d')
			
			radio = r
			context = canvas.getContext('2d')
			if(canvas.width < minwidth) {canvas.width = minwidth}

			context.translate(canvas.width/2+canvas.oImage.width/2,canvas.height)
		} 
		else if (rotation == 1.5*Math.PI) 
		{
			if(canvas.oImage.width > mh){
				canvas.height = mh
				radio1 = mh/canvas.oImage.width
				canvas.width = Math.round(canvas.oImage.height * radio1)
				radio = radio1 * r
				
			}else{
				radio1 = 1
				canvas.width = canvas.oImage.height
				canvas.height = canvas.oImage.width
				radio = r
			}	
			context = canvas.getContext('2d')

			if(canvas.width < minwidth) {canvas.width = minwidth}
			context.translate(canvas.width/2-canvas.oImage.height*radio1/2,canvas.height)
			
			rotation -= 2*Math.PI
		} 
		else 
		{
			canvas.width = canvas.oImage.height
			canvas.height = canvas.oImage.widht
			context = canvas.getContext('2d')
			radio = r
			context.translate(0,-sintheta*canvas.oImage.width);
		}
		
		context.scale(radio,radio)
		context.rotate(rotation);	
	//	context.drawImage(canvas.oImage, posx, posy, canvas.oImage.width, canvas.oImage.height);
		context.drawImage(canvas.oImage, 0,0);

	}
	canvas.id = p.id;
	canvas.angle = p.angle;
	p.parentNode.replaceChild(canvas, p);
}

jQuery.fn.rotateRight = function(minwidth,radio,mh,img,angle) {
	var p = this.get(0);
	var s = mh
	var r = radio
	var t = minwidth
	if(p != undefined && img != undefined)
	{
		p.oImage = img;
	}
	this.rotate(t,r,s,angle==undefined?90:angle);
}

jQuery.fn.rotateLeft = function(minwidth,radio,mh,img, angle) {
	var p = this.get(0);
	var s = mh	
	var r = radio
	var t = minwidth
	if(p != undefined && img != undefined)
	{
		p.oImage = img;
	}
	this.rotate(t,r,s,angle==undefined?-90:-angle);
}
