/*
	Copyright 2008 Hans Schmucker
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
	var CanvasCollision=function(canvasOrImageMap,canvasOrImageSprite){
		this.map=[canvasOrImageMap];
		this.sprite=canvasOrImageSprite;
		this.canvas=document.createElement("canvas");
		this.context=this.canvas.getContext("2d");
		this.minicanvas=document.createElement("canvas");
		this.minicontext=this.canvas.getContext("2d");
		this.setScaleAngle(1,0);
	};
	CanvasCollision.prototype={
		map:null,
		sprite:null,
		canvas:null,
		context:null,
		minicanvas:null,
		minicontext:null,
		a:0,
		s:1,
		dirty:false,
		tsprite_w:0,
		tsprite_h:0,
		tsprite_cx:0,
		tsprite_cy:0,
		mx:0,
		my:0,
		i:0,
		j:0,
		imgdata:null,
		setScaleAngle:function(scale,angle){
			if(typeof(scale)=="number" && scale!=NaN  && scale!=Infinity)
				this.s=scale;
				
			if(typeof(angle)=="number" && angle!=NaN  && angle!=Infinity)
				this.a=angle;
			
			this.dirty=((this.a%360)!=0 || this.s!=1);
			
			var w=this.sprite.width;
			var h=this.sprite.height;
			if(this.dirty){
				var sin=Math.sin(this.a*Math.PI/180);
				var cos=Math.cos(this.a*Math.PI/180);
				var xn=w*this.s/2;
				var yn=h*this.s/2;
				var x1y1_x=cos*xn-sin*yn;
				var x1y1_y=sin*xn+cos*yn;
				var x1y0_x=cos*xn-sin*yn*(-1);
				var x1y0_y=sin*xn+cos*yn*(-1);
				w=Math.ceil($js.max(x1y1_x,x1y0_x,x1y0_x*(-1),x1y1_x*(-1))-$js.min(x1y1_x,x1y0_x,x1y0_x*(-1),x1y1_x*(-1)));
				h=Math.ceil($js.max(x1y1_y,x1y0_y,x1y0_y*(-1),x1y1_y*(-1))-$js.min(x1y1_x,x1y0_x,x1y0_x*(-1),x1y1_x*(-1)));
			}
			if(w!=this.canvas.width||h!=this.canvas.height){
				this.canvas.width=w;
				this.canvas.height=h;
			}
			this.tsprite_cx=Math.floor(w/2);
			this.tsprite_cy=Math.floor(h/2);
			this.tsprite_w=w;
			this.tsprite_h=h;
			this.mx=Math.ceil(this.tsprite_w/4);
			this.my=Math.ceil(this.tsprite_h/4);
		},
		collide:function(x,y){
			if( x>(this.map[0].width-this.tsprite_cx-2)
				|| y>(this.map[0].height-this.tsprite_cy-2)
				|| x<(this.tsprite_cx+2)
				|| y<(this.tsprite_cy+2)
			) return true;
			
			for(this.i=0;this.i<this.map.length;this.i++){
				this.context.setTransform(1,0,0,1,0,0);
				this.context.globalCompositeOperation="copy";
				this.context.fillStyle="#FFF";
				this.context.fillRect(0,0,this.tsprite_w,this.tsprite_h);
				this.context.globalCompositeOperation="destination-out";
				if(this.dirty){
					this.context.translate(this.tsprite_cx,this.tsprite_cy);
					this.context.rotate(this.a*Math.PI/180);
					this.context.scale(this.s,this.s);
					this.context.translate(-Math.floor(this.sprite.width/2),-Math.floor(this.sprite.height/2));
				}
				
				this.context.drawImage(this.sprite,0,0);
				
				this.context.setTransform(1,0,0,1,0,0);
				this.context.globalCompositeOperation="source-out";

				this.context.drawImage(this.map[this.i],x-this.tsprite_cx,y-this.tsprite_cy,this.tsprite_w,this.tsprite_h,0,0,this.tsprite_w,this.tsprite_h);
				
				this.context.globalCompositeOperation="copy";
				this.minicontext.drawImage(this.canvas,0,0,this.tsprite_w,this.tsprite_h,0,0,this.mx,this.my);
				
				this.imgdata=this.minicontext.getImageData(0,0,this.mx,this.my);
				
				for(this.j=3;this.j<this.imgdata.data.length;this.j+=4)
						if(this.imgdata.data[this.j]>0){
							return true;
						}
			}
			return false;
		}
	};