var Char = function(params)
{
	this.imgpath = 'imgs/';
	this.dir = 'S';
	this.id = 'char'+Math.floor(Math.random()*10000);
	this.stepi = 0;
	this.char = null;
	this.imgs = {};
	this.pos = {left:20,top:20};
	
	this.step=function(i)
	{
		if(is(i,'number') && i < 4 )
		{
			this.stepi=i;
			return;
		}
		
		this.stepi++;
		if(this.stepi == 4)
			this.stepi=0;
	};
	
	this.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();
	};
	
	this.make = function()
	{
		var imgs={}, img, dir;
		var dirs = ['N','S','E','W'];
		do
		{
			dir = dirs.current();
			imgs[dir]=[];
			img = new Image(24,32);
			img.src = this.imgpath + this.id + dir + '0.png';
			
			imgs[dir][0] =
			imgs[dir][2] = img;
			
			img = new Image(24,32);
			img.src = this.imgpath + this.id + dir + '1.png';
			imgs[dir][1] = img;
			
			img = new Image(24,32);
			img.src = this.imgpath + this.id + dir + '2.png';
			imgs[dir][3] = img;
				
			this.imgs[dir] = imgs[dir];
			
		} while (is(dirs.next()));
		
		return this;
	};
	
	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))
		{
			this.pos[p] = this.pos[p]+i;
			this.char.style[p] = this.pos[p]+'px';
		}
	};
	
	this.display = function()
	{
		if(!is(document.getElementById(this.id)))
		{
			this.char = new Image(24,32);
			with(this.char)
			{
				src = this.src();
				id = this.id;
			};
			
			with(this.char.style)
			{
				position = 'absolute';
				left = this.pos.left;
				top = this.pos.top;
			};
			
			document.body.appendChild(this.char);
		}
	};
	
	this.src=function()
	{
		return this.imgs[this.dir][this.stepi].src;
	};
	
	this.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();
	};
	
	
	//construct from params
	if(is(params,'object'))
	{
		for(var i in params)
		{
			if(is(this[i],'function'))
				this[i](params[i]);
			else
				this[i]=params[i];	
		}
		
		this.make().display();
	};
	
	this.forward = function()
	{
		this.step();
		this.move();
		this.char.src = this.src();
	};
};
