function attackMaskClass(unitObj) {
/*==============================================================================
attackMaskClass
requires: createLayer() function
==============================================================================*/
	
	this.unitObj = unitObj;
	this.newNodes = new Array();
	this.expandedNodes = new Array();
	this.weaponMinRange = this.unitObj.weaponMinRange;
	this.weaponMaxRange = this.unitObj.weaponMaxRange;	
	this.mapWidth = this.unitObj.game.mapWidth;
	this.mapHeight = this.unitObj.game.mapHeight;

	this.testNode = function( nodeToExpand, newX, newY ) {
		var nodeExpanded = false;
		var nodeExists = false;
		var newCost;
		var i;
		var j;
		
		if (newX >= 0 && newY >= 0 && newX <= this.mapWidth && newY <= this.mapHeight) {
			// Calculate the cost to shoot to this Node
			newCost = nodeToExpand.cost+1;

			// Test to see if Node is Expaned already
			if (newCost <= this.weaponMaxRange) {
				for (i=0; i<this.expandedNodes.length; i++) {
					if (this.expandedNodes[i].x == newX && this.expandedNodes[i].y == newY) { 
						nodeExpanded = true;
						if (newCost < this.expandedNodes[i].cost) {
							this.expandedNodes[i].cost = newCost;
						}
						break; 
					}
				}
			}
			// Test to see if Node waiting to be expanded
			if (newCost <= this.weaponMaxRange && !nodeExpanded) {
				for (i=0; i<this.newNodes.length; i++) {
					if (this.newNodes[i].x == newX && this.newNodes[i].y == newY) {
						nodeExists = true;
						if (newCost < this.newNodes[i].cost) {
							this.newNodes[i].cost = newCost;
						}
						break; 	
					}
				}
			}
			if (newCost <= this.weaponMaxRange && !nodeExpanded && !nodeExists) {
				var newNode = new attackMaskNodeClass(this.unitObj,newX,newY,newCost);
				this.newNodes.push(newNode);
			}
		}
	}
		
	this.expandNode = function( nodeToExpand ) {
		this.testNode( nodeToExpand, nodeToExpand.x, nodeToExpand.y-1 );
		this.testNode( nodeToExpand, nodeToExpand.x+1, nodeToExpand.y );
		this.testNode( nodeToExpand, nodeToExpand.x, nodeToExpand.y+1 );
		this.testNode( nodeToExpand, nodeToExpand.x-1, nodeToExpand.y );
		//Put this node in the expanded array
		this.expandedNodes.push(nodeToExpand);
	}
	
	//Destroy nodes
	this.destroyNodes = function() {
		for (i=0; i<this.expandedNodes.length; i++) {
			if (this.expandedNodes[i].layer) destroyLayer('AMN'+this.expandedNodes[i].x+'_'+this.expandedNodes[i].y,'unitsDiv');
		}
		delete this.expandedNodes;	
	}
	
    // First node at origin
	this.newNodes[0] = new attackMaskNodeClass(this.unitObj,unitObj.x,unitObj.y,0);
	
	// Expand nodes
	do {
		this.expandNode(this.newNodes.shift());
	} while (this.newNodes.length);
		
}

function attackMaskNodeClass(unitObj,x,y,cost) {
	this.unitObj = unitObj;
	this.x = x;
	this.y = y;
	this.cost = cost;
	
	if (this.cost >= this.unitObj.weaponMinRange) { //Outside minimum range
		this.layer = createLayer('AMN'+this.x+'_'+this.y,'unitsDiv',this.x*32,this.y*32,32,32,'',null,null,11);
		this.layer.parent = this;
		this.layer.className = "attackMask";
		
		// Test to see if Node is occupied by the enemy
		for (i=0; i<this.unitObj.game.numberOfPlayers; i++) {
			if (this.unitObj.game.activePlayer != i) { // Looking only at enemy players
				for (j=0; j<this.unitObj.game.player[i].units.length; j++) {
					if (this.unitObj.game.player[i].units[j].x == this.x && this.unitObj.game.player[i].units[j].y == this.y) {
						this.layer.className = "targetMask";
						this.target = this.unitObj.game.player[i].units[j];
						this.layer.onclick = function() {
							this.parent.unitObj.attack(this.parent.target,this.parent.cost);
							this.parent.unitObj.destroyMask();
						}
					}
				}
			}
		}
	}
}

