    var self = this;
    var iterator = 0;
    var PodCounter;
    var DodCounter;
    var variant1 = ['1_3', '2_2', '3_2', '4_2', '5_3', '6_1', '7_1', '8_1', '9_3'];
    var definicja_audio;
    var inDefinition;
    var definition_text;
    var krzak;
    var SkyTable = [];
    var cursors;
    var playertween;
    var bgtween;
    var bgtween2;
    var player;
    var NPC;
    var NPCtween;
    var PytanieText;
    var SelectedButton;
    var picture;

    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;
        var SavedLocation;
        var item;
        var ZmiennaMieszajaca;
        var ButtonList = [];
        var GameIsBusy = false;
        var info;
        var left = false;
        var right = false;
    };

    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;
    }
    function UpTheHill() {
        playertween.to({
            x: 1100,
            y: 700
        }, 3000, Phaser.Easing.Sinusoidal.InOut, true, 0, 0);
        playertween.onComplete.add(HillTalk, this);
    }
    function HillTalk() {
        playertween.stop();
        playertween.pause();
        player.animations.play('idle');
        intro = true;
        lektor.play();
        this.destroyIntro();
        this.SpawnBlocks();
        this.showInstruction();
    }
    Game1.Game.prototype = {
        create: function () {
            cursors = this.input.keyboard.createCursorKeys();
            PodCounter = 0;
            DodCounter = 0;
            tween = null;
            GameIsBusy = false;
            ButtonList = [];
            ItemInputList = [];
            PodItemList = [];
            DodItemList = [];
            ZmiennaMieszajaca = false;
            calc_value = "";
            input = 0;
            shoppinglist = "";
            shoppingcart = [];
            var SavedLocation = null;
            var LocationIsSet;
            this.LocationIsSet = false;
            shuffle(variant1);
            lektor = new Audio("./sound/ZIP_g_I_3_2.mp3");
            lektor.load();
            definicja_audio = new Audio("./sound/ZIP_g_I_3_1.mp3");
            definicja_audio.load();
            sound = new Audio();
            equationTable = [4, 2, 1, 10, 3, 2, 5, 9, 9, 5, 0, 10];
            playedEquation = 0;
            isReadyToRead = false;
            intro = false;
            ItemList = "";
            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.buttonHelp = this.game.add.sprite(1768, 100 / 2, 'help', 0);
            this.buttonHelp.scale.setTo(0.8, 0.8);
            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);  
            inDefinition = 0;
            var item;
            var selectexbox1 = null;
            var selectexbox2 = null;
            var TotalPoints = 0;
            var equal;
            var j;
            PytanieText = this.game.add.text(200, 850, '', {
                font: "56px Arial",
                fill: "#000000",
            });  
            this.showDefinition();
        },
        over: function (button, pointer) {
            playertween = this.game.add.tween(player);
            playertween.to({
                x: 1000
            }, 4000);
            playertween.onComplete.add(UpTheHill, this);
            player.animations.play('right');
            playertween.start();
            bgtween.start();
        },
        createIntro: function () {
            this.introBG = this.game.add.sprite(0, 0, 'bg_ingame0');
            this.info = this.game.add.sprite(711, 888, 'info');
            this.info.inputEnabled = true;
            this.info.events.onInputDown.add(this.over, this);

            for (var i = 1; i < 10; i++) {
                if (i < 4) {
                    this.sky = this.game.add.sprite((Math.random() * (-1000 * i)), 0 + (100 * (i - 1)), 'sky_' + (i));
                }
                if (i >= 4 && i < 7) {
                    this.sky = this.game.add.sprite(600 + (Math.random() * (200 * i)), 0 + (100 * ((i - 3) - 1)), 'sky_' + ((i - 3)));
                }
                if (i >= 7) {
                    this.sky = this.game.add.sprite((Math.random() * (-1000 * (i - 6))), 0 + (100 * ((i - 6) - 1)), 'sky_' + (i - 6));
                }
                SkyTable.push(this.sky);
            }

            this.game.add.tween(SkyTable[0]).to({
                x: this.game.world.width
            }, 17676, Phaser.Easing.Linear.None, true, 0, -1);
            this.game.add.tween(SkyTable[1]).to({
                x: this.game.world.width
            }, 18630, Phaser.Easing.Linear.None, true, 0, -1);
            this.game.add.tween(SkyTable[2]).to({
                x: this.game.world.width
            }, 19760, Phaser.Easing.Linear.None, true, 0, -1);
            this.game.add.tween(SkyTable[6]).to({
                x: (this.game.world.width)
            }, 17676, Phaser.Easing.Linear.None, true, 9000, -1);
            this.game.add.tween(SkyTable[7]).to({
                x: (this.game.world.width)
            }, 18630, Phaser.Easing.Linear.None, true, 9000, -1);
            this.game.add.tween(SkyTable[8]).to({
                x: (this.game.world.width)
            }, 19760, Phaser.Easing.Linear.None, true, 9000, -1);
            this.game.add.tween(SkyTable[3]).to({
                x: (this.game.world.width + 1000)
            }, 17676, Phaser.Easing.Linear.None, true, 0, 0);
            this.game.add.tween(SkyTable[4]).to({
                x: (this.game.world.width + 1000)
            }, 18630, Phaser.Easing.Linear.None, true, 0, 0);
            this.game.add.tween(SkyTable[5]).to({
                x: (this.game.world.width + 1000)
            }, 19760, Phaser.Easing.Linear.None, true, 0, 0);
            player = this.add.sprite(100, 750, 'gracz');
            this.physics.enable(player, Phaser.Physics.ARCADE);
            player.body.collideWorldBounds = true;
            player.body.setSize(115, 210, 0, 0);
            player.animations.add('left', [0, 1], 2, true);
            player.animations.add('right', [0, 1], 2, true);
            player.animations.add('idle', [0], 0, true);
            bgtween = this.game.add.tween(this.introBG).to({
                x: -1000
            }, 4500);
        },
        destroyIntro: function () {
            this.introBG.visible = false;
            this.info.visible = false;
            for (var i = 0; i < SkyTable.length; i++) {
                SkyTable[i].visible = false;
            }
            player.visible = false;
        },
        FinnishIntro: function () {
            PytanieText.setText("");
            for (var i =0; i<ButtonList.length;i++)
            {
                ButtonList[i].visible = false;
            }
            this.introBG.visible = true;
            this.info.visible = true;
            for (var i = 0; i < SkyTable.length; i++) {
                SkyTable[i].visible = true;
            }
            player.visible = true;
        },
        showDefinition: function () {
            inDefinition = 1;
            GameIsBusy = true;
            definicja_audio.play();
            this.guiHelp = this.game.add.sprite(1210, 350, 'guiPomocnik_good');
            this.guiHelp.fixedToCamera = true;
            this.guiBubble = this.game.add.sprite(95, 96, 'Finish');
            this.guiBubble.scale.setTo(1.3, 1.3);
            this.guiBubble.fixedToCamera = true;
            definition_text = this.game.add.text(200, 330, 'Nad stawem przyjemnie jest\nzorganizować piknik.\nJednak zanim zaczniesz posiłek\nodpowiedz na pytania Przewodnika.', {
                font: "46px Arial",
                fill: "#000000",
            });
            definition_text.fixedToCamera = true;
        },
        hideDefinition: function () {
            inDefinition = 0;
            GameIsBusy = false;
            this.guiHelp.visible = false;
            this.guiBubble.visible = false;
            definition_text.visible = false;
            this.createIntro();
        },
        SpawnBlocks: function () {
            this.SpawnItem();
        },
		SpawnItem: function () {
			if (iterator <= 8) {
                this.item = this.game.add.sprite(836, 216, variant1[iterator]);
                this.item.name = variant1[iterator];
                this.item.value = variant1[iterator].slice(-2);
                ItemInputList.push(this.item);
                iterator++;
                this.SpawnButtons();
            }
        },
        SpawnButtons: function () {
            ButtonList = [];
            var arr = [1,2,3];

            for (var i = 0; i < 3; i++) {
                this.item = this.game.add.sprite(330 + (547 * i), 762, "p_" + arr[i]);
                this.item.inputEnabled = true;
                this.item.name = "odpowiedzi";
                this.item.value = arr[i];
                this.item.events.onInputDown.add(this.down, this);
                this.item.events.onInputUp.add(this.up, this);
                ButtonList.push(this.item);
            }
        },
        SaveLocation: function (button, pointer) {
            if (GameIsBusy == false) {
                if (playedEquation < 10) {
                    button.bringToTop();
                }
            }
        },
        up: function (button, pointer) {
        button.frame = 0;
		},
		down2: function (button, pointer) {
			button.frame = 1;
			if (!isPlaying(lektor) || lektor.ended) {
				intro = true;
				this.showInstruction();
				lektor.play();
			}
		},
        StopInput: function () {
            if (ItemInputList.length > 0) {
                for (var index = 0; index < ItemInputList.length; ++index) {
                    ItemInputList[index].inputEnabled = false;
                }
            };
        },
        ReStartInput: function () {
            if (ItemInputList.length > 0) {
                for (var index = 0; index < ItemInputList.length; ++index) {
                    ItemInputList[index].inputEnabled = true;
                }
            };
        },
        showInstruction: function () {
            GameIsBusy = true;
            this.StopInput();
            this.guiHelp = this.game.add.sprite(1210, 350, 'guiPomocnik_good');
            this.guiHelp.fixedToCamera = true;
            this.guiBubble = this.game.add.sprite(135, 96, 'Finish');
            this.guiBubble.scale.setTo(1.2, 1.2);
            this.guiBubble.fixedToCamera = true;
            this.titleText = this.game.add.text(200, 330, 'W jakich trzech stanach skupienia\nwystępuje woda?', {
                font: "40px Arial",
                fill: "#000000",
            });
            this.titleText.fixedToCamera = true;
        },
        hideInstruction: function () {
            GameIsBusy = false;
            this.ReStartInput();
            this.guiHelp.visible = false;
            this.guiBubble.visible = false;
            this.titleText.visible = false;
        },
        playSound: function () {
            sound.pause();
            sound.load();
            sound.loop = false;
            sound.play();
        },
        playWrong: function () { 
           GameIsBusy = true;
            sound.pause();
            sound = new Audio("./sound/z_" + (Math.floor(Math.random() * 4) + 1) + ".mp3");
            sound.load();
            sound.loop = false;
            sound.play();
        },
        playGood: function () {
           GameIsBusy = true;
            sound.pause();
            sound = new Audio("./sound/d_" + (Math.floor(Math.random() * 4) + 1) + ".mp3");
            sound.load();
            sound.loop = false;
            sound.play();
        },
        showFinnish: function () {
            playedEquation = 13;
        }, 
        hideFinnish: function () {
            sound.pause();
            this.ReStartInput(); 
            this.guiBack.kill();
            this.guiBg.kill();
            this.guiBoo.kill();
            this.guiHelp.kill();
            this.guiBubble.kill();
            this.guiBasket.kill();
            this.guiCounter.kill();
            this.guiText.setText('');
            text.setText('');
        },
        down: function (button, pointer) {
            button.frame = 1;
            if (button.name == "odpowiedzi") {
                if (button.value == (ItemInputList[0].value.slice(-1)) || button.value == (ItemInputList[0].value.slice(-2,-1) )) {
                    if (iterator <= 8) {
                        this.playGood();
                    }
                    ItemInputList[0].destroy();
                    ItemInputList.pop();
                    if (ButtonList.length > 0) {
                        
						for (var index = 0; index < ButtonList.length; ++index) {
                            ButtonList[index].kill();
                        }
                    
					}
                    this.SpawnItem();
                    playedEquation++;
                } else this.playWrong();
            }
        },
        update: function () {
			if ((!isPlaying(definicja_audio) || definicja_audio.ended) && inDefinition == 1) {
                this.hideDefinition();
            }
            if (tween != null)
                tween.onComplete.removeAll();
            if (ZmiennaMieszajaca) {
                playedEquation == 10;
            }
            if (playedEquation == 9 && (!isPlaying(sound) || sound.ended)) {
                sound.pause();
                playedEquation = 13;
            }
            if (playedEquation == 13 && (!isPlaying(sound) || sound.ended)) {
                this.state.start('Game1');
            }
            if (playedEquation < 12) {
                if ((!isPlaying(lektor) || lektor.ended) && intro) {
                    intro = false;
                    this.hideInstruction();
                    isReadyToRead = true;
                }
            }
            if (playedEquation == 14) {
                this.state.start('Game1');
            }
        },
        render: function () {
        }
    };