/*

Quicktetris published by Thomas Kjeldahl Nilsson under 
Creative Commons Attribution 3.0 license.
License terms: http://creativecommons.org/licenses/by/3.0/

*/

var QuickTetris = {

    gameModes: {
        titleScreen: "titleScreen",
        gamePlay: "gamePlay",
        gameOver: "gameOver"
    },

    dropSpeed: 3,
    lastDifficultyRaiseMS: null,
    gameMode: null,

    getViewPortWidthHeight: function() {
        var viewportWidth;
        var viewportHeight;

        // the more standards compliant browsers (mozilla/netscape/opera/IE7) 
        //   use window.innerWidth and window.innerHeight
        if (typeof window.innerWidth != 'undefined') {
            viewportWidth = window.innerWidth;
            viewportHeight = window.innerHeight;
        }

        // IE6 in standards compliant mode 
        //   (i.e. with a valid doctype as the first line in the document)
        else if (typeof document.documentElement !== 'undefined' && typeof document.documentElement.clientWidth !== 'undefined' && document.documentElement.clientWidth !== 0) {
            viewportWidth = document.documentElement.clientWidth;
            viewportHeight = document.documentElement.clientHeight;
        }

        // older versions of IE
        else {
            viewportWidth = document.getElementsByTagName('body')[0].clientWidth;
            viewportHeight = document.getElementsByTagName('body')[0].clientHeight;
        }

        return [viewportWidth, viewportHeight];
    },

    getFieldCenteredXPos: function() {
        var fieldWidth = Field.WIDTH * Piece.State.tileWidth;

        var viewportDimensions = this.getViewPortWidthHeight();
        var viewportWidth = viewportDimensions[0];

        return ((Math.round(viewportWidth / 2)) - (Math.round(fieldWidth / 2)));
    },

    getCenteredYPos: function() {
        var viewportDimensions = this.getViewPortWidthHeight();
        return viewportDimensions[1];
    },

    gotoTitleScreen: function() {
        this.gameMode = this.gameModes.titleScreen;
        Graphics.clearGameContainer();

	//Graphics.preloadBackground();
	Field.init("white", "white", this.getFieldCenteredXPos(), 50);

        Graphics.drawString("- PRESS SPACE TO START -", 0, 0);

        setKeyReaction(function(keyCode) {
            if (keyCode === SPACE_KEY) {
                QuickTetris.gotoGamePlay();
            }
        });
    },

    gotoGameOver: function() {
        this.gameMode = this.gameModes.gamePlay;
        Graphics.clearGameContainer();

        Graphics.drawString("- PRESS SPACE TO RETRY -", 0, 0);

        setKeyReaction(function(keyCode) {
            if (keyCode === SPACE_KEY) {
                QuickTetris.gotoGamePlay();
            }
        });
    },

    gotoGamePlay: function() {
	Sound.playAmbientMusic();

        Graphics.clearGameContainer();

	Field.init("white", "white", this.getFieldCenteredXPos(), 50);
        Piece.init("white");

        this.dropSpeed = 3;

        setKeyMemory();

	this.gameMode = this.gameModes.gamePlay;
    },

    // Raise speed of pieces every 30 secs
    adjustDifficulty: function() {
        var dateObject = new Date();
        var currentTimeMS = dateObject.getTime();

        if (this.lastDifficultyRaiseMS) {
            if ((currentTimeMS - this.lastDifficultyRaiseMS) >= 30 * 1000) {
                this.dropSpeed = this.dropSpeed + 2;
                this.lastDifficultyRaiseMS = currentTimeMS;
            }
        } else {
            this.lastDifficultyRaiseMS = currentTimeMS;
        }

    },

    // Main game loop
    gameLoop: function() {
        if (QuickTetris.gameMode === QuickTetris.gameModes.gamePlay) {
            QuickTetris.reactToKeyPress(currentKeyPress);
            QuickTetris.adjustDifficulty();
            Piece.moveDown(QuickTetris.dropSpeed);
        }
    },

    reactToKeyPress: function(keyCode) {
        if (currentKeyPress === null) {
            return;
        }

        switch (keyCode) {
        case DIR_KEY_DOWN:
            Piece.moveDown(15);
            break;
        case DIR_KEY_LEFT:
            Piece.moveLeft(Piece.State.tileWidth);
            break;
        case DIR_KEY_RIGHT:
            Piece.moveRight(Piece.State.tileWidth);
            break;
        case SPACE_KEY:
            Piece.rotate(true);
            break;
        }

        //Reset key
        currentKeyPress = null;
    },

    startDefaultGameLoop: function() {

        this.gotoTitleScreen();

        // Launch game loop - set it to fire every X milliseconds
        setInterval(this.gameLoop, 50); // Attempting 20 FPS  
    }

};




