local script = globalObject;

local interface;
local commandMap = {};

local function ExecuteCommands()
	while(SetNextCommand(script))
	do
		local command = GetCurrentCommand(script);
		if (commandMap[command])
		then
			commandMap[command]();
		end
	end
end

local loading = true;
local locations = {};
locations[1] = 1;
locations[2] = 2;
locations[3] = 3;
locations[4] = 4;
locations[5] = 5;
locations[6] = 6;
locations[7] = 1;
locations[8] = 2;
locations[9] = 3;
locations[10] = 4;
locations[11] = 5;
locations[12] = 6;
locations[13] = 1;
locations[14] = 2;
locations[15] = 3;
locations[16] = 4;
locations[17] = 5;
locations[18] = 6;
locations[19] = 1;
locations[20] = 2;
locations[21] = 3;
locations[22] = 4;
locations[23] = 5;
locations[24] = 6;

local types = {};
types[1] = 1;
types[2] = 0;
types[3] = 1;
types[4] = 0;
types[5] = 1;
types[6] = 0;

types[7] = 0;
types[8] = 1;
types[9] = 0;
types[10] = 1;
types[11] = 0;
types[12] = 1;

types[13] = 0;
types[14] = 1;
types[15] = 0;
types[16] = 1;
types[17] = 0;
types[18] = 1;

types[19] = 1;
types[20] = 0;
types[21] = 1;
types[22] = 0;
types[23] = 1;
types[24] = 0;

local function UpdateMissionInfo()
	local current = GetCurrentRace();
	for i = 1,24
	do
			SetWindowVisible(interface, "_level"..i, false);
	end
	SetWindowVisible(interface, "_gold", false);
	SetWindowVisible(interface, "_silver", false);
	SetWindowVisible(interface, "_bronze", false);
	if (GetMedalType(current) == 3)
	then
		SetWindowVisible(interface, "_gold", true);
	elseif (GetMedalType(current) == 2)
	then
		SetWindowVisible(interface, "_silver", true);
	elseif (GetMedalType(current) == 1)
	then
		SetWindowVisible(interface, "_bronze", true);
	end	
	SetWindowVisible(interface, "_level"..current, true);
	SetText(interface, "race_number_txt", current.."/24");
	SetTextString(interface, "location_txt", "road"..locations[current]);
	SetTextString(interface, "race_type_txt", "race_type"..types[current]);
	
end

local ftime = 0;
local bshow = false;
local function AddObject()
	local obj = GetCurrentParams(script);
	interface = StringToPointer(obj);
	
	local num = GetCurrentMission();

	if (num == 1 and GetNumRacesPassed() == 1)
	then
		SetTextString(interface, "tiph", "tiph1");
		SetTextString(interface, "tip", "tip1");
	elseif (num == 2 and GetNumRacesPassed() == 2)
	then
		SetTextString(interface, "tiph", "tiph2");
		SetTextString(interface, "tip", "tip2");
	else
		local nnn = math.random(14);
		SetTextString(interface, "tiph", "tiph"..nnn);
		SetTextString(interface, "tip", "tip"..nnn);
	end
	
	UpdateMissionInfo();
	
	loading = true;
	SetWindowVisible(interface, "level1", true);
	SetWindowVisible(interface, "level2", false);
	
	UpdateInterfaceBuffers(interface);
	ftime = 0;
	bshow = false;
	
end

local function DeleteObject()
	Release(interface);
	StopScript(script);
	interface = nil;
end

local function ReleaseAll()
	Release(interface);
	interface = nil;
end

local function UpdateGameInterface1()
	UpdateGameInterface(interface);
end
local function PressAnyKey()
	loading = false;
	SetWindowVisible(interface, "level1", false);
	SetWindowVisible(interface, "level2", true);
	UpdateInterfaceBuffers(interface);
end

commandMap["release_all"] = ReleaseAll;
commandMap["add_object"] = AddObject;
commandMap["delete_object"] = DeleteObject;
commandMap["pressanykey"] = PressAnyKey;


while(true)
do
	if (not loading)
	then
		ftime = ftime + GetDeltaTime();
		if (ftime >= 0.5)
		then
			ftime = 0;
			SetWindowVisible(interface, "level2", bshow);
			bshow = not bshow;
		end
		UpdateInterfaceBuffers(interface);
	end
		
	ExecuteCommands();
	UpdateInterface(interface);

	Update();
end
