// $Id: $

var rot_array=new Array();
var busy_array=new Array();

jQuery.fn.rotate = function(angle,whence) {
	var p = this.get(0);
	var id=-1;
	
	for (i=0; i<busy_array.length; i++){
		if (busy_array[i]==0){
			id=i;
			break;
		}
	}
	
	if (id<0){
		id=busy_array.length;
	}

	busy_array[id]=1;
	
	if (!rot_array[id]){
		rot_array[id] = new Rotate();
	}
	
	rot_array[id].rotate_img(p, angle, whence, function(old_pic,new_pic,obj){$(old_pic).before(new_pic).remove();}, null);
	busy_array[id]=0;
}

jQuery.fn.rotateRight = function(angle) {
	this.rotate(angle==undefined?90:angle);
}

jQuery.fn.rotateLeft = function(angle) {
	this.rotate(angle==undefined?-90:-angle);
}

function Rotate() {

	this.rotate_canvas_ie = function(new_img,callback,obj){
		with (new_img){
			style.left = orig_pic.style.left;
			style.top = orig_pic.style.top;			
			
			style.zIndex = orig_pic.style.zIndex;		
			style.position = orig_pic.style.position;
			style.visibility = orig_pic.style.visibility;
			
			// Added Border and Dropshadow
			style.filter = "Alpha(opacity=100) progid:DXImageTransform.Microsoft.Matrix(M11="+costheta+",M12="+(-sintheta)+",M21="+sintheta+",M22="+costheta+",SizingMethod='auto expand') progid:DXImageTransform.Microsoft.DropShadow(Color=#AAAAAA, OffX=3, OffY=3, Positive=1)";
			style.border = "solid 5px #fff";
			
			id = orig_pic.id;
			
			// Added by darookee
			// Sets alt and title attribute for newly created canvas
			alt = orig_pic.alt;
			title = orig_pic.title;
		}
					
		if (callback)
			callback(new_img.orig_pic,new_img,obj);
	}
		
	this.rotate_canvas = function(new_img,callback,obj){
		var canvas = document.createElement('canvas');
//		canvas.angle=angle;
			
		var w=new_img.width;
		var h=new_img.height;
		
		canvas.style.width = canvas.width = Math.abs(new_img.costheta * w) + Math.abs(new_img.sintheta * h) + 24;
		canvas.style.height = canvas.height = Math.abs(new_img.costheta * h) + Math.abs(new_img.sintheta * w) + 24;
		
		var context = canvas.getContext('2d');
		context.save();

		if (new_img.rotation <= Math.PI/2) {
			context.translate(new_img.sintheta * h+8,8);
		} else if (new_img.rotation <= Math.PI) {
			context.translate(new_img.canvas.width+8,-new_img.costheta * h+8);
		} else if (new_img.rotation <= 1.5*Math.PI) {
			context.translate(-new_img.costheta * w+8,new_img.canvas.height+8);
		} else {
			context.translate(8,-new_img.sintheta * w+8);
		}
		
		context.rotate(new_img.rotation);
		
		// Added by darookee
		// Creates a Border and another border
		// And a Dropshadow
		context.fillStyle = 'rgba(0,0,0,0.1)';
		context.fillRect(0,0,w+12,h+12);
		
		context.fillStyle = 'rgba(0,0,0,0.11)';
		context.fillRect(-6,-6,w+12,h+12);
		
		context.fillStyle = '#ffffff';
		context.fillRect(-5,-5,w+10,h+10);
		
		try {
			context.drawImage(new_img, 0, 0, w, h);
		} catch(err) {
//			return null;
		}
		
		context.restore();
		
		with (canvas){
			id = new_img.orig_pic.id;
			alt = new_img.orig_pic.alt;
			title = new_img.orig_pic.title;

			style.zIndex = new_img.orig_pic.style.zIndex;		
			style.position = new_img.orig_pic.style.position;
			style.display = new_img.orig_pic.style.display;
			style.visibility = new_img.orig_pic.style.visibility;

			left = new_img.orig_pic.left;
			top = new_img.orig_pic.top;

			style.left = new_img.orig_pic.style.left;
			style.top = new_img.orig_pic.style.top;
		}

		canvas.angle=new_img.angle;
		
		if (callback)
			callback(new_img.orig_pic,canvas,obj);	
	}

	this.rotate_img = function(p,angle,whence,callback,obj) {
		// 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 (typeof document.createElementNS != 'undefined')
			this.new_img = document.createElementNS('http://www.w3.org/1999/xhtml', 'img');
		else
			this.new_img = document.createElement('img');
				
		this.new_img.angle=angle;
		this.new_img.orig_pic=p;
		this.new_img.costheta=costheta;
		this.new_img.sintheta=sintheta;
		this.new_img.rotation=rotation;
		this.new_img.rotate=this;
		this.new_img.callback=callback;
		this.new_img.obj=obj;
		this.new_img.src = "";

		if (document.all && !window.opera) {
			if (!p.complete){
				this.new_img.onload = function() { this.rotate.rotate_canvas_ie(this,this.callback,this.obj) };
				this.new_img.src = p.src;
			}else{
				this.new_img.src = p.src;
				this.rotate_canvas_ie(this.new_img,callback,obj);
			}
		} else {
//			this.canvas = document.createElement('canvas');
//			this.canvas.angle=angle;
			
			if (!p.complete){
				this.new_img.onload = function() { this.rotate.rotate_canvas(this,this.callback,this.obj) };
				this.new_img.src = p.src;
			}else{
				this.new_img.src = p.src;
				this.rotate_canvas(this.new_img,callback,obj);
			}
		}
	}

}
