/*================================
			Player Class
===============================*/
cPlayer.prototype = new cChar(); 
cPlayer.prototype.constructor = cPlayer;
function cPlayer()
{
	this.jumpstart;
	this.jumpspeed;
	this.gravity;
	this.health;
	this.lives;
	
	this.score;
	this.hiscore;
	
	this.hasKey;

	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.direction = 0;
		this.hasKey = 0;
		this.score = 0;
		this.hiscore = 0;
		
		this.width = 15;
		this.height = 15;
		this.centre_width = this.width / 2;
		this.centre_height = this.width / 2;
		
		this.animFrame = 0;
		
		// added LANDED event handler
		this.landed = this.onLanded;
	}
	
	this.draw = function( domParent )
	{
		var tile = document.createElement( "div" );
		tile.style.position = "absolute";
		tile.style.width = this.width + "px";
		tile.style.height = this.height + "px";
		
		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";
		tile.style.backgroundPosition = "-" + ((this.width) + (this.width * this.direction)) + "px 0px";
		
		tile.style.overflow = "hidden";
		
		this.domRef = tile;
		domParent.appendChild( tile );
	}
	
	this.jump = function(){	
		this.jumpspeed = this.jumpspeed + this.gravity;
		if ( this.jumpspeed > this.height - 1 ){
			this.jumpspeed = this.height - 1;
		}
		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; // Changed to one-hit-and-your-dead.
		this.health = -1;
		if ( this.health < 0 )
		{
			this.setState( CHAR_DYING );
		}
		else
		{
			this.setHealthBar();
		}
	}
	
	this.setHealthBar = function(){
		//var bar = document.getElementById( "health" );
		//bar.style.height = this.health;
	}
	
	this.checkInput = function(){
		if ( key[ 4 ] && ! this.jumping ){
			// Space	
			this.jumping = true;
			this.falling = false;
			this.jumpspeed = this.jumpstart;	
			
			this.domRef.style.backgroundPosition = "-" + (this.width * this.direction) + "px 0px";
			
		}

		if ( key[ KEY.left ] )
		{
			if (!key[ KEY.right ]) 
			{
				this.direction = 2;
				if ( ! this.jumping )
				{
					this.domRef.style.backgroundPosition = "-" + ( (this.animFrame++ % 2 * this.width) + ( this.width * this.direction) ) + "px 0px";
				}
				this.moveChar( -1, 0, 0 );
			}
		}
		else if (key[ KEY.right ]) 
		{
			this.direction = 0;
			if ( ! this.jumping )
			{
				this.domRef.style.backgroundPosition = "-" + ( (this.animFrame++ % 2 * this.width) + ( this.width * this.direction) ) + "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 );
				//this.setHealthBar();
				break;
			case CHAR_RUN:
				this.checkInput();		
				if ( this.jumping || this.falling ){
					this.jump();
				}
				break;
			case CHAR_DYING:
				if ( this.stateFrame == 0 ){
					this.stateLength = 20;
				}
				if ( this.stateFrame % 2 == 0 ){
					this.domRef.style.opacity = 0;
				}
				else{
					this.domRef.style.opacity = 1;
				}
				if ( this.stateFrame++ == this.stateLength ){
					this.domRef.style.opacity = 1;//border = "";
					this.setState( CHAR_DEAD );
				}
				break;
			case CHAR_DEAD:
				this.lives--;
				if ( this.lives >= 0 ){	
					document.getElementById("p" + ( this.lives + 1 ) ).style.backgroundPosition = "-60px -20px";
					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++ ){
			var currentItem = game.currentMapItems[ i ];
			if ( currentItem.x == this.xTile && currentItem.y == this.yTile ){
				var removeItem = true;
				// Add score
				switch( currentItem.type ){
					
					case item_types.COIN:
						this.score += 150;
						break;
					case item_types.KEY:
						this.score += 400;
						this.hasKey++;
						break;
					case item_types.FINAL_DOOR:
						if( this.hasKey == 2 ){
							// Make the door opened!
							game.tiles.tiles[ g_maps[game.currentMap][ this.yTile + 1 ][ this.xTile ] ].walkable = true;
						}
						else{
							removeItem = false;
						}
						break;
					case item_types.FINAL_PRIZE:
						this.score += 1000;
						game.setState( state_win );
					default:
						break;
				}
				
				$("playerScore").innerHTML = this.score;
				
				if( removeItem ){
					currentItem.got();
					currentItem.erase( game.domRef );
					game.currentMapItems.remove( currentItem );
				}
			}
		}

	}
	
	this.onLanded = function()
	{
		// Set animation fram to standing
		this.domRef.style.backgroundPosition = "-" + (this.width + (this.width * this.direction)) + "px 0px";
	}
	
	this.constructor();
}

