/*

Quicktetris published by Thomas Kjeldahl Nilsson under 
Creative Commons Attribution 3.0 license.
License terms: http://creativecommons.org/licenses/by/3.0/

*/

// Graphics "paint" functions
var Graphics = {

    getGameContainer: function() {
        return document.getElementById("gameContainer");
    },

    clearGameContainer: function() {
        var node = this.getGameContainer();

        if (node.hasChildNodes()) {
            while (node.childNodes.length >= 1) {
                node.removeChild(node.firstChild);
            }
        }
    },

    removeNodeFromGameContainer: function(node) {
        try {
            this.getGameContainer().removeChild(node);
        }
        catch(err) {
            // If no such node, fine. 
        }
    },

    createRectangleDiv: function(bgcolor, x, y, width, height, zIndex) {
        var rect = document.createElement('div');

        rect.style.position = "absolute";
        rect.style.top = y + "px";
        rect.style.left = x + "px";
        rect.style.zIndex = "0";
        rect.style.height = height + "px";
        rect.style.width = width + "px";
        rect.style.backgroundColor = bgcolor;

        this.getGameContainer().appendChild(rect);

        return rect;
    },

   
    preloadBackground: function() {
	this.createFieldBackground("white", 0, 0, 400, 400).style.visibility = "hidden";
    },
	

    createFieldBackground: function(bgcolor, x, y, width, height) {
        var rect = document.createElement('div');

        rect.style.position = "absolute";
        rect.style.top = y + "px";
        rect.style.left = x + "px";
        rect.style.zIndex = "-1";
        rect.style.height = height + "px";
        rect.style.width = width + "px";
        rect.style.backgroundColor = bgcolor;

        rect.style.backgroundImage = "url('assets/images/gameplayScreen.png')";
        rect.style.backgroundRepeat = "no-repeat";
        rect.style.backgroundPosition = "0px 0px";

        this.getGameContainer().appendChild(rect);

        return rect;
    },

    drawnString: null,
    drawString: function(text, x, y) {

        if (this.drawnString) {
            this.removeNodeFromGameContainer(drawnString);
        }

        drawnString = document.createElement('div');
        drawnString.style.top = y + "px";
        drawnString.style.left = x + "px";
        var txtNode = document.createTextNode(text);
        drawnString.appendChild(txtNode);

        this.getGameContainer().appendChild(drawnString);
    }

};