var Pi2 = 3.14 * 2;
var Init = false;
var bg_color = '#F0F0F0';

var kUp = false;
var kDown = false;
var kLeft = false;
var kRigth = false;
var kInventory = false;
var kStay = false;

var tex_Card =  new Array();
for (var I = 1; I <= 4; I++)
  tex_Card[I] = new Image();
tex_Card[1].src = 'Data/c_w.png';
tex_Card[2].src = 'Data/c_f.png';
tex_Card[3].src = 'Data/c_n.png';
tex_Card[4].src = 'Data/c_p.png';


var ShowMessage = function(Text) {

}
	
// инициализация
function MainInit() {

	var canvas_obj = document.getElementById('canvas_obj');
	var my_canvas  = canvas_obj.getContext('2d');
    var width  =  canvas_obj.width;
    var height = canvas_obj.height;
	Init = true;
	
	var FormManager = new TFormManager(my_canvas, 0, 0, width, height, '#000000');
	ShowMessage = function(Text) {
		FormManager.MessageForm.IsShow = true;
		FormManager.MessageForm.MessageText = Text;
	}
	
	var GameForm  = new TForm(my_canvas, FormManager, width / 2, height - 16, width, 32, '#555555');
	var HeroForm  = new THeroForm(my_canvas, FormManager, width / 2, height / 2 + 16, width / 2 + 84, height - 80, '#BDBDBD');
	var CardsForm = new TCardsForm(my_canvas, FormManager, width / 2, height / 2, width - 40, height - 50, '#555555');
	var ChestForm = new TChestForm(my_canvas, FormManager, width / 2, height / 2, width / 2 + 62, height / 2 + 40, '#555555');
	var ShopForm  = new TShopForm(my_canvas, FormManager, width / 2, height / 2, width / 2 + 62, height / 2 + 40, '#555555');
	
	var FightForm = new TFightForm(my_canvas, FormManager, width / 2, height - 64, width - 32, 64, '#555555');
	var MoveText  = new TMoveText(my_canvas);
	
	
	var MapForm  = new TForm(my_canvas, FormManager, width / 2, height - 22, width, 42, '#555555');
	var MinMap = new TMinMap(my_canvas, 0, 0, width, height);


	//WinForm.Update = function() {};
	var Game = new TGame(my_canvas, 0, 0, width, height);
	Game.MinMap = MinMap;
	Game.MainInit();
	
	var WinForm = new TForm(my_canvas, FormManager, width / 2, height / 2, width, height, '#255597');
	WinForm.AddNewButton(0, 0, 310, 50, '#CCECFF', '#CCECFF', 'ПОБЕДА!').OnClick = function(mX, mY){
		GameForm.ShowMessage('Да-да! Это победа');
	};
	
	WinForm.AddNewButton(0, 150, 280, 35, '#CCECAF', '#CCECAF', 'продолжить игру просто так').OnClick = function(mX, mY){
		GameForm.Show(GameForm);
	};
	
	var FailForm = new TForm(my_canvas, FormManager, width / 2, height / 2, width, height, '#975522');
	FailForm.AddNewButton(0, 0, 310, 50, '#FFECCC', '#FFECCC', 'ПОРАЖЕНИЕ!').OnClick = function(mX, mY){
		GameForm.ShowMessage('Нам очень жаль ((');
	};
	
	FailForm.AddNewButton(0, 150, 280, 35, '#FFECCC', '#FFECCC', 'нажмите F5, для рестрта. Ctrl+W для выхода.').OnClick = function(mX, mY){
/*	
	Game.StartGame();
		Game.Hero.StartHero();
		Game.Hero.Health = 10;		
		GameForm.Show(GameForm);
*/
	};
	
	FightForm.MoveText = MoveText;
	FightForm.CardsForm = CardsForm;
	FightForm.GameForm  = GameForm;
	FightForm.PartnerCard = CardsForm.PartnerCard;
	FightForm.Hero = Game.Hero;
	
	CardsForm.Hero = Game.Hero;
	ShopForm.Hero = Game.Hero;
	HeroForm.Hero = Game.Hero;
	MinMap.Hero = Game.Hero;
	
	ShopForm.CardsForm = CardsForm;
	ChestForm.CardsForm = CardsForm;
	
	Game.Hero.FightForm = FightForm;
	//Game.Hero.FightForm.Init(Game.Ship[0]);
	Game.Hero.ChestForm = ChestForm;
	Game.Hero.ShopForm  = ShopForm;
	Game.Hero.GameForm  = GameForm;
	Game.Hero.CardsForm  = CardsForm;
	Game.Hero.WinForm = WinForm;
	Game.Hero.FailForm = FailForm;
	
	Game.Hero.BarInit(width / 2, width);
	
	GameForm.AddNewButton((1- 7/2 - 0.5)*40, 0, 20, 20, '#CCCCCC', '#EEEEEE', 'L').OnClick = function(mX, mY){
	  Game.Hero.MoveBtn(-1, 0);
	};
	GameForm.AddNewButton((2- 7/2 - 0.5)*40, 0, 20, 20, '#CCCCCC', '#EEEEEE', 'R').OnClick = function(mX, mY){
	  Game.Hero.MoveBtn(1, 0);
	};	
	GameForm.AddNewButton((3- 7/2 - 0.5)*40, 0, 20, 20, '#CCCCCC', '#EEEEEE', 'U').OnClick = function(mX, mY){
	  Game.Hero.MoveBtn(0, -1);
	};	
	GameForm.AddNewButton((4- 7/2 - 0.5)*40, 0, 20, 20, '#CCCCCC', '#EEEEEE', 'D').OnClick = function(mX, mY){
	  Game.Hero.MoveBtn(0, 1);
	};	
	GameForm.AddNewButton((5- 7/2 - 0.5)*40, 0, 20, 20, '#CCCCCC', '#EEEEEE', 'I').OnClick = function(mX, mY){CardsForm.Init(GameForm, 0);};
	GameForm.AddNewButton((6- 7/2 - 0.5)*40, 0, 20, 20, '#CCCCCC', '#EEEEEE', 'P').OnClick = function(mX, mY){HeroForm.Show(GameForm);};
	GameForm.AddNewButton((7- 7/2 - 0.5)*40, 0, 20, 20, '#CCCCCC', '#EEEEEE', 'M').OnClick = function(mX, mY){MapForm.Show(GameForm);};
	
	MapForm.AddNewButton(0, 0, 100, 28, '#CCCCCC', '#EEEEEE', 'Закрыть').OnClick = function(mX, mY){MapForm.Close()}
	
	GameForm.Scene  = Game;
	CardsForm.Scene = Game;
	ChestForm.Scene = Game;
	ShopForm.Scene = Game;
	FightForm.Scene = Game;
	HeroForm.Scene = Game;
	MapForm.Scene = MinMap;
		
	GameForm.Show(GameForm);
	GameForm.ShowMessage('Мой ученик, мы в лесу. А теперь, прокачай наших драконов до 5 уровня! Ты же читал readme?!');
	
	//HeroForm.Show(GameForm);
	//FormManager.UseForm = GameForm;
	
	//FormManager.ChangeForm(Form);
	//		Card[I] = new TButton(ctx, canvas.width / 2 + (I - CardCount/2 - 0.5) * 80, 128, 64, 64, '#CCCCCC', '#EEEEEE');
			

	function Draw() {
		my_canvas.fillStyle = bg_color;
		my_canvas.fillRect(0, 0, width, height);
		FormManager.Draw();
		MoveText.Draw();
	}
	
	function Update() {
		FormManager.Update();
		MoveText.Update();
		Draw();
	}
	
	setInterval(Update, 17); 
	
	my_canvas.textBaseline = "middle";
	my_canvas.textAlign = "center";
	
	canvas_obj.onmousedown = function(e) {
		e = e || window.event;
		fixPageXY(e);
	    FormManager.Mouse(e.layerX, e.layerY);
	}
	
	// keyboard
	window.onkeydown = function(e) {
		var K = e.keyCode;
		//alert(K);
		if ((K == 87) || (K == 38)) kUp = true;
		if ((K == 83) || (K == 40)) kDown = true;
		if ((K == 65) || (K == 37)) kLeft = true;
		if ((K == 68) || (K == 39)) kRigth = true;
		if (K == 32) kStay = true;
	};
	
	window.onkeyup = function(e) {
		var K = e.keyCode;

		if ((K == 87) || (K == 38)) kUp = false;
		if ((K == 83) || (K == 40)) kDown = false;
		if ((K == 65) || (K == 37)) kLeft = false;
		if ((K == 68) || (K == 39)) kRigth = false;
		if (K == 32) kStay = false;
	};
	
	
}


