function OnCreateContent()
-- helpScreen
	
	CreateGraphics();
    CreateButtons();
	PlaySound('L99S01');
end

function OnFreeContent()
end

function HelpScreen_OnButtonPress(name)
	if ( name == 'MSG_NEXT_PAGE' ) then	
	    if ( helpScreen.activePage == 'page1') then
	        helpScreen.activePage = 'page2';
            SetPageVisible('page1', false);
            SetPageVisible('page2', true);
			auxSetButtonActive(helpScreen.prevButton, true);
	        return;
	    end
	    if ( helpScreen.activePage == 'page2') then
	        helpScreen.activePage = 'page3';
            SetPageVisible('page2', false);
            SetPageVisible('page3', true);
			auxSetButtonActive(helpScreen.nextButton, false);
	        return;
	    end
--	    if (helpScreen.activePage == 'page3') then
--		    return;
--		end
	end
	if ( name == 'MSG_PREV_PAGE' ) then
        if (helpScreen.activePage == 'page2') then
	        helpScreen.activePage = 'page1';
            SetPageVisible('page1', true);
            SetPageVisible('page2', false);
			auxSetButtonActive(helpScreen.prevButton, false);
	        return;
	    end;
        if (helpScreen.activePage == 'page3') then
	        helpScreen.activePage = 'page2';
            SetPageVisible('page2', true);
            SetPageVisible('page3', false);
			auxSetButtonActive(helpScreen.nextButton, true);
	        return;
	    end;
	end
	if ( name == 'MSG_CLOSE' ) then
	    helpScreen:ScreenDestroy();
	    ReleaseFocus(helpScreen);
		return;
	end
end

------------------------------------------------------------------------------
--// auxiliary functions

function CreateGraphics()
	local bg = CreateSprite(); 
	bg:AttachTo(helpScreen);
	bg:SetSprite('CreditsWindowTexture');
	bg:SetXY(SCREEN_WIDTH/2.0, SCREEN_HEIGHT/2.0);
	bg:SetZ(-0.2);
--bg:SetColor('0x10FFFFFF');
	bg:SetVisible(true);

	local textTitle = CreateTextEntity('GUICaptionFont', 'HELP');
    textTitle:AttachTo(bg);
	textTitle:SetXY(-10.0, -115.0);
	textTitle:SetZ(0.1);
	textTitle:SetScale(1.2);

--// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    do
        local page = CreateNewPage(bg,'page1');
    	page:SetOpacity(1.0);
    	CreateCursorDescription(page, -240.0 - 20.0, -90.0,        1.0, 'CursorArrow', 		40.0 + 20.0, 0.0 + 10.0, 'CURSOR_ARROW_DESCRIPTION');
    	CreateCursorDescription(page, -240.0 - 30.0, -20.0, 	   1.0, 'CursorArms',  		40.0 + 30.0, 0.0 - 10.0, 'CURSOR_ARMS_DESCRIPTION');
    	CreateCursorDescription(page, -240.0,        40.0 + 20.0,  1.0, 'CursorNavigation', 40.0,        0.0 - 20.0, 'CURSOR_NAVIG_DESCRIPTION');
	    CreateCursorDescription(page, -240.0 - 20.0, 100.0 + 10.0, 1.0, 'CursorZoom', 		40.0 + 20.0, 0.0,        'CURSOR_ZOOM_DESCRIPTION');
	end
	do
        local page = CreateNewPage(bg,'page2');
    	page:SetOpacity(0.0);
    	CreateCubeDescription(page, -220.0, -50.0, 0.6, 'GamePanel_CubeAnimation_0to90', -40.0, -20.0,  'CUBE_DESCRIPTION');
	end
	do
        local page = CreateNewPage(bg,'page3');
    	page:SetOpacity(0.0);
    	CreateHintDescription(page, -220.0, -70.0, 0.8, -40.0,0.0,  'HINT_DESCRIPTION');
	end
	helpScreen.activePage = 'page1';

	CreateWindowShade(bg);
end

function CreateButtons()
	do
		local button = CreateButton('ButtonUp','ButtonOver','ButtonDown', 'MSG_PREV_PAGE', helpScreen);
		helpScreen.prevButton = button;
		button:AttachTo(helpScreen);
		button:SetXY(SCREEN_WIDTH/2.0 - 166.0, 570.0);
		button:SetZ(-0.1);
		button:SetVisible(true);
		auxSetButtonActive(button, false);

		local caption = CreateTextEntity('GUICaptionFont', 'PREV');
	    caption:AttachTo(button);
		caption:SetXY(0,0);
		caption:SetZ(0.05);
	end
	do
		local button = CreateButton('ButtonUp','ButtonOver','ButtonDown', 'MSG_NEXT_PAGE', helpScreen);
		helpScreen.nextButton = button;
		button:AttachTo(helpScreen);
		button:SetXY(SCREEN_WIDTH/2.0, 570.0);
		button:SetZ(-0.1);
		button:SetVisible(true);

		local caption = CreateTextEntity('GUICaptionFont', 'NEXT');
	    caption:AttachTo(button);
		caption:SetXY(0,0);
		caption:SetZ(0.05);
	end
	do
		local button = CreateButton('ButtonUp','ButtonOver','ButtonDown', 'MSG_CLOSE', helpScreen);
		helpScreen.closeButton = button;
		button:AttachTo(helpScreen);
		button:SetXY(SCREEN_WIDTH/2.0 + 166.0, 570.0);
		button:SetZ(-0.1);
		button:SetVisible(true);

		local caption = CreateTextEntity('GUICaptionFont', 'CLOSE');
	    caption:AttachTo(button);
		caption:SetXY(0,0);
		caption:SetZ(0.05);
	end
end

function CreateNewPage(parent, name)
	local page = CreateSprite();
	page.name = name;
	page:AttachTo(parent);
	page:SetSprite('DummyTexture');
	page:SetColor('0x00FFFFFF');
	page:SetVisible(true);

    if (helpScreen.pages == nil) then
        helpScreen.pages = {};
    end        
    assert(helpScreen.pages[name] == nil);
    helpScreen.pages[name] = page;
    return page;
end

function SetPageVisible(pageName, bShowPage)
    assert(helpScreen.pages[pageName] ~= nil);
    local object = helpScreen.pages[pageName];
    local af = CreateAnimationEffector();
    af:AttachTo(object);
    if (bShowPage) then
        af:SetKey(_TRACK_O, 0.0, 0.0);
        af:SetKey(_TRACK_O, 0.5, 1.0);
    else
        af:SetKey(_TRACK_O, 0.0, 1.0);
        af:SetKey(_TRACK_O, 0.5, 0.0);
    end
    af:Play();
end

function CreateAnimatedIcon(parent, posX,posY, scale, spriteName)
 	local icon = CreateAnimationEntity();
	icon:AttachTo(parent);
 	icon:SetAnimCopy(spriteName);
 	icon:SetVisible(true);
 	icon:SetXY(posX, posY);
 	icon:SetScale(scale);
 	icon:SetZ(0.3);
 	icon:Play();
 	return icon;
end

function CreateDescription(parent, posX,posY, descriptionText)
 	local text = CreateTextEntity('GUITextFont', descriptionText);
	text:AttachTo(parent);
 	text:SetTextAlignment(HGETEXT_LEFT + HGETEXT_MIDDLE);
 	text:SetXY(posX, posY);
 	text:SetZ(0.1);
 	text:SetSpacing(0.6);
 	text:SetScale(1.0);
 	return text;
end

function CreateCursorDescription(page, posX,posY, iconScale, spriteName, textDX,textDY, descriptionText) 	
    page.icon = CreateAnimatedIcon(page, posX,posY, iconScale, spriteName);
    page.text = CreateDescription(page, posX+textDX,posY+textDY, descriptionText);
end

function CreateCubeDescription(page, posX,posY, iconScale, spriteName, textDX,textDY, descriptionText)
    page.animations = {};
    page.animations[0] = CreateAnimatedIcon(page, posX,posY, iconScale, spriteName);
    page.text = CreateDescription(page, posX+textDX,posY+textDY, descriptionText);

 	page.animations[0]:GetAnimation():SetMode(HGEANIM_PINGPONG + HGEANIM_LOOP);
end

function CreateHintDescription(page, posX,posY, iconScale, textDX,textDY, descriptionText)
	local hintStand = CreateSprite(); 
	hintStand:AttachTo(page);
	hintStand:SetSprite('GameGUI_Panel_HintBallStand');
	hintStand:SetXY(posX, posY + 20.0);
	hintStand:SetScale(iconScale);
	hintStand:SetZ(0.1);
	hintStand:SetVisible(true);

	local hintStand = CreateSprite(); 
	hintStand:AttachTo(page);
	hintStand:SetSprite('GameGUI_Panel_HintBallTexture');
	hintStand:SetXY(posX + 5.0, posY + 15.0);
	hintStand:SetScale(iconScale);
	hintStand:SetZ(0.05);
	hintStand:SetVisible(true);

    page.text = CreateDescription(page, posX+textDX,posY+textDY, descriptionText);
end

function auxChangeStateWithDelay(button, state)
    assert(button);
	if (state == false) then
	    button:SetOpacity(0.5);
--		button:GetAnimation():SetColor('0xFF888888');
		button:Stop();
	else
	    button:SetOpacity(1.0);
--		button:GetAnimation():SetColor('0xFFFFFFFF');
		button:Go();
	end
end

function auxSetButtonActive(button, state)
	assert(button ~=nil and state ~= nil);
	local delay = 0.075;
	globalButton = button;
    GTimerDoString('auxChangeStateWithDelay(globalButton,'..(state==true and 'true' or 'false')..')', delay);
end
