require("httpTimeshift")
----------------------------------------------------------------------
local function escapeJSString(str)
 if str == nil then return nil end
 str = string.gsub(str,'\\','\\\\')
 str = string.gsub(str,'"','\\"')
 str = string.gsub(str,'\n','\\n')
 str = string.gsub(str,'\r','\\r')
 return str
end
----------------------------------------------------------------------
local function translateHtml(Object)

  local scr = 'm_Version = "' .. httpTimeshift.getVersion() .. '";'
  m_simpleTV.Dialog.ExecScript(Object,scr)
  
  m_simpleTV.Dialog.ExecScript(Object,'doTranslate();')
end
----------------------------------------------------------------------
local function baseInit(Object)
 
 local scr = 'setStartVals('
                       .. tostring(httpTimeshift.isEnabled())
                .. ',' .. tostring(httpTimeshift.isGuiEnabled())
                .. ',' .. httpTimeshift.startRecordMargin()
                .. ',' .. httpTimeshift.endRecordMargin()
                .. ',' .. tostring(httpTimeshift.isOldEnabled())
                .. ');'
 
 m_simpleTV.Dialog.ExecScript(Object,scr)
end
----------------------------------------------------------------------
function OnNavigateComplete(Object)
  translateHtml(Object)
  baseInit(Object) 
end
----------------------------------------------------------------------
function OnOk(Object)
 local t = m_simpleTV.Dialog.ExecScriptParam(Object,'getVals();')
 if type(t) == 'table' then
   
   local needRestart = false
   
   if t.Enabled~=httpTimeshift.isEnabled() then
      needRestart = true
      httpTimeshift.setEnabled(t.Enabled);
   end
   if t.EnabledGUI~=httpTimeshift.isGuiEnabled() then
      needRestart = true
      httpTimeshift.setGuiEnabled(t.EnabledGUI);
   end   
   if t.EnabledOld~=httpTimeshift.isOldEnabled() then
      needRestart = true
      httpTimeshift.setOldEnabled(t.EnabledOld);
   end   
   
   httpTimeshift.setStartRecordMargin(tonumber(t.StartMargin));
   httpTimeshift.setEndRecordMargin(tonumber(t.EndMargin));
   
   if needRestart then
     m_simpleTV.Config.Apply('NEED_RESTART_APP')
   end
 end   
end