﻿function MTurret(point, name) {
    this.init(point, name);
    this.setMarker();
    
    //Initial parameters
    this.upgradePrice = 2;
    this.sellPrice = 2;
    this.damagePower = MTurret.damagePower;
    this.range = MTurret.range; //Range in meters
}
MTurret.inheritsFrom(Turret);

//Instance methods
MTurret.prototype.upgrade = function() {
    this.level++;
    switch (this.level) {
        case 2:
            this.upgradePrice += 3;
            this.damagePower += 0.5;
            this.sellPrice += 1;
            break;
        case 3:
            this.upgradePrice += 3;
            this.range += 25;
            this.sellPrice += 1;
            break;
        case 4:
            this.upgradePrice += 3;
            this.range += 25;
			this.damagePower += 0.5;
            this.sellPrice += 1;
            break;
    }
}

MTurret.prototype.attackTargets = function() {
    this.setAttacking(true);
	this.targets[0].takeDamage(this.damagePower);
}

MTurret.prototype.onClick = function(latlng) {
    infoHTML =
    this.name + " Level " + this.level +
    "<Div Style='text-align:left'>" +
    "<BR />Damage: " + this.damagePower +
    "<BR />Range: " + this.range + " meters" +
    "<BR /><a href=\"javascript:game.removeTurret('" + this.name + "');\">Sell this turret for $" + this.sellPrice + "</a>";

    if (this.level < this.constructor.maxLevel)
        infoHTML += "<BR /><a href=\"javascript:game.upgradeTurret('" + this.name + "');\">Upgrade this turret for $" + this.upgradePrice + "</a>";

    infoHTML += this.constructor.info;
    infoHTML += "</Div>";
    this.overlayMarker.openInfoWindowHtml(infoHTML);
}

MTurret.prototype.setMarker = function() {
    oldMarker = this.marker;
    //console.log("Setting turret: " + this.curFaceDir + " and " + this.isAttacking);
    switch (this.curFaceDir) {
        case "nw":
            if (this.isAttacking)
                this.marker = new GMarker(this.point, { icon: this.constructor.facingNWFiringIcon, title: this.name, zIndexProcess: function(){return -50;} });
            else
                this.marker = new GMarker(this.point, { icon: this.constructor.facingNWIcon, title: this.name, zIndexProcess: function(){return -50;} });
            break;
        case "sw":
            if (this.isAttacking)
                this.marker = new GMarker(this.point, { icon: this.constructor.facingSWFiringIcon, title: this.name, zIndexProcess: function(){return -50;} });
            else
                this.marker = new GMarker(this.point, { icon: this.constructor.facingSWIcon, title: this.name, zIndexProcess: function(){return -50;} });
            break;
        case "ne":
            if (this.isAttacking)
                this.marker = new GMarker(this.point, { icon: this.constructor.facingNEFiringIcon, title: this.name, zIndexProcess: function(){return -50;} });
            else
                this.marker = new GMarker(this.point, { icon: this.constructor.facingNEIcon, title: this.name, zIndexProcess: function(){return -50;} });
            break;
        case "se":
            if (this.isAttacking)
                this.marker = new GMarker(this.point, { icon: this.constructor.facingSEFiringIcon, title: this.name, zIndexProcess: function(){return -50;} });
            else
                this.marker = new GMarker(this.point, { icon: this.constructor.facingSEIcon, title: this.name, zIndexProcess: function(){return -50;} });
            break;
    }
    GEvent.bind(this.marker, "click", this, this.onClick);
    this.onChangeIcon.fire(oldMarker);
}

//Static properties
MTurret.price = 4;
MTurret.maxLevel = 4;
MTurret.attackColor = null;
MTurret.info = "";
MTurret.damagePower = 0.5;
MTurret.range = 200;

//Static icon properties
var smallMapIcon = new GIcon(G_DEFAULT_ICON);
smallMapIcon.image = "http://labs.ribaudio.com/herosquad/img/turretFacingNW.png";
smallMapIcon.shadow = "";
smallMapIcon.iconSize = new GSize(20, 20);
smallMapIcon.iconAnchor = new GPoint(10, 15);
MTurret.smallMapIcon = smallMapIcon;

var facingNWIcon = new GIcon(G_DEFAULT_ICON);
facingNWIcon.image = "http://labs.ribaudio.com/herosquad/img/turretFacingNW.png";
facingNWIcon.shadow = "";
facingNWIcon.iconSize = new GSize(40, 40);
facingNWIcon.iconAnchor = new GPoint(20, 30);
facingNWIcon.imageMap = [0, 0, 39, 0, 39, 39, 0, 39];
MTurret.facingNWIcon = facingNWIcon;

MTurret.facingNEIcon = new GIcon(MTurret.facingNWIcon, "http://labs.ribaudio.com/herosquad/img/turretFacingNE.png");
MTurret.facingSWIcon = new GIcon(MTurret.facingNWIcon, "http://labs.ribaudio.com/herosquad/img/turretFacingSW.png");
MTurret.facingSEIcon = new GIcon(MTurret.facingNWIcon, "http://labs.ribaudio.com/herosquad/img/turretFacingSE.png");

MTurret.facingNWFiringIcon = new GIcon(MTurret.facingNWIcon, "http://labs.ribaudio.com/herosquad/img/turretFacingNWFiring.png");
MTurret.facingNEFiringIcon = new GIcon(MTurret.facingNWIcon, "http://labs.ribaudio.com/herosquad/img/turretFacingNEFiring.png");
MTurret.facingSWFiringIcon = new GIcon(MTurret.facingNWIcon, "http://labs.ribaudio.com/herosquad/img/turretFacingSWFiring.png");
MTurret.facingSEFiringIcon = new GIcon(MTurret.facingNWIcon, "http://labs.ribaudio.com/herosquad/img/turretFacingSEFiring.png");