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

--------------------------------------------------------------------------------
function BuildOptionsScreenBack()

    local zOffset = 50;

    local screen = CreateScreen();
    screen:SetLayer(POPUP_WINDOWS_LAYER);
    
    local movieDuration = 12;
    
    -- Layer GearWheel_1
    anim = screen:CreateAnimation("data/Texture/GUI/Options/00004.png");
    anim:SetHotSpot(51.5, 51.5);
    anim:SetPosXY(518, 410.5);
    anim:SetPosZ(16 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetOpacity(1);

    effector = CreateAnimationEffector();
    effector:SetLifeTime(0, 12);
    effector:AttachTo(anim);
    effector:Play();

    effector:SetMode(_ANIM_LOOP);
    effector:SetAnimationTime(movieDuration);

    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 11.9583333333333, -12.5489173075);

    -- Layer GearWheel_2
    anim = screen:CreateAnimation("data/Texture/GUI/Options/00003.png");
    anim:SetHotSpot(120.5, 120.5);
    anim:SetPosXY(427, 415.5);
    anim:SetPosZ(15 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetOpacity(1);

    effector = CreateAnimationEffector();
    effector:SetLifeTime(0, 12);
    effector:AttachTo(anim);
    effector:Play();

    effector:SetMode(_ANIM_LOOP);
    effector:SetAnimationTime(movieDuration);

    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 11.9583333333333, 12.5489173075);

    -- Layer GearWheel_3
    anim = screen:CreateAnimation("data/Texture/GUI/Options/00001.png");
    anim:SetHotSpot(68.5, 68.5);
    anim:SetPosXY(657.20426940918, 487.475296020508);
    anim:SetPosZ(14 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetOpacity(1);

    effector = CreateAnimationEffector();
    effector:SetLifeTime(0, 12);
    effector:AttachTo(anim);
    effector:Play();

    effector:SetMode(_ANIM_LOOP);
    effector:SetAnimationTime(movieDuration);

    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 11.9583333333333, -6.2657320075);

    -- Layer GearWheel_4
    anim = screen:CreateAnimation("data/Texture/GUI/Options/00001.png");
    anim:SetHotSpot(68.5, 68.5);
    anim:SetPosXY(576.228988647461, 345.768539428711);
    anim:SetPosZ(13 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetOpacity(1);

    effector = CreateAnimationEffector();
    effector:SetLifeTime(0, 12);
    effector:AttachTo(anim);
    effector:Play();

    effector:SetMode(_ANIM_LOOP);
    effector:SetAnimationTime(movieDuration);

    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 11.9583333333333, -6.2657320075);

    -- Layer GearWheel_5
    anim = screen:CreateAnimation("data/Texture/GUI/Options/00001.png");
    anim:SetHotSpot(68.5, 68.5);
    anim:SetPosXY(343.425033569336, 364.747116088867);
    anim:SetPosZ(12 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetOpacity(1);

    effector = CreateAnimationEffector();
    effector:SetLifeTime(0, 12);
    effector:AttachTo(anim);
    effector:Play();

    effector:SetMode(_ANIM_LOOP);
    effector:SetAnimationTime(movieDuration);

    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 11.9583333333333, -6.2657320075);

    -- Layer GearWheel_6
    anim = screen:CreateAnimation("data/Texture/GUI/Options/00001.png");
    anim:SetHotSpot(68.5, 68.5);
    anim:SetPosXY(661, 406.5);
    anim:SetPosZ(11 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetOpacity(1);

    effector = CreateAnimationEffector();
    effector:SetLifeTime(0, 12);
    effector:AttachTo(anim);
    effector:Play();

    effector:SetMode(_ANIM_LOOP);
    effector:SetAnimationTime(movieDuration);

    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 11.9583333333333, -6.2657320075);

    -- Layer GearWheel_7
    anim = screen:CreateAnimation("data/Texture/GUI/Options/00002.png");
    anim:SetHotSpot(51.5, 51.5);
    anim:SetPosXY(576, 346.5);
    anim:SetPosZ(10 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetOpacity(1);

    effector = CreateAnimationEffector();
    effector:SetLifeTime(0, 12);
    effector:AttachTo(anim);
    effector:Play();

    effector:SetMode(_ANIM_LOOP);
    effector:SetAnimationTime(movieDuration);

    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 11.9583333333333, 12.5489173075);

    -- Layer GearWheel_8
    anim = screen:CreateAnimation("data/Texture/GUI/Options/0001.png");
    anim:SetHotSpot(51.5, 51.5);
    anim:SetPosXY(658, 486.5);
    anim:SetPosZ(9 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetOpacity(1);

    effector = CreateAnimationEffector();
    effector:SetLifeTime(0, 12);
    effector:AttachTo(anim);
    effector:Play();

    effector:SetMode(_ANIM_LOOP);
    effector:SetAnimationTime(movieDuration);

    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 11.9583333333333, 18.8321026075);

    -- Layer GearWheel_9
    anim = screen:CreateAnimation("data/Texture/GUI/Options/0002.png");
    anim:SetHotSpot(120.5, 120.5);
    anim:SetPosXY(577, 347.5);
    anim:SetPosZ(8 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetOpacity(1);

    effector = CreateAnimationEffector();
    effector:SetLifeTime(0, 12);
    effector:AttachTo(anim);
    effector:Play();

    effector:SetMode(_ANIM_LOOP);
    effector:SetAnimationTime(movieDuration);

    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 11.9583333333333, -6.2657320075);

    -- Layer GearWheel_10
    anim = screen:CreateAnimation("data/Texture/GUI/Options/002.png");
    anim:SetHotSpot(51.5, 51.5);
    anim:SetPosXY(569.902801513672, 537.084838867188);
    anim:SetPosZ(7 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetOpacity(1);

    effector = CreateAnimationEffector();
    effector:SetLifeTime(0, 12);
    effector:AttachTo(anim);
    effector:Play();

    effector:SetMode(_ANIM_LOOP);
    effector:SetAnimationTime(movieDuration);

    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 11.9583333333333, -12.5489173075);

    -- Layer GearWheel_11
    anim = screen:CreateAnimation("data/Texture/GUI/Options/002.png");
    anim:SetHotSpot(51.5, 51.5);
    anim:SetPosXY(477.54035949707, 285.302307128906);
    anim:SetPosZ(6 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetOpacity(1);

    effector = CreateAnimationEffector();
    effector:SetLifeTime(0, 12);
    effector:AttachTo(anim);
    effector:Play();

    effector:SetMode(_ANIM_LOOP);
    effector:SetAnimationTime(movieDuration);

    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 11.9583333333333, 18.8321026075);

    -- Layer GearWheel_12
    anim = screen:CreateAnimation("data/Texture/GUI/Options/002.png");
    anim:SetHotSpot(51.5, 51.5);
    anim:SetPosXY(661, 405.5);
    anim:SetPosZ(5 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetOpacity(1);

    effector = CreateAnimationEffector();
    effector:SetLifeTime(0, 12);
    effector:AttachTo(anim);
    effector:Play();

    effector:SetMode(_ANIM_LOOP);
    effector:SetAnimationTime(movieDuration);

    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 11.9583333333333, 18.8321026075);

    -- Layer GearWheel_13
    anim = screen:CreateAnimation("data/Texture/GUI/Options/001.png");
    anim:SetHotSpot(68.5, 68.5);
    anim:SetPosXY(685.373977661133, 377.277603149414);
    anim:SetPosZ(4 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetOpacity(1);

    effector = CreateAnimationEffector();
    effector:SetLifeTime(0, 12);
    effector:AttachTo(anim);
    effector:Play();

    effector:SetMode(_ANIM_LOOP);
    effector:SetAnimationTime(movieDuration);

    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 11.9583333333333, 6.2657320075);

    -- Layer GearWheel_14
    anim = screen:CreateAnimation("data/Texture/GUI/Options/001.png");
    anim:SetHotSpot(68.5, 68.5);
    anim:SetPosXY(614.520599365234, 443.070022583008);
    anim:SetPosZ(3 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetOpacity(1);

    effector = CreateAnimationEffector();
    effector:SetLifeTime(0, 12);
    effector:AttachTo(anim);
    effector:Play();

    effector:SetMode(_ANIM_LOOP);
    effector:SetAnimationTime(movieDuration);

    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 11.9583333333333, 6.2657320075);

    -- Layer GearWheel_15
    anim = screen:CreateAnimation("data/Texture/GUI/Options/001.png");
    anim:SetHotSpot(68.5, 68.5);
    anim:SetPosXY(426, 416.5);
    anim:SetPosZ(2 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetOpacity(1);

    effector = CreateAnimationEffector();
    effector:SetLifeTime(0, 12);
    effector:AttachTo(anim);
    effector:Play();

    effector:SetMode(_ANIM_LOOP);
    effector:SetAnimationTime(movieDuration);

    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 11.9583333333333, 6.2657320075);

    -- Layer Back_1
    anim = screen:CreateAnimation("data/Texture/GUI/Options/back.png");
    anim:SetHotSpot(269.5, 150.5);
    anim:SetPosXY(512, 399.5);
    anim:SetPosZ(1 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetAngle(0);
    anim:SetOpacity(1);

   
    return screen;

end
--------------------------------------------------------------------------------
function StartOptionsScreen()

    optionsScreen = BuildOptionsScreenBack();
    
     local zOffset = 50;
        
    -- Layer Front_1
    anim = optionsScreen:CreateAnimation("data/Texture/GUI/Options/options.png");
    anim:SetHotSpot(353, 243);
    anim:SetPosXY(512, 406.5);
    anim:SetPosZ(17 + zOffset);
    anim:SetScaleXY(1, 1);
    anim:SetAngle(0);
    anim:SetOpacity(1);
    
    local textBox = CreateTextEntity(INGAME_MENU_FONT, "MUSIC_VOLUME");
	textBox:SetXY(512, 300);
	textBox:SetScale(0.7);
	textBox:SetZ(20 + zOffset);
	textBox:SetRect(412, 264, 200, 100);
	textBox:AttachTo(optionsScreen);
	
	local textBox = CreateTextEntity(INGAME_MENU_FONT, "SOUND_VOLUME");
	textBox:SetXY(512, 372);
	textBox:SetScale(0.7);
	textBox:SetZ(20 + zOffset);
	textBox:SetRect(412, 264, 200, 100);
	textBox:AttachTo(optionsScreen);
	
	local textBox = CreateTextEntity(INGAME_MENU_FONT, "FULLSCREEN");
	textBox:SetXY(390, 445);
	textBox:SetScale(0.7);
	textBox:SetZ(20 + zOffset);
	textBox:SetRect(412, 264, 200, 100);
	textBox:AttachTo(optionsScreen);
	
	local textBox = CreateTextEntity(INGAME_MENU_FONT, "CUSTOM_CURSOR");
	textBox:SetXY(623, 445);
	textBox:SetScale(0.7);
	textBox:SetZ(20 + zOffset);
	textBox:SetRect(412, 264, 200, 100);
	textBox:AttachTo(optionsScreen);
    
    --
    
    local OKButton = MakeButton(435, 580, 100, 40);
    OKButton:GetCaption():SetFont(BUTTONS_FONT);
    OKButton:SetZ(20 + zOffset);
    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(600, 580, 100, 40);
	cancelButton:GetCaption():SetFont(BUTTONS_FONT);
	cancelButton:SetZ(20 + zOffset);
	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 = CreateSlider();
	musicSlider:SetRect(350, 310, 335, 40);
	musicSlider:SetZ(21 + zOffset);
	musicSlider:SetKnobTexture("data/Texture/GUI/Options/mask_01.png");
	musicSlider:SetKnobSize(44);
	musicSlider:AttachTo(optionsScreen);
	musicVolume = GM:GetMusicVolume();
	musicSlider:SetValue(musicVolume);
	musicSlider:SetAction("SetMusicVolume");
	
	--
	
	soundSlider = CreateSlider();
	soundSlider:SetRect(350, 385, 335, 40);
	soundSlider:SetZ(21 + zOffset);
	soundSlider:SetKnobTexture("data/Texture/GUI/Options/mask_02.png");
	soundSlider:SetKnobSize(44);
	soundSlider:AttachTo(optionsScreen);
	soundVolume = GM:GetSoundVolume();
	soundSlider:SetValue(soundVolume);
	soundSlider:SetAction("SetSoundVolume");
	
	-- 
	fullscreen = APP:IsFullScreen();
	
	fullscreenCheckbox = CreateCheckBox();
	fullscreenCheckbox:SetRect(382, 476, 47, 45);
	fullscreenCheckbox:SetZ(21 + zOffset);
	fullscreenCheckbox:SetOnTexture("data/Texture/GUI/Options/gal.png");
	fullscreenCheckbox:AttachTo(optionsScreen);
	fullscreenCheckbox:SetState(fullscreen and 1 or 0);
	fullscreenCheckbox:SetAction("SetFullscreen");
	
	optionsScreen.customCursorVisible = GM:GetShowCustomCursor();
	
	customCursorCheckbox = CreateCheckBox();
	customCursorCheckbox:SetRect(605, 476, 47, 45);
	customCursorCheckbox:SetZ(21 + zOffset);
	customCursorCheckbox:SetOnTexture("data/Texture/GUI/Options/gal.png");
	customCursorCheckbox:AttachTo(optionsScreen);
	customCursorCheckbox:SetState(optionsScreen.customCursorVisible and 1 or 0);
	customCursorCheckbox:SetAction("ToggleCustomCursorVisible");
	
	optionsScreen.fullscreenCheckbox = fullscreenCheckbox;
	optionsScreen.customCursorCheckbox = customCursorCheckbox;
	optionsScreen.testSound = CreateSoundEntity('Item_found');
	optionsScreen.testSound:AttachTo(optionsScreen);
    
	
	PopUpScreensMgr:Push(optionsScreen);
	
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);
        optionsScreen.testSound:StopSound();
        optionsScreen.testSound:Play(false);
    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