function buildingClass(game, name, type, x, y, owned, owner) {
/*==============================================================================
shipClass
requires: createLayer() function
==============================================================================*/
	this.name = name;
	this.baseName = name.replace(/([^a-zA-Z0-9])/g, "", name);
	this.game = game;
	this.type = type;
	this.x = x;
	this.y = y;
	
	//Owner info
	this.owned = owned;
	this.owner = owner;
	this.active = 0;

	//Layer for building
	this.layerObj = createLayer(this.baseName+'Lyr','unitsDiv',32*this.x,32*this.y,32,32,'',null,null,8);
	this.layerObj.parent = this;
	if (this.layerObj.style) { this.buildingStyle=this.layerObj.style; } else { this.buildingStyle=this.layerObj; }
	if (this.owned) { this.buildingStyle.backgroundImage = 'url("images/'+this.game.player[this.owner].colour+'/buildings.gif")'; 
	} else { this.buildingStyle.backgroundImage = 'url("images/buildings.gif")'; };
	this.buildingStyle.backgroundPosition = (-32*this.type)+'px 0px';

	this.layerObj.onmousedown = function() {
		this.parent.buildingClicked();
	};
	
	this.setOwner = function( owned, owner ) {
		this.owned = owned;
		this.owner = owner;
		if (this.owned) {
			this.buildingStyle.backgroundImage = 'url("images/'+this.game.player[this.owner].colour+'/buildings.gif")';
		} else {
			this.buildingStyle.backgroundImage = 'url("images/buildings.gif")';
		};
	};
	
	this.activate = function() { 
		this.layerObj.innerHTML = '<img src="images/active.gif" width="32" height="32" border="0">';
		this.game.buildingActive = true;
		this.active = true;
	};
	
	this.unactivate = function() { 
		this.layerObj.innerHTML = '';
		this.game.selectedUnitOptions.innerHTML = '';
		this.game.buildingActive = false;
		this.active = false;
	};
	
	this.buildingClicked = function() {
		if (this.game.buildingActive) {
			if (this.active) this.unactivate();
		} else if (this.game.buildingTypes[this.type].canCreate && this.owned && this.owner == this.game.activePlayer && this.game.unitActive == false) {
			// Activate building
			this.activate();	
			// Show build options
			var buildOptionStr = '';
			for (var i=0; i < this.game.unitTypes.length; i++ ) {
				if (this.game.player[this.game.activePlayer].funds >= this.game.unitTypes[i].cost) {
					buildOptionStr += '<a href="javascript:'+this.game.name+'.buildUnit('+i+');">'+this.game.unitTypes[i].name+'</a><br>';
				} else {
					buildOptionStr += this.game.unitTypes[i].name+'<br>';
				};
			};
			this.game.selectedUnitOptions.innerHTML = buildOptionStr;
		};
	};
	
	this.unitConstruct = function( unitType ) {
		this.game.setUnit( this.game.activePlayer, unitType, this.x, this.y );
		this.game.player[this.game.activePlayer].funds -= this.game.unitTypes[unitType].cost;
		this.game.player[this.game.activePlayer].units[this.game.player[this.game.activePlayer].units.length-1].exhaust();
		this.game.setStatusDisplay(this.game.activePlayer);
		this.unactivate();
	}
	
}

