var GA = [];
var playedEquationGame3;
Game1.Game3 = function (game) {
    var buttonLeft;
    var buttonRight;
    var int = 0;
    var spriteList = new Array();
    var selectedbox1;
    var selectedbox2;
    var equationTable;
    var isReadyToRead;
    var intro;
    var input = 0;
    var calc_value;
    var text ;
};

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.Game3.prototype = {
    create: function () {
        GA = [];
        GA = [1,2,4,6];
        calc_value ="";
        var input = "0";
        lektor = new Audio("./sound/ZIP_g_I_2_7.mp3");
        lektor.load();
        finnish = new Audio("");
        finnish.load();
        sound = new Audio();
        variant1 = [1,2,3,4,5,6];
        shuffle(variant1);
        playedEquationGame3 = 0;
        isReadyToRead = false;
        intro = true;  
        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((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);
        var item;
        var selectexbox1 = null;
        var selectexbox2 = null;
        var TotalPoints = 0;
        var equal;
        var j;

        for (var i = 0; i<6; i++)
        {
            if (i<3){
				this.item = this.game.add.sprite(356+(420*(i)), 320, "op_"+variant1[i]);
				this.item.anchor.x=0.5;
				this.item.anchor.y=0.5;        
				this.item.scale.setTo(0.8,0.8);    
				this.game.physics.arcade.enable(this.item);
				this.item.inputEnabled = true;
				this.item.name = 'rownanie';
				this.item.value = variant1[i];
				this.item.events.onInputDown.add(this.down, this);   
            }
			if (i>=3){
				this.item = this.game.add.sprite(356+(420*(i-3)), 320+(380), "op_"+variant1[i]);
				this.item.anchor.x=0.5;
				this.item.anchor.y=0.5; 
				this.item.scale.setTo(0.8,0.8);    
				this.game.physics.arcade.enable(this.item);
				this.item.inputEnabled = true;
				this.item.name = 'rownanie';
				this.item.value = variant1[i];
				this.item.events.onInputDown.add(this.down, this);   
            }    
        }
        
       this.showInstruction();
       lektor.play();                       
    },
    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, 450, 'guiPomocnik_good');
        this.guiHelp.fixedToCamera = true;
        this.guiBubble = this.game.add.sprite(135, 96, 'Finish');
         this.guiBubble.scale.setTo(1.6,1.6); 
        this.guiBubble.fixedToCamera = true;
        this.titleText = this.game.add.text(250, 400, 'Jakie zwierzę można spotkać w parku?', {
            font: "50px Arial",
            fill: "#000000",
        });
        this.titleText.fixedToCamera = true;
    },
    hideInstruction: function () {
        this.guiHelp.visible = false;
        this.guiBubble.visible = false;
        this.titleText.visible = false;
    },
    stopDrag: function () {
        this.sprite.body.moves = true;
    },
    playSound: function () {
        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 () { 
		playedEquationGame3++;
        sound.pause();
        sound = new Audio("./sound/d_" + (Math.floor(Math.random() * 4) + 1) + ".mp3");
        sound.load();
        sound.loop = false;
        sound.play();
    },
    showFinnish: function () {
        this.introBG = this.game.add.sprite(-1000, 0, 'bg_ingame0');

        for (var i = 1; i < 10; i++) {
            if (i < 4) {
                this.sky = this.game.add.sprite((Math.random() * (-1000 * i)), 0 + (100 * (i - 1)), 'sky_' + (i));
            }
            if (i >= 4 && i < 7) {
                this.sky = this.game.add.sprite(600 + (Math.random() * (200 * i)), 0 + (100 * ((i - 3) - 1)), 'sky_' + ((i - 3)));
            }
            if (i >= 7) {
                this.sky = this.game.add.sprite((Math.random() * (-1000 * (i - 6))), 0 + (100 * ((i - 6) - 1)), 'sky_' + (i - 6));
            }
            SkyTable.push(this.sky);
        }

        this.game.add.tween(SkyTable[0]).to({
            x: this.game.world.width
        }, 17676, Phaser.Easing.Linear.None, true, 0, -1);
        this.game.add.tween(SkyTable[1]).to({
            x: this.game.world.width
        }, 18630, Phaser.Easing.Linear.None, true, 0, -1);
        this.game.add.tween(SkyTable[2]).to({
            x: this.game.world.width
        }, 19760, Phaser.Easing.Linear.None, true, 0, -1);
        this.game.add.tween(SkyTable[6]).to({
            x: (this.game.world.width)
        }, 17676, Phaser.Easing.Linear.None, true, 9000, -1);
        this.game.add.tween(SkyTable[7]).to({
            x: (this.game.world.width)
        }, 18630, Phaser.Easing.Linear.None, true, 9000, -1);
        this.game.add.tween(SkyTable[8]).to({
            x: (this.game.world.width)
        }, 19760, Phaser.Easing.Linear.None, true, 9000, -1);
        this.game.add.tween(SkyTable[3]).to({
            x: (this.game.world.width + 1000)
        }, 17676, Phaser.Easing.Linear.None, true, 0, 0);
        this.game.add.tween(SkyTable[4]).to({
            x: (this.game.world.width + 1000)
        }, 18630, Phaser.Easing.Linear.None, true, 0, 0);
        this.game.add.tween(SkyTable[5]).to({
            x: (this.game.world.width + 1000)
        }, 19760, Phaser.Easing.Linear.None, true, 0, 0);
        player = this.add.sprite(1100, 700, 'gracz');
        this.physics.enable(player, Phaser.Physics.ARCADE);
        player.body.collideWorldBounds = true;
        player.body.setSize(115, 210, 0, 0);
        this.guiHelp = this.game.add.sprite(1410, 450, 'guiPomocnik_good');
        this.guiHelp.fixedToCamera = true;
        this.guiBubble = this.game.add.sprite(135, 96, 'Finish');
        this.guiBubble.scale.setTo(1.6,1.6); 
        this.guiBubble.fixedToCamera = true;
        this.titleText = this.game.add.text(250, 400, '', {
            font: "58px Arial",
            fill: "#000000",
        });
        this.titleText.fixedToCamera = true;
        finnish.play();
    },
    hideFinnish: function () {
        setTimeout(function(){
			playedEquationGame3 = 14;
		}, 5000);     
    },
    down: function (button, pointer) {
        if (playedEquationGame3 <4){
            for (var i = 0; i < GA.length; i++){
                if (button.value == GA[i] && button.scale.x != 1.2 && button.scale.y != 1.2)
                {
					this.playGood();
					button.scale.setTo(1.2,1.2);
					break;
                }
				else if (button.scale.x == 1.2 && button.scale.y == 1.2)
				{	
					playedEquationGame3--;
					button.scale.setTo(0.8,0.8);
					break;
                }
                else (button.value != GA[i])
                {
					this.playWrong();
                    button.tint = 0xffffff;
					button.scale.setTo(0.8,0.8);
                }
           }
		}
    },
    update: function () {
        if (playedEquationGame3 == 4 && (!isPlaying(sound) || sound.ended)) {
            sound.pause();
            playedEquationGame3 = 13;
            this.state.start('Game4'); 
        }
        if (playedEquationGame3 == 13 && (!isPlaying(finnish) || finnish.ended)) {
        }
        if (playedEquationGame3 < 4) {
            if ((!isPlaying(lektor) || lektor.ended) && intro) {
                intro = false;
                this.hideInstruction();
                isReadyToRead = true;
            }
        }
        if (playedEquationGame3 ==14)
        {
           this.state.start('Game4');  
        }
    },
    render: function () {
    }
};