﻿Game.prototype = new BasePage();
Game.prototype.constructor = Game;

function Game(pageID)
{
    BasePage.call(this, pageID);
    this.ID = pageID;
}

Game.prototype.setContent = function()
{
    var n = this.holder;
    document.body.appendChild(n);
    var gm = this.game = document.createElement('div');
    document.body.appendChild(gm);
    gm.id = "gameHolder";
    EventCenter.addListener(this, Events.BOUNDARY_RESETS, "centerPage");
    EventCenter.addListener(this, Events.GAME_OVER, "closePage");
    
    with(gm.style)
    {
        position = "absolute";
        top = Settings.GAME_Y_INSET + "px";
        left = (document.body.offsetWidth - Settings.GAME_WIDTH) / 2 + "px";
        background = "#EEEEEE";
        width = Settings.GAME_WIDTH + "px";
        height = Settings.GAME_HEIGHT + "px";
        border = "1px solid #000000";
        display = "none";
    }
    
}

Game.prototype.openPage = function()
{
    this.closing = false;
    this.holder.style.display = "block";
    this.game.style.display = "block";
    Boundary.setBounds(this.game);
    this.timer = new Timer();
    this.header = new Header(this.game);
    this.ship = new Ship(this.game);
    this.shieldHolder = new ShieldHolder(this.game);
    this.enemyHolder = new EnemyHolder(this.game);
    this.levelManager = new LevelManager();
    
    window.onresize = function()
    {
        Boundary.reset();
    }
}

Game.prototype.closePage = function()
{
    if(!this.closing)
    {
        this.closing = true;
        for(var i in Bullet.bullets)
        {
            Bullet.bullets[i].die();
        }
        this.shieldHolder.die();
        this.enemyHolder.die();
        this.timer.pause();
        this.header.die();
        this.game.style.display = "none";
       // this.game.innerHTML = '';
        this.holder.style.display = "none";
        EventCenter.broadcast(Events.OPEN_PAGE, Pages.GAME_OVER);
    }
}

Game.prototype.centerPage = function(evt)
{

    //this.game.style.left = (document.body.offsetWidth - Settings.GAME_WIDTH) / 2 + "px";
}

