    var self = this;
    var itemsUp;
    var itemsMiddle;
    var itemsDown;
    var definicja;
    var definicja_audio;
    var definicja_audio2;
    var definicja_audio3;
    var definicja_audio4;
    var definicja_audio5;
    var inDefinition;
    var definition_text;
    Game1.Game3 = 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 iterator = 1;

    };

    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;
    }

    Game1.Game3.prototype = {


        create: function () {

            inDefinition = 0;
            definicja_audio = new Audio('./sound/OB_g_I_3_2.mp3');
            definicja_audio2 = new Audio('./sound/OB_g_I_3_3.mp3');
            definicja_audio3 = new Audio('./sound/OB_g_I_3_4.mp3');
            definicja_audio4 = new Audio('./sound/OB_g_I_3_5.mp3');
            definicja_audio5 = new Audio('./sound/OB_g_I_3_5.mp3');
            itemsUp = 0;
            itemsMiddle = 0;
            itemsDown = 0;
            iterator = 0;
            GameIsBusy = false;
            ButtonList = [];
            ItemInputList = [];
            ZmiennaMieszajaca = false;
            calc_value = "";
            input = 0;
            shoppinglist = "";
            shoppingcart = [[]];
            var SavedLocation = null;
            var LocationIsSet;
            this.LocationIsSet = false;
            variant1 = ['1_0', '1_1', '1_2', '2_0', '2_1', '2_2', '3_0', '3_1', '3_2', '4_0', '4_1', '4_2', '5_0', '5_1', '5_2', '6_0', '6_1', '6_2'];
            // variant1 = ['1_0','1_1','1_2','2_0','2_1','2_2','3_1','3_2','4_1','4_2','5_2','6_2'];
            shuffle(variant1);
            strona = new Audio("./sound/page.mp3");
            strona.load();
            lektor = new Audio("./sound/OB_g_I_3_1.mp3");
            lektor.load();
            finnish = new Audio("./sound/page.mp3");
            finnish.load();
            sound = new Audio();
            equationTable = [4, 2, 1, 10, 3, 2, 5, 9, 9, 5, 0, 10];
            playedEquation = 0;
            isReadyToRead = false;
            intro = true;
            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.boo = this.game.add.sprite(0, 0, "book");

            this.regal = this.game.add.sprite(173, 40, 'regal1');
            this.listaZakupow = this.game.add.sprite((1613), 435, 'lista');
            // calc = this.game.add.sprite(600, 766, "calc_bg");
            // calc_2 = this.game.add.sprite(1053, 766, "calc_bg");
            //calc.scale.setTo(0.5, 0.5);
            this.button = this.game.add.sprite(1395, 248, 'button1', 0);
            //  this.button.scale.setTo(0.4, 0.4);
            this.button.inputEnabled = true;
            this.button.name = 'gora';
            this.button.events.onInputDown.add(this.down, this);
            this.button.events.onInputUp.add(this.up, this);

            this.button = this.game.add.sprite(1395, 523, 'button2', 0);
            //  this.button.scale.setTo(0.4, 0.4);
            this.button.inputEnabled = true;
            this.button.name = 'srodek';
            this.button.events.onInputDown.add(this.down, this);
            this.button.events.onInputUp.add(this.up, this);

            this.button = this.game.add.sprite(1395, 799, 'button3', 0);
            // this.button.scale.setTo(0.4, 0.4);
            this.button.inputEnabled = true;
            this.button.name = 'dol';
            this.button.events.onInputDown.add(this.down, this);
            this.button.events.onInputUp.add(this.up, this);

            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);

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

            this.definicja = this.game.add.sprite(1400, 50, 'definicja');
            this.definicja.inputEnabled = true;
            this.definicja.name = "definicjaButton";
            this.definicja.fixedToCamera = true;
            this.definicja.events.onInputDown.add(this.down2, this);
            this.definicja.events.onInputUp.add(this.up, this);
            this.createItem();
            this.showInstruction();

            shoppinglist = this.game.add.text(1362, 310, ItemList, {
                font: "40px Roboto",
                fill: "#000000",
                align: "left"
            });

          //  lektor.play();

        },

        createItem: function () {
            if (iterator <= 17) {
                this.item = this.game.add.sprite(1640, 455, "wielo_"+variant1[iterator]);

                this.item.name = variant1[iterator];
                this.item.value = variant1[iterator].slice(2)
                ItemInputList.push(this.item);

                iterator++;
            } else if (iterator == 18) {
                iterator++;
            }
        },



        up: function (button, pointer) {
            button.frame = 0;
        },
        showDefinition: function () {
            inDefinition = 4;

            definicja.inputEnabled = false;
            definicja_audio4.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, 'W sklepach wielobranżowych\nartykuły chemiczne i czystościowe leżą oddzielnie\nod produktów spożywczych.\nChipsy i przekąski najczęściej stoją obok napoi.\nNatomiast przy kasie znajdują się\ndrobne i niedrogie produkty dalszej potrzeby,\njak gumy do żucia, żelki, drobne zabawki, długopisy itp.', {
                font: "36px Arial",
                fill: "#000000",
            });
            definition_text.fixedToCamera = true;

        },

        hideDefinition: function () {
            inDefinition = 0;
definicja_audio.pause();
            definicja_audio2.pause();
            definicja_audio3.pause();
            definicja_audio4.pause();
            definicja_audio = new Audio('./sound/OB_g_I_3_2.mp3');
            definicja_audio2 = new Audio('./sound/OB_g_I_3_3.mp3');
            definicja_audio3 = new Audio('./sound/OB_g_I_3_4.mp3');
            definicja_audio4 = new Audio('./sound/OB_g_I_3_5.mp3');
            definicja_audio5 = new Audio('./sound/OB_g_I_3_5.mp3');
            // this.definicja.inputEnabled = true;
            //this.ReStartInput();
            this.guiHelp.visible = false;
            this.guiBubble.visible = false;
            definition_text.visible = false;
            //this.lektor.hide();
        },

        down2: function (button, pointer) {
            if (!GameIsBusy) {
                button.frame = 1;
                if (button.name == 'definicjaButton') {
                    if (inDefinition!= 0)
                        this.hideDefinition();
                    else
                        this.showDefinition();
                } 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, 'Produkty w sklepie ułożone są tak,\nby mógł po nie sięgnąć przede wszystkim\nten klient, dla którego są one przeznaczone.\nPoukładaj produkty na półkach\nwg wskazanych zasad.', {
                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;
            //this.lektor.hide();
        },

        playSound: function () {
          sound.pause();
            //   sound = new Audio("./sound/MW_g_I_8_działanie" + variant1[playedEquation] + ".mp3");
            sound.load();
            sound.loop = false;
            sound.play();

        },
        playParent: function(){
        sound.pause();
            sound = new Audio("./sound/komentarze/czy_rodzice.mp3");
            sound.load();
            sound.loop = false;
            sound.play();   
        },
        playKid: function(){
            sound.pause();
            sound = new Audio("./sound/komentarze/nie_dzieci_" + (Math.floor(Math.random() * 2) + 1) + ".mp3");
            sound.load();
            sound.loop = false;
            sound.play();
        },
        playWrong: function () {
           sound.pause();
            // if (!isPlaying(sound)) {
            sound = new Audio("./sound/komentarze/z_" + (Math.floor(Math.random() * 2) + 1) + ".mp3");
            sound.load();
            sound.loop = false;
            sound.play();

            //   }
        },
        playGood: function () {
            sound.pause();
            sound = new Audio("./sound/komentarze/g" + (Math.floor(Math.random() * 2) + 1) + ".mp3");
            sound.load();
            sound.loop = false;
            sound.play();

        },
        showFinnish: function () {
            this.StopInput();
            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.guiBoo = this.game.add.sprite(0, 0, "book");
            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.\nWidzę, że potrafisz dopasować produkt do \nklienta.', {
                font: "36px Arial",
                fill: "#000000",
            });
            sound.pause();
            sound = new Audio("./sound/komentarze/g_1.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) {
                if (button.name == "gora") {
                    // //console.log('gora');
                    if ((ItemInputList[0].value) % 3 == 1) {
                        this.game.add.tween(ItemInputList[0]).to({
                            x: (228 + (120 * itemsUp)),
                            y: 140
                        }, 1000, Phaser.Easing.Back.Out, true);
this.playGood();
                        itemsUp++;
                        ItemInputList.pop();
                        this.createItem();


                        ////console.log(ItemInputList[0].value+"%3 = 0");
                    } else {
this.playWrong();
                        //this.playParent();
                        //console.log("źle!");
                    }
                }
                // text.setText(input.toString());   
                else if (button.name == "srodek") {
                    //console.log('srodek');
                    if ((ItemInputList[0].value) % 3 == 2) {
                        this.game.add.tween(ItemInputList[0]).to({
                            x: (228 + (120 * itemsMiddle)),
                            y: 380
                        }, 1000, Phaser.Easing.Back.Out, true);
                        itemsMiddle++;
                        ItemInputList.pop();
                        this.createItem();
                        this.playGood();
                        ////console.log(ItemInputList[0].value+"%3 = 1");
                    } else {
                        this.playWrong();
                        //console.log("źle!");
                    }
                } else if (button.name == 'dol') {
                    //console.log('dol');
                    if ((ItemInputList[0].value) % 3 == 0) {
                        this.game.add.tween(ItemInputList[0]).to({
                            x: (650 + (100 * itemsDown)),
                            y: 800
                        }, 1000, Phaser.Easing.Back.Out, true);
                        itemsDown++;
                        ItemInputList.pop();
                        this.createItem();
                        this.playGood();
                        // //console.log(ItemInputList[0].value+"%3 = 2");
                    } else {
                      // this.playKid();
                         this.playWrong();
                        //console.log("źle!");
                    }
                }
            }

        },
        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);    
        },


        update: function () {

 /*           if ((!isPlaying(definicja_audio) || definicja_audio.ended) && inDefinition == 1) {
                definicja_audio2.play();
                definition_text.setText('Na dolnych półkach często kładzie się\nopakowania zbiorcze i ciężkie, np. zgrzewki wody, mąki.\nWyżej układane są produkty sprzedawane\nna sztuki oraz te najpopularniejsze i cieszące się dużym\nzainteresowaniem.\nNajwyższej lokowane są częściej produkty lekkie,\nponieważ łatwo je zdjąć, oraz szczególnie cenne,\nby nie uległy przypadkowemu zrzuceniu (np. szklane).');
                inDefinition = 2;
            }
            if ((!isPlaying(definicja_audio2) || definicja_audio2.ended) && inDefinition == 2) {
                definicja_audio3.play();
                inDefinition = 3;
                definition_text.setText('W sklepie warzywniczym nisko układa się\ntzw. warzywa brudne, jak ziemniaki, włoszczyznę, buraki,\na także np. beczki z ogórkami czy kapustą kiszoną.\nNa półkach środkowych, umieszczane są warzywa i owoce\nsprzedawane na sztuki lub na wagę,\nnp. maliny, pomidory, fasolka.\nNa wyższych półkach można znaleźć\nprodukty paczkowane i drobne, jak rodzynki, orzeszki itp.');

            }
            if ((!isPlaying(definicja_audio3) || definicja_audio3.ended) && inDefinition == 3) {
                definicja_audio4.play();
                inDefinition = 4;

                definition_text.setText('W sklepach wielobranżowych\nartykuły chemiczne i czystościowe leżą oddzielnie\nod produktów spożywczych.\nNatomiast chipsy i przekąski najczęściej stoją obok napoi.\nNatomiast przy kasie znajdują się\ndrobne i niedrogie produkty dalszej potrzeby,\njak gumy do żucia, żelki, drobne zabawki długopisy itp.');

            }*/
            if ((!isPlaying(definicja_audio4) || definicja_audio4.ended) && inDefinition == 4) {
                this.hideDefinition();

            }

            if (iterator >= 19 && (!isPlaying(sound) || sound.ended)) {
                sound.pause();
                iterator = -1;
                playedEquation = 12;
                this.showFinnish();
            }
            if (playedEquation == 13 && (!isPlaying(sound) || sound.ended)) {
                sound.pause();
                strona.play();
                this.state.start('Menu');
                // this.hideFinnish();
            }
            if (playedEquation < 12) {
                if ((!isPlaying(lektor) || lektor.ended) && intro) {
                    intro = false;
                    this.hideInstruction();
                    isReadyToRead = true;
                    this.showDefinition();
                }
                /*            if (isReadyToRead && (!isPlaying(sound) || sound.ended)) {
                isReadyToRead = false;
             //   this.playSound();
            }*/
            }
            if (playedEquation == 14) {
                this.state.start('Menu');
            }
        },

        render: function () {



        }
    };