Game1.Game = function (game) {
    var buttonLeft;
    var buttonRight;
    var int = 0;
    var spriteList = new Array();
    var selectedbox1;
    var selectedbox2;
    var equationTable;
    var playedEquation;
    var isReadyToRead;
    var intro;
    var input = 0;
    var calc_value;
    var text ;
};
function isPlaying(audelem) {
    return !audelem.paused;
}
function shuffle(array) {
    var currentIndex = array.length,
        temporaryValue, randomIndex;
		
		while (0 !== currentIndex) {
			randomIndex = Math.floor(Math.random() * currentIndex);
			currentIndex -= 1;
			temporaryValue = array[currentIndex];
			array[currentIndex] = array[randomIndex];
			array[randomIndex] = temporaryValue;
		}
		
    return array;
}
Game1.Game.prototype = {
    create: function () {
        calc_value ="";
        var input = "0";
        sound = new Audio();
        equationTable = [4, 2, 1, 10, 3, 2, 5, 9, 9, 5, 0, 10];
        playedEquation = 0;
        isReadyToRead = false;
        intro = true;
        this.bg = this.game.add.sprite(0, 0, 'bg_ingame');
        this.game.stage.backgroundColor = '#ffffff';
        this.game.physics.startSystem(Phaser.Physics.ARCADE);
        this.time.advancedTiming = true;
        this.calc = this.game.add.sprite(1390, 243, "calc_bg")
        this.buttonHelp = this.game.add.sprite((1920 - 150 / 2 - 132), 100 / 2, 'help', 0);
        this.buttonHelp.scale.setTo(0.9, 0.9);
        this.buttonHelp.name = "Help";
        this.buttonHelp.inputEnabled = true;
        this.buttonHelp.fixedToCamera = true;
        this.buttonHelp.events.onInputDown.add(this.down2, this);
        this.buttonHelp.events.onInputUp.add(this.up, this);
        var item;
        var selectexbox1 = null;
        var selectexbox2 = null;
        var TotalPoints = 0;
        var equal;
        var j;

        for (var i = 0; i < 12; i++) {
            if (i < 3) {
                this.item = this.game.add.sprite(1424 + (152 * (i % 3)), 454 + (105 * (0)), 'calc_' + i, 0);
            }
            if (i >= 3 && i < 6) {
                this.item = this.game.add.sprite(1424 + (152 * (i % 3)), 454 + (105 * (1)), 'calc_' + i, 0);
            }
            if (i >= 6 && i < 9) {
                this.item = this.game.add.sprite(1424 + (152 * (i % 3)), 454 + (105 * (2)), 'calc_' + i, 0);
            }
            if (i == 9) {
                this.item = this.game.add.sprite(1424 + (152 * (0)), 454 + (105 * (3)), 'calc_' + i, 0);
            }
            if (i == 10) {
                this.item = this.game.add.sprite(1424 + (152 * (1)), 454 + (105 * (3)), 'calc_' + i, 0);
            }
            if (i == 11) {
                this.item = this.game.add.sprite(1424 + (152 * (2)), 454 + (105 * (3)), 'calc_' + i, 0);
            }
            this.game.physics.arcade.enable(this.item);
            this.item.inputEnabled = true;
            this.item.name = 'kalkulator';
            this.item.events.onInputDown.add(this.down, this);
            if (i == 11) {
                this.item.value = "=";
            } else if (i == 10) {
                this.item.value = "c";
            } else if (i == 9) {
                this.item.value = 0;
            } else {
                this.item.value = i + 1;
            }
        }
        this.item = this.game.add.sprite(247, 73, '1');
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true;
        this.item.name = 'rownanie';
        this.item.value = '18';
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(406, 73, '2');
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true;
        this.item.name = 'rownanie';
        this.item.value = '22';
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(727, 73, '3');
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true;
        this.item.name = 'rownanie';
        this.item.value = '13';
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(982, 73, '4');
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true;
        this.item.name = 'rownanie';
        this.item.value = '9';
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(247, 344, '5');
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true;
        this.item.name = 'rownanie';
        this.item.value = '4';
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(402, 345, '6');
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true;
        this.item.name = 'rownanie';
        this.item.value = '56';
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(722, 282, '7');
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true;
        this.item.name = 'rownanie';
        this.item.value = '24';
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(921, 342, '8');
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true;
        this.item.name = 'rownanie';
        this.item.value = '21';
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(247, 606, '9');
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true;
        this.item.name = 'rownanie';
        this.item.value = '7';
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(477, 545, '10');
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true;
        this.item.name = 'rownanie';
        this.item.value = '5';
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(655, 522, '11');
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true;
        this.item.name = 'rownanie';
        this.item.value = '40';
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(915, 593, '12');
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true;
        this.item.name = 'rownanie';
        this.item.value = '52';
        this.item.events.onInputDown.add(this.down, this);
        text = this.game.add.bitmapText(1430, 330, 'font_0', input, 90);
        this.showInstruction();                  
    },
    up: function (button, pointer) {
        button.frame = 0;
    },
    down2: function (button, pointer) {
        button.frame = 1;
        if (button.name == 'Speaker') {
        } else {
            intro = true;
            this.showInstruction();
        }
    },
    showInstruction: function () {
        this.guiHelp = this.game.add.sprite(1410, 590, 'guiPomocnik');
        this.guiHelp.fixedToCamera = true;
        this.guiBubble = this.game.add.sprite(135, 90, 'Finish');
        this.guiBubble.fixedToCamera = true;
        this.titleText = this.game.add.text(250, 400, 'Wykonaj działania i odkryj,\nco kryje się pod puzzlami.\n\nKażde dobrze rozwiązane działanie\nodkrywa jeden puzzel.', {
            font: "58px Arial",
            fill: "#000000",
        });
        this.titleText.fixedToCamera = true;
        this.game.time.events.add(Phaser.Timer.SECOND * (5), function () {
            try {
                this.guiHelp.visible = false;
				this.guiBubble.visible = false;
				this.titleText.visible = false;  
			} catch (e) {}
        }, this);
    },
    hideInstruction: function () {
        this.guiHelp.visible = false;
        this.guiBubble.visible = false;
        this.titleText.visible = false;
    },
    stopDrag: function () {
        this.sprite.body.moves = true;
    },
    playSound: function () {
        sound.load();
        sound.loop = false;
        sound.play();
    },
    playWrong: function () {
        sound = new Audio("./sound/err.mp3");
        sound.load();
        sound.loop = false;
        sound.play();
    },
    playGood: function () {
        sound.pause();
        sound = new Audio("./sound/win.mp3");
        sound.load();
        sound.loop = false;
        sound.play();
    },
    showFinnish: function () {
        this.guiHelp = this.game.add.sprite(1410, 590, 'guiPomocnik');
        this.guiHelp.fixedToCamera = true;
        this.guiBubble = this.game.add.sprite(135, 90, 'Finish');
        this.guiBubble.fixedToCamera = true;
        this.titleText = this.game.add.text(250, 400, 'Gratulacje!', {
            font: "58px Arial",
            fill: "#000000",
        });
        this.titleText.fixedToCamera = true;
        this.game.time.events.add(Phaser.Timer.SECOND * (5), function () {
            try {
                this.guiHelp.visible = false;
				this.guiBubble.visible = false;
				this.titleText.visible = false;  
				location.reload();
			} catch (e) {}
        }, this);
    },
    hideFinnish: function () {
    },
    down: function (button, pointer) {
        if (this.selectexbox1 == null) {
            if (button.name == 'rownanie') {
                this.selectexbox1 = button;
                this.selectexbox1.tint = 0xddffa1;
            }
        }
        if (this.selectexbox1 != null) {
            if (button.name == "kalkulator") {
                if (button.value == "c") {
                    console.log("czyszczenie");
                    calc_value ="";
                    input = "";
                    text.setText(input);
                } else if (button.value == "=") {
                    if (calc_value == this.selectexbox1.value) {
                       this.fadePicture(this.selectexbox1);
                        calc_value ="";
						input = "";
						text.setText(input);
                        this.selectexbox1 = null;
                        playedEquation++;
                        this.playGood();
                        if( playedEquation == 12)
                        {
                            this.showFinnish();
                        }
                        isReadyToRead = true;
                    } else {
                        this.playWrong();
						calc_value ="";
						input = "";
						text.setText(input);
                    }
                } else {
                    this.AddDigit(button.value);
                }
            } else if (button.name == 'rownanie') {
                this.selectexbox1.tint = 0xffffff;
                this.selectexbox1 = button;
                this.selectexbox1.tint = 0xddffa1;
            }
        }
    },
	AddDigit : function(string)
    {
		{
			if (calc_value.length > 6)
			{ 
			} else
			{ 
				if ((eval(calc_value) == 0) && (calc_value.indexOf(".") == -1))
				{ 
					calc_value = string;
				} else
				{ 
					calc_value = calc_value + string;
				};
			}; 
		};
		input = calc_value;
		text.setText(input);    
    },
    fadePicture: function (button) {
        var tween = this.game.add.tween(button).to({
            alpha: 0
        }, 2000, Phaser.Easing.Linear.None, true);
    },
    update: function () {
    },
    render: function () {
    }
};