jQuery.fn.rotate = function(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');
		
		// create the image
		if (!p.oImage) {
			canvas.oImage = new Image();
			canvas.oImage.src = p.src;
		} else {
			canvas.oImage = p.oImage;
		}
    
    var oImgWidth = canvas.oImage.width;
    var oImgHeight = canvas.oImage.height;

		var context = canvas.getContext('2d');
		context.save();


    // canvas.style.width = canvas.width = Math.abs(costheta*canvas.oImage.width) + Math.abs(sintheta*canvas.oImage.height);
    // canvas.style.height = canvas.height = Math.abs(costheta*canvas.oImage.height) + Math.abs(sintheta*canvas.oImage.width);
    
    // scenario 1, the original image is taller than it is wide
    if(oImgHeight > oImgWidth) {
      if (rotation == Math.PI/2) {
        if(oImgHeight < 400) {
          canvas.style.width = canvas.width = oImgHeight;
          canvas.style.height = canvas.height = oImgWidth;
    			context.translate(sintheta * oImgHeight, 0);
        } else {
          canvas.style.width = canvas.width = Math.min(oImgHeight, 400);
          canvas.style.height = canvas.height = (400 * oImgWidth) / oImgHeight;
    			context.translate(sintheta * Math.min(oImgHeight, 400), 0);
        }
        context.rotate(rotation);
        context.drawImage(canvas.oImage, 0, 0, oImgWidth, oImgHeight, 0, 0, canvas.height, canvas.width);
  		} else if (rotation == Math.PI) {
  		  canvas.style.width = canvas.width = Math.min(oImgWidth, 400);
        canvas.style.height = canvas.height = oImgHeight;
  			context.translate(canvas.width, -costheta * oImgHeight);
  			context.rotate(rotation);
        context.drawImage(canvas.oImage, 0, 0, oImgWidth, oImgHeight);
  		} else if (rotation == 1.5*Math.PI) {
        if(oImgHeight < 400) {
          canvas.style.width = canvas.width = oImgHeight;
          canvas.style.height = canvas.height = oImgWidth;
          context.translate(-costheta * oImgWidth, canvas.height);
        } else {
          canvas.style.width = canvas.width = Math.min(oImgHeight, 400);
          canvas.style.height = canvas.height = (400 * oImgWidth) / oImgHeight;
          context.translate(-costheta * oImgWidth, canvas.height);
        }
        context.rotate(rotation);
        context.drawImage(canvas.oImage, 0, 0, oImgWidth, oImgHeight, 0, 0, canvas.height, canvas.width);
  		} else if (rotation == 0){
  		  canvas.style.width = canvas.width = Math.min(oImgWidth, 400);
        canvas.style.height = canvas.height = oImgHeight;
  			context.translate(0,-sintheta*canvas.oImage.width);
  			context.rotate(rotation);
        context.drawImage(canvas.oImage, 0, 0, oImgWidth, oImgHeight);
  		}  		
    }
    // scenario 2, the original image is wider than it is tall 
    else {      
      if (rotation == Math.PI/2) {
        canvas.style.width = canvas.width = Math.max(oImgHeight, 400);
        canvas.style.height = canvas.height = oImgWidth;
  			context.translate(sintheta * oImgHeight, 0);
  		} else if (rotation == Math.PI) {
  		  canvas.style.width = canvas.width = Math.max(oImgWidth, 400);
        canvas.style.height = canvas.height = oImgHeight;
  			context.translate(canvas.width, -costheta*oImgHeight);
  		} else if (rotation == 1.5*Math.PI) {
        canvas.style.width = canvas.width = Math.max(oImgHeight, 400);
        canvas.style.height = canvas.height = oImgWidth;  		  
  			context.translate(-costheta * oImgWidth, canvas.height);
  		} else if (rotation == 0){
  		  canvas.style.width = canvas.width = Math.max(oImgWidth, 400);
        canvas.style.height = canvas.height = oImgHeight;
  			context.translate(0,-sintheta*canvas.oImage.width);
  		}
      
  		context.rotate(rotation);
      context.drawImage(canvas.oImage, 0, 0, oImgWidth, oImgHeight);  		
    }

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

jQuery.fn.rotateRight = function(angle) {
	this.rotate(angle==undefined?90:angle);
}

jQuery.fn.rotateLeft = function(angle) {
	this.rotate(angle==undefined?-90:-angle);
}
