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

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 () {
        variant1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
        shuffle(variant1);
        lektor = new Audio("./sound/MW_g_I_9.mp3");
        lektor.load();
        finnish = new Audio("./sound/gratulacje.mp3");
        finnish.load();
        sound = new Audio();
        equationTable = [15, 12, 13, 20, 11, 8, 15, 19,17,2, 10,6];
        playedEquation = 0;
        isReadyToRead = false;
        intro = true;
        this.bg = this.game.add.sprite(0, 0, 'bg');
        this.game.stage.backgroundColor = '#ffffff';
        this.game.physics.startSystem(Phaser.Physics.ARCADE);
        this.time.advancedTiming = true;
        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.buttonHelp = this.game.add.sprite((1920 - 150 / 2 - 132), 100 / 2, 'help', 0);
        this.buttonHelp.scale.setTo(0.9,0.9);
        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.buttonRight = this.game.add.sprite(220/2, 900/2, 'arrow',2);
        //    this.buttonRight.name = "right";
        //    this.buttonRight.inputEnabled = true;
        //    this.buttonRight.fixedToCamera = true;
        ///*    this.buttonRight.events.onInputOver.add(this.over,this);
        //    this.buttonRight.events.onInputOut.add(this.out,this);
        //    this.buttonRight.events.onInputDown.add(this.down,this);
        //    this.buttonRight.events.onInputUp.add(this.up,this);   */
        var item;
        var selectexbox1 = null;
        var selectexbox2 = null;
        var TotalPoints = 0;
        var equal;
        var j;
        for (var i = 0; i < 12; i++) {

            this.item = this.game.add.sprite(150 + (400 * (i % 4)), 200 + (140 * (i % 3)), 'dzialanie' + (i + 1), 0);
            this.game.physics.arcade.enable(this.item);
            this.item.inputEnabled = true;
            this.item.name = 'rownanie';

            this.item.SoundName = i + 1;
            this.item.events.onInputDown.add(this.down, this);
        }

        for (var i = 0; i < 12; i++) {
            if (i < 6) {
                this.item = this.game.add.sprite(150 + (278 * (i % 6)), 680, 'odpowedz' + (i + 1), 0);
            } else if (i >= 6) {
                this.item = this.game.add.sprite(150 + (278 * (i % 6)), 680 + (140), 'odpowedz' + (i + 1), 0);
            }
            this.game.physics.arcade.enable(this.item);
            this.item.inputEnabled = true;
            this.item.name = 'odpowiedz';
            this.item.answer = equationTable[i];
            this.item.events.onInputDown.add(this.down, this);
        }
        this.showInstruction();
        lektor.play();

        //   console.log('variant1 = '+variant1);
        //   console.log('file = '+variant1[playedEquation])
        //   console.log('equetion = '+equationTable[[playedEquation]])
        //  item.events.onDragStop.add(fixLocation);                               
    },
    up: function (button, pointer) {
        button.frame = 0;
    },
    down2: function (button, pointer) {
        button.frame = 1;
        if (button.name == 'Speaker') {
            if (!isPlaying(sound) || sound.ended)
                this.playSound();
        } else if (!isPlaying(lektor) || lektor.ended) {
            intro = true;
            this.showInstruction();
            lektor.play();
        }
    },
    showInstruction: function () {
        this.guiHelp = this.game.add.sprite(1410, 380, 'guiPomocnik');
        //this.guiHelp.scale.setTo(0.5,0.5);
        this.guiHelp.fixedToCamera = true;
        this.guiBubble = this.game.add.sprite(135, 96, 'Finish');
        // this.guiBubble.scale.setTo(0.5,0.5); 
        this.guiBubble.fixedToCamera = true;
        this.titleText = this.game.add.text(240, 440, 'U góry są działania a na dole wyniki.\nWskaż działania odczytanie przez lektora\na następnie jego wynik.', {
            font: "58px Arial",
            fill: "#000000",
        });
        this.titleText.fixedToCamera = true;
    },

    hideInstruction: function () {
        this.guiHelp.visible = false;
        this.guiBubble.visible = false;
        this.titleText.visible = false;
        //this.lektor.hide();
    },

    stopDrag: function () {

        // And re-enable it upon release
        this.sprite.body.moves = true;

    },
    playSound: function () {
        sound = new Audio("./sound/MW_g_I_9_" + variant1[playedEquation] + ".mp3");
        sound.load();
        sound.loop = false;
        sound.play();
    
    },
    playWrong: function () {
    sound.pause();
        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.guiHelp = this.game.add.sprite(1410, 380, 'guiPomocnik');
        //this.guiHelp.scale.setTo(0.5,0.5);
        this.guiHelp.fixedToCamera = true;
        this.guiBubble = this.game.add.sprite(135, 96, 'Finish');
        // this.guiBubble.scale.setTo(0.5,0.5); 
        this.guiBubble.fixedToCamera = true;
        this.titleText = this.game.add.text(250, 400, 'Gratulacje!', {
            font: "58px Arial",
            fill: "#000000",
        });
        this.titleText.fixedToCamera = true;
        finnish.play();
    },
    hideFinnish: function()
    {
    this.guiHelp.kill();    
    this.guiBubble.kill();
    this.state.start('Menu');
    },
    down: function (button, pointer) {
       console.log("test: "+ playedEquation);
        //console.log('equetion = '+equationTable[playedEquation]);
        //console.log('eq z variant1: '+ equationTable[(variant1[playedEquation])-1])    
        if (this.selectexbox1 == null && ( !isPlaying(lektor) || lektor.ended)) {
            //console.log("button.SoundName: "+button.SoundName+variant1[playedEquation]+" = "+equationTable[variant1[playedEquation]]);
             console.log('equetion = '+equationTable[variant1[playedEquation]]);
            if (button.SoundName == variant1[playedEquation]) {
                //if (button.CustomArgument == "+")
                // this.equal = parseInt(button.CustomValue1) + parseInt(button.CustomValue2);
                // if (button.CustomArgument =="-")
                // this.equal = parseInt(button.CustomValue1) - parseInt(button.CustomValue2);

                // console.log('this.name:'+ button.name+'its value is: '+ button.CustomValue1+button.CustomArgument+button.CustomValue2+ " = " + this.equal);
                this.selectexbox1 = button;
                this.selectexbox1.frame = 1;
                //this.game.add.tween(this.selectexbox1.scale).to({x:1.1, y:1.1},100,Phaser.Easing.Linear.None, true);
            } else {
                this.playWrong();
                this.selectexbox1 = null;
                //odtworzenie innego banku dzwieku.
                //this.game.add.tween(this.selectexbox1.scale).to({x:1, y:1},100,Phaser.Easing.Linear.None, true);
                //this.selectexbox1 = null;    

            }

            //this.selectexbox1
        }
        if (this.selectexbox1 != null && this.selectexbox2 == null) {
            if (button.name == "odpowiedz") {
                this.selectexbox2 = button;
                this.selectexbox2.frame = 1;
                // this.game.add.tween(this.selectexbox2.scale).to({x:1.1, y:1.1},200,Phaser.Easing.Linear.None, true);
                if (this.selectexbox2.answer != null)
                console.log("!!: "+this.selectexbox2.answer);   
                    if (this.selectexbox2.answer == equationTable[(variant1[playedEquation]) - 1]) {
                        this.selectexbox2.frame = 1;
                        this.fadePicture(this.selectexbox1);
                        this.fadePicture(this.selectexbox2);

                        this.selectexbox1 = null;
                        this.selectexbox2 = null;
                        this.TotalPoints++;
                        
                        playedEquation++;
                        this.playGood();
                        isReadyToRead = true;
                    } else {
                        this.playWrong();
                        //this.selectexbox1.frame = 0;
                        this.selectexbox2.frame = 0;
                        //this.game.add.tween(this.selectexbox2.scale).to({x:1, y:1},200,Phaser.Easing.Linear.None, true);
                        //this.game.add.tween(this.selectexbox1.scale).to({x:1, y:1},200,Phaser.Easing.Linear.None, true);
                        //this.selectexbox1 = null;
                        this.selectexbox2 = null;

                    }
                console.log("2gie klikniecie sprawdzenie:" + this.equal);
            } else {
                console.log('Zaznaczono rownanie a nie odpowedz!');
            }
        }

        //this.fadePicture(button);    
    },

    fadePicture: function (button) {

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



    update: function () {
        if (playedEquation == 12 && (!isPlaying(sound) || sound.ended)) {
            playedEquation = 13;
            this.showFinnish();
        }
        if (playedEquation == 13 && (!isPlaying(finnish) || finnish.ended)) {
            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();
            }
        }
    },

    render: function () {

        // this.game.debug.text(this.time.fps || '--', 2, 14, "#00ff00");
        //this.game.debug.text(!isPlaying(lektor) + " " + "playedEquation:" + playedEquation, 40, 40);
        // game.debug.body(player);
        // game.debug.bodyInfo(player, 16, 24);

    }
};