function log(str, severity)
	sf.lua.GetLog().LogString(severity or 0, str)
	--print((severity or 0)..". "..str)
end

game = {}

game.app = sf.core.g_Application

game.load_window = function(_owner, _window, _id)

	if _owner==nil then log("owner is nil. "..T_(_id).c_str()) end

	_window.Load(	game.app.GetSettings().GetChild(_T("GUI"), false).GetChildByAttribute(_T("window"), _T("id"), _id, true),
					game.app.GetSettings().GetChild(_T("GUITemplates"), false))
						
	_window.UpdateLayout()
	
	_window.SetOffset(	(_owner.GetSize().X - _window.GetSize().X) / 2.0,
						(_owner.GetSize().Y - _window.GetSize().Y) / 2.0)

end

game.is_correct_time = function (_time)

	return 	(_time ~= nil and 
			_time.wYear ~= nil and _time.wYear >= 1601 and _time.wYear<=30827 and
			_time.wMonth ~= nil and _time.wMonth >= 1 and _time.wMonth<=12 and
			_time.wDay ~= nil and _time.wDay >= 1 and _time.wDay<=31 and
			_time.wHour ~= nil and _time.wHour >= 0 and _time.wHour<=23 and
			_time.wMinute ~= nil and _time.wMinute >= 0 and _time.wMinute<=59 and
			_time.wSecond ~= nil and _time.wSecond >= 0 and _time.wSecond<=59)
end 

game.get_lua_time = function (_time)
	
	return os.time{year=_time.wYear, month=_time.wMonth, day=_time.wDay, 
					hour=_time.wHour, min=_time.wMinute, sec=_time.wSecond}
end

s4.g_MainLua.LoadScript(_T("scripts\\bonuses.lua"))
s4.g_MainLua.LoadScript(_T("scripts\\game\\main.lua"))
s4.g_MainLua.LoadScript(_T("scripts\\le\\main.lua"))

function game.wait_action(_owner, _action, _child)
	if _action == _T("ended") then 
		_owner.EndModal(1) 
	end
end

function game.ok_action(_owner, _action, _child)
	if _action==_T("key_down_enter") then
		_owner.EndModal(1)
	elseif _child.GetId()==_T("ok") and _action==_T("pressed")then
		_owner.EndModal(1)
	end
end

function game.yes_no_action(_owner, _action, _child)
	if _action==_T("key_down_enter") then
		_owner.EndModal(1)
	elseif _action==_T("key_down_escape") then
		_owner.EndModal(0)
	elseif _child.GetId()==_T("yes") and _action==_T("pressed") then
		_owner.EndModal(1)
	elseif _child.GetId()==_T("no") and _action==_T("pressed") then
		_owner.EndModal(0)
	end
end

game.version = sf.misc.IntVector(-1, -1)

local version_info = game.app.GetSettings().GetChild(_T("version"), false)
if not version_info.GetId().empty() then
	game.version.X = tonumber(T_(version_info.GetValue(_T("x"))).c_str())
	game.version.Y = tonumber(T_(version_info.GetValue(_T("y"))).c_str())
end

local enable_game_gui  = game.app.GetSettings().GetChild(_T("settings"), false).GetValue(_T("enable_game_gui"))
g_ScreenResolution = {}
g_ScreenResolution.X = 1024
g_ScreenResolution.Y = 768

if enable_game_gui == _T("true") then
	
	log("s4 main menu")
	main_menu.show_main_menu(true, true)
else
	local test = game.app.GetSettings().GetChild(_T("GUI"), false).GetChildByAttribute(_T("window"), _T("id"), _T("test"), true)
	log('test=='..T_(test.GetId()).c_str())
	if test.GetId()~=_T("") then
		log("test window created")
		local window = sf.gui.CWindow()

		window.Load(
			test, 
			game.app.GetSettings().GetChild(_T("GUITemplates"), false))
		window.UpdateLayout()

		local window_ptr = sf.gui.CBaseWindowPtrT(window)
		game.app.SetMainWindow(window_ptr)
		
		window_ptr = nil
		
	else
		local window = s4.CGameWindow()

		if (window.InitGame) then 
			if s4.le.g_Campaigns.Instance().GetCampaign(0)~=nil then
				window.InitGame(s4.le.g_Campaigns.Instance().GetCampaign(0).GetId(), 0, 0, 0, 3) 
			else
				log('no campaigns loaded!')
			end
		else
			window.Load(
				game.app.GetSettings().GetChild(_T("GUI"), false).GetChild(_T("s4_game_window"), false), 
				game.app.GetSettings().GetChild(_T("GUITemplates"), false))
			window.UpdateLayout()
		end
		local window_ptr = sf.gui.CBaseWindowPtrT(window)
		game.app.SetMainWindow(window_ptr)
		window_ptr = nil
		
	end
end	

collectgarbage()
