    var self = this;
    var definicja_audio;
    var definicja_audio2;
    var inDefinition;
    var definition_text;
	var polka =[];
	var cal_amount=0;
	var cal2_amount=0;
    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 definicja;           
    };
    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 = {  
        upList: function () {
            if (ButtonList.length > 0) {
                
				for (var index = 0; index < ButtonList.length; ++index) 
				{
                    ButtonList[index].kill();
                }
				
            };

            ZmiennaMieszajaca = false;
            if (shoppingcart.length > 1)
                
				for (var j = 1; j < shoppingcart.length; j++) 
				{
                    this.button = this.game.add.sprite(1800, 275 + (42 * (j)), 'calc_', 0);
                    this.button.scale.setTo(0.4, 0.4);
                    this.button.inputEnabled = true;
                    this.button.name = 'kalkulator';
                    this.button.item = j;
                    this.button.events.onInputDown.add(this.down, this);
                    ButtonList.push(this.button);
                }

        },
        create: function () {
            
			for (var i = 0; i<9;i++)
			{
                if (i<3){          
					polka[i]= [385+(181*(i%3)),397+(173*0)];
				}
				if (i>=3 && i<6){
					polka[i]= [385+(181*(i%3)),397+(173*1)];
				}
                if (i>=6){
					polka[i]= [385+(181*(i%3)),397+(173*2)];
				}
            }

            variant1= [];
            inDefinition = 0;
            definicja_audio = new Audio('./sound/OS_g_I_9_2.mp3');
            definicja_audio2 = new Audio('./sound/OS_g_I_9_2.mp3');
            tween = null;
            GameIsBusy = false;
            ButtonList = [];
            ItemInputList = [];
            ZmiennaMieszajaca = false;
            calc_value = "";
            input = 0;
            shoppinglist = "";
            shoppingcart = [[]];
            var SavedLocation = null;
            var LocationIsSet;
            this.LocationIsSet = false;
            variant1 = ["1_0","2_0","3_0","5_0","8_0","12_0","14_0","4_1","6_1","7_1","9_1","11_1","13_1","10_1"];
            shuffle(variant1);
            lektor = new Audio("./sound/OS_g_I_9_2.mp3");
            lektor.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;
            calc = this.game.add.sprite(348, 352, 'szafa1');
            calc_2 = this.game.add.sprite(1023, 352, 'szafa2');
            this.item = this.game.add.sprite(108,599,'player1');
            this.item = this.game.add.sprite(1629,599,'player2');
            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.CreateItems();
            this.showInstruction();
            lektor.play();
        },
        CreateItems: function () {
          
            for (var i = 1; i < 15; i++) {
                if (i <=7) {
                    var res = variant1[i - 1].split("_");
                    this.item = this.game.add.sprite(300 + (190*(i-1)), 11, res[0]);             
                }
                if (i > 7 ) {
                    var res = variant1[i - 1].split("_");
                    this.item = this.game.add.sprite(300 + (190 * (i-8 )), 160, res[0]);
                }
                this.item.orginalPositionX = this.item.x;
                this.item.orginalPositionY = this.item.y;
                this.game.physics.arcade.enable(this.item);
                this.item.inputEnabled = true;
                this.item.name = 'items';
                this.item.value =res[1];
                this.item.input.enableDrag(false, true);
                this.item.events.onDragStop.add(this.stopDrag, this);
                this.item.events.onInputDown.add(this.down, this);
                ItemInputList.push(this.item);
            }
			
        },
        SaveLocation: function (button, pointer) {
            if (GameIsBusy == false) {   
            }
        },
        up: function (button, pointer) {
            button.frame = 0;
        },
        down2: function (button, pointer) {
            if (!GameIsBusy) {
                button.frame = 1;
                if (button.name == 'definicjaButton') {
                    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 () {
            if (!GameIsBusy) {
                GameIsBusy = true;
                this.StopInput();
                this.guiHelp = this.game.add.sprite(1310, 395, 'guiPomocnik_good');
				this.guiHelp.scale.setTo(1.3,1.3);
				this.guiHelp.fixedToCamera = true;
				this.guiBubble = this.game.add.sprite(135, 96, 'Finish');
				this.guiBubble.scale.setTo(0.9,0.9); 
				this.guiBubble.fixedToCamera = true;
                this.titleText = this.game.add.text(249, 360, 'Who usually wears these clothes?\nClick and drag.', {
                    font: "46px 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;
        },
        showDefinition: function () {
            if (!GameIsBusy) {
                inDefinition = 1;
                GameIsBusy = true;
                definicja_audio.play();
                this.StopInput();
                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, 'Produkty pierwszej potrzeby to te artykuły,\nktóre są niezbędne do zaspokojenia najważniejszych\npotrzeb człowieka, są konieczne w codziennym życiu.\nPrzykładem takich produktów są podstawowe\nartykuły spożywcze (jedzenie, woda)\noraz podstawowe środki higieny (jak mydło).', {
                    font: "36px Arial",
                    fill: "#000000",
                });
                definition_text.fixedToCamera = true;
            }
        },
        hideDefinition: function () {
            inDefinition = false;
            GameIsBusy = false;
            this.ReStartInput();
            this.guiHelp.visible = false;
            this.guiBubble.visible = false;
            definition_text.visible = false;
        },
        stopDrag: function (button, pointer) {
            if (GameIsBusy == false) {   
                var mover = button;
                if (button.overlap(calc)) {
                    if (button.value == '0') {
                        this.game.add.tween(button).to({
                        x: polka[cal_amount][0],
                        y: polka[cal_amount][1]
                    }, 1000, Phaser.Easing.Back.Out, true);
                       this.playGood(); 
                        cal_amount++;
                        playedEquation++;
						button.inputEnabled = false;
                    } else {
                        this.playWrong();
                        this.game.add.tween(button).to({
                        x: button.orginalPositionX,
                        y: button.orginalPositionY
						}, 1000, Phaser.Easing.Back.Out, true);
                    }
                }
                if (button.overlap(calc_2)) {
                    if (button.value == '1') {
                        this.game.add.tween(button).to({
                        x: polka[cal2_amount][0]+677,
                        y: polka[cal2_amount][1]
                    }, 1000, Phaser.Easing.Back.Out, true);
                        this.playGood();
						cal2_amount++;
                        playedEquation++;
						button.inputEnabled = false;
                    } else {
                        this.playWrong();
                        this.game.add.tween(button).to({
                        x: button.orginalPositionX,
                        y: button.orginalPositionY
						}, 1000, Phaser.Easing.Back.Out, true);
                    }
                }
                ZmiennaMieszajaca = true;
            }
        },
        playSound: function () {
            sound.load();
            sound.loop = false;
            sound.play();
        },
        playWrong: function () {
            sound.pause();
            sound = new Audio("./sound/z" + (Math.floor(Math.random() * 2) +1) + ".mp3");
            sound.load();
            sound.loop = false;
            sound.play();
        },
        playGood: function () {
            sound.pause();
            sound = new Audio("./sound/g1.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.guiHelp = this.game.add.sprite(1310, 395, 'guiPomocnik_good');
			this.guiHelp.scale.setTo(1.3,1.3);
			this.guiHelp.fixedToCamera = true;
			this.guiBubble = this.game.add.sprite(135, 96, 'Finish');
			this.guiBubble.scale.setTo(0.9,0.9); 
			this.guiBubble.fixedToCamera = true;
			this.guiText = this.game.add.text(220, 330, 'Well done!', {
				font: "50px Arial",
				fill: "#000000",
			});
			sound.pause();
			sound = new Audio("./sound/gratulacje.mp3");
            sound.load();
            sound.loop = false;
            sound.play();
            this.titleText.fixedToCamera = true;
            playedEquation = 17;
        },
        hideFinnish: function () {
            sound.pause();
            this.ReStartInput();
            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) {
            if (GameIsBusy == false) {
                if (button.name == "kalkulator") {
                    if (shoppingcart[button.item][1] > 1) {
                        shoppingcart[button.item][1] = shoppingcart[button.item][1] - 1;
                        input = input - shoppingcart[button.item][2];
                    } else if (shoppingcart[button.item][1] == 1) {
                        button.destroy();
                        this.upList();
                        input = input - shoppingcart[button.item][2];
                        shoppingcart.splice(button.item, 1);
                    }
                    ZmiennaMieszajaca = true;
                }
            }
        },
		update: function () {
            if (tween != null)
                tween.onComplete.removeAll();
            if (ZmiennaMieszajaca) {
                this.upList();
            }
            if (playedEquation == 14 && (!isPlaying(sound) || sound.ended)) {
                sound.pause();
                playedEquation = 17;
                this.showFinnish();
            }
            if (playedEquation == 17 && (!isPlaying(sound) || sound.ended)) {
				this.game.time.events.add(Phaser.Timer.SECOND * 2, function () {
                    try {
                        this.state.start('Menu');
						location.reload();
                    }
                    catch (e) {
                    }
                }, this);
            }
            if (playedEquation < 14) {
                if ((!isPlaying(lektor) || lektor.ended) && intro) {
                    intro = false;
                    this.hideInstruction();
                    isReadyToRead = true;
                }
            }
            if (playedEquation == 18) {
                this.state.start('Menu');
            }
        },
        render: function () {
        }
    };