var width = 0;
var height = 0;

var mImg = new Image();
mImg.src = 'Data/m.png';
var bgImg = new Image();
bgImg.src = 'Data/10.png';

function MainInit() {
	var canvas1 = document.getElementById('layer1');
	var ctx = canvas1.getContext('2d');
	var width =  canvas1.width;
	var height = canvas1.height;
	
	//ctx.lineWidth = 2;
	Map = new TMap(ctx);
	ACM = new TACM();
	
	var SceneManager = new TSceneManager(ctx, 0, 0, width, height, '#000000');
	var MainMenu  = new TMenu(ctx, SceneManager, Map, ACM);
	
	var Game  = new TGame(ctx, SceneManager, Map, ACM);
	MainMenu.Game = Game;
	
	MainMenu.Show(MainMenu);
	MainMenu.OnShow();
	//Game.Show(Game);
	//Game.OnShow();
	//SceneManager.ChangeScene(MainMenu);
	
	function Draw() {
		ctx.drawImage(bgImg, 0, 0);
		SceneManager.Draw();
		//MoveText.Draw();
	}
	
	function Update() {
		Map.worldUpdate();
		SceneManager.Update();
		//MoveText.Update();
		Draw();
	}
	
	setInterval(Update, 21); 
	
	canvas1.onmousedown = function(e) {
		e = e || window.event;
		fixPageXY(e);
	    SceneManager.Mouse(e.layerX, e.layerY);
		Map.keyMe2.kAtack = true;
	}
	document.onmousedown = function(e) {
		Map.keyMe2.kAtack = true;
	}
	document.onmouseup = function(e) {
		Map.keyMe2.kAtack = false;
	}
	
	function SetKey(K, S) {
		//alert(K)
		switch (K) {
			case 87 : Map.keyMe.kUp = S; break;
			case 83 : Map.keyMe.kDown = S; break;
			case 65 : Map.keyMe.kLeft = S; break;
			case 68 : Map.keyMe.kRigth = S; break;
			case 32 : Map.keyMe.kAtack = S; break;			
			case 69 : Map.keyMe.kAtack = S; break;		
			
			case 38 : Map.keyMe2.kUp = S; break;
			case 40 : Map.keyMe2.kDown = S; break;
			case 37 : Map.keyMe2.kLeft = S; break;
			case 39 : Map.keyMe2.kRigth = S; break;
			case 13 : Map.keyMe2.kAtack = S;  SceneManager.kEnter = S;
			break;
		}
	}
	window.onkeydown = function(e) {
		SetKey(e.keyCode, true);
	};
	
	window.onkeyup = function(e) {
		SetKey(e.keyCode, false);
	};	

}