--------------------------------------------------------------------------------
optionsScreen = nil;
soundVolume = nil;
musicVolume = nil;
fullscreen = nil;
soundSlider = nil;
musicSlider = nil;
fullscreenCheckbox = nil;

--------------------------------------------------------------------------------
function StartOptionsScreen(back_fading, hidePrevScreen)
    hidePrevScreen = (hidePrevScreen == nil) and true or hidePrevScreen
    
    optionsScreen = CreateScreen();
    optionsScreen:SetLayer(POPUP_WINDOWS_LAYER);
        
    -- Layer Front_1
    anim = optionsScreen:CreateAnimation("data/Texture/GUI/Options/options.png"); -- 618 474
    anim:SetHotSpot(309, 237);
    anim:SetPosXY(512, 350);
    anim:SetPosZ(10);
    anim:SetScaleXY(1, 1);
    anim:SetAngle(0);
    anim:SetOpacity(1);
    
    if back_fading then
        anim = optionsScreen:CreateAnimation('data/Texture/Scenes/Common/fade.png');
        anim:SetHotSpot(256,192)
        anim:SetPosXY(512, 384);
        anim:SetPosZ(9);
        anim:SetScaleXY(2, 2);
        anim:SetBlendMode(2);
        anim:SetOpacity(0.8);
    end
    
    local textBox = CreateTextEntity(INGAME_MENU_FONT, "MUSIC_VOLUME");
	textBox:SetXY(512, 190);
	textBox:SetScale(0.8);
	textBox:SetZ(20);
	textBox:SetRect(412, 264, 200, 100);
	textBox:AttachTo(optionsScreen);
	
	local textBox = CreateTextEntity(INGAME_MENU_FONT, "SOUND_VOLUME");
	textBox:SetXY(512, 300);
	textBox:SetScale(0.8);
	textBox:SetZ(20);
	textBox:SetRect(412, 264, 200, 100);
	textBox:AttachTo(optionsScreen);
	
	local textBox = CreateTextEntity(INGAME_MENU_FONT, "FULLSCREEN");
	textBox:SetXY(516, 430);
	textBox:SetScale(0.8);
	textBox:SetZ(20);
	textBox:SetRect(412, 264, 200, 100);
	textBox:AttachTo(optionsScreen);
	
	local textBox = CreateTextEntity(INGAME_MENU_FONT, "CUSTOM_CURSOR");
	textBox:SetXY(485, 500);
	textBox:SetScale(0.8);
	textBox:SetZ(20);
	textBox:SetRect(412, 264, 200, 100);
	textBox:AttachTo(optionsScreen);
    
    --
    
    local OKButton = MakeButton(446, 570, 100, 40);
    OKButton:GetCaption():SetFont(BUTTONS_FONT);
    OKButton:SetZ(20);
    --OKButton:SetScale(0.85);
	--OKButton:GetCaption():SetScale(.7);
	
	OKButton:AttachTo(optionsScreen);
	OKButton:SetAction("OptionsScreenConfirm");
	OKButton:SetSpriteUp("OKButton");
	OKButton:SetSpriteOver("OKButtonOver");
	OKButton:SetSpriteDown("OKButtonDown");
	OKButton:SetActionSound("Menu_button_click");
	--OKButton:SetTitle("OK");
	
	local cancelButton = MakeButton(578, 570, 100, 40);
	cancelButton:GetCaption():SetFont(BUTTONS_FONT);
	cancelButton:SetZ(20);
	--cancelButton:SetScale(0.85);
	--cancelButton:GetCaption():SetScale(.7);
	
	cancelButton:AttachTo(optionsScreen);
	cancelButton:SetAction("OptionsScreenDiscard");
	cancelButton:SetSpriteUp("CancelButton");
	cancelButton:SetSpriteOver("CancelButtonOver");
	cancelButton:SetSpriteDown("CancelButtonDown");
	cancelButton:SetActionSound("Menu_button_click");
	--cancelButton:SetTitle("Close");
	
	--
	
	musicSlider = CreateNumberedSlider();
	musicSlider:SetRect(335, 225, 365, 45);
	musicSlider:SetZ(21);
	musicSlider:SetKnobTexture("data/Texture/GUI/Options/knob.png");
	musicSlider:SetKnobSize(70);
	musicSlider:AttachTo(optionsScreen);
	musicVolume = GM:GetMusicVolume();
	musicSlider:SetValue(musicVolume);
	musicSlider:SetAction("SetMusicVolume");
	
	--
	
	soundSlider = CreateNumberedSlider();
	soundSlider:SetRect(335, 335, 365, 45);
	soundSlider:SetZ(21);
	soundSlider:SetKnobTexture("data/Texture/GUI/Options/knob.png");
	soundSlider:SetKnobSize(70);
	soundSlider:AttachTo(optionsScreen);
	soundVolume = GM:GetSoundVolume();
	soundSlider:SetValue(soundVolume);
	soundSlider:SetAction("SetSoundVolume");
	
	-- 
	fullscreen = APP:IsFullScreen();
	
	fullscreenCheckbox = CreateCheckBox();
	fullscreenCheckbox:SetRect(628, 400, 55, 55);
	fullscreenCheckbox:SetZ(21);
	fullscreenCheckbox:SetOnTexture("data/Texture/GUI/Options/gal.png");
	fullscreenCheckbox:AttachTo(optionsScreen);
	fullscreenCheckbox:SetState(fullscreen and 1 or 0);
	fullscreenCheckbox:SetAction("SetFullscreen");
	
	optionsScreen.fullscreenCheckbox = fullscreenCheckbox;
	
	optionsScreen.customCursorVisible = GM:GetShowCustomCursor();
	
	customCursorCheckbox = CreateCheckBox();
	customCursorCheckbox:SetRect(628, 475, 55, 55);
	customCursorCheckbox:SetZ(21);
	customCursorCheckbox:SetOnTexture("data/Texture/GUI/Options/gal.png");
	customCursorCheckbox:AttachTo(optionsScreen);
	customCursorCheckbox:SetState(optionsScreen.customCursorVisible and 1 or 0);
	customCursorCheckbox:SetAction("ToggleCustomCursorVisible");
	
	optionsScreen.customCursorCheckbox = customCursorCheckbox;	
	
	PopUpScreensMgr:Push(optionsScreen, hidePrevScreen);
	
end

--------------------------------------------------------------------------------
function DestroyOptionsScreen()

    if (optionsScreen)
    then
        PopUpScreensMgr:Pop();
        optionsScreen = nil;
    end
    
    optionsScreen = nil;
    soundVolume = nil;
    musicVolume = nil;
    soundSlider = nil;
    musicSlider = nil;
    fullscreenCheckbox = nil;
    
    
    
end

--------------------------------------------------------------------------------
function OptionsScreenConfirm()

    DestroyOptionsScreen();
    
    GM:SetFullScreen(fullscreen);

end

--------------------------------------------------------------------------------
function OptionsScreenDiscard()

    if (soundVolume)
    then
        GM:SetSoundVolume(soundVolume);
    end
    
    if (musicVolume)
    then
        GM:SetMusicVolume(musicVolume);
    end
    
    GM:SetShowCustomCursor(optionsScreen.customCursorVisible);
    
    -- todo
    DestroyOptionsScreen();

end

--------------------------------------------------------------------------------
function SetSoundVolume(slider)

    local vol = soundSlider:GetValue();
    if (vol)
    then
        GM:SetSoundVolume(vol);
        PlaySound("Item_found");
    end

end

--------------------------------------------------------------------------------
function SetMusicVolume()

    local vol = musicSlider:GetValue();
    if (vol)
    then
        GM:SetMusicVolume(vol);
    end

end

--------------------------------------------------------------------------------
function SetFullscreen()

    if (fullscreenCheckbox:GetState() == 1)
    then
        fullscreen = true;
    else
        fullscreen = false;
    end
     
end

--------------------------------------------------------------------------------
function ToggleCustomCursorVisible()

    GM:SetShowCustomCursor(optionsScreen.customCursorCheckbox:GetState() == 1);

end