function OnCreateContent()
--	optionsScreen = screen;
	
	mainMenuState ={};

	CreateGraphics();
    CreateButtons();
	PlaySound('L99S01');
end

function OnFreeContent()
--	optionsScreen= nil;
end


function OptionScreen_OnButtonPress(name)
	if (name == 'MODE') then
		assert(mainMenuState.fullScreenCheck ~= nil);
		mainMenuState.fullScreenCheck:SetVisible(not mainMenuState.fullScreenCheck:IsVisible());
	end
	if (name == 'CUSTOM_CURSOR') then
		assert(mainMenuState.customCursorCheck ~= nil);
		mainMenuState.customCursorCheck:SetVisible(not mainMenuState.customCursorCheck:IsVisible());
	end
	if (name == 'SOUND') then
		mainMenuState.currentVolumeButton = mainMenuState.soundVolumeButton;
	end
	if (name == 'MUSIC') then
		mainMenuState.currentVolumeButton = mainMenuState.musicVolumeButton;
	end
end

function OptionScreen_OnButtonDepress(name)
	if (name == 'SOUND' or name == 'MUSIC') then
		mainMenuState.currentVolumeButton = nil;
	end
end

function OptionScreen_OnMouseMove(dx, dy)
    assert(optionsScreen ~= nil);
	if (mainMenuState.currentVolumeButton ~= nil ) then
		local x = mainMenuState.currentVolumeButton:GetScrX() + dx;
		if (x < mainMenuState.minPos) then
		    x = mainMenuState.minPos;
		end
		if (x > mainMenuState.maxPos) then
		    x = mainMenuState.maxPos;
		end
--		local y = mainMenuState.currentVolumeButton:GetY();
--		mainMenuState.currentVolumeButton:SetScrXY(x + dx, y + dy);
        mainMenuState.currentVolumeButton:SetScrXY(x, mainMenuState.currentVolumeButton:GetScrY());
	end
end

function OnUpdate(dt)
end

function OptionScreen_GetFullScreenVideoMode()
    return mainMenuState.fullScreenCheck:IsVisible();
end

function OptionScreen_GetCustomCursorMode()
    return mainMenuState.customCursorCheck:IsVisible();
end

function OptionScreen_GetSoundVolume()
    return CalculateVolumeBySliderPos(mainMenuState.soundVolumeButton);
end

function OptionScreen_GetMusicVolume()
    return CalculateVolumeBySliderPos(mainMenuState.musicVolumeButton);
end

function OptionScreen_SetOptionsSettings(soundVolume, musicVolume, bVideoMode, customCursorMode)
	mainMenuState.customCursorCheck:SetVisible(customCursorMode);
    mainMenuState.fullScreenCheck:SetVisible(bVideoMode);
    SetSliderPosByValue(mainMenuState.soundVolumeButton, soundVolume);
    SetSliderPosByValue(mainMenuState.musicVolumeButton, musicVolume);
end

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

function CalculateVolumeBySliderPos(slider)
    local volume = (slider:GetScrX() - mainMenuState.minPos + 0.01) / (mainMenuState.maxPos - mainMenuState.minPos);
    return volume;
end

function SetSliderPosByValue(slider, value)
    local x = mainMenuState.minPos + value * (mainMenuState.maxPos - mainMenuState.minPos);
    slider:SetScrXY(x, slider:GetScrY());
end

function CreateGraphics()
	local window = CreateSprite(); 
	window:SetName('OptionsWindow');
	window:AttachTo(optionsScreen);
	window:SetSprite('OptionsWindowTexture');
	window:SetXY(SCREEN_WIDTH/2.0, SCREEN_HEIGHT/2.0);
	window:SetZ(0.1);
	window:SetVisible(true);

	local textTitle = CreateTextEntity('GUICaptionFont', 'OPTIONS');
	textTitle:SetName('OptionsTitle');
    textTitle:AttachTo(window);
	textTitle:SetXY(-10.0, -115.0);
	textTitle:SetZ(0.1);
	textTitle:SetScale(1.2);

	local textSndVolume = CreateTextEntity('GUITextFont', 'SOUND_VOLUME');
	textSndVolume:SetName('CoundVolumeText');
    textSndVolume:AttachTo(window);
	textSndVolume:SetXY(-10.0, -30.0);
	textSndVolume:SetZ(0.1);

	local textMsVolume = CreateTextEntity('GUITextFont', 'MUSIC_VOLUME');
	textMsVolume:SetName('MusicVolumeText');
    textMsVolume:AttachTo(window);
	textMsVolume:SetXY(-10.0, 65.0);
	textMsVolume:SetZ(0.1);
	
	textMode = CreateTextEntity('GUITextFont', 'FULLSCREEN');
	textMode:SetName('FullScreenText');
    textMode:AttachTo(window);
	textMode:SetXY(-160.0, 110.0);
	textMode:SetZ(0.1);

	cursorMode = CreateTextEntity('GUITextFont', 'CUSTOM_CURSOR');
	cursorMode:SetName('CustmCursorText');
    cursorMode:AttachTo(window);
	cursorMode:SetXY(70.0, 110.0);
	cursorMode:SetZ(0.1);

	CreateWindowShade(window);
end

function CreateButtons()
	do
		local button = CreateButton('ButtonUp','ButtonOver','ButtonDown', 'OK', optionsScreen);
		button:AttachTo(optionsScreen);
		button:SetXY(SCREEN_WIDTH/2.0, 570.0);
		button:SetZ(1.0);
		button:SetVisible(true);

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

	--// system cursor checkbox
	do
	    local checkboxButton = optionsScreen:AddButton(100.0, 0.0, 1.0,
	                            'OptionsModeCheckboxButtonUp','OptionsModeCheckboxButtonOver','OptionsModeCheckboxButtonDown', 'MODE');
		checkboxButton:AttachTo(textMode);
		do
			local check = CreateSprite(); 
			check:SetName('FullScreenCheck');
			check:AttachTo(checkboxButton);
			check:SetSprite('OptionsModeCheckboxTexture');
			check:SetXY(0.0, 0.0);
			check:SetZ(0.1);
			check:SetVisible(true);
			mainMenuState.fullScreenCheck = check;
		end
	end

	--// fullscreen mode checkbox
	do
	    local checkboxButton = optionsScreen:AddButton(110.0, 0.0, 1.0,
	                            'OptionsModeCheckboxButtonUp','OptionsModeCheckboxButtonOver','OptionsModeCheckboxButtonDown', 'CUSTOM_CURSOR');
		checkboxButton:AttachTo(cursorMode);
		do
			local check = CreateSprite(); 
			check:SetName('CustomCursorCheck');
			check:AttachTo(checkboxButton);
			check:SetSprite('OptionsModeCheckboxTexture');
			check:SetXY(0.0, 0.0);
			check:SetZ(0.1);
			check:SetVisible(true);
			mainMenuState.customCursorCheck = check;
		end
	end

	mainMenuState.minPos = SCREEN_WIDTH/2.0 - 210.0;
	mainMenuState.maxPos = SCREEN_WIDTH/2.0 + 200.0;
    mainMenuState.soundVolumeButton = optionsScreen:AddButton(mainMenuState.minPos, SCREEN_HEIGHT/2.0 - 65.0, 1.0,
                            'OptionsSoundVolumeButtonUp','OptionsSoundVolumeButtonOver','OptionsSoundVolumeButtonDown', 'SOUND');
    mainMenuState.musicVolumeButton = optionsScreen:AddButton(mainMenuState.minPos, SCREEN_HEIGHT/2.0 + 30.0, 1.0,
                            'OptionsMusicVolumeButtonUp','OptionsMusicVolumeButtonOver','OptionsMusicVolumeButtonDown', 'MUSIC');
	optionsScreen.currentVolumeButton = nil;
end
