﻿function Header(holder)
{
    this.holder = holder;
    EventCenter.addListener(this, Events.SCORES, "updateScore");
    this.init();
}

Header.prototype.init = function()
{
    this.header = this.holder.appendChild(enTag('div', '', {id:"gameHeader"}));
    this.score = this.header.appendChild(enTag('span', '', {id:"score"}));
    this.highScore = this.header.appendChild(enTag('span', '', {id:"highScore"}));
    this.lives = this.header.appendChild(enTag('div', '', {id:"lives"}));
}

Header.prototype.updateScore = function(evt)
{
    var score = evt.data.score;
    var highScore = evt.data.highScore;
    var lives = evt.data.lives;
    
    
    this.score.innerHTML = "score: " + score;
    this.highScore.innerHTML = "highScore: " + highScore;
   // trace(this.lifeCount + " " +  lives);
    if(this.lifeCount != lives) this.buildLives(lives);
    this.lifeCount = (!this.lifeCount) ? lives : this.lifeCount;
}

Header.prototype.buildLives = function(num)
{
    this.lifeCount = num;
    this.lives.innerHTML = '';
    //oTrace(this.lives.childNodes);
    /*for(var i in this.lives.childNodes)
    { 
    //trace(typeof this.lives.childNodes[i] + " || " + i);
        if(typeof this.lives.childNodes[i] == 'object')
        {
        trace("removed");
            this.lives.removeChild(this.lives.childNodes[i]);
        }
        
    }
    
    var kids = this.lives.childNodes;
    for(var j = kids.length; j > 0; j--)
    {
        if(typeof this.lives.childNodes[i] == 'object')
        {
            this.lives.removeChild(kids[j]);
        }
    }*/
    for(var i = 0; i < num; i++)
    {
        var tmp = document.createElement('div');
        tmp.id = "__life__" + i;
        with(tmp.style)
        {
            height = Settings.SHIP_HEIGHT + "px";
            width = Settings.SHIP_WIDTH + "px";
            background = Settings.SHIP_IMAGE || "#444444";
            marginLeft = "10px";
            border = "1px solid #FFFF00";
        }
        this.lives.appendChild(tmp);
    }
}

Header.prototype.die = function()
{
    this.header.innerHTML = '';
}
