/**
@author bibby
A moveable character
**/

var Char = function(params)
{
	this.extends(Tangible)
	.extend({
		imgpath:'imgs/',
		dir:'S',
		id:'char'+Math.floor(Math.random()*10000),
		stepi:0,
		char:null,
		imgs:{},
		
		step:function(i)
		{
			if(is(i,'number') && i < 4 )
			{
				this.stepi=i;
				return;
			}
			
			this.stepi++;
			if(this.stepi == 4)
				this.stepi=0;
		},
		
		turn:function(dir)
		{
			if(is(dir,'string'))
			{
				dir = dir.toUpperCase();
				switch (dir)
				{
					case 'N':
					case 'E':
					case 'S':
					case 'W':
						this.dir = dir;
						break;
					case 'L':
						switch (this.dir)
						{
							case 'N':
								this.dir = 'W';
								break;
							case 'E':
								this.dir = 'N';
									break;
							case 'S':
								this.dir = 'E';
								break;
							case 'W':
								this.dir = 'S';
								break;
						}
						break;
					case 'R':
						switch (this.dir)
						{
							case 'N':
								this.dir = 'E';
								break;
							case 'E':
								this.dir = 'S';
									break;
							case 'S':
								this.dir = 'W';
								break;
							case 'W':
								this.dir = 'N';
								break;
						}
						break;
				}
			}
			
			if(is(this.char,'object'))
				this.char.src = this.src();
		},
		
		make:function()
		{
			var imgs={}, img, dir;
			var dirs = ['N','S','E','W'];
			do
			{
				dir = dirs.current();
				imgs[dir]=[];
				img = new Image(this.dim.width,this.dim.height);
				img.src = this.imgpath + this.id + dir + '0.png';
				
				imgs[dir][0] =
				imgs[dir][2] = img;
				
				img = new Image(this.dim.width,this.dim.height);
				img.src = this.imgpath + this.id + dir + '1.png';
				imgs[dir][1] = img;
				
				img = new Image(this.dim.width,this.dim.height);
				img.src = this.imgpath + this.id + dir + '2.png';
				imgs[dir][3] = img;
					
				this.imgs[dir] = imgs[dir];
				
			} while (is(dirs.next()));
			
			return this;
		},
		
		move:function()
		{
			var p,i,xy=[4,6];
			switch(this.dir)
			{
				case 'N':
					p = 'top';
					i = xy[1]*-1;
					break;
				case 'S':
					p = 'top';
					i = xy[1];
					break;
				case 'E':
					p = 'left';
					i = xy[0];
					break;
				case 'W':
					p = 'left';
					i = xy[0]*-1;
					break;
			}
			
			if(is(i) && is(p))
			{
				var to = this.pos[p]+i;
				if(to<0)
					return;
				
				var before = this.pos;
				
				var area = this.area(p,to);
				
				if(!Contact.mayMove(area,this.id,[p,i]))
				{
					return;
				}
				
				this.pos[p] = to;
				this.char.style[p] = this.pos[p]+'px';
				Contact.update(this);
			}
		},
		
		display:function()
		{
			if(!is(document.getElementById(this.id)))
			{
				this.char = new Image(this.dim.width,this.dim.height);
				with(this.char)
				{
					src = this.src();
					id = this.id;
				};
				
				with(this.char.style)
				{
					position = 'absolute';
					left = this.pos.left;
					top = this.pos.top;
					zIndex = 100;
				};
				
				document.body.appendChild(this.char);
			}
		},
		
		src:function()
		{
			return this.imgs[this.dir][this.stepi].src;
		},
		
		handle:function(key)
		{
			//	   38
			//	37 40 39
			var keys = [];
			keys[37] = 'W';
			keys[38] = 'N';
			keys[39] = 'E';
			keys[40] = 'S';
			if(!is(keys[key]))
				return;
			
			if(keys[key] != this.dir)
				this.turn(keys[key]);
			else
				this.forward();
		},
		
		forward:function()
		{
			this.step();
			this.move();
			this.char.src = this.src();
		},
		
		load:function()
		{
			this.make().display();
			if(is(Contact,'object'))
				Contact.register(this,'Char');
		}
	})
	.extend(params,true)
	.init();
};
