    var self = this;
    var iterator = 0;
    var PodCounter;
    var DodCounter;
    var variant1 = [];
    var definicja;
    var definicja_audio;
    var definicja_audio2;
    var inDefinition;
    var definition_text;

    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;

    };

    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.Game.prototype = {

        create: function () {


            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;

            for (var i = 1; i < 16; i++) {
                variant1.push(i);
            }
            //console.log(variant1);
            shuffle(variant1);
            //console.log(variant1);
            strona = new Audio("./sound/page.mp3");
            strona.load();
            lektor = new Audio("./sound/OB_g_I_5_2.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.cecha_pod = this.game.add.sprite(237, 166, 'cecha_pod', 0);
            this.cecha_dod = this.game.add.sprite(1324, 166, 'cecha_dod', 0);

            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;
            definicja_audio = new Audio('./sound/OB_g_I_5_1.mp3');
            definicja_audio2 = new Audio('./sound/OB_g_I_5_1.mp3');
            definicja = this.game.add.sprite(1508, 50, 'definicja');
            definicja.scale.setTo(0.8, 0.8);
            definicja.inputEnabled = true;
            definicja.name = "definicjaButton";

            definicja.events.onInputDown.add(this.down, this);

            /*        this.GoButton = this.game.add.sprite(1768,940,'GoButton');
        this.GoButton.name = "NextButton";
        this.GoButton.inputEnabled = true;
        this.GoButton.fixedToCamera = true;
        this.GoButton.events.onInputDown.add(this.down2, this);
        this.GoButton.events.onInputUp.add(this.up, this);*/

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


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

            /*        shoppinglist = this.game.add.text(1362,310, ItemList,{
        font: "40px Roboto",
        fill: "#000000",
        align: "left"
    });*/
            /*        ZadanieTresc = this.game.add.text(1500,210, 'Posiadane środki:',{
        font: "30px Roboto",
        fill: "#000000",
        align: "center"
    });
        ZadanieIloscSrodkow = this.game.add.text(1560,240, '210 zł',{
        font: "48px Roboto",
        fill: "#0040ff",
        align: "center"
    }); */

            this.showInstruction();
            // lektor.play();

        },
        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, 'Każdy produkt ma cechy niezbędne, czyli takie,\nbez których nie spełniałby swoich podstawowych funkcji.\nNa przykład piłka musi być okrągła i dobrze się odbijać.\nKażdy produkt ma też cechy dodatkowe,\nktóre mają sprawić, by był on bardziej atrakcyjny,\nnp. takie jak kolor czy wielkość.', {
                font: "36px Arial",
                fill: "#000000",
            });
            definition_text.fixedToCamera = true;

        },

        hideDefinition: function () {
            inDefinition = false;
            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();
        },

        SpawnBlocks: function () {
            if (ItemInputList.length > 0) {
                for (var index = 0; index < ItemInputList.length; ++index) {
                    ItemInputList[index].kill();
                }
            }
            for (var i = 0; i < 6; i++) {
                if (i < 3) {
                    this.item = this.game.add.sprite(237, 326 + (120 * i), 'empty');
                    this.item.name = "pod_" + i;
                    this.item.isEmpty = true;
                    PodItemList.push(this.item);
                } else {
                    this.item = this.game.add.sprite(1324, 326 + (120 * (i - 3)), 'empty');
                    this.item.name = "dod_" + (i - 3);
                    this.item.isEmpty = true;
                    DodItemList.push(this.item);
                }
            }
        },
        SpawnItem: function () {

            this.item = this.game.add.sprite(760, 260, (variant1[iterator]).toString());
            this.item.name = variant1[iterator];
            ItemInputList.push(this.item);
            //console.log(ItemInputList);


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

        SpawnButtons: function () {
            if (ButtonList.length > 0) {
                for (var index = 0; index < ButtonList.length; ++index) {
                    ButtonList[index].kill();
                }
            }
            var arr = [1, 2, 3, 4];

            //console.log(arr);
            //wylaczenie szuflowania tablicy odpowiedzi..
            shuffle(arr);
            for (var i = 0; i < 4; i++) {
                this.item = this.game.add.sprite(170 + (427 * i), 802, variant1[iterator] + "_" + arr[i]);
                this.item.inputEnabled = true;
                this.item.name = "odpowiedzi";
                this.item.value = arr[i];
                this.item.input.enableDrag(false, true);
                this.item.orginalPositionX = this.item.x;
                this.item.orginalPositionY = this.item.y;
                this.item.events.onDragStart.add(this.SaveLocation, this);
                //this.item.events.onDragStop.add(this.stopDrag, this);
                ButtonList.push(this.item);
                //this.item.events.onInputDown.add(this.down, this);
                this.item.events.onInputUp.add(this.stopDrag, this);
            }
        },

        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, 'Podziel cechy przedstawionych produktów\nna podstawowe (niezbędne) oraz dodatkowe.', {
                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();
        },



        stopDrag: function (button, pointer) {


            //console.log(button.name);
            var mover = button;
            var ToInsert = true;
            //  PodItemList
            //  DodItemList

            for (var i = 0; i < PodItemList.length; i++) {
                if (mover.overlap(PodItemList[i])) {
                    if ((mover.value == 1) || (mover.value == 2)) {
                        //console.log("nad Podstaw");
                        for (var j = 0; j < 2; j++) {
                            if (PodItemList[j].isEmpty && ToInsert) {
                                ToInsert = false;
                                mover.orginalPositionX = PodItemList[j].x;
                                mover.orginalPositionY = PodItemList[j].y;
                                PodItemList[j].isEmpty = false;
                                PodCounter++;
                                this.playSound();
                                //  break; 
                            }
                        }
                        //break;  
                    } else {
                        //  this.game.add.tween(button).to( {x:button.orginalPositionX, y:button.orginalPositionY }, 1000, Phaser.Easing.Back.Out, true);

                        this.playWrong();
                        //console.warn("error bledne dopasowanie")}

                    }
                }
            }


            for (var i = 0; i < DodItemList.length; i++) {
                if (mover.overlap(DodItemList[i])) {
                    if ((mover.value == 3) || (mover.value == 4)) {
                        //console.log("nad Dodstaw");
                        for (var j = 0; j < 2; j++) {
                            if (DodItemList[j].isEmpty && ToInsert) {
                                ToInsert = false;
                                mover.orginalPositionX = DodItemList[j].x;
                                mover.orginalPositionY = DodItemList[j].y;
                                DodItemList[j].isEmpty = false;
                                DodCounter++;
                                this.playSound();
                                //      break;   
                            }
                        }
                        // break;  
                    } else {
                        //  this.game.add.tween(button).to( {x:button.orginalPositionX, y:button.orginalPositionY }, 1000, Phaser.Easing.Back.Out, true);

                        this.playGood();
                        //console.warn("error bledne dopasowanie")}
                    }
                }







            }
            this.game.add.tween(mover).to({
                x: mover.orginalPositionX,
                y: mover.orginalPositionY
            }, 1000, Phaser.Easing.Back.Out, true);
        },
        playSound: function () {
            
            sound.pause();
            sound = new Audio("./sound/g" +(Math.floor(Math.random() * 2) + 1)+ ".mp3");
            sound.load();
            sound.loop = false;
            sound.play();

        },
        playWrong: function () { //cecha dodatkowa
            // if (!isPlaying(sound)) {
            sound.pause();
            sound = new Audio("./sound/OB_g_I_5_3.mp3");
            sound.load();
            sound.loop = false;
            sound.play();

            //   }
        },
        playGood: function () { // cecha niezbedna
            sound.pause();
            sound = new Audio("./sound/OB_g_I_5_4.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, 'Widzę, że potrafisz rozróżniać\ncechy produktów.', {
                font: "50px Arial",
                fill: "#000000",
            });
            sound.pause();
            sound = new Audio("./sound/OB_g_I_5_5.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 == 'definicjaButton') {
                    this.showDefinition();
                }
                if (button.name == "odpowiedzi") {
                    if (button.value == ItemInputList[0].value) {
                        //console.log('yeeah');
                        ItemInputList[0].destroy();
                        ItemInputList.pop();
                        this.SpawnItem();
                        playedEquation++;
                    } else if (button.value != ItemInputList[0].value) {
                        this.playWrong();
                    }
                }

            }
        },
        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('Każdy produkt ma cechy niezbędne, czyli takie,\n bez których nie spełniałby swoich podstawowych funkcji.\nNa przykład piłka musi być okrągła i dobrze się odbijać.\nKażdy produkt ma też cechy dodatkowe,\nktóre mają sprawić by był on bardziej atrakcyjny,\nnp. takie jak kolor czy wielkość.');
                this.hideDefinition();
                inDefinition = 2;
            }
            if ((DodCounter == 2 && PodCounter == 2) && (!isPlaying(sound) || sound.ended)) //Warunek konieczny stworzenia nowych wiązanek
            {
                if (ButtonList.length > 0) {
                    for (var index = 0; index < ButtonList.length; ++index) {
                        ButtonList[index].kill();
                    }
                }
 
                DodItemList = [];
                PodItemList = [];
                DodCounter = 0;
                PodCounter = 0;
                this.SpawnBlocks();
                this.SpawnItem();
                playedEquation++;
            }
            if (tween != null)
                tween.onComplete.removeAll();
            if (ZmiennaMieszajaca) {
                //this.showFinnish();
                playedEquation == 10;
                // //console.log("timeout!");
                //   this.upList(); 


            }
            if (playedEquation == 12 && (!isPlaying(sound) || sound.ended)) {
                sound.pause();
                playedEquation = 13;
                this.showFinnish();
            }
            if (playedEquation == 13 && (!isPlaying(sound) || sound.ended)) {
                strona.play();
                this.state.start('Menu');
                //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('Menu');
            }
        },

        render: function () {



        }
    };