-- sound options menu
--------------------------------------------------------------------
local Scrolls = {scroll_main = nil,  scroll_music = nil,  scroll_dialogs = nil, scroll_fx = nil }
local SettingsMap = {}
local OldSettings = {}
local Settings
          
function OnShow()
  InitScrolls()
  
  for k,v in pairs(SettingsMap) do
    OldSettings[v] = Settings:GetParameter(v)
  end
  
  s.sys_menu_opt_snd=1
end

function OnCreate()
  s.sys_menu_opt_snd=1
end

function OnDestroy()
    s.sys_menu_opt_snd=0
end                          

function OnHide()
    s.sys_menu_opt_snd=0
    local resources = { "OptionsBG", "OptionsHScrollRightPart", "OptionsHScrollLeftPart", "OptionsHScrollTrackSlider","OptionsHSlider" }
    for _, v in pairs(resources) do
      UI:UnloadResource(v)
    end
    UI:DelWidget(self:GetName())
end
-- --------------------------------------------------------------------
--button
function btnBackOpt(_sender)
  Widget("MainMenu"):SetVisible(true)
  self:SetVisible(false)
  
  for k,v in pairs(OldSettings) do
    Settings:SetParameter(k, v)
  end
  Audio:UpdateVolume()
end

function btnAccept(_sender)
  Widget("MainMenu"):SetVisible(true)
  self:SetVisible(false)
  Settings:Save()
end
-- --------------------------------------------------------------------
 function OnScrollMouseRelease(_sender,_pos)
 
  local scroll = Scrolls[_sender:GetName()]
  Settings:SetParameter(SettingsMap[_sender:GetName()], scroll:GetScrollPosition() / (scroll:GetScrollRange() - 1) )
  
  Audio:UpdateVolume()
  if _sender:GetName() == "scroll_fx" or _sender:GetName() == "scroll_main" then Audio:Play( "snd_ho_take_item",false, false, -1, 0, false) end
 end

 local function CalculatePos(scroll, option)
  local v, x, r
  v = Settings:GetParameter(option)
  if v == "" then
    v = 0.5
  end
  r = scroll:GetScrollRange()
  x = r * v
  scroll:SetScrollPosition(x)
end

 function InitScrolls()
   Settings = Game:GetSettings()
  
   Scrolls["scroll_main"]     = HScroll( self:FindChild("scroll_main") )
   Scrolls["scroll_music"]    = HScroll( self:FindChild("scroll_music") )
   --Scrolls["scroll_dialogs"]  = HScroll( self:FindChild("scroll_dialogs") )
   Scrolls["scroll_fx"]       = HScroll( self:FindChild("scroll_fx") )
  
   SettingsMap["scroll_main"]    = "audio_volume_master"
   SettingsMap["scroll_music"]   = "audio_volume_music"
   --SettingsMap["scroll_dialogs"] = "audio_volume_dialog"
   SettingsMap["scroll_fx"]      = "audio_volume_sfx"
  
   CalculatePos(Scrolls["scroll_main"]   , SettingsMap["scroll_main"] )
   CalculatePos(Scrolls["scroll_music"]  , SettingsMap["scroll_music"] )
   --CalculatePos(Scrolls["scroll_dialogs"], SettingsMap["scroll_dialogs"] )
   CalculatePos(Scrolls["scroll_fx"]     , SettingsMap["scroll_fx"] )
end 
