/* jshint devel: true */
var PewPew= {};

!function() {
	'use strict';

	/**
	 * Upgrade class
	 */
	function Upgrade(name, maxLevel) {
		this.name = name;
		this.maxLevel = maxLevel;
		this.level = 0;
		this.cost = function() {
			return (this.level + 1) * 100;
		};
		this.getElementId = function() {
			return "upgrade-" + this.name;
		};
		this.onBuy = function() {
			document.getElementById(this.getElementId() + "-cost").innerHTML = this
					.cost();
			document.getElementById(this.getElementId() + "-level").innerHTML = this.level;
		};
		this.getHtml = function() {
			return '<div id="'+ this.getElementId() + '" class="tile">' + this.name + "<br>" + 'Level: <span id="'
			+ this.getElementId() + '-level">0</span><br>'
			+ 'Cost: <span id="' + this.getElementId() + '-cost">'
			+ this.cost() + "</span>" + "</div>";
		}
	}

	/**
	 * player singleton
	 */
	PewPew.player = {
		money : 0,
		upgrades : {
			damage : new Upgrade("damage", 10),
			income : new Upgrade("income", 10)
		},
		income : function() {
			return this.upgrades.income.level + 1;
		},
		damage : function() {
			return this.upgrades.damage.level + 1;
		},
		changeMoney : function(newMoney) {
			this.money = newMoney;
		},
		addMoney : function(money) {
			return this.changeMoney(this.money + money);
		},
		substractMoney : function(money) {
			return this.addMoney(-money);
		}
	};
	/**
	 * Fill upgrades tab
	 */
	var html="";
	for ( var upgrade in PewPew.player.upgrades) {
		if (PewPew.player.upgrades.hasOwnProperty(upgrade)) {
			html += PewPew.player.upgrades[upgrade].getHtml();
		}
	}
	document.getElementById("upgrade-tiles").innerHTML=html;
	

	/**
	 * Enemy class
	 */
	function Enemy(hp, x, y, image, value) {
		this.hp = hp;
		this.x = x;
		this.y = y;
		this.setX = function(x) {
			this.x = x;
		};
		this.setY = function(y) {
			this.y = y;
		};
		this.image = image;
		this.value = value;
		/**
		 * deals damage to the enemy
		 */
		this.recieveDamage = function(damage) {
			this.hp -= damage;
			if (this.hp <= 0) {
				this.onDestroy();
			}
		};
		/**
		 * called when an enemy is destroyed
		 */
		this.onDestroy = function() {
			PewPew.player.addMoney(this.value * PewPew.player.income());
			var index = PewPew.game.enemies.indexOf(this);
			if (index > -1) {
				PewPew.game.enemies.splice(index, 1);
			}
		};
		PewPew.game.enemies[game.enemies.length] = this;
	}

	PewPew.game = {
		level : 1,
		enemies : []
	};
}();
@@ -829,16 +829,249 @@
 %3E%22;%0A%09%09%7D%0A
+%09%09this.tryBuy = function() %7B%0A%09%09%09if(this.level%3E=this.maxLevel)%7B%0A%09%09%09%09return false;%0A%09%09%09%7D%0A%09%09%09if(PewPew.player.money%3Cthis.cost())%7B%0A%09%09%09%09return false;%0A%09%09%09%7D%0A%09%09%09PewPew.player.substractMoney(this.cost());%0A%09%09%09this.level++;%0A%09%09%09this.onBuy();%0A%09%09%7D%0A
 %09%7D%0A%0A%09/**
@@ -1431,16 +1431,74 @@
 wMoney;%0A
+%09%09%09document.getElementById(%22money%22).innerHTML=this.money;%0A
 %09%09%7D,%0A%09%09a

Add a function to buy an upgrade