﻿function ITurret(point, name) {
    this.init(point, name);
    this.setMarker();
    
    //Initial parameters
    this.upgradePrice = 3;
    this.slowDivider = 2.0;
    this.effectDuration = 5000; //in Ms
    this.sellPrice = 2;
    this.damagePower = ITurret.damagePower;
    this.range = ITurret.range; //Range in meters
    this.maxTargets = ITurret.maxTargets;
}
ITurret.inheritsFrom(Turret);

//Instance methods
ITurret.prototype.upgrade = function() {
    this.level++;
    switch (this.level) {
        case 2:
            this.upgradePrice += 3;
            this.range += 25;
			this.maxTargets +=1;
            this.sellPrice += 1;
            break;
        case 3:
            this.upgradePrice += 4;
            this.range += 25;
			this.maxTargets +=1;
            this.sellPrice += 1;
            break;
        case 4:
            this.upgradePrice += 4;
            this.range += 25;
            this.damgePower += 1;
            this.sellPrice += 1;
            break;
    }
}

ITurret.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);
}

ITurret.prototype.attackTargets = function() {
    this.setAttacking(true);
	//Attack up to 3 targets
	var numTargets = this.maxTargets;
	if(this.targets.length < numTargets)numTargets = this.targets.length;
	
	for(var i=0;i<numTargets;i++)
	{
	    this.targets[i].takeDamage(this.damagePower);
    	this.targets[i].slowSpeed(this.slowDivider, this.effectDuration);
	}
	
	this.onAttack.fire(ITurret.attackColor);
}

ITurret.prototype.setMarker = function() {
    oldMarker = this.marker;
    //console.log("Setting turret: " + this.curFaceDir + " and " + this.isAttacking);
    switch (this.curFaceDir) {
        case "nw":
            this.marker = new GMarker(this.point, { icon: this.constructor.facingNWIcon, title: this.name, zIndexProcess: function(){return -50;}});
            break;
        case "sw":
            this.marker = new GMarker(this.point, { icon: this.constructor.facingSWIcon, title: this.name, zIndexProcess: function(){return -50;}});
            break;
        case "ne":
            this.marker = new GMarker(this.point, { icon: this.constructor.facingNEIcon, title: this.name, zIndexProcess: function(){return -50;}});
            break;
        case "se":
            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
ITurret.price = 7;
ITurret.maxLevel = 4;
ITurret.attackColor = "#51F8F6";
ITurret.info = "<BR />This turret slows enemies";
ITurret.range = 150;
ITurret.damagePower = 0.5;
ITurret.maxTargets = 2;

//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);
ITurret.smallMapIcon = smallMapIcon;

var facingNWIcon = new GIcon(G_DEFAULT_ICON);
facingNWIcon.image = "http://labs.ribaudio.com/herosquad/img/lTurretFacingNW.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];
ITurret.facingNWIcon = facingNWIcon;

ITurret.facingNEIcon = new GIcon(facingNWIcon, "http://labs.ribaudio.com/herosquad/img/lTurretFacingNE.png");
ITurret.facingSWIcon = new GIcon(facingNWIcon, "http://labs.ribaudio.com/herosquad/img/lTurretFacingSW.png");
ITurret.facingSEIcon = new GIcon(facingNWIcon, "http://labs.ribaudio.com/herosquad/img/lTurretFacingSE.png");