
-- handles to main widgets
MainMenu      = nil
MenuTop       = nil
MenuSettings  = nil
LoadMenu      = nil
SaveMenu      = nil
MoviesMenu    = nil
Credits       = nil
SoundSettings = nil
GfxSettings   = nil
InventoryWidget = nil
HiddenObjectGUI = nil
HOCounterGUI    = nil
DiaryWidget     = nil
ProfileChooser  = nil
MessageBoxWnd   = nil
HintWnd         = nil
HintWndBox      = nil


function GetGUIControlName(name)
  if s.sys_makeda_gui == 1 then return name.."_m" end
  return name
end

function SetInterfaceCaption( caption )
  if caption == nil then caption = "" end
  states["sys_interface_caption"] = caption
end

function GetFontScale(screenW, screenH, refH)
    --print("screenW", screenW, " screenH",screenH, " refH" ,refH)
    return 1.1 * screenH / refH
end

function OpenGfxSettings()
  if GfxSettings == nil then
    UI:AddWidget("opt_gfx.layout")
  end
  GfxSettings = Widget("opt_gfx")
  GfxSettings:SetVisible(true)  
end 
    
function OpenSoundSettings()
  if SoundSettings == nil then
   UI:AddWidget("opt_snd.layout")
  end
  SoundSettings = Widget("opt_snd")
  SoundSettings:SetVisible(true)
end

function OpenInventory()
  if InventoryWidget == nil then
    UI:AddWidget("inventory.layout")
  end
  InventoryWidget = Widget("Inventory")
  if not MystInventory:GetActive() then
    MystInventory:Create("ch01", "Widgets.Inventory")
  end
  InventoryWidget:SetVisible(true)
  return InventoryWidget
end

function OpenTopMenu()
  if MenuTop == nil then
    UI:AddWidget("menu_top.layout")
  end
  MenuTop = Widget("menu_top")
  MenuTop:SetVisible(true)
end

function OpenMainMenu()
  local canOpen = true
  canOpen = canOpen and (s.sys_dialog_active == 0)
  canOpen = canOpen and (Cursors:GetAttachmentName() == "")
  
  if not canOpen  then
    return
  end
  
  if s.sys_block_esc == 1 then 
	return 
  end 
  
  if s.sys_game_start == 1 and s.sys_game_finished == 0 then
    SaveMgr:SaveSlot(0)
  end
  
--[[
  if MainMenu == nil then
    UI:AddWidget("menu.layout")
  end
  MainMenu = Widget("MainMenu")
  OpenTopMenu() 
  MainMenu:SetVisible(true)  
  return MainMenu
  ]]
--[[
  OpenTopMenu()
  OpenInventory()
  local Viewport = MystViewportManager:GetInstance():CreateViewport("Menu")
  Viewport:ChangeCamera("s00_0000_menu")  
]]
  OpenTopMenu()
  OpenInventory()
  Widgets.Inventory.Lock(false)
  GoTo("s00_0000_menu")
end

function OpenSettingsMenu()
  UI:AddWidget("opt_game.layout")
  MenuSettings = Widget("opt_snd")
  MenuSettings:SetVisible(true)
  return MenuSettings
end

function OpenCredits()
  --if Credits == nil then
    --UI:LoadResource("credits_images.xml")
    UI:LoadResource("credits.xml")
    UI:AddWidget("credits.layout")
  --end
  Credits = Widget("menu_credits")
  Credits:SetVisible(true)
  return Credits
end

 function OpenHiddenObjectGUI()
   if not UI:WidgetExists("ObjectsList") then
     UI:AddWidget("objects_list.layout")
   end
   HiddenObjectGUI = Widget("ObjectsList")
   HiddenObjectGUI:SetVisible(true)
   return HiddenObjectGUI
 end

function OpenDiary()
  if s.sys_block_esc == 1 or s.sys_block_only_esc == 1 then     
	if s.sys_diary_open_in_ho == 0 then
		ShowGameHint("game_hint_28") 
		s.sys_diary_open_in_ho = 1
	else
		Talk("abt_uni_100_hero")
	end
		fake_diary = public()
		function fake_diary:SetVisible(a) end
    return fake_diary()
  end
  if DiaryWidget == nil then
    UI:AddWidget("notes.layout")
  end
  
  DiaryWidget = Widget("notes_window")
  --DiaryWidget:SetVisible(true)
  return DiaryWidget
end


function OpenProfileChooser()
  UI:AddWidget("profile_chooser.layout")
  ProfileChooser = Widget("profile_chooser")
  ProfileChooser:SetVisible(true)
  return ProfileChooser
end


function MessageBox(msg, fnOnOk, fnOnCancel, Buttons)
  
  if MessageBoxWnd == nil then
    UI:AddWidget("message_box.layout")
  end
  MessageBoxWnd = Widget("message_box")  
  Widgets.message_box.SetText(msg)
  Widgets.message_box.OnOKCallback =  fnOnOk
  Widgets.message_box.OnCancelCallback = fnOnCancel
  if Buttons ~= nil then
    Widgets.message_box.SetButtons( Buttons[1], Buttons[2], Buttons[3] )
  else
    Widgets.message_box.SetButtons( true, true, false )
  end
  MessageBoxWnd:SetVisible(true, true)
end

function OpenHOCounterGUI()
  if HOCounterGUI == nil then
    UI:AddWidget("hidden_object_counter_gui.layout")
  end
  HOCounterGUI = Widget("ho_counter_list")
  HOCounterGUI:SetVisible(true)
  return HOCounterGUI  
end

function OpenHintWindow()
  if HintWnd == nil then
    UI:AddWidget("hint.layout")
  end
  HintWnd = Widget("hint_window")
  HintWnd:SetVisible(true)
  return HintWnd
end

function OpenHintWindowBox(msg)
  if HintWndBox == nil then
    UI:AddWidget("hint_window_box.layout")
  end
  HintWndBox = Widget("hint_window_box")
  HintWndBox:SetVisible(true)
  Widgets.hint_window_box.SetText(msg)    
  return HintWndBox
end
