--credits menu
--------------------------------------------------------------------

function OnCreate()
    s.sys_menu_credits=1
end

function OnDestroy()
    s.sys_menu_credits=0
end


function OnHide()
    s.sys_menu_credits=0
    UI:DelWidget("menu_credits_layers")
    UI:DelWidget("menu_credits")
    UI:UnloadResource("")
    Widget("menu_top"):SetVisible(true)  
end
--------------------------------------------------------------------
--button
function btnBackMenu()
  Back()  
end
--------------------------------------------------------------------

local Grp = "images"
local Res = "Credits"

local DefaultTextColor = "#FFFFFF"
local TextV = 0.05
local TextH = 0

local FadeTime   = 5
local SlideTime  = 5
local TextPos    = 0
local Widgets    = {}
local Images     = { "credits_image_1", "credits_image_2", "credits_image_3", "credits_image_4", "credits_image_5",}
local Time       = 0
local Which      = true
local LastIndex = 1

local TextY = 0.8

local PositionsOffsets = {}

function OnShow()
  UI:AddWidget("credits_layers.layout")
  Widget("menu_credits_layers"):SetVisible(true)
  
  Widgets[1] = StaticImage(self:FindChild("pic_one"))
  Widgets[2] = StaticImage(self:FindChild("pic_two"))
  
  Time      = 0
  LastIndex = 1
  
  s.sys_menu_credits=1
  TextY = 0.8
  LoadText()
  Widget("menu_top"):SetVisible(false)  
end

local StopTime = 0
function Update(dt)

  Time = Time + dt
  if Time > FadeTime then 
    StopTime = StopTime + dt
    Time = Time- dt
    if StopTime > 5 then 
      StopTime = 0
      Time = 0     
      LastIndex = NextIndex(LastIndex)
      self:SetSkin(Images[LastIndex])
      Widgets[Which and 2 or 1]:SetSkin(Images[LastIndex])
      Which = not Which
    end
  end
  
  TextY = TextY - (TextV * dt)
  
  local lastText
  for i = 1,3 do
    txt = StaticText(self:FindChild( string.format("text_%d", i) ))
    if txt:GetCaption() == "" then break end    
    txt:SetRealPos(0, TextY + PositionsOffsets[i])    
    lastText = txt
  end
  
  Widgets[Which and 1 or 2]:SetAlpha( Time / FadeTime)
  Widgets[Which and 2 or 1]:SetAlpha( 1- (Time / FadeTime))
  
  if lastText:GetHeight() + lastText:GetY() < 0 then
    table.insert(_G.FunctionsToDoAfterFrame, Back )
  end -- last txt
end

function Back()
  if self:FindChild("text_1"):GetRealY() > 0.7 then return end
  Widget("MainMenu"):SetVisible(true)
  Widget("menu_top"):SetVisible(true)
  self:SetVisible(false)
  
  UI:DelWidget("menu_credits")
  
  UI:UnloadResource("credits_top_layer")
  UI:UnloadResource("credits_bottom_layer")
  for i = 1,6 do
    UI:UnloadResource("credits_image_"..tostring(i))
  end
  
end

function NextIndex(i)
  if i >= #Images then return 1 end
  return i + 1
end

function LoadText()
  local str = "credits"
  local i = 1
  local credits = ""
  local textNo = 1
  local current_text_lines = 0
  local lastTextPos = 0
  local line
  local width = self:FindChild("text_1"):GetWidth()
  str = "credits" .. string.format("%03d", i)
  while L10n:StringExists(str) do
    
    line = L10n:ExpandString(str):trim()
    f = string.sub(line, 1, 1) 
    if      f == ":" then      line = string.sub(line, 2) line = "#000000"..line..DefaultTextColor 
    elseif  f == "*" then      line = string.sub(line, 2) line = "#000000"..line.."\n"..DefaultTextColor 
    end
    credits = credits.."\n"..line    
    i = i + 1
    str = "credits" .. string.format("%03d", i)
    
    current_text_lines = current_text_lines + 1
    
    if current_text_lines > 70 then
      txt = StaticText(self:FindChild( string.format("text_%d", textNo) ))
      txt:SetText("")
      txt:SetRealPos(0, lastTextPos)
      txt:SetText(credits)
      txt:SetHeightToText()
      txt:SetSize(width, -1)
      current_text_lines = 0
      PositionsOffsets[textNo] = lastTextPos      
      lastTextPos = lastTextPos + (txt:GetHeight()/720)
      textNo = textNo + 1
      credits = ""
    end
  end
  txt = StaticText(self:FindChild( string.format("text_%d", textNo) ))
  txt:SetText("")
  txt:SetRealPos(0,lastTextPos)
  txt:SetText(credits)
  txt:SetHeightToText()  
  txt:SetSize(width, -1)
  PositionsOffsets[textNo] = lastTextPos
end

