(function(){

    var Button = function(game, x, y, w, h, text, color, noshadow, radio){
        var self = this;

        self.radio = radio || 6;

        self.w = w;
        self.h = h;

        var fontstyle = {fontSize: h/3, font: "Arial", fill: "#000000", align: "center", fontWeight: "bolder", fontStyle: "normal"};

        Phaser.Graphics.call(self, game, w, h);

        self.isMouseOver = false;

        // draw shadow rect
        if(!noshadow) {
            self.lineStyle(0);
            self.beginFill(0x000000, 1);
            self.drawRoundedRect(6, 6, self.w, self.h, self.radio);
            self.endFill();
        }

        // draw rounded rect
        self.setBgColor(color);

        self.text = new Phaser.Text(game, 0, 0, text, fontstyle);
        self.addChild(self.text);

        self.text.x = self.width/2;
        self.text.y = self.height/2;
        self.text.anchor.set(0.5, 0.5);

        self.x = x;
        self.y = y;
        self.inputEnabled = true;


        //eventos
        var originalColor;

        //cambio el color de la fuente en el mouse down
        self.events.onInputDown.add(
            function(){
                originalColor = self.text.style.fill;
                self.setForeColor("#FFFFFF");
            }
        );

        //cambio el color de la fuente en el mouse up
        self.events.onInputUp.add(
            function(){
                self.setForeColor(originalColor);
            }
        );

        //mouse over
        self.events.onInputOver.add(
            function(){
                self.isMouseOver = true;
                originalColor = self.text.style.fill;
            }
        );

        //mouse out
        self.events.onInputOut.add(
            function(){
                self.isMouseOver = false;
                self.setForeColor(originalColor);
            }
        );

        game.world.add(self);

        return self;
    }

    var p = Button.prototype = Object.create(Phaser.Graphics.prototype);

    p.setBgColor = function(color){
        var self = this;
        self.lineStyle(0);
        self.beginFill(color, 1);
        self.drawRoundedRect(0, 0, self.w, self.h, self.radio);
        self.endFill();
    }

    p.setForeColor = function(color){
        var self = this;
        var f = self.text.style;
        f.fill = color;
        self.text.setStyle(f);
    }

    window.Button = Button;
})();