    var self = this;
    var iterator = 0;
    var PodCounter;
    var DodCounter;
    var variant1 = [];
    var variant2 = [];
    var emptyspacelist = [[453,273],[400,437],[403,561],[430,689],[436,842],[1244,241],[1244,357],[1288,527],[1202,779],[1202,895]];
    var definicja;
    var definicja_audio;
    var definicja_audio2;
    var inDefinition;
    var definition_text;
	var arrows;
    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 (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 () {
            PodCounter = 0;
            DodCounter = 0;
            tween = null;
            GameIsBusy = false;
            ButtonList = [];
            ItemInputList = [];
            PodItemList = [];
            DodItemList = [];
            ZmiennaMieszajaca = false;
            calc_value = "";
            input = 0;
            shoppinglist = "";
            shoppingcart = [];
            iterator = 0;
            var SavedLocation = null;
            var LocationIsSet;
            this.LocationIsSet = false;
            variant1 = [];
            variant2 = [];
            
			for (var i = 1; i < 11; i++) {
                variant1.push(i);
                variant2.push(i);
            }

            shuffle(variant1);
            shuffle(variant2);
            console.log(variant1);
            console.log(variant2);
            lektor = new Audio("./sound/OS_g_I_10_.mp3");
            lektor.load();
            finnish = new Audio("./sound/gratulacje.mp3");
            finnish.load();
            sound = new Audio();
            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.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);
            arrows = this.game.add.sprite(328,230, 'arrows');
            var item;
            var selectexbox1 = null;
            var selectexbox2 = null;
            var TotalPoints = 0;
            var equal = 0;
            var j = 0;
            this.SpawnBlocks();
            this.SpawnItem();
            this.showInstruction();
            lektor.play();
        },
        SpawnBlocks: function () {
            
            for (var i = 1; i < 11; i++) {
                this.item = this.game.add.sprite((emptyspacelist[i-1][0]), emptyspacelist[i-1][1], 'empty');
                this.item.name =  i;
                this.item.isEmpty = true;
                PodItemList.push(this.item);
            }
                
        }, 
        MainMenu: function(){
             this.state.start('Menu');
        },
        SpawnItem: function () {
            this.SpawnButtons();
        },
        SpawnButtons: function () {
             
			 for (var i = 0; i < 10; i++) {
                if (i < 5) {
                    this.item = this.game.add.sprite(475 + (185 * i), 16 , variant1[i]);
                } else {
                    this.item = this.game.add.sprite(475 + (185 * (i - 5)), 116 , variant1[i]);                 
                }
                this.item.inputEnabled = true;
                this.item.name = "odpowiedzi";
                this.item.value = variant1[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);
                ButtonList.push(this.item);
                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(1410, 550, 'guiPomocnik_good');
            this.guiHelp.fixedToCamera = true;
            this.guiBubble = this.game.add.sprite(135, 96, 'Finish');
            this.guiBubble.fixedToCamera = true;
            this.titleText = this.game.add.text(250, 470, 'Listen to the names of body parts.\nDrag name in the right place.', {
                font: "64px 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;
        },
        stopDrag: function (button, pointer) {
            var mover = button;
            var ToInsert = true;
            if (button.value != variant2[iterator])
            { 
                this.playWrong();
            }
            else {
            
				for (var i = 0; i < PodItemList.length; i++) {
					if (mover.overlap(PodItemList[i])) {
						if ((mover.value == PodItemList[i].name)) {
								if (PodItemList[i].isEmpty && ToInsert) {
									ToInsert = false;
									mover.orginalPositionX = PodItemList[i].x;
									mover.orginalPositionY = PodItemList[i].y;
									PodItemList[i].isEmpty = false;
									PodCounter++;
									iterator++;
									this.playGood();
									break; 
								} 
						} else {
							this.playWrong();
						}
					}
				}
				
            }
            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/" +variant2[iterator]+ ".mp3");
            sound.load();
            sound.loop = false;
            sound.play();
        },
        playWrong: function () {
            sound.pause();
            sound = new Audio("./sound/z1.mp3");
            sound.load();
            sound.loop = false;
            sound.play();
            isReadyToRead = true;
        },
        playGood: function () {
            sound.pause();
            sound = new Audio("./sound/g1.mp3");
            sound.load();
            sound.loop = false;
            sound.play();
            isReadyToRead = true;
            playedEquation++;
        },
        showFinnish: function () {
            this.StopInput();
            if (ButtonList.length > 0) {
                
				for (var index = 0; index < ButtonList.length; ++index) {
                    ButtonList[index].kill();
                }
            
			}
            this.guiHelp = this.game.add.sprite(1410, 470, 'guiPomocnik_good');
			this.guiHelp.scale.setTo(1.2,1.2);
			this.guiHelp.fixedToCamera = true;
			this.guiBubble = this.game.add.sprite(135, 96, 'Finish');
			this.guiBubble.fixedToCamera = true;
			this.titleText = this.game.add.text(350, 500, 'Well done!', {
				font: "78px Arial",
				fill: "#000000",
			});
			this.titleText.fixedToCamera = true;
            sound.pause();
            sound = new Audio("./sound/gratulacje.mp3");
            sound.load();
            sound.loop = false;
            sound.play();
            this.titleText.fixedToCamera = true;
            playedEquation = 13;
        },
        hideFinnish: function () {
            sound.pause();
            this.ReStartInput();   
            this.guiBack.kill();
            this.guiBg.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) {
                        ItemInputList[0].destroy();
                        ItemInputList.pop();
                        this.SpawnItem();
                        playedEquation++;
                    } else if (button.value != ItemInputList[0].value) {
                        this.playWrong();
                    }
                }
            }
        },
        update: function () { 
            if ((PodCounter == 12) && (!isPlaying(sound) || sound.ended))
            {
                if (ButtonList.length > 0) {
                    
					for (var index = 0; index < ButtonList.length; ++index) {
                        ButtonList[index].kill();
                    }
                
				}
                if (PodItemList.length>0) {
                    
					for (var index = 0; index < PodItemList.length; ++index) {
                        PodItemList[index].kill();
                    }
                    
				}
                arrows.kill();
            }
            if (tween != null)
                tween.onComplete.removeAll();
            if (ZmiennaMieszajaca) {
                playedEquation == 10;
            }
            if (playedEquation == 10 && (!isPlaying(sound) || sound.ended)) {
                sound.pause();
                playedEquation = 13;
                this.showFinnish();
            }
            if (playedEquation == 13 && (!isPlaying(sound) || sound.ended)) {
                this.state.start('Menu');
            }
            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 () {
        }
    };