    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;
    var SpeakerButton;

    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 there remain elements to shuffle...
        while (0 !== currentIndex) {

            // Pick a remaining element...
            randomIndex = Math.floor(Math.random() * currentIndex);
            currentIndex -= 1;

            // And swap it with the current element.
            temporaryValue = array[currentIndex];
            array[currentIndex] = array[randomIndex];
            array[randomIndex] = temporaryValue;
        }

        return array;
    }

    function UpTheHill() {
         playertween.stop();
        playertween.pause();
        player.animations.play('idle');
          playedEquation = 14;
       /* playertween.to({
            x: 1200,
          
        }, 1000, Phaser.Easing.Sinusoidal.InOut, true, 0, 0);
        playertween.onComplete.add(HillTalk, this);
*/
        //playertween.start();
    }

    function HillTalk() {
        playertween.stop();
        playertween.pause();
        player.animations.play('idle');
        playedEquation = 14;
        // 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;
            //this.load.BitmapFont('font_0', 'font/font_0_0.png',font_json);
            GameIsBusy = false;
            ButtonList = [];
            ItemInputList = [];
            PodItemList = [];
            DodItemList = [];
            ZmiennaMieszajaca = false;
            calc_value = "";
            input = 0;
            shoppinglist = "";
            shoppingcart = [];
            var SavedLocation = null;
            var LocationIsSet;
            this.LocationIsSet = false;

            
            //console.log(variant1);
            shuffle(variant1);
            //console.log(variant1);
            lektor = new Audio("./sound/ZIP_g_I_2_1.mp3");
            lektor.load();
            definicja_audio = new Audio("./sound/ZIP_g_I_2_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);
            
            SpeakerButton = this.game.add.sprite(50,50,'speaker');
            //SpeakerButton.scale.setTo(0.8, 0.8);
            SpeakerButton.name = "Speaker";
            SpeakerButton.inputEnabled = true;
            SpeakerButton.fixedToCamera = true;
            SpeakerButton.events.onInputDown.add(this.down2, this);
            SpeakerButton.events.onInputUp.add(this.up, this);
            
            inDefinition = 0;

            var item;
            var selectexbox1 = null;
            var selectexbox2 = null;
            var TotalPoints = 0;
            var equal;
            var j;


            //this.SpawnBlocks();
            //this.SpawnItem();


            //  this.createIntro();
            PytanieText = this.game.add.text(200, 850, '', {
                font: "56px Arial",
                fill: "#000000",
            });
            // PytanieText.fixedToCamera = true;          
            this.showDefinition();
            


            // this.showInstruction();
            //  lektor.play();
            // this.info = this.game.add.sprite(811,818,'info');
            // this.info.events.onInputOver.add(this.over, this);


            //this.game.camera.follow(player);

        },

        over: function (button, pointer) {


            playertween = this.game.add.tween(player);
            playertween.to({
                x: 1200
            }, 4000);
            playertween.onComplete.add(UpTheHill, this);
            player.animations.play('right');
            //playertween = this.game.add.tween(player).to({x:1560, y: 200},9000,Phaser.Easing.Sinusoidal.InOut);
            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);
            // this.info.events.onInputOver.add(this.over, this);
            //this.info.events.onInputOver.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].pause();
                SkyTable[i].visible = false;
                //SkyTable[i].destroy();
            }
            player.visible = false;
          //  player.destroy();
         
           // NPC.destroy();
        },

        FinnishIntro: function () {
         //   picture.visible = false;
            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].pause();
                SkyTable[i].visible = true;
                //SkyTable[i].destroy();
            }
            player.visible = true;
          //  player.destroy();
          
           // NPC.destroy();
        },

        showDefinition: function () {
            inDefinition = 1;
            GameIsBusy = true;
            //play.inputEnabled = false;
            // definicja.inputEnabled = false;
            definicja_audio.play();
            //this.StopInput();
            this.guiHelp = this.game.add.sprite(1210, 350, 'guiPomocnik_good');
            //this.guiHelp.scale.setTo(0.8,0.8);
            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, 'Twoim zadaniem jest\nprzejście przez park.', {
                font: "46px Arial",
                fill: "#000000",
            });
            definition_text.fixedToCamera = true;

        },

        hideDefinition: function () {
            inDefinition = 0;
            GameIsBusy = false;
            //play.inputEnabled = true;
           // definicja.inputEnabled = true;
            //this.ReStartInput();
            this.guiHelp.visible = false;
            this.guiBubble.visible = false;
            definition_text.visible = false;
            //this.lektor.hide();
           this.createIntro();
           /* intro = true;
            lektor.play();
            this.SpawnBlocks();
            this.showInstruction();*/
        },

        SpawnBlocks: function () {
          //pierwsza gra. 
           // picture = this.game.add.sprite(287, 60, 'pytanie');
            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);
                //console.log(ItemInputList);

                iterator++;
                this.SpawnButtons();
            }
        },

        SpawnButtons: function () {
            ButtonList = [];
            var arr = [1,2,3];
            
            //console.log(arr);
            //wylaczenie szuflowania tablicy odpowiedzi..
           // shuffle(arr);
            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 (GameIsBusy == false) {
                if (button.name == 'guiBack' || button.name == 'NextButton') {
                    if (button.name == 'guiBack')
                        this.hideFinnish();
                    else if (button.name == 'NextButton')
                        this.showFinnish();
                } else if (button.name == 'Speaker') {
                    if (!isPlaying(sound) || sound.ended)
                        this.playSound();
                } else if (((button.name != 'guiBack') || (button.name != 'Speaker')) && (!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.scale.setTo(0.8,0.8);
            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, '', {
                font: "40px Arial",
                fill: "#000000",
            });
            this.titleText.fixedToCamera = true;
        },

        hideInstruction: function () {
            // this.destroyIntro();
            GameIsBusy = false;
            this.ReStartInput();
            this.guiHelp.visible = false;
            this.guiBubble.visible = false;
            this.titleText.visible = false;
            // this.lektor.hide();
            this.playSound();
        },

        playSound: function () {

            sound.pause();
           // sound = new Audio("./sound/ZIP_g_I_" +iterator+ ".mp3");
            sound.load();
            sound.loop = false;
            sound.play();

        },
        playWrong: function () { //cecha dodatkowa
            // if (!isPlaying(sound)) {
           GameIsBusy = true;
            sound.pause();
            sound = new Audio("./sound/z_" + (Math.floor(Math.random() * 5) + 1) + ".mp3");
            sound.load();
            sound.loop = false;
            sound.play();


            //   }
        },
        playGood: function () { // cecha niezbedna
           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 () {
      /*      this.StopInput();
         //   picture.visible = false;
          //  picture.destroy();
            if (ButtonList.length > 0) {
                for (var index = 0; index < ButtonList.length; ++index) {
                    ButtonList[index].kill();

                }
            }

            //this.guiBg = this.game.add.sprite(0, 0, 'bg_ingame');
           
            this.guiBubble = this.game.add.sprite(174, 85, 'Finish');
            //Ustalenie poprawności wykonania zadania

            this.guiHelp = this.game.add.sprite(1050, 290, 'guiPomocnik_good');
            this.guiText = this.game.add.text(220, 330, 'Brawo! zdobyłeś szczyt.', {
                font: "50px Arial",
                fill: "#000000",
            });
            sound.pause();
            sound = new Audio("./sound/ZIP_g_I_4_7.mp3");
            sound.load();
            sound.loop = false;
            sound.play();
            this.titleText.fixedToCamera = true;*/
            playedEquation = 13;
        },
        
        hideFinnish: function () {
            sound.pause();
            this.ReStartInput();
            //   this.guiHelp.kill();
            //   this.guiBubble.kill();
            //this.upList();    
            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 (GameIsBusy == false) {
                //SelectedButton = button;
                if (button.name == 'definicjaButton') {
                    this.showDefinition();
                }
               if (button.name == "odpowiedzi") {
                 //   console.log(ItemInputList[0].value.slice(-2,-1)+" "+ItemInputList[0].value.slice(-1));
                    if (button.value == (ItemInputList[0].value.slice(-1)) || button.value == (ItemInputList[0].value.slice(-2,-1) )) {
                        if (iterator <= 8) {
                            this.playGood();
                        }
                        //console.log('yeeah');
                        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) {
                // definicja_audio2.play();

                this.hideDefinition();
               // inDefinition = 2;
            }
           /* if (SelectedButton != null) {
                if ((SelectedButton.value  == (ItemInputList[0].value.slice(-1)) || SelectedButton.value == (ItemInputList[0].value.slice(-2,-1) )) && (!isPlaying(sound) || sound.ended)) //Warunek konieczny stworzenia nowych wiązanek
                {
                  //  iterator++;
                    SelectedButton.frame = 0;
                    SelectedButton = null;
                   // playedEquation++;
                    if (playedEquation<8){
                    //this.SpawnBlocks();
                    this.SpawnItem();
                   // this.playSound();
                    }
                    
                    GameIsBusy = false;
                }
                if ((SelectedButton.value  != (ItemInputList[0].value.slice(-1)) || SelectedButton.value != (ItemInputList[0].value.slice(-2,-1) ))  && (!isPlaying(sound) || sound.ended)) //Warunek konieczny stworzenia nowych wiązanek
                {
                    SelectedButton.frame = 0;
                    SelectedButton = null;
                    GameIsBusy = false;
                }
            }
*/

            if (tween != null)
                tween.onComplete.removeAll();
            if (ZmiennaMieszajaca) {
                //this.showFinnish();
                playedEquation == 10;
                // //console.log("timeout!");
                //   this.upList(); 


            }
            if (playedEquation == 9 && (!isPlaying(sound) || sound.ended)) {
                sound.pause();
                playedEquation = 13;
                //this.FinnishIntro();
               // this.showFinnish();
            }
            if (playedEquation == 13 && (!isPlaying(sound) || sound.ended)) {
                this.state.start('Game1');
                //this.hideFinnish();
            }
            if (playedEquation < 12) {
                if ((!isPlaying(lektor) || lektor.ended) && intro) {
                    intro = false;
                    this.hideInstruction();
                    isReadyToRead = true;
                }
                /*            if (isReadyToRead && (!isPlaying(sound) || sound.ended)) {
                isReadyToRead = false;
             //   this.playSound();
            }*/
            }
            if (playedEquation == 14) {
                this.state.start('Game0');
            }
        },

        render: function () {



        }
    };