function cTileController()
{
	this.tiles = [];
	this.movingTiles = [];
	
	// tile0:  see thorugh
	var blankTile = new cTile();
	blankTile.walkable = true;
	blankTile.draw = function( x, y, domParent ){
		var tile = document.createElement( "div" );
		tile.style.position = "absolute";
		tile.style.width = g_tileW + "px";
		tile.style.height = g_tileH + "px";
		tile.style.left = x * g_tileW + "px";
		tile.style.top = y * g_tileH + "px";
		domParent.appendChild( tile );
		return tile;
	};
	this.tiles.push( blankTile ); //0
	
	// SOlid dirt
	this.tiles.push( tileFactory( false, 2, 0 ) ); //1
	
	// Dooors
	var door1 = new cDoor(1, 1, 6);
	var door2 = new cDoor(2, 22, 4);
	
	this.tiles.push( door1 ); //2
	this.tiles.push( door2 ); //3
	
	// Clouds
	var cloud1 = new cCloud();
	this.tiles.push( cloud1 ); //4
	
	// Dooors
	var door3 = new cDoor(1, 22, 4);
	var door4 = new cDoor(0, 1, 4);
	this.tiles.push( door3 ); //5
	this.tiles.push( door4 ); //6
	
	// Moving tles
	var move1 = new cMovingTile1();
	this.tiles.push( move1 );
	this.movingTiles.push( this.tiles.length - 1 );
	
	this.tiles.push( tileFactory( false, 1, 0 ) ); // Dirt with cover : 8
	
	// More doors
	this.tiles.push( new cDoor( 1, 22, 10 ) ); //9
	this.tiles.push( new cDoor( 0, 1, 12 ) ); //10
	
	this.tiles.push( tileFactory( false, 5, 0 ) ); // flat stone rounded right : 11
	
	// flat stone rounded left
	this.tiles.push( tileFactory( false, 6, 0 ) ); //12
	
	this.tiles.push( tileFactory( false, 2, 1 ) ); // Magic Door (end game) : 13
	this.tiles.push( tileFactory( false, 3, 1 ) ); // Mossey Rock Left : 14
	this.tiles.push( tileFactory( false, 5, 1 ) ); // Mossey Rock Right : 15
	this.tiles.push( tileFactory( true, 4, 1 ) ); // Mossey under left : 16
	this.tiles.push( tileFactory( true, 6, 1 ) ); // Mossey under right : 17
	
	// More doors: screen 0 to screen 3(new screen)
	this.tiles.push( new cDoor( 3, 1, 3 ) ); //18
	this.tiles.push( new cDoor( 0, 22, 3 ) ); //19

	this.tiles.push( new cDoor( 3, 1, 11 ) ); //20
	this.tiles.push( new cDoor( 0, 22, 7 ) ); //21
	
	this.tiles.push( new cGenericDoor( 0, 22, 12 ) ); //21
	
	this.tiles.push( tileFactory( true, 7, 0 ) ); // 22 - brown bot
	this.tiles.push( tileFactory( true, 8, 0 ) ); // 23 - green bot
	this.tiles.push( tileFactory( true, 0, 2 ) ); // 22 - green bot 2
	this.tiles.push( tileFactory( true, 1, 2 ) ); // 23 - brown bot 2
		
	this.drawMoving = function( map ){
		for(var i = 0; i<g_moving[ map ]; i++){
			
		}
	}
}

function tileFactory( walkable, tileX, tileY ){
	var abstractTile = new cTile();
	abstractTile.walkable = walkable;
	abstractTile.draw = function( x, y, domParent )	{
		domParent.appendChild( createTileDOMelement( x, y, tileX, tileY ) );
	};
	return abstractTile;
}

function createTileDOMelement( x, y, tileX, tileY ){
	var tile = document.createElement( "div" );
	tile.style.position = "absolute";
	tile.style.width = g_tileW + "px";
	tile.style.height = g_tileH + "px";
	tile.style.left = x * g_tileW + "px";
	tile.style.top = y * g_tileH + "px";
	tile.style.backgroundImage = "url(images/tiles.png)";
	tile.style.backgroundPosition = "-" + (tileX * g_tileW) + "px -" + (tileY * g_tileH) + "px";
	return tile;
}

function cTile(){
	this.walkable;
	this.draw = function( x, y, domParent )	{
		// Overrideable
	};
}

function cDoor( p_newMap, p_newCharX, p_newCharY ){
	// This should really inherit from cTile
	this.walkable = true;
	this.draw = function( x, y, domParent ){
		domParent.appendChild( createTileDOMelement( x, y, 4, 0 ) );
	};	
	this.door = true;
	this.newMap = p_newMap;
	this.newCharX = p_newCharX;
	this.newCharY = p_newCharY;
}

function cGenericDoor(){
	// This should really inherit from cTile
	this.walkable = true;
	this.draw = function( x, y, domParent ){
		domParent.appendChild( createTileDOMelement( x, y, 4, 0 ) );
	};	
	this.door = true;
	/*this.newMap = p_newMap;
	this.newCharX = p_newCharX;
	this.newCharY = p_newCharY;*/
}

function cCloud(){
	// This should really inherit from cTile
	this.walkable = true;
	this.cloud = true;
	this.draw = function( x, y, domParent ){
		// Overrideable
		domParent.appendChild( createTileDOMelement( x, y, 3, 0 ) );
	};
}

cMovingTile1.prototype = new cTile(); 
cMovingTile1.prototype.constructor = cMovingTile1;
function cMovingTile1( p_id ){
	this.speed = 2;
	this.dirx = 0;
	this.diry = 1;
	this.minY = 0;
	this.maxY = 2;
	this.walkable = true;
	this.cloud = true;
	this.moving = true;
	this.domRef = null;
	
	this.width = g_tileW / 2;
	this.height = g_tileH / 2;
	this.id = p_id;
	
	this.draw = function( x, y, domParent ){
		// Overrideable
		var tile = createTileDOMelement( x, y, 3, 0 );
		this.domRef = tile;
		domParent.appendChild( tile );
	};
	
	this.draw2 = function( x, y, domParent ){
		this.xTile = x * g_tileW;
		this.yTile = y * g_tileH;
		this.x = x;
		this.y = y;
		
		// Overrideable
		var tile = document.createElement( "div" );
		tile.style.position = "absolute";
		tile.style.width = g_tileW + "px";
		tile.style.height = g_tileH + "px";
		tile.style.left = x * g_tileW + "px";
		tile.style.top = y * g_tileH + "px";
		tile.style.border = "1px solid #252";
		tile.style.backgroundColor = "#ddd";
		domParent.appendChild( tile );
	};
	
}

