/**
 * Name: Isometric map generator.
 * By:   FireFly (firefly.nu).
 * Date: 2009-04-22.
 */
Game.map.setupTiles = function() {
	var img1 = new Image();
	var img2 = new Image();
	img1.src = 'img/t-grass.png';
	img2.src = 'img/t-water.png';
	
	Game.map.tileTypes.push(new TileType(img1));
	Game.map.tileTypes.push(new TileType(img2, {water: true, borders: true}));
	
	Game.map.heightImage = new Image();
	Game.map.heightImage.src = 'img/height.png';
}

Game.map.setupMap = function() {
	Debug.print("Fixar kantdelar... ");
	Game.map.fixBorders();
//	Game.map.drawAll(true);
//	Debug.print("klar!");
}

Game.map.generateMap = function() {
	Game.map.width  = parseInt(document.getElementById('settWidth').value);
	Game.map.height = parseInt(document.getElementById('settHeight').value);
	var amount      = parseFloat(document.getElementById('settAmount').value);
	var probability =
			parseFloat(document.getElementById('settProbability').value);
	var multiplier  =
			parseFloat(document.getElementById('settMultiplier').value);
	
	Debug.print("Genererar karta... ");
	Game.map.map = generateStandardMap(Game.map.width, Game.map.height, 
			Game.map.tileTypes, probability, amount, multiplier);
	Debug.print("klar!");
};

Game.map.fixBorders = function() {
	var map = Game.map.map;
	var bordering;
	
	for (var x=0; x<map.length; x++) {
		for (var y=0; y<map[x].length; y++) {
			var t = map[x][y];
			
			if (t.type.hasBorders) {
				var bordering = t.neighbours(function(t2) {
					return t2.type != t.type;
				});
				
				t.subtile = bordering;
				t.below = Game.map.tileTypes[0];
			}
		}
	}
};

Game.map.draw = function(x, y, bordered, DEBUG_id) {
	var t = Game.map.map[x][y];
	var p = Game.utils.convertIso(x, y);
	
	if (t.below != null) {
		Game.ctx.drawImage(t.below.image, p.x, p.y - t.height*4);
	}
	Game.ctx.drawImage(t.type.image, 0, 16*t.subtile, 33, 16, p.x, p.y - t.height*4, 33, 16);
	
	for (var i=0; i<t.height; i++) {
		Game.ctx.drawImage(Game.map.heightImage, p.x, p.y - i*4 + 1);
	}
	
	if (bordered) {
		var th = p.y - Game.map.map[x][y].height*4;
		
		Game.ctx.beginPath();
		Game.ctx.moveTo(p.x+16, th);
		Game.ctx.lineTo(p.x+33, th+8);
		Game.ctx.lineTo(p.x+16, th+16);
		Game.ctx.lineTo(p.x, th+8);
		Game.ctx.closePath();
		Game.ctx.stroke();
	}
	
	/*
	Game.ctx.fillStyle = 'rgba(0, 0, 255, ' + ((DEBUG_id % 30) / 30) + ')';
	Game.ctx.fillRect(tx+13, ty+6 - t.height*4, 5, 5);
	Game.ctx.fillStyle = 'rgba(255, 0, 0, ' + (DEBUG_id / 900) + ')';
	Game.ctx.fillRect(tx+13, ty+6 - t.height*4, 5, 5);
	//*/
};

Game.map.drawAll = function(bordered) {
	var element, gridDiv, overlay;
	var width=Game.map.map.length, height=Game.map.map[0].length;
	var k = 0;
	
	Game.ctx.strokeStyle = 'rgba(0, 0, 0, 0.2)';
	for (var i=height-1; i>=0; i--) {
		for (var j=0; j<height; j++) {
			k++;
			Game.map.draw(j, i, bordered, k);
		}
	}
};

	/*** Prototype: TileType ***/
function TileType(image, obj) {
	this.image = image;
	
	this.hasBorders = false;
	this.isWater = false;
	
	for (var k in obj) {
		switch (k) {
			case 'borders': this.hasBorders = !!obj[k]; break;
			case 'water': this.isWater = !!obj[k]; break;
			default: throw new TypeError("Key '" + k + "' is unknown (TileType)");
		}
	}
}

	/*** Prototype: MapTile ***/
function MapTile(x, y, height, map, type) {
	this.x = x;
	this.y = y;
	this.height = height;
	this.map = map;
	this.type = type;
	this.subtile = 0;
	this.below = null;
//	this.selected = false;
}
MapTile.prototype.neighbours = function(f) {
	bordering = 0; // Markera bordering med biten som representerar varje tile-riktning.
	if (this.x>0)
		bordering |= f(this.map[this.x-1][this.y]) << 0;
	if (this.x<Game.map.width-1)
		bordering |= f(this.map[this.x+1][this.y]) << 1;
	if (this.y>0)
		bordering |= f(this.map[this.x][this.y-1]) << 2;
	if (this.y<Game.map.height-1)
		bordering |= f(this.map[this.x][this.y+1]) << 3;
	
	return bordering;
};