    var self = this;
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 = {
    
    upList: function()
    {
        if( ButtonList.length>0)
        {
    for (var index = 0; index < ButtonList.length; ++index) {
    ButtonList[index].kill();
    }
    };
    
        ZmiennaMieszajaca = false;  
        //   if (this.ZmiennaMieszajaca){
       
       ItemList = "";
        if ( shoppingcart.length>1)
     for (var j =1; j< shoppingcart.length;j++){
       ItemList = ItemList+j+". "+shoppingcart[j][0]+"    sztuk: "+shoppingcart[j][1]+"\n";
       //this.item = self.Game1.game.add.sprite(1424 + (152 * (i % 3)), 454 + (105 * (0)), 'calc_' + i, 0);
       
         
         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);
          console.log("timeout!");
     }
      shoppinglist.setText(ItemList);
      
       console.log("timeout!");
           
   // }
    },
    create: function () {
        //this.load.BitmapFont('font_0', 'font/font_0_0.png',font_json);
        GameIsBusy = false;
        ButtonList = [];
        ItemInputList = [];
        ZmiennaMieszajaca = false;
        calc_value ="";
        input = 0;
        shoppinglist = "";
        shoppingcart= [[]];
        var SavedLocation = null;
        var LocationIsSet;
        this.LocationIsSet = false;
        // variant1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
        //  shuffle(variant1);
        strona = new Audio("./sound/page.mp3");
        strona.load();
        lektor = new Audio("./sound/OB_g_I_9_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.buttonLeft = this.game.add.sprite(150 / 2, 100 / 2, 'speaker', 0);
        //this.buttonLeft.name = "Speaker";
        //this.buttonLeft.inputEnabled = true;
        // this.buttonLeft.fixedToCamera = true;
        //this.buttonLeft.events.onInputDown.add(this.down2, this);
        // this.buttonLeft.events.onInputUp.add(this.up, this);
        this.regal = this.game.add.sprite(154,0,'regal');
        this.listaZakupow = this.game.add.sprite((1342),171,'lista');
        calc = this.game.add.sprite(1053, 766, "calc_bg");
        //calc.scale.setTo(0.5, 0.5);
        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);
        
        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.item = this.game.add.sprite(230, 180, '1');
        this.item.orginalPositionX = this.item.x; this.item.orginalPositionY = this.item.y;  this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true; ItemInputList.push(this.item);
        this.item.name = 'Pasek';
        this.item.value = 10;
        this.item.input.enableDrag();
        this.item.events.onDragStart.add(this.SaveLocation, this);
        this.item.events.onDragStop.add(this.stopDrag);
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(455, 180, '2');
        this.item.orginalPositionX = this.item.x; this.item.orginalPositionY = this.item.y;  this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true; ItemInputList.push(this.item);
        this.item.name = 'Szorty';
        this.item.value = 50;
        this.item.input.enableDrag();
        this.item.events.onDragStart.add(this.SaveLocation, this);
        this.item.events.onDragStop.add(this.stopDrag);
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(680, 180, '3');
        this.item.orginalPositionX = this.item.x; this.item.orginalPositionY = this.item.y;
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true; ItemInputList.push(this.item);
        this.item.name = 'Bluzka';
        this.item.value = 20;
        this.item.input.enableDrag();
        this.item.events.onDragStart.add(this.SaveLocation, this);
        this.item.events.onDragStop.add(this.stopDrag);
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(895, 180, '4');
        this.item.orginalPositionX = this.item.x; this.item.orginalPositionY = this.item.y;
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true; ItemInputList.push(this.item);
        this.item.name = 'Koszulka';
        this.item.value = 40;
        this.item.input.enableDrag();
        this.item.events.onDragStart.add(this.SaveLocation, this);
        this.item.events.onDragStop.add(this.stopDrag);
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(210, 400, '5');
        this.item.orginalPositionX = this.item.x; this.item.orginalPositionY = this.item.y;
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true; ItemInputList.push(this.item);
        this.item.name = 'Branzoletka';
        this.item.value = 25;
        this.item.input.enableDrag();
        this.item.events.onDragStart.add(this.SaveLocation, this);
        this.item.events.onDragStop.add(this.stopDrag);
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(455, 405, '6');
        this.item.orginalPositionX = this.item.x; this.item.orginalPositionY = this.item.y;  this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true; ItemInputList.push(this.item);
        this.item.name = 'Pierścionek';
        this.item.value = 30;
        this.item.input.enableDrag();
        this.item.events.onDragStart.add(this.SaveLocation, this);
        this.item.events.onDragStop.add(this.stopDrag);
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(680, 405, '7');
        this.item.orginalPositionX = this.item.x; this.item.orginalPositionY = this.item.y;  this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true; ItemInputList.push(this.item);
        this.item.name = 'Kolczyki';
        this.item.value = 25;
        this.item.input.enableDrag();
        this.item.events.onDragStart.add(this.SaveLocation, this);
        this.item.events.onDragStop.add(this.stopDrag);
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(905, 405, '8');
        this.item.orginalPositionX = this.item.x;
        this.item.orginalPositionY = this.item.y;
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true; ItemInputList.push(this.item);
        this.item.name = 'Naszyjnik';
        this.item.value = 50;
        this.item.input.enableDrag();
        this.item.events.onDragStart.add(this.SaveLocation, this);
        this.item.events.onDragStop.add(this.stopDrag);
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(215, 630, '9');
        this.item.orginalPositionX = this.item.x;
        this.item.orginalPositionY = this.item.y;
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true; ItemInputList.push(this.item);
        this.item.name = 'Czapka';
        this.item.value = 20;
        this.item.input.enableDrag();
        this.item.events.onDragStart.add(this.SaveLocation, this);
        this.item.events.onDragStop.add(this.stopDrag);
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(455, 630, '10');
        this.item.orginalPositionX = this.item.x; this.item.orginalPositionY = this.item.y;
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true; ItemInputList.push(this.item);
        this.item.name = 'Szalik';
        this.item.value = 30;
        this.item.input.enableDrag();
        this.item.events.onDragStart.add(this.SaveLocation, this);
        this.item.events.onDragStop.add(this.stopDrag);
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(680, 630, '11');
        this.item.orginalPositionX = this.item.x; this.item.orginalPositionY = this.item.y;
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true; ItemInputList.push(this.item);
        this.item.name = 'Rękawiczki';
        this.item.value = 15;
        this.item.input.enableDrag();
        this.item.events.onDragStart.add(this.SaveLocation, this);
        this.item.events.onDragStop.add(this.stopDrag);
        this.item.events.onInputDown.add(this.down, this);
        this.item = this.game.add.sprite(905, 630, '12');
        this.item.orginalPositionX = this.item.x; this.item.orginalPositionY = this.item.y;
        this.game.physics.arcade.enable(this.item);
        this.item.inputEnabled = true; ItemInputList.push(this.item);
        this.item.name = 'Skarpetki';
        this.item.value = 10;
        this.item.input.enableDrag(false, true);
        this.item.events.onDragStart.add(this.SaveLocation, this);
        this.item.events.onDragStop.add(this.stopDrag, this);
        this.item.events.onInputDown.add(this.down, this);

       this.showInstruction();
        
        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"
    }); 
         lektor.play();
                              
    },
    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 (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, 'Masz 210 złotych.\nZrób zakupy za możliwie całą kwotę.\nNie możesz wydać więcej pieniędzy niż masz.\nWłóż produkty do koszyka i przejdź do kasy,\nby sprawdzić czy właściwie zaplanowałeś wydatki.', {
            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) {
       if (GameIsBusy == false){
        console.log(button.name);
        if (button.overlap(calc)) {
            console.log("its over!");
            input = input + button.value;
            var itemInBasket = true;
          // text.setText(input.toString());
            if (shoppingcart.length > 0) {
                for (var i = 0; i < shoppingcart.length; i++) {
                    if (shoppingcart[i][0] == button.name) {

                        itemInBasket = true;
                        shoppingcart[i][1] = shoppingcart[i][1] + 1;
                        break;
                    } else {
                        itemInBasket = false;
                    }
                }
                if (!itemInBasket) {
                    shoppingcart.push([button.name, 1, button.value]);
                }
            } else {
                shoppingcart.push([button.name, 1,button.value]);
            }

        }
        //this.add.tween(button).to({button.orginalPositionX,button.orginalPositionY}, 2000, Phaser.Easing.Linear.None, true);
        button.x = button.orginalPositionX;
        button.y = button.orginalPositionY;
        ZmiennaMieszajaca = true;
        // And re-enable it upon release
       // this.sprite.body.moves = true;

    }
    },

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

    },
    playWrong: function () {
       // if (!isPlaying(sound)) {
            sound = new Audio("./sound/z" + (Math.floor(Math.random() * 4) + 1) + ".mp3");
            sound.load();
            sound.loop = false;
            sound.play();

     //   }
    },
    playGood: function () {
        sound.pause();
        sound = new Audio("./sound/g" + (Math.floor(Math.random() * 4) + 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
        if (input >= 190 && input <=210)
       {this.guiHelp = this.game.add.sprite(1050, 290, 'guiPomocnik_good');
        this.guiText = this.game.add.text(220, 330, 'Widzę, że dobrze gospodarujesz\nswoimi pieniędzmi.', {
            font: "40px Arial",
            fill: "#000000",
        });
        sound.pause();
        sound = new Audio("./sound/OB_g_I_7_7.mp3");
        sound.load();
        sound.loop = false;
        sound.play();
        this.titleText.fixedToCamera = true;
        playedEquation = 13;
        //Widzę, że dobrze gospodarujesz swoimi pieniędzmi.
       }
        else
        {
             this.guiHelp = this.game.add.sprite(1050, 290, 'guiPomocnik_bad')
            if(input<190){
            this.guiText = this.game.add.text(220, 330, 'Nie wydałeś wszystkiego.\nMoże jeszcze coś kupisz?', {
            font: "40px Arial",
            fill: "#000000",
        });
        sound.pause();
        sound = new Audio("./sound/OB_g_I_7_10.mp3");
        sound.load();
        sound.loop = false;
        sound.play();
        this.titleText.fixedToCamera = true;
                //Nie wydałeś wszystkiego. Może jeszcze coś kupisz?
            }
            if(input>210){
            this.guiText = this.game.add.text(220, 330, 'Chcesz wydać za dużo.\nPolicz jeszcze raz.', {
            font: "40px Arial",
            fill: "#000000",
        });
        sound.pause();
        sound = new Audio("./sound/OB_g_I_7_8.mp3");
        sound.load();
        sound.loop = false;
        sound.play();        
        this.titleText.fixedToCamera = true;
                //Chcesz wydać za dużo. Policz jeszcze raz.
            }
        }
        
        
    // koniec ustalania    
       this.guiBack = this.game.add.sprite(213,888,'backButton');
       this.guiBack.name = "guiBack";
        this.guiBack.inputEnabled = true;
        this.guiBack.fixedToCamera = true;
        this.guiBack.events.onInputDown.add(this.down2, this);
        this.guiBack.events.onInputUp.add(this.up, this); 
       this.guiHelp.fixedToCamera = true;
       
       
       this.guiBubble.fixedToCamera = true;
      this.guiBasket = this.game.add.sprite(385,713,"calc_bg");
      this.guiCounter = this.game.add.sprite(0,710,'counter');
        text = this.game.add.bitmapText(1600, 770, 'font_0', input, 180);
        text.scale.setTo(0.5, 0.5);
        text.setText(input.toString());
        
        // this.titleText = this.game.add.text(250, 400, 'Gratulacje!', {
      //  font: "58px Arial",
      //      fill: "#000000",
      //  });
      //  this.titleText.fixedToCamera = true;
       // finnish.play();
    },
    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) {
       if(GameIsBusy == false){
        if (button.name == "kalkulator")
        {
            console.log("klakulator nr:"+button.item);
            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();
                
               
               // button.update();
                input = input -shoppingcart[button.item][2];
              shoppingcart.splice(button.item,1);
             
            }
            // text.setText(input.toString());   
             ZmiennaMieszajaca = true;
        }
   // button.orginalPosition = button.position;   
 /*       if (this.selectexbox1 == null && (!isPlaying(lektor) || lektor.ended)) {
           
            if (button.name == 'rownanie') {
                               this.selectexbox1 = button;
                this.selectexbox1.tint = 0xddffa1;
            }
           
        }
      
        if (this.selectexbox1 != null) {
            if (button.name == "kalkulator") {
                if (button.value == "c") {
                    console.log("czyszczenie");
                    calc_value ="";
                    input = "";
                    text.setText(input);
                   } else if (button.value == "=") {
                    if (calc_value == this.selectexbox1.value) {
                                              this.fadePicture(this.selectexbox1);
                        calc_value ="";
                    input = "";
                    text.setText(input);
                                               this.selectexbox1 = null;
                       

                        playedEquation++;
                         this.playGood();
                        isReadyToRead = true;
                    } else {
                        this.playWrong();
                    calc_value ="";
                    input = "";
                    text.setText(input);
     

                    }
                } else {
                    this.AddDigit(button.value);
                }
            } else if (button.name == 'rownanie') {
                this.selectexbox1.tint = 0xffffff;
                this.selectexbox1 = button;
                this.selectexbox1.tint = 0xddffa1;
            }
        }


*/    }
    },
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);    
    },
    
   /* fadePicture: function (button) {

        var tween = this.game.add.tween(button).to({
            alpha: 0
        }, 2000, Phaser.Easing.Linear.None, true);
    },*/



    update: function () {
       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('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('Game1');  
        }
    },

    render: function () {

         //this.game.debug.text(this.time.fps || '--', 2, 14, "#00ff00");
        //this.game.debug.text( playedEquation, 80, 80);
        // game.debug.body(player);
        // game.debug.bodyInfo(player, 16, 24);

    }
};