var game;
var gameManager;
window.onload = function(){
    //_inmobi.getNewAd(document.getElementById("ad-frame"));

	game = new Game(gravis.WINDOW_WIDTH,gravis.WINDOW_HEIGHT);
	game.fps = 30;
	game.preload(
        gravis.DATA_LIST
    );

	game.onload = function(){
        //local data load
        localforage.getItem("save", function(data){
            if( data != null ) {
                gravis.Utility.savedata = JSON.parse(data);
            }
            //game.rootScene.backgroundColor = "black";
            gameManager = new gravis.GameManager();
            game.rootScene.addChild(gameManager);

            game.rootScene.addEventListener("enterframe", function() {
                gameManager.proc();
            });
        });
	};
	game.start();
};

gravis.GameManager = enchant.Class.create(enchant.Group, {
    initialize: function() {
        enchant.Group.call(this);

        this.sceneTitle = new gravis.SceneTitle();
        this.sceneGame = new gravis.SceneGame();

        this.addChild(this.sceneTitle);

        this.appState = gravis.GameManager.APPSTATE_TITLE;
    },

    proc: function() {
        switch(this.appState) {
            case gravis.GameManager.APPSTATE_TITLE:
                this.sceneTitle.proc();
                this.sceneTitle.draw();
                break;
            case gravis.GameManager.APPSTATE_GAME:
                this.sceneGame.proc();
                this.sceneGame.draw();
                break;
        }
    },

    changeScene: function(nextState) {
        switch(this.appState) {
            case gravis.GameManager.APPSTATE_TITLE:
                this.sceneTitle.end();
                this.removeChild(this.sceneTitle);
                break;
            case gravis.GameManager.APPSTATE_GAME:
                this.sceneGame.end();
                this.removeChild(this.sceneGame);
                break;
        }

        switch(nextState) {
            case gravis.GameManager.APPSTATE_TITLE:
                this.sceneTitle.start();
                this.addChild(this.sceneTitle);
                break;
            case gravis.GameManager.APPSTATE_GAME:
                this.sceneGame.start();
                this.addChild(this.sceneGame);
                break;
        }

        this.appState = nextState;
    }
});

gravis.GameManager.APPSTATE_TITLE = 0;
gravis.GameManager.APPSTATE_GAME = 1;

