/*
jQuery.fn.flipIma = function(direction){	
	//Written By Pulkit - 21 May 2007
	var p = this.get(0);		
	//Browser Detection !
   	if (document.all && !window.opera){
		var canvas = document.createElement('img');
		canvas.src = p.src;
		canvas.height = p.height;
		canvas.width = p.width;
		if (direction == 'h'){canvas.style.filter = 'fliph';}
		if (direction == 'v'){canvas.style.filter = 'flipv';}			
	}else{  	   				
		if($('image_back')){
			var canvas = $('image_back');
			//alert('not adding');
		}else{
			var canvas = document.createElement('canvas');
			//alert('add');
		}		
		// if (!p.oImage){
			// canvas.oImage = new Image();
			// canvas.oImage.src = p.src;						
		// } else{
			// canvas.oImage = p.oImage;
			// alert(p.oImage);
		// }						
		context = canvas.getContext('2d');	
		//context.save();		
		canvas.style.width = canvas.width = canvas.oImage.width;;
		canvas.style.height = canvas.height = canvas.oImage.height;		    		
		if (direction == 'h'){context.translate(canvas.oImage.width,0);context.scale(-1, 1);}
		if (direction == 'v'){context.translate(0,canvas.oImage.height);context.scale(1, -1);}
		context.drawImage(canvas.oImage, 0, 0, canvas.oImage.width, canvas.oImage.height);
		//context.restore();
	} 
	canvas.id = p.id + '_back';	
	p.oImage = canvas.oImage;
	//if(!($('image_back'))){p.parentNode.appendChild(canvas);}
	if(!($('image_back'))){p.parentNode.appendChild(canvas);}
 }	
*/
jQuery.fn.rotate = function(angle,whence){
	var p = this.get(0);	
	// we store the angle inside the image tag for persistence			
	
	//alert'p.angle = '+p.angle);	
	
	if (!whence){
			if((angle =='h')||(angle =='v')){
				p.angle = ((p.angle==undefined?0:p.angle) +0) % 360;			
			}else{
				p.angle = ((p.angle==undefined?0:p.angle) + angle) % 360;
			}
	} else {
			//alertangle);
			p.angle = (angle==undefined?p.angle:angle);
	}
	
	//alert'setting p.angle = '+p.angle);	
	
	if((angle=='h')||(angle=='v')){	
		if(p.flip==undefined){
			if(angle =='h')p.flip = 2;
			if(angle =='v')p.flip = 1;			
		}else{
			if(angle =='h')p.flip =p.flip^2; 
			if(angle =='v')p.flip =p.flip^1; 
		}		
	}
		
	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 {
		if($('image_back')){
			var canvas = $('image_back');
			////alert'not adding');
		}else{
			var canvas = document.createElement('canvas');
		}
		//var canvas = document.createElement('canvas');		
		if (!p.oImage) {
			canvas.oImage = new Image();
			canvas.oImage.src = p.src;
			//p.oImage = canvas.oImage;
			////alert'n');
		} else {
			canvas.oImage = p.oImage;
			////alert'o');
		}
		
		//if((angle!='h')&&(angle!='v')){	
		/*
		var	nW = Math.abs(costheta*canvas.oImage.width) + Math.abs(sintheta*canvas.oImage.height);
		var nH = Math.abs(costheta*canvas.oImage.height) + Math.abs(sintheta*canvas.oImage.width);
		if(nH > nW){
			var bck = nH;
			nH = nW;
			nW = nW * (nW/bck);			
		}		
		canvas.style.width = canvas.width = nW;
		canvas.style.height = canvas.height = nH;
		*/ //Commented by pulkit - on september 19 2007
		
		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);
		
		//console.log('ct = '+ costheta + ' w : ' + canvas.style.width);
		//console.log('st = '+ sintheta + 'h : ' + canvas.style.height);
		
		var context = canvas.getContext('2d');
		
		//context.save();
		
		if (rotation <= Math.PI/2) {
			context.translate(sintheta*canvas.oImage.height,0);
		} else if (rotation <= Math.PI) {
			context.translate(canvas.width,-costheta*canvas.oImage.height);
		} else if (rotation <= 1.5*Math.PI) {
			context.translate(-costheta*canvas.oImage.width,canvas.height);
		} else {
			context.translate(0,-sintheta*canvas.oImage.width);
		}
		context.rotate(rotation);
		context.drawImage(canvas.oImage, 0, 0, canvas.oImage.width, canvas.oImage.height);
		//context.restore();
		
		/*
		if (rotation <= Math.PI/2) {
			//context.translate(sintheta*nH,0);
			context.translate(sintheta*nW,0);
		} else if (rotation <= Math.PI) {
			context.translate(canvas.width,-costheta*nH);
		} else if (rotation <= 1.5*Math.PI) {
			context.translate(-costheta*nW,canvas.height);
			//context.translate(sintheta*nH,canvas.height);
		} else {
			context.translate(0,-sintheta*nW);
		}
		context.rotate(rotation);
	
		//if((angle == 'h')&&((p.flip&&2) == 2)){context.translate(nW,0);context.scale(-1, 1);}
		//if((angle == 'v')&&((p.flip&&1) ==1)){context.translate(0,nH);context.scale(1, -1);}
				
		//alert'Flip is : '+ p.flip);
		
		if((p.flip == 2)||(p.flip == 3)){
			context.translate(nW,0);context.scale(-1, 1);
			//alert'h');
		}
		if((p.flip == 1)||(p.flip == 3)){
			context.translate(0,nH);context.scale(1, -1);
			//alert'v');
		}
		
		if(nH > nW){
			context.drawImage(canvas.oImage, 0, 0, nH, nW);
		}else{
			context.drawImage(canvas.oImage, 0, 0, nW, nH);
		}
		*/
		//context.restore();	
		//canvas.id = p.id;
		//canvas.angle = p.angle;
		//p.parentNode.replaceChild(canvas, p);
		canvas.id = p.id + '_back';
		//alert'Angle : '+p.angle);
		canvas.angle = p.angle;			
		//p.oImage = canvas.oImage;
		if(!($('image_back'))){p.parentNode.appendChild(canvas);}
}
}

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

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

