/**
* MooTris
* An implementation of the popular computer game 'Tetris' using the
* MooTools JavaScript framework.
*
* This file consists of the Block class.
*
* @author  Alan Shaw
* @link    http://www.freestyle-developments.co.uk
* @version 1.0
* @package uk.co.fsd.Tetris
*/

/**
* Represents a block, and manages the block DOM element
*
* This class is responsible for setting block styles, creating the block
* DOM element and managing its position on the screen.
*
* @package uk.co.fsd.Tetris
*/
var Block = new Class({

    x: 0, // x position in array
    y: 0, // y position in array
    el: null,
    injected: false,
    options: {},
    
    /**
    * Constructor which creates the DOM block element, sets its styles and
    * classes.
    *
    * Each block is given the class 'block', and depending on the type value
    * passed, is also given a class representing the shape it came from. This
    * class could be one of: 'I', 'J', 'L', 'O', 'S', 'T', 'Z'
    * @param integer type shape type # used to set block style class
    * @param integer x the x coordinate of this block
    * @param integer y the y coordinate of this block
    * @param object options the game options
    */
    initialize: function(type, x, y, options) {

        this.x = x;
        this.y = y;
        this.options = options;
        
        this.el = new Element('div');
        
        // Add generic block class
        this.el.addClass('block');
        
        // Determine the CSS class name for this block from the shape type
        switch(type) {
            case 0:
                this.el.addClass('I');
            break;
            case 1:
                this.el.addClass('J');
            break;
            case 2:
                this.el.addClass('L');
            break;
            case 3:
                this.el.addClass('O');
            break;
            case 4:
                this.el.addClass('S');
            break;
            case 5:
                this.el.addClass('T');
            break;
            case 6:
                this.el.addClass('Z');
            break;
            default:
                this.el.addClass('unknown');
            break;
        }

        this.el.setStyles({
            'z-index':138,
            'position':'absolute',
            'width':this.options.size+'px',
            'height':this.options.size+'px'
        });
    },
    
    /**
    * Draws the block within the given DOM element.
    * @param object grid the DOM element to draw the box in (the grid)
    */
    draw: function(grid) {

        if(this.el != null) {

            var gridCoords = grid.getCoordinates();
            var top;
            var left;
    
            // Get coords of the block in relation to the grid
            top = gridCoords.top + (this.y * this.options.size);
            left = gridCoords.left + (this.x * this.options.size);
    
            this.el.setStyles({
                'top':top+'px',
                'left':left+'px'
            });
    
            if(this.injected == false) {
                this.el.injectInside(grid);
                this.injected = true;
            }
        }
    },
    
    /**
    * Removes the DOM element representing the block from the document.
    */
    erase: function() {
        this.el.remove();
        this.el = null;
    }

});

Block.implement(new Options);
