print("init Message Window begin");


local menu = game:getSceneManager():getMelodyBookWindow()
local vault = resourceManager:getVault(ResourceManager.COMMON)

local bgFadeInDelay = 0
local bgFadeInTime = 0.3
local bgFadeOutTime = 0.3

local fadeInDelay = 0
local fadeInTime = 0.2
local fadeOutDelay = 0.2
local fadeOutTime = 0.2

local pageFadeInDelay = 0.2
local pageFadeInTime = 0.2
local pageFadeOutTime = 0.2

local outlineFadeInDelay = 0.2
local outlineFadeInTime = 0.01
local outlineFadeOutDelay = 0.2
local outlineFadeOutTime = 0.01

local hoverDelay = 0
local hoverIn = 0.2
local hoverOut = 0.2
local hoverLeavingIn = 0.2
local hoverLeavingOut = 0.2

local windowLeft = 390
local windowTop = 273

local frameW = 260
local frameH = 180

pi = 3.1415926535897932384626433832795

-- Создание бакграунда
-- Должен добавляться раньше всех. Ибо в противном случае он накрывает
-- остальные контролы и не дает им мышь


-- local sprite = KrSprite(vault:GetSpriteResource("hatch"))
local sprite = KrSprite(vault:GetSpriteResource("hatch_black"))
setNodeScale(sprite, 256, 192);
-- aniFadeInFadeOut(sprite,0,.3,.3,60)
aniFadeInFadeOut(sprite,bgFadeInDelay,bgFadeInTime,bgFadeOutTime,127)
menu:setBackground(sprite)


--[[
local sprite = KrSprite(vault:GetSpriteResource("hatch"))
sprite:SetColor(KrColorTransform(KrRGBA(0, 0, 0, 96)), -1);
--sprite:SetPos(-windowLeft, -windowTop, -1)
menu:setBackground(sprite)
menu:setBackgroundSize(game:screenWidth(), game:screenHeight())
]]--

windowLeft = 356
windowTop = 273


local windowBack = KrSprite(vault:GetSpriteResource("book_bg"))

local pageX = 60;
local pageY = 130;

windowBack:SetPos(pageX, pageY, -1)

-- aniFadeInFadeOut(windowBack, fadeInDelay, fadeInTime, fadeOutTime, 255)
aniOutline(windowBack, fadeInDelay, fadeInTime, fadeOutDelay, fadeOutTime, 255)


menu:setImageFrame(windowBack)

-- Pages

-- pageX = pageX - 6;
-- pageY = pageY - 39;

local page1 = KrImNode()
page1:SetPos(pageX, pageY, -1)
aniFadeInFadeOut(page1, pageFadeInDelay, pageFadeInTime, pageFadeOutTime, 255)
menu:addPage(page1);


local x1 = 250
local x2 = 639
local y1 = 95 + 10

local x = x1
local y = y1

local width = 300
local height = 30
local sensX = -47
local sensY = -15
local textY = sensY + 3;

local rText = 0;
local gText = 0;
local bText = 0;
local aText = 216;

local rHText = 255;
local gHText = 255;
local bHText = 255;
local aHText = 216;

local rowsInPage = 2;

menu:setRowsPerPage(rowsInPage);

local pageSideFlag = 0;
local j = 0;

local mList = game:getMelodyList();
local mCount = mList:getCount();

local shsh = 22;

for i=0, mCount-1 do
	local mItem = mList:getItem(i);




	local button = XButton();

	local tBox = KrTextBox(game:getFontResource("occ24b"), 370, 60, 0, KrTextBox.CENTER);
	tBox:SetPos(x - 185, y - 45, -1);
	tBox:SetTextMultiLine( getText( mItem:getTitleID() ) );
	setTextColor(tBox, 163, 50, 9, 255)
	addNode(tBox, page1);
	menu:registerGalka(tBox)

	local descBox = KrTextBox(game:getFontResource("occ14b"), 290, 66, 0, KrTextBox.LEFT);
	descBox:SetPos(x - 185, y - 45 - 15, -1);
	descBox:SetTextMultiLine( getText( mItem:getTitleID().."_desc" ) );
	setTextColor(descBox, 104, 80, 38, 200)
	addNode(descBox, page1);
	menu:registerGalka2(descBox)
	                                       

	local uzor;

	if y < 300 then
		if pageSideFlag == 0 then
			tBox:SetPos(x - 185 + 5, y - 45 - 16 , -1);
			descBox:SetPos(x - 185 + 5 + 46 + 31, y - 45 - 16 + 63, -1);
			uzor = KrSprite(vault:GetSpriteResource("uzor_lu"));
			uzor:SetPos(83 - 60, 139 - 131 + 7, -1);
			createBrokenOutline(menu, vault, page1, mList:getItem(i):getID(), mList:getItem(i):getPiecesCount(), "LU", 27 + 189 + pageX, y + 77 + pageY, outlineFadeInDelay, outlineFadeInTime, outlineFadeOutDelay, outlineFadeOutTime);
			initXBtn(button, "fake_hatch", 27 + 189, y + 77, -2, -2, 88, 88, vault)
		else
			tBox:SetPos(x - 185 + 7, y - 45 - 17 , -1);
			descBox:SetPos(x - 185 + 7 + 46 + 48 - 10, y - 45 - 17 + 63, -1);
			uzor = KrSprite(vault:GetSpriteResource("uzor_ru"));
			uzor:SetPos(83 - 60 - 10, 139 - 131 + 7, -1);
			createBrokenOutline(menu, vault, page1, mList:getItem(i):getID(), mList:getItem(i):getPiecesCount(), "RU", 416 + 189 + pageX, y + 78 + pageY, outlineFadeInDelay, outlineFadeInTime, outlineFadeOutDelay, outlineFadeOutTime);
			initXBtn(button, "fake_hatch", 416 + 189, y + 78, -2, -2, 88, 88, vault)
		end
	else
		if pageSideFlag == 0 then
			tBox:SetPos(x - 185, y - 45 - 22, -1);
			descBox:SetPos(x - 185 + 46 + 37, y - 45 - 22 + 63 - 1, -1);
			uzor = KrSprite(vault:GetSpriteResource("uzor_ld"));
			uzor:SetPos(83 - 60, 139 - 131 + 7, -1);
			createBrokenOutline(menu, vault, page1, mList:getItem(i):getID(), mList:getItem(i):getPiecesCount(), "LD", 24 + 189 + pageX, y + 77 + pageY, outlineFadeInDelay, outlineFadeInTime, outlineFadeOutDelay, outlineFadeOutTime);
			initXBtn(button, "fake_hatch", 24 + 189, y + 77, -2, -2, 88, 88, vault)
		else
			tBox:SetPos(x - 185 + 10, y - 45 - 22, -1);
			descBox:SetPos(x - 185 + 10 + 46 + 44 - 10, y - 45 - 22 + 63 - 1, -1);
			uzor = KrSprite(vault:GetSpriteResource("uzor_rd"));
			uzor:SetPos(83 - 60 - 10, 139 - 131 + 7, -1);
			createBrokenOutline(menu, vault, page1, mList:getItem(i):getID(), mList:getItem(i):getPiecesCount(), "RD", 417 + 189 + pageX, y + 78 + pageY, outlineFadeInDelay, outlineFadeInTime, outlineFadeOutDelay, outlineFadeOutTime);
			initXBtn(button, "fake_hatch", 417 + 189, y + 78, -2, -2, 88, 88, vault)
		end
	end

	local uzorRoot = KrImNode();

--	uzor:SetPos(83 - 60, 139 - 131 + 7, -1);

	if( j == rowsInPage - 1 ) then
		local separator = KrSprite(vault:GetSpriteResource("melody_separator"));
		if pageSideFlag == 0 then
			separator:SetPos(83 - 60 + 94, 139 - 131 + 271, -1);
		else
			separator:SetPos(83 - 60 + 486, 139 - 131 + 271, -1);
		end
		addNodeAdopt(separator, uzorRoot);
	end


	addNodeAdopt(uzor, uzorRoot);

	addNode(uzorRoot, page1);
	menu:registerGalka3(uzorRoot)

	initXButtonTextUnshifted(button, "", -185, -45 - shsh, 370, 60, "occ24b", KrTextBox.CENTER)
	addNode(button:getRoot(), page1);

	menu:registerMelodyButton(button);

	y = y + 253;

	j = j + 1;

	if( j == rowsInPage ) then
		j = 0;
		pageSideFlag = pageSideFlag + 1;
		if( pageSideFlag == 2 ) then
			pageSideFlag = 0;
			x = x1;
			y = y1;
            page1 = KrImNode();
			page1:SetPos(pageX, pageY, -1)
			aniFadeInFadeOut(page1, pageFadeInDelay, pageFadeInTime, pageFadeOutTime, 255)
			menu:addPage(page1);

		else
			x = x2;
			y = y1;
		end

	end
end;

-- if( i > 0 ) then 
--	menu:addPage(page1);
-- end

local hover = KrSprite(vault:GetSpriteResource("hover_LU"))
hover:SetPos(29 + 189 + pageX, 105 + 78 + pageY, -1)
aniHover(hover, hoverDelay, hoverIn, hoverOut, hoverLeavingIn, hoverLeavingOut, 255)
menu:addHover(hover);

hover = KrSprite(vault:GetSpriteResource("hover_RU"))
hover:SetPos(418 + 189 + pageX, 105 + 78 + pageY, -1)
aniHover(hover, hoverDelay, hoverIn, hoverOut, hoverLeavingIn, hoverLeavingOut, 255)
menu:addHover(hover);

hover = KrSprite(vault:GetSpriteResource("hover_LD"))
hover:SetPos(26 + 189 + pageX, 105 + 253 + 78 + pageY, -1)
aniHover(hover, hoverDelay, hoverIn, hoverOut, hoverLeavingIn, hoverLeavingOut, 255)
menu:addHover(hover);

hover = KrSprite(vault:GetSpriteResource("hover_RD"))
hover:SetPos(419 + 189 + pageX, 105 + 253 + 78 + pageY, -1)
aniHover(hover, hoverDelay, hoverIn, hoverOut, hoverLeavingIn, hoverLeavingOut, 255)
menu:addHover(hover);





---------------------------------------------------

	local button = XButton();

	local x = 1024 - 68
	local y = 18

	local width = 94
	local height = 30
	local sensX = -47
	local sensY = -15
	local textY = sensY + 3;

	local rText = 245;
	local gText = 203;
	local bText = 99;
	local aText = 216;

	local rHText = 255;
	local gHText = 255;
	local bHText = 255;
	local aHText = 216;

	initXBtn(button, "Btn_game", x, y, sensX, sensY, width, height, vault)
	initXButtonText(button, "GAME_SCREEN.MENU",
						sensX, textY, 
						width, height, 
						"occ24bg", KrTextBox.CENTER)

	aniFadeInFadeOut(button:getRoot(),fadeInDelay, fadeInTime, fadeOutTime, 255)
	button:setTextColor(rText, gText, bText, aText);
	button:setTextColorBType(XButton.NORMAL_HIGHLIGHTED, rHText, gHText, bHText, aHText);
	button:setTextColorBType(XButton.NORMAL_PRESSED, rHText, gHText, bHText, aHText);
	button:setAccelerator(XButton.ESCAPE);
	menu:setButton(button);



	button = XButton();
	initXBtn(button, "Btn_arrow_l", 194, 630, -47, -43, 54, 62, vault)
	aniFadeInFadeOut(button:getRoot(),fadeInDelay, fadeInTime, fadeOutTime, 255)
	menu:setLeftArrowButton(button);

	button = XButton();
	initXBtn(button, "Btn_arrow_r", 829, 631, -8, -32, 49, 60, vault)
	aniFadeInFadeOut(button:getRoot(),fadeInDelay, fadeInTime, fadeOutTime, 255)
	menu:setRightArrowButton(button);

-- create and init textBox

local width = 250;
local height= 150;

local textBox = KrTextBox(game:getFontResource("occ24"), width, height, 2, KrTextBox.CENTER);
aniFadeInFadeOut(textBox, fadeInDelay, fadeInTime , fadeOutTime)

textBox:SetPos(windowLeft + 40 + width/2, windowTop + 35 + height/2, -1);

menu:setTextBox(textBox);

-- end crate text box

print("init Message Window end");


