function moveMaskClass(unitObj) {
/*==============================================================================
moveMaskClass
requires: createLayer() function
==============================================================================*/
	this.newNodes = new Array();
	this.expandedNodes = new Array();
	this.unitObj = unitObj;
	this.mapTypes = this.unitObj.game.mapTypes;
	this.mapArray = this.unitObj.game.mapArray;
	this.moveAllowance = unitObj.moveAllowance;
	
	this.testNode = function( nodeToExpand, newX, newY, direction) {
		var nodeExpanded = false;
		var nodeExists = false;
		var nodeOccupied = false;
		var newCost;
		var i;
		var j;
		
		if (newX >= 0 && newY >= 0 && newX < this.mapArray[0].length && newY < this.mapArray.length) {
			// Calculate the movement cost for this Node
			newCost = nodeToExpand.cost+this.mapTypes[this.mapArray[newY][newX]].moveCost;
			// Test to see if Node is occupied by the enemy
			if (newCost <= this.moveAllowance) {
				for (i=0; i<nodeToExpand.unitObj.game.numberOfPlayers; i++) {
					if (nodeToExpand.unitObj.game.activePlayer != i) { // Looking only at enemy players
						for (j=0; j<nodeToExpand.unitObj.game.player[i].units.length; j++) {
							if (nodeToExpand.unitObj.game.player[i].units[j].x == newX && nodeToExpand.unitObj.game.player[i].units[j].y == newY) {
								nodeOccupied = true;
								break;
							}
						}
					}
				}
			}
			// Test to see if Node is Expaned already
			if (newCost <= this.moveAllowance && !nodeOccupied) {
				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;
							this.expandedNodes[i].parent = nodeToExpand;
							this.expandedNodes[i].directionFromParent = direction;
						}
						break; 
					}
				}
			}
			// Test to see if Node waiting to be expanded
			if (newCost <= this.moveAllowance && !nodeOccupied && !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;
							this.newNodes[i].parent = nodeToExpand;
							this.newNodes[i].directionFromParent = direction;
						}
						break; 	
					}
				}
			}
			if (newCost <= this.moveAllowance && !nodeOccupied && !nodeExpanded && !nodeExists) {
				var newNode = new moveMaskNodeClass(this.unitObj,nodeToExpand,direction,newX,newY,newCost);
				this.newNodes.push(newNode);
			}
		}
	}
		
	this.expandNode = function( nodeToExpand ) {
		this.testNode( nodeToExpand, nodeToExpand.x, nodeToExpand.y-1, 0 );
		this.testNode( nodeToExpand, nodeToExpand.x+1, nodeToExpand.y, 1 );
		this.testNode( nodeToExpand, nodeToExpand.x, nodeToExpand.y+1, 2 );
		this.testNode( nodeToExpand, nodeToExpand.x-1, nodeToExpand.y, 3 );
		//Put this node in the expanded array
		this.expandedNodes.push(nodeToExpand);
	}
	
	//Destroy nodes
	this.destroyNodes = function() {
		for (i=0; i<this.expandedNodes.length; i++) {
			destroyLayer('MaskNode'+this.expandedNodes[i].x+'_'+this.expandedNodes[i].y,'unitsDiv');
		}
		delete this.expandedNodes;	
	}
	
    // First node at origin
	this.newNodes[0] = new moveMaskNodeClass(this.unitObj,null,null,unitObj.x,unitObj.y,0);
	
	// Expand nodes
	do {
		this.expandNode(this.newNodes.shift());
	} while (this.newNodes.length);
		
}

function moveMaskNodeClass(unitObj,parent,directionFromParent,x,y,cost) {
	this.unitObj = unitObj;
	this.layer = createLayer('MaskNode'+x+'_'+y,'unitsDiv',x*32,y*32,32,32,'',null,null,9);
	this.layer.className = "moveMask";
	this.layer.parent = this;
	this.cost = cost;
	this.x = x;
	this.y = y;
	this.parent = parent;
	this.directionFromParent = directionFromParent;
	this.pathHere = new Array();
	if (this.parent) {
		this.pathHere = this.pathHere.concat(this.parent.pathHere);
		this.pathHere.push(directionFromParent);
	}
	
	this.layer.onclick = function() {
		this.parent.unitObj.setPath(this.parent.pathHere);
		this.parent.unitObj.destroyMask();
		this.parent.unitObj.animatePath();
	}
}
