/*================================
			Player Class
===============================*/
cPlayer.prototype = new cChar(); 
cPlayer.prototype.constructor = cPlayer;
function cPlayer()
{
	this.jumpstart;
	this.jumpspeed;
	this.gravity;
	this.jumping;
	this.health;
	this.lives;
		

	this.constructor = function()
	{
		this.speed = 5;

		this.jumpstart = -14;
		this.jumpspeed = 0;
		this.gravity = 2;
		this.jumping = false;
		this.health = 160;
		this.lives = 2;
		this.state = CHAR_INIT;
		
		this.width = 15;
		this.height = 15;
		this.centre_width = this.width / 2;
		this.centre_height = this.width / 2;
		
		this.animFrame = 0;
	}
	
	this.draw = function( domParent )
	{
		var tile = document.createElement( "div" );
		tile.style.position = "absolute";
		tile.style.width = this.width;
		tile.style.height = this.height;
		
		tile.style.left = this.x - this.centre_width + "px";
		tile.style.top = this.y - this.centre_height + "px";
		
		tile.style.backgroundImage = "url(images/characters.png)";
		tile.style.backgroundPosition = "0px 0px";
		
		this.domRef = tile;
		domParent.appendChild( tile );
	}
	
	this.jump = function()
	{

		this.jumpspeed = this.jumpspeed + this.gravity;
		if ( this.jumpspeed > g_tileH - this.height )
		{
			this.jumpspeed = g_tileH - this.height
		}
		if ( this.jumpspeed < 0 )
		{
			this.moveChar( 0, -1, -1 )
		}
		else if ( this.jumpspeed > 0 )
		{
			this.moveChar( 0, 1, 1 );
		}
		
	}
	
	this.hit = function()
	{
		if ( this.state != CHAR_RUN ) return;
		
		this.health -= 40;
		if ( this.health < 0 )
		{
			this.setState( CHAR_DYING );
		}
		else
		{
			var bar = document.getElementById( "health" );
			bar.style.height = this.health;
		}
	}
	this.checkInput = function()
	{
		if ( key[ 4 ] && ! this.jumping )
		{
			// Space	
			this.jumping = true;
			this.jumpspeed = this.jumpstart;	
		}

		if ( key[ 0 ] )
		{
			if (!key[1]) 
			{
				if ( ! this.jumping )
					this.domRef.style.backgroundPosition = "-" + (this.animFrame++ % 2 * this.width ) + "px 0px";
			
				this.moveChar( -1, 0, 0 );
			}
		}
		else if (key[1]) 
		{
			if ( ! this.jumping )
				this.domRef.style.backgroundPosition = "-" + (this.animFrame++ % 2 * this.width ) + "px 0px";
	
			this.moveChar( 1, 0, 0 );
				
		}
		if (key[2] && !key[3]) {
			//this.player.moveChar( 0, -1, 0 );
		}
		else if (key[3] && !key[2]) {
			this.moveChar( 0, 1, 0 );
		}
	}
	
	this.update = function()
	{
		switch( this.state )
		{
			case CHAR_INIT:
				this.setState( CHAR_RUN );
				break;
			case CHAR_RUN:
				this.checkInput();		
				if ( this.jumping )
				{
					this.jump();
				}
				break;
			case CHAR_DYING:
				if ( this.stateFrame == 0 )
				{
					this.stateLength = 20;
				}
				if ( this.stateFrame % 2 == 0 )
				{
					this.domRef.style.border = "1px solid #ff0";
				}
				else
				{
					this.domRef.style.border = "0";
				}
				if ( this.stateFrame++ == this.stateLength )
 				{
					this.domRef.style.border = "";
					this.setState( CHAR_DEAD );
				}
				break;
			case CHAR_DEAD:
				this.lives--;
				if ( this.lives >= 0 )
				{
					game.updateLevelStatus();
					game.setState( state_startLevel );
					this.setState( CHAR_INIT );
				}
				else
				{
					game.setState( state_finished );
				}

				break;
		}
		
			
		// Check if on a door
		if ( game.tiles.tiles[ g_maps[game.currentMap][ this.yTile ][ this.xTile ] ].door )
		{
			game.changeMap( game.tiles.tiles[ g_maps[ game.currentMap ][ this.yTile ][ this.xTile ] ] );
		}
		

		// Check if items
		for ( var i = 0; i < game.currentMapItems.length; i++ )
		{
			if ( game.currentMapItems[ i ].x == this.xTile && game.currentMapItems[ i ].y == this.yTile )
			{
				// Add score
				game.currentMapItems[ i ].got();
				game.currentMapItems[ i ].erase( game.domRef );
				game.currentMapItems.remove( game.currentMapItems[ i ] );
				
			}
		}

	}
	
	this.constructor();
}
