
function Player()
{
    var startTod = document.getElementById('startTod');
    
    this.x = 250;
    this.y = 10;
    
    if(startTod) {
        
        var pos = collisions.getColBox(startTod);
        
        this.x = pos.x;
        this.y = pos.y;
    }
    
    this.dy = 1;
    this.dir = 0;
    
    this.anims = {};
    
    for(k in Animation.animations.Angel) {
        
        this.anims[k] = new Animation(Animation.animations.Angel[k].frames);
    }
    
    this.curAnim = k;
    this.hitGround = false;
    this.playerWidth = this.anims[this.curAnim].getCurFrame().width;
    this.playerHeight = this.anims[this.curAnim].getCurFrame().height;
    
    this.label = document.createElement('div');
    
    this.label.className = 'swbig';
    this.label.style.position = 'absolute';
    this.label.style.top = this.y + 'px';
    this.label.style.left = this.x + 'px';
    this.label.style.width = this.playerWidth + 'px';
    this.label.style.marginTop = "-1.25em";
    this.label.style.textAlign = 'center';
    
    this.label.appendChild(document.createTextNode("Tod"));
    
    document.body.appendChild(this.label);
}

Player.prototype.update = function()
{
    var l = engine.key[37];
    var r = engine.key[39];
    var u = engine.key[38];
    
    var hideme = null;

    var maxy = engine.getHeight() - (this.playerHeight) + engine.getScroll();
    var maxx = engine.getWidth() - (this.playerWidth);
    var minx = 0;
    var miny = 0;
    
    this.label.style.top = this.y + 'px';
    this.label.style.left = this.x + 'px';
    
    var oldAnim = this.curAnim;
    
    if(!this.hitGround) {
    
        if(this.dy < -.3) {
            
            this.curAnim = this.dir ? 'up_right' : 'up_left';
        }
        else if(this.dy > .3) {
            
            this.curAnim = this.dir ? 'down_right' : 'down_left';
        }
        else {
            
            this.curAnim = this.dir ? 'hover_right' : 'hover_left';
        }
    }
    else {
        
        this.dy = 0;
        
        if(r) {
            this.curAnim = 'walk_right';
            this.dir = 1;
        }
        else if(l) {
            this.curAnim = 'walk_left';
            this.dir = 0;
        }
        else {
            if(this.dir)
                this.curAnim = 'walk_right';
            else
                this.curAnim = 'walk_left';
        }
    }
    
    if(oldAnim != this.curAnim)
        hideme = this.anims[oldAnim].getCurFrame();
    
    var anim = this.anims[this.curAnim];
    
    anim.update();
    
    if(l) {
        
        var newx = this.x - 2;
        
        //if(!collisions.isSolid(newx, this.y, this.playerWidth, this.playerHeight))
            this.x = newx;
        
        this.dir = 0;
    }
    
    if(r) {
        
        var newx = this.x + 2;
        
        //if(!collisions.isSolid(newx, this.y, this.playerWidth, this.playerHeight))
            this.x = newx;
        
        this.dir = 1;
    }
    
    if(this.dy < 12)
        this.dy += .01;

    if(u && !this.oldU)
        this.dy = -1;
    
    this.oldU = u;
    
    var newy = this.y + this.dy;
    
    if(newy < miny)
        newy = miny;
    
    if(newy != this.y) {
        
        this.hitGround = false;
        
        var coly = newy + this.playerHeight * 9 / 10;
        var colh = this.playerHeight * 1 / 10;
        
        if(newy > maxy)
            this.hitGround = true;
        else if(this.dy > 0 && collisions.isSolid(this.x, coly, this.playerWidth, colh))
            this.hitGround = true;
        else
            this.y = newy;
    }
    
    if((l || r) && !anim.running)
        anim.start();
    if(!l && !r && anim.running)
        anim.stop();
    
    if(this.x > maxx)
        this.x = maxx;
    
    if(this.x < minx)
        this.x = minx;
    
    anim.getCurFrame().moveTo(this.x, this.y);
    anim.getCurFrame().display();
    
    if(hideme != null)
        hideme.hide();
}
