jQuery.fn.rotateAndResize = function(angle, whence, maxWidth) {
    if (typeof(maxWidth) == 'undefined') {
        maxWidth = -1;
    }
    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;
    }

    while (p.angle < 0) p.angle += 360;
    while (p.angle > 359) p.angle -= 360;
    
    var rotation = Math.PI * 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.style.filter = "progid:DXImageTransform.Microsoft.Matrix(M11="+costheta+",M12="+(-sintheta)+",M21="+sintheta+",M22="+costheta+",SizingMethod='auto expand')";
        
        if (maxWidth > 0 && canvas.width > maxWidth) {
            var ratio;
            var w;
            var h;
            
            // This has to be a 2-step process...
            if (p.angle == 90 || p.angle == 270) {
                ratio = maxWidth / canvas.height;
                w = Math.floor(canvas.width * ratio);
                h = Math.floor(canvas.height * ratio);
            } else {
                ratio = maxWidth / canvas.width;
                w = Math.floor(canvas.width * ratio);
                h = Math.floor(canvas.height * ratio);
            }
            
            canvas.style.width = w;
            canvas.style.height = h;
            
            canvas.width = null;
            canvas.height = null;
        }
        
    } else {
        var canvas = document.createElement('canvas');
        if (!p.oImage) {
            canvas.oImage = new Image();

            var imageHeight = p.height;
            var imageWidth = p.width;

            // Added following as suggested in http://code.google.com/p/jquery-rotate/issues/detail?id=3
            canvas.oImage.onload = function() {
                var ratio = 0;
                canvas.style.height = imageHeight + "px";
                canvas.style.width = imageWidth + "px";

                var tempWidth = Math.abs(costheta*imageWidth) + Math.abs(sintheta*imageHeight);
                var tempHeight = Math.abs(costheta*imageHeight) + Math.abs(sintheta*imageWidth);
                canvas.style.width  = tempWidth + "px";
                canvas.style.height = tempHeight + "px";
                canvas.width        = tempWidth;
                canvas.height       = tempHeight;

                if (maxWidth > 0 && canvas.width > maxWidth) {
                    ratio = maxWidth / canvas.width;
                    canvas.style.width = Math.floor(canvas.width * ratio) + "px";
                    canvas.style.height = Math.floor(canvas.height * ratio) + "px";
                }
                
                var context = canvas.getContext('2d');
                context.save();
                
                if (rotation <= Math.PI/2) {
                    context.translate(sintheta*imageHeight,0);
                } else if (rotation <= Math.PI) {
                    context.translate(canvas.width,-costheta*imageHeight);
                } else if (rotation <= 1.5*Math.PI) {
                    context.translate(-costheta*imageWidth,canvas.height);
                } else {
                    context.translate(0,-sintheta*imageWidth);
                }
                
                context.rotate(rotation);
                
                context.drawImage(canvas.oImage, 0, 0);
                
                context.restore();
            }
            canvas.oImage.src = p.src;
        } else {
            canvas.oImage = p.oImage;
        }

    }
    canvas.id = p.id;
    canvas.angle = p.angle;
    p.parentNode.replaceChild(canvas, p);
}

jQuery.fn.rotateRightAndResize = function(angle, maxWidth) {
    this.rotateAndResize(angle==undefined?90:angle, null, maxWidth);
}

jQuery.fn.rotateLeftAndResize = function(angle, maxWidth) {
    this.rotateAndResize(angle==undefined?-90:-angle, null, maxWidth);
}



