var __extends = this.__extends || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};
var SelectImage;
var zad_zaliczone = 0;
(function (_SelectImage) {
    var SelectImage = (function (_super) {
        __extends(SelectImage, _super);
        function SelectImage(game, name, x, y, json) {
            _super.call(this, game, name, x, y, json);
            this.selectedImageList = [];
            this.textureList = [];
            this.groupCount = 0;
            this.soundBank = new Audio();
        }
        SelectImage.prototype.preload = function () {
            _super.prototype.preload.call(this);
            this.game.load.onLoadComplete.addOnce(this.create, this);
            this.loadImages();
            this.game.load.start();
        };
        SelectImage.prototype.loadImages = function () {
            for (var i = 0; i < this.Elements.TaskList.length; i++) {
                this.game.load.image(this.Elements.TaskList[i].Key, this.Elements.TaskList[i].Select_img);
                if (this.Elements.TaskList[i].AltKey)
                    this.game.load.image(this.Elements.TaskList[i].AltKey, this.Elements.TaskList[i].AltSelect_img);
            }
            for (var i = 0; i < this.Elements.StaticElements.length; i++) {
                if (this.Elements.StaticElements[i].Key)
                    this.game.load.image(this.Elements.StaticElements[i].Key, this.Elements.StaticElements[i].Select_img);
                if (this.Elements.StaticElements[i].AltKey)
                    this.game.load.image(this.Elements.StaticElements[i].AltKey, this.Elements.StaticElements[i].AltSelect_img);
            }
        };
        SelectImage.prototype.create = function () {
            if (this.json.isStandalone) {
                _super.prototype.create.call(this);
            }
            this.groupCount = this.Elements.Init.GroupCount;
            this.insertStaticImages();
            this.insertImages();
            this.selectedItemsCount = 0;
            this.NumberOfSelections = this.Elements.Init.NoSelections;
            EU.createFillElements(this);
        };
        SelectImage.prototype.insertStaticImages = function () {
            var currentAnchorX = 0;
            var maxImageSize = 0;
            var currentAnchorY = 0;
            var currentGroup = 0;
            var soundButtonWidth = 0;
            var soundButtonHeight = 0;
            if (this.Elements.Init.HasSounds == true) {
                soundButtonWidth = this.game.cache.getImage("speaker").width / 2;
                soundButtonHeight = this.game.cache.getImage("speaker").height / 2;
            }
            for (var i = 0; i < this.Elements.StaticElements.length; i++) {
                if (this.Elements.StaticElements[i].isText) {
                    this.staticImage = this.game.add.text(this.Elements.StaticElements[i].PosX, this.Elements.StaticElements[i].PosY, this.Elements.StaticElements[i].Text, this.Elements.StaticElements[i].Style);
                }
                else {
                    this.staticImage = this.game.add.sprite(this.Elements.StaticElements[i].PosX, this.Elements.StaticElements[i].PosY, this.Elements.StaticElements[i].Key);
                    this.staticImage.scale.setTo(0.8, 0.8);
                }
                this.staticImage.anchor.set(0.5);
                this.addChild(this.staticImage);
                if (this.Elements.Init.StaticHasSounds == true && this.Elements.StaticElements[i].Sound !== "") {
                    switch (this.Elements.StaticElements[i].SoundPosition) {
                        case "ToLeft":
                            {
                                this.soundButton = this.game.add.sprite(this.Elements.StaticElements[i].PosX - (this.staticImage.width / 2) - soundButtonWidth - 10, this.Elements.StaticElements[i].PosY - soundButtonHeight, "speaker");
                                this.soundButton.soundBankName = this.Elements.StaticElements[i].Sound;
                                this.addChild(this.soundButton);
                                this.soundButton.inputEnabled = true;
                                this.soundButton.events.onInputUp.add(this.soundButtonDown, this);
                                break;
                            }
                        case "ToRight":
                            {
                                this.soundButton = this.game.add.sprite(this.Elements.StaticElements[i].PosX + (this.staticImage.width / 2) + 10, this.Elements.StaticElements[i].PosY - soundButtonHeight, "speaker");
                                this.soundButton.soundBankName = this.Elements.StaticElements[i].Sound;
                                this.addChild(this.soundButton);
                                this.soundButton.inputEnabled = true;
                                this.soundButton.events.onInputUp.add(this.soundButtonDown, this);
                                break;
                            }
                        default:
                            {
                                this.soundButton = this.game.add.sprite(this.Elements.StaticElements[i].PosX + (this.staticImage.width / 2) + 10, this.Elements.StaticElements[i].PosY - soundButtonHeight, "speaker");
                                this.soundButton.soundBankName = this.Elements.StaticElements[i].Sound;
                                this.addChild(this.soundButton);
                                this.soundButton.inputEnabled = true;
                                this.soundButton.events.onInputUp.add(this.soundButtonDown, this);
                                break;
                            }
                    }
                }
            }
        };
        SelectImage.prototype.insertImages = function () {
            var currentAnchorX = 0;
            var maxImageSize = 0;
            var currentAnchorY = 0;
            var currentGroup = 0;
            var soundButtonWidth = 0;
            if (this.Elements.Init.HasSounds == true) {
                soundButtonWidth = this.game.cache.getImage("speaker").width / 2;
            }
            for (var i = 0; i < this.Elements.TaskList.length; i++) {
                if (this.Elements.TaskList[i].Group == 0) {
                    if (maxImageSize < this.game.cache.getImage(this.Elements.TaskList[i].Key).height)
                        maxImageSize = this.game.cache.getImage(this.Elements.TaskList[i].Key).height;
                    if ((currentAnchorX + this.game.cache.getImage(this.Elements.TaskList[i].Key).width + this.Elements.Init.Spacing) >= Global.Main.mainContainerDims.wd - (Global.Main.mainContainerDims.wd * 0.1)) {
                        currentAnchorX = 0;
                        currentAnchorY = currentAnchorY + maxImageSize;
                        maxImageSize = 0;
                    }
                }
                else if (this.Elements.TaskList[i].Group > 0) {
                    if (maxImageSize < this.game.cache.getImage(this.Elements.TaskList[i].Key).height)
                        maxImageSize = this.game.cache.getImage(this.Elements.TaskList[i].Key).height;
                    if (currentGroup != this.Elements.TaskList[i].Group) {
                        currentAnchorX = 0;
                        currentAnchorY = currentAnchorY + maxImageSize;
                        maxImageSize = 0;
                        currentGroup = this.Elements.TaskList[i].Group;
                    }
                }
                currentAnchorX = currentAnchorX + this.game.cache.getImage(this.Elements.TaskList[i].Key).width + this.Elements.Init.Spacing + soundButtonWidth;
                if (this.Elements.Init.ManualPosition == true)
                    this.shadow = this.game.add.sprite(this.Elements.TaskList[i].PosX, this.Elements.TaskList[i].PosY, this.Elements.TaskList[i].Key);
                else {
                    this.shadow = this.game.add.sprite(this.Elements.Init.StartingPlaceX + (currentAnchorX), (this.Elements.Init.StartingPlaceY + currentAnchorY), this.Elements.TaskList[i].Key);
                }
                this.shadow.scale.setTo(0.8, 0.8);
                this.shadow.anchor.set(0.5);
                this.shadow.tint = 0x000000;
                this.shadow.alpha = 0.6;
                this.shadow.name = this.Elements.TaskList[i].Key;
                if (this.Elements.Init.ManualPosition == true)
                    this.item = this.game.add.sprite(this.Elements.TaskList[i].PosX, this.Elements.TaskList[i].PosY, this.Elements.TaskList[i].Key);
                else {
                    this.item = this.game.add.sprite(this.Elements.Init.StartingPlaceX + (currentAnchorX), (this.Elements.Init.StartingPlaceY + currentAnchorY), this.Elements.TaskList[i].Key);
                }
                this.item.scale.setTo(0.8, 0.8);
                this.item.anchor.set(0.5, 0.5);
                this.item.isCorrect = this.Elements.TaskList[i].Correct;
                this.item.isSelected = false;
                this.item.inputEnabled = true;
                this.item.shadowObj = this.shadow;
                this.item.group = this.Elements.TaskList[i].Group;
                if (this.Elements.Init.AlternateImage) {
                    if (this.Elements.TaskList[i].AltKey == "" || this.Elements.TaskList[i].AltKey == null) {
                        this.item.orginalTexture = this.Elements.TaskList[i].Key;
                        this.item.alternativeTexture = this.Elements.TaskList[i].Key;
                    }
                    else {
                        this.item.orginalTexture = this.Elements.TaskList[i].Key;
                        this.item.alternativeTexture = this.Elements.TaskList[i].AltKey;
                    }
                }
                if (this.Elements.Init.Crossable) {
                    if (this.Elements.Init.ManualPosition == true)
                        this.crossObject = this.game.add.graphics(this.Elements.TaskList[i].PosX, this.Elements.TaskList[i].PosY);
                    else {
                        this.crossObject = this.game.add.graphics(this.Elements.Init.StartingPlaceX + (currentAnchorX), (this.Elements.Init.StartingPlaceY + currentAnchorY));
                    }
                    this.crossObject.lineStyle(4, 0xb70000, 1);
                    this.crossObject.moveTo(0 - this.item.width / 2, 0 - this.item.height / 2);
                    this.crossObject.lineTo(0 + this.item.width / 2, 0 + this.item.height / 2);
                    this.crossObject.visible = false;
                    this.item.events.onInputUp.add(this.buttonCrossing, this);
                    this.item.cross = this.crossObject;
                    this.addChild(this.crossObject);
                }
                if (!this.Elements.Init.Crossable) {
                    this.item.events.onInputUp.add(this.buttonDown, this);
                }
                if (this.Elements.Init.HasSounds == true && this.Elements.TaskList[i].Sound !== "") {
                    if (this.Elements.Init.ManualPosition == true) {
                        this.soundButton = this.game.add.sprite(this.Elements.TaskList[i].PosX + (this.item.width / 2) + 5, this.Elements.TaskList[i].PosY, "speaker");
                    }
                    else {
                        this.soundButton = this.game.add.sprite(this.Elements.Init.StartingPlaceX + (currentAnchorX) + soundButtonWidth * 2, (this.Elements.Init.StartingPlaceY + currentAnchorY), "speaker");
                    }
                    this.soundButton.soundBankName = this.Elements.TaskList[i].Sound;
                    this.addChild(this.soundButton);
                    this.soundButton.inputEnabled = true;
                    this.soundButton.events.onInputUp.add(this.soundButtonDown, this);
                }
                this.addChild(this.shadow);
                this.addChild(this.item);
                if (this.Elements.Init.Crossable)
                    this.addChild(this.crossObject);
                this.selectedImageList.push(this.item);
            }
        };
        SelectImage.prototype.soundButtonDown = function (button) {
            this.soundBank = new Audio(button.soundBankName);
            Global.Main.PlayMusic(this.soundBank);
        };
        SelectImage.prototype.buttonCrossing = function (button) {
            if (this.groupCount != 0) {
                var tmpButonsInGroup = [];
                var tmpSelectedItemsInGroup = 0;
                for (var i = 0; i < this.selectedImageList.length; i++) {
                    if (this.selectedImageList[i].group == button.group) {
                        tmpButonsInGroup.push(this.selectedImageList[i]);
                        if (this.selectedImageList[i].isSelected == true)
                            tmpSelectedItemsInGroup++;
                    }
                }
                for (var i = 0; i < tmpButonsInGroup.length; i++) {
                    if (tmpSelectedItemsInGroup >= parseInt(this.Elements.Init.NoSelections)) {
                        if (button.isSelected) {
                            tmpSelectedItemsInGroup--;
                            button.isSelected = false;
                            button.cross.visible = false;
                            break;
                        }
                        else {
                            for (var i = 0; i < tmpButonsInGroup.length; i++) {
                                if (tmpButonsInGroup[i].isSelected) {
                                    tmpButonsInGroup[i].isSelected = false;
                                    tmpButonsInGroup[i].cross.visible = false;
                                }
                            }
                            tmpSelectedItemsInGroup = 0;
                            tmpSelectedItemsInGroup++;
                            button.isSelected = !button.isSelected;
                            button.cross.visible = true;
                            break;
                        }
                    }
                    else if (button.isSelected) {
                        tmpSelectedItemsInGroup--;
                        button.isSelected = !button.isSelected;
                        button.cross.visible = false;
                        break;
                    }
                    else {
                        tmpSelectedItemsInGroup++;
                        button.isSelected = !button.isSelected;
                        button.cross.visible = true;
                        break;
                    }
                }
            }
            else {
                if ((this.selectedItemsCount >= parseInt(this.Elements.Init.NoSelections))) {
                    if (button.isSelected) {
                        this.selectedItemsCount--;
                        button.isSelected = !button.isSelected;
                        button.cross.visible = false;
                    }
                    else {
                        for (var i = 0; i < this.selectedImageList.length; i++) {
                            if (this.selectedImageList[i].isSelected) {
                                this.selectedImageList[i].isSelected = false;
                                this.selectedImageList[i].cross.visible = false;
                            }
                        }
                        this.selectedItemsCount = 0;
                        this.selectedItemsCount++;
                        button.isSelected = !button.isSelected;
                        button.cross.visible = true;
                    }
                }
                else if (button.isSelected) {
                    this.selectedItemsCount--;
                    button.isSelected = !button.isSelected;
                    button.cross.visible = false;
                }
                else {
                    this.selectedItemsCount++;
                    button.isSelected = !button.isSelected;
                    button.cross.visible = true;
                }
            }
            this.checkComplition();
        };
        SelectImage.prototype.buttonDown = function (button) {
            if (this.groupCount != 0) {
                var tmpButonsInGroup = [];
                var tmpSelectedItemsInGroup = 0;
                for (var i = 0; i < this.selectedImageList.length; i++) {
                    if (this.selectedImageList[i].group == button.group) {
                        tmpButonsInGroup.push(this.selectedImageList[i]);
                        if (this.selectedImageList[i].isSelected == true)
                            tmpSelectedItemsInGroup++;
                    }
                }
                for (var i = 0; i < tmpButonsInGroup.length; i++) {
                    if (tmpSelectedItemsInGroup >= parseInt(this.Elements.Init.NoSelections)) {
                        if (button.isSelected) {
                            if (this.Elements.Init.AlternateImage) {
                                button.loadTexture(button.orginalTexture);
                                button.shadowObj.loadTexture(button.orginalTexture);
                            }
                            tmpSelectedItemsInGroup--;
                            button.scale.setTo(0.8, 0.8);
                            button.isSelected = !button.isSelected;
                            button.shadowObj.scale.setTo(0.8, 0.8);
                            button.shadowObj.x = button.x;
                            button.shadowObj.y = button.y;
                            break;
                        }
                        else {
                            for (var i = 0; i < tmpButonsInGroup.length; i++) {
                                if (tmpButonsInGroup[i].isSelected) {
                                    tmpButonsInGroup[i].scale.setTo(0.8, 0.8);
                                    tmpButonsInGroup[i].isSelected = false;
                                    tmpButonsInGroup[i].shadowObj.scale.setTo(0.8, 0.8);
                                    tmpButonsInGroup[i].shadowObj.x = tmpButonsInGroup[i].x;
                                    tmpButonsInGroup[i].shadowObj.y = tmpButonsInGroup[i].y;
                                }
                            }
                            tmpSelectedItemsInGroup = 0;
                            tmpSelectedItemsInGroup++;
                            button.scale.setTo(1, 1);
                            if (this.Elements.Init.AlternateImage) {
                                button.loadTexture(button.alternativeTexture);
                                button.shadowObj.loadTexture(button.alternativeTexture);
                            }
                            button.isSelected = !button.isSelected;
                            button.shadowObj.scale.setTo(1, 1);
                            button.shadowObj.x = button.x + 8;
                            button.shadowObj.y = button.y + 10;
                            break;
                        }
                    }
                    else if (button.isSelected) {
                        tmpSelectedItemsInGroup--;
                        button.scale.setTo(0.8, 0.8);
                        if (this.Elements.Init.AlternateImage) {
                            button.loadTexture(button.orginalTexture);
                            button.shadowObj.loadTexture(button.orginalTexture);
                        }
                        button.isSelected = !button.isSelected;
                        button.shadowObj.scale.setTo(0.8, 0.8);
                        button.shadowObj.x = button.x;
                        button.shadowObj.y = button.y;
                        break;
                    }
                    else {
                        tmpSelectedItemsInGroup++;
                        button.scale.setTo(1, 1);
                        if (this.Elements.Init.AlternateImage) {
                            button.loadTexture(button.alternativeTexture);
                            button.shadowObj.loadTexture(button.alternativeTexture);
                        }
                        button.isSelected = !button.isSelected;
                        button.shadowObj.scale.setTo(1, 1);
                        button.shadowObj.x = button.x + 8;
                        button.shadowObj.y = button.y + 10;
                        break;
                    }
                }
            }
            else {
                if ((this.selectedItemsCount >= parseInt(this.Elements.Init.NoSelections))) {
                    if (button.isSelected) {
                        this.selectedItemsCount--;
                        button.scale.setTo(0.8, 0.8);
                        if (this.Elements.Init.AlternateImage) {
                            button.loadTexture(button.orginalTexture);
                            button.shadowObj.loadTexture(button.orginalTexture);
                        }
                        button.isSelected = !button.isSelected;
                        button.shadowObj.scale.setTo(0.8, 0.8);
                        button.shadowObj.x = button.x;
                        button.shadowObj.y = button.y;
                    }
                    else {
                        for (var i = 0; i < this.selectedImageList.length; i++) {
                            if (this.selectedImageList[i].isSelected) {
                                this.selectedImageList[i].scale.setTo(0.8, 0.8);
                                this.selectedImageList[i].isSelected = false;
                                this.selectedImageList[i].shadowObj.scale.setTo(0.8, 0.8);
                                this.selectedImageList[i].shadowObj.x = this.selectedImageList[i].x;
                                this.selectedImageList[i].shadowObj.y = this.selectedImageList[i].y;
                            }
                        }
                        this.selectedItemsCount = 0;
                        this.selectedItemsCount++;
                        button.scale.setTo(1, 1);
                        if (this.Elements.Init.AlternateImage) {
                            button.loadTexture(button.alternativeTexture);
                            button.shadowObj.loadTexture(button.alternativeTexture);
                        }
                        button.isSelected = !button.isSelected;
                        button.shadowObj.scale.setTo(1, 1);
                        button.shadowObj.x = button.x + 8;
                        button.shadowObj.y = button.y + 10;
                    }
                }
                else if (button.isSelected) {
                    this.selectedItemsCount--;
                    button.scale.setTo(0.8, 0.8);
                    if (this.Elements.Init.AlternateImage) {
                        button.loadTexture(button.orginalTexture);
                        button.shadowObj.loadTexture(button.orginalTexture);
                    }
                    button.isSelected = !button.isSelected;
                    button.shadowObj.scale.setTo(0.8, 0.8);
                    button.shadowObj.x = button.x;
                    button.shadowObj.y = button.y;
                }
                else {
                    this.selectedItemsCount++;
                    button.scale.setTo(1, 1);
                    if (this.Elements.Init.AlternateImage) {
                        button.loadTexture(button.alternativeTexture);
                        button.shadowObj.loadTexture(button.alternativeTexture);
                    }
                    button.isSelected = !button.isSelected;
                    button.shadowObj.scale.setTo(1, 1);
                    button.shadowObj.x = button.x + 8;
                    button.shadowObj.y = button.y + 10;
                }
            }
            this.checkComplition();
        };
		SelectImage.prototype.nextScreen = function () {
			if (zad_zaliczone == 1){
				this.game.input.enabled = false;
				EU.killKeyboard();
				var tw = this.game.add.tween(this.game.camera);
				tw.to({ x: this.game.camera.x + Global.Main.dims.x }, 1500, Phaser.Easing.Exponential.Out);
				tw.onComplete.add(function () {
					this.game.input.enabled = true;
				}, this);
				tw.start();
				Global.Main.currentPageNumber++;
				Global.Main.returnPage(Global.Main.currentPageNumber).pageChange();
				console.log(Global.Main.currentPageNumber);
			}
			else {
				//nie rób nic
			}
        };
        SelectImage.prototype.checkComplition = function () {
            var localComplitionState = 0;
            var localFalseComplitionState = 0;
            var groupTrueStatements = 0;
            var groupFalseStatemests = 0;
            for (var i = 0; i < this.selectedImageList.length; i++) {
                if ((this.selectedImageList[i].isSelected == true) && (this.selectedImageList[i].isCorrect == "true"))
                    localComplitionState++;
                if ((this.selectedImageList[i].isSelected == false) && (this.selectedImageList[i].isCorrect == "false"))
                    localFalseComplitionState++;
            }
            if (this.groupCount == 0) {
                if (localComplitionState == this.NumberOfSelections && EU.checkIfFillsAreCorrect(this)) {
					zad_zaliczone = 1;
					console.log("zad_zaliczone = "+zad_zaliczone);
                }
                else {
					zad_zaliczone = 0;
					console.log("zad_zaliczone = "+zad_zaliczone);
                }
            }
            else
                (this.groupCount != 0);
            {
                for (var i = 0; i < this.selectedImageList.length; i++) {
                    if (this.selectedImageList[i].isCorrect == "true")
                        groupTrueStatements++;
                    if (this.selectedImageList[i].isCorrect == "false")
                        groupFalseStatemests++;
                }
                if (localComplitionState == groupTrueStatements && localFalseComplitionState == groupFalseStatemests && EU.checkIfFillsAreCorrect(this)) {
					zad_zaliczone = 1;
					console.log("zad_zaliczone = "+zad_zaliczone);
                    if (TYPE === "gm") {
						var next = this.game.add.button(1825 - 120 - 76, 730, "next", this.nextScreen, this, 0, 0, 1, 0);
						next.scale.setTo(0.8,0.8);
						this.addChild(next);
                        _super.prototype.nextTask.call(this);
                    }
                }
                else {
					zad_zaliczone = 0;
					console.log("zad_zaliczone = "+zad_zaliczone);
                }
            }
        };
        SelectImage.prototype.init = function (json) {
            _super.prototype.init.call(this, json);
            this.Elements = json.GameData;
        };
        return SelectImage;
    })(Base.BasicGroup);
    _SelectImage.SelectImage = SelectImage;
})(SelectImage || (SelectImage = {}));
//# sourceMappingURL=SelectImage.js.map