var spriteList= [];
var lettercode = 0;
var a = [];
var GoodJob = false;
var PuzzlePlace;
Game1.Game = function (game) {
    var buttonLeft;
    var buttonRight;
    var int = 0;  
    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 (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 () {
	
		a = ['a', 'aa', 'b', 'c', 'cc', 'd', 'e', 'ee', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'll', 'm', 'n', 'nn', 'o', 'oo', 'p', 'r', 's', 'ss', 't', 'u', 'w', 'y', 'z', 'zz', 'zzz'];
	
		for ( var j = 0; j < a.length; j++)
		{
			{
				for (var i = 1; i < 5; i++ )
				{
					this.load.image(a[j]+"_"+i, "img/"+a[j]+"_0"+i+".png");  
				}
			}   
		}
	
		shuffle(a);
		lektor = new Audio("./sound/MW_g_I_6.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.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;
		this.CreatePuzzle();    
		this.showInstruction();
		lektor.play();                           
    },
    CreatePuzzle: function(){    
		if (spriteList.length >1)
		{
		
			for (var i = 0 ; i<spriteList.length; i++)
			{
				spriteList[i].kill;
				spriteList[i].visible = false;
			}
		
		}
		spriteList = [];
           
		for (var i = 0; i < 4; i++) {
			if (i<2){
				this.item = this.game.add.sprite(420 , 200 + ((460*0.8)*(i%2) ), a[lettercode]+'_'+(i+1), 0);
				this.item.properX = 0;   
            }
			else if(i>=2)
            {
				this.item = this.game.add.sprite(420+ ((658*0.8)*1), 200 + ((460*0.8)*(i%2) ), a[lettercode]+'_'+(i+1), 0);
				this.item.properX = 1; 
            }
            this.item.name= i;
            this.item.value= i;
            this.item.properY =Math.floor(i/2); 
            this.item.scale.setTo(0.8,0.8);
            this.game.physics.arcade.enable(this.item);
            this.item.inputEnabled = true;
            this.item.input.enableDrag(false, true);
            this.item.orginalPositionX = this.item.x; this.item.orginalPositionY = this.item.y;
            this.item.events.onInputUp.add(this.down, this);
            spriteList.push(this.item);
        }  
		
        this.ShufflePuzzle();
    },
    ShufflePuzzle: function(){
		var j = spriteList.length;
        var i = 0;
        
		while (0 !== j) 
		{
			i = Math.floor(Math.random() * j);
			j -= 1;  
            spriteList[j].x = spriteList[i].x;
            spriteList[j].y = spriteList[i].y;
            spriteList[i].x = spriteList[j].orginalPositionX;
            spriteList[i].y = spriteList[j].orginalPositionY;
            spriteList[j].orginalPositionX = spriteList[j].x;
            spriteList[j].orginalPositionY = spriteList[j].y;
            spriteList[i].orginalPositionX = spriteList[i].x;
            spriteList[i].orginalPositionY = spriteList[i].y;
			var tmlvalue = spriteList[j].value;
            spriteList[j].value = spriteList[i].value;
            spriteList[i].value = tmlvalue;
        }
		
		PuzzlePlace = "";    
		
		for (var i = 0; i<spriteList.length; i++)
		{
			PuzzlePlace += spriteList[i].value;
		} 
		
		if (PuzzlePlace == "0213" || PuzzlePlace == '2031')
		{
			this.ShufflePuzzle();
		}
    },
    up: function (button, pointer) {
        button.frame = 0;
    },
    down2: function (button, pointer) {
        button.frame = 1;
        if (button.name == 'Speaker') {
            if (!isPlaying(sound) || sound.ended)
            intro = true;
            this.showInstruction();
            lektor.play();
        } 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.fixedToCamera = true;
        this.guiBubble = this.game.add.sprite(135, 96, 'Finish');
        this.guiBubble.fixedToCamera = true;
        this.titleText = this.game.add.text(240, 440, 'Ułóż literę z puzzli.', {
            font: "78px 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 = new Audio("./sound/"+a[lettercode]+".mp3");
        sound.load();
        sound.loop = false;
        sound.play();
    },
    showFinnish: function () {
        this.guiHelp = this.game.add.sprite(1410, 380, 'guiPomocnik');
        this.guiHelp.fixedToCamera = true;
        this.guiBubble = this.game.add.sprite(135, 96, 'Finish');
        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) {
        var InGoodPlace = 0;    
		
		for (var i = 0; i<spriteList.length; i++)
		{
			if (spriteList[i].input.checkPointerOver(pointer,true)){
				button.x = spriteList[i].x;
				button.y = spriteList[i].y;
				spriteList[i].x = button.orginalPositionX;
				spriteList[i].y = button.orginalPositionY;
				button.orginalPositionX = button.x;
				button.orginalPositionY = button.y;
				spriteList[i].orginalPositionX = spriteList[i].x;
				spriteList[i].orginalPositionY = spriteList[i].y;
				var tmlvalue = button.value;
				button.value = spriteList[i].value;
				spriteList[i].value = tmlvalue;
            }
        }
		
		PuzzlePlace = "";
		var NameList = "";    
		
		for (var i = 0; i<spriteList.length; i++)
		{
			PuzzlePlace += spriteList[i].value;
			NameList += spriteList[i].name;  
		} 
		
		if (PuzzlePlace == "0213" || PuzzlePlace == '2031')
		{
			GoodJob = true;
			this.playSound();
		}
    },
    fadePicture: function (button) {
        var tween = this.game.add.tween(button).to({
            alpha: 0
        }, 2000, Phaser.Easing.Linear.None, true);
    },
    update: function () {
		if (GoodJob && (!isPlaying(sound) || sound.ended))
		{
			GoodJob = false;
			lettercode++;
			if (lettercode <= 32)
				this.CreatePuzzle();
		}
        if (lettercode == 32 && (!isPlaying(sound) || sound.ended)) {
            lettercode = 33;
            playedEquation = 13;
			
			for (var i = 0 ; i<spriteList.length; i++)
			{
				spriteList[i].kill;
				spriteList[i].visible = false;
			}
            
			this.showFinnish();
        }
        if (playedEquation == 13 && (!isPlaying(finnish) || finnish.ended)) {
            this.state.start('Menu');
			location.reload();
        }
        if (playedEquation < 12) {
            if ((!isPlaying(lektor)||  lektor.ended) && intro) {
                intro = false;
                this.hideInstruction();
                isReadyToRead = true;
            }
            if (isReadyToRead && (!isPlaying(sound) || sound.ended)) {
                isReadyToRead = false;
            }
        }
    },
    render: function () {
    }
};