local script = globalObject;

local interface;
local commandMap = {};

local delay = 0;

local strBack = "char";

local current = 1;

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 reversed = {};
reversed[1] = 1;
reversed[2] = 1;
reversed[3] = 1;
reversed[4] = 1;
reversed[5] = 1;
reversed[6] = 1;

reversed[7] = 0;
reversed[8] = 0;
reversed[9] = 0;
reversed[10] = 0;
reversed[11] = 0;
reversed[12] = 0;

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

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

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

laps[7] = 2;
laps[8] = 2;
laps[9] = 2;
laps[10] = 2;
laps[11] = 2;
laps[12] = 2;

laps[13] = 3;
laps[14] = 3;
laps[15] = 3;
laps[16] = 3;
laps[17] = 3;
laps[18] = 3;

laps[19] = 4;
laps[20] = 4;
laps[21] = 4;
laps[22] = 4;
laps[23] = 4;
laps[24] = 4;



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

local function UpdateLocks()
	SetWindowVisible(interface, "lock1_", false);
	SetWindowVisible(interface, "lock2_", false);
	SetWindowVisible(interface, "lock3_", false);
	SetText(interface, "lock1", "");
	SetText(interface, "lock2", "");
	SetText(interface, "lock3", "");
	
	local n1 = GetMedalsCount(1);
	local n2 = GetMedalsCount(2);
	local n3 = GetMedalsCount(3);
	if (n1 < 6 and GetNumRacesPassed() <= 6)
	then
		SetWindowVisible(interface, "lock1_", true);
		SetTextString(interface, "lock1", "lock1");
	end
	if (n2 < 12 and GetNumRacesPassed() <= 12)
	then
		SetWindowVisible(interface, "lock2_", true);
		SetTextString(interface, "lock2", "lock2");
	end
	if (n3 < 18 and GetNumRacesPassed() <= 18)
	then
		SetWindowVisible(interface, "lock3_", true);
		SetTextString(interface, "lock3", "lock3");
	end
end

local function UpdateButtons(cur, avail)
	for i = 1,24
	do
		SetWindowVisible(interface, "check"..i, false);
		SetWindowVisible(interface, i, false);
		SetWindowVisible(interface, "blank"..i, true);
		SetWindowVisible(interface, "gold"..i, false);
		SetWindowVisible(interface, "silver"..i, false);
		SetWindowVisible(interface, "bronze"..i, false);
		if (GetMedalType(i) == 3)
		then
			SetWindowVisible(interface, "gold"..i, true);
		elseif (GetMedalType(i) == 2)
		then
			SetWindowVisible(interface, "silver"..i, true);
		elseif (GetMedalType(i) == 1)
		then
			SetWindowVisible(interface, "bronze"..i, true);
		end	
	end
	for i = 1,avail
	do
		SetWindowVisible(interface, i, true);
		SetWindowVisible(interface, "blank"..i, false);
	end
	SetWindowVisible(interface, cur, false);
	SetWindowVisible(interface, "check"..cur, true);
	UpdateInterfaceBuffers(interface);
end

local function UpdateMissionInfo()
	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 function Button()
	local name = GetCurrentParams(script);
	if (name == "back")
	then
		LockButton(interface, "single");
		LockButton(interface, "champ");
		LockButton(interface, "back");
		SendCommandByName("mainmenu", "set_new_game", "false");
--		Update();
		SendCommandByName("main", "mainmenu", "2"); 	
	elseif (name == "race")
	 then
		 LockButton(interface, "single");
			LockButton(interface, "champ");
		 LockButton(interface, "back");
 SendCommandByName("main", "set_single", "true");
		 SendCommandByName("main", "game_mode", "");
		 SetNumRounds(laps[current]);
Update();
		SendCommandByName("main", "mission", "mission"..current); 
				SetCurrentRace(current);
				SetDeathRace(types[current]);
				SetReversed(reversed[current]);


	elseif(name == "champ")
	then
		LockButton(interface, "single");
		LockButton(interface, "champ");
		LockButton(interface, "back");
		SendCommandByName("main", "set_single", "false");
		SendCommandByName("main", "char", "day"); 
		--SendCommandByName("main", "road", "");
--~ 		if (GetNumRacesPassed() == 1)
--~ 		then
--~ 			SetLevelTransportCount(3);
--~ 			SetNumRounds(5);
--~ 		elseif (GetNumRacesPassed() == 2)
--~ 		then
--~ 			SetLevelTransportCount(3);
--~ 			SetNumRounds(5);
--~ 		elseif (GetNumRacesPassed() == 3)
--~ 		then
--~ 			SetLevelTransportCount(3);
--~ 			SetNumRounds(5);
--~ 		elseif (GetNumRacesPassed() == 4)
--~ 		then
--~ 			SetLevelTransportCount(3);
--~ 			SetNumRounds(3);
--~ 		elseif (GetNumRacesPassed() == 5)
--~ 		then
--~ 			SetLevelTransportCount(5);
--~ 			SetNumRounds(5);
--~ 		elseif (GetNumRacesPassed() == 6)
--~ 		then
--~ 			SetLevelTransportCount(3);
--~ 			SetNumRounds(5);
--~ 		end
	else
		current = tonumber(name);
		UpdateMissionInfo();
		UpdateButtons(current, GetNumRacesPassed());
	end
end

local flag = true;
local function MouseOver()
	local name = GetCurrentParams(script);
end

local function MouseOut()
end 

local function Window()
	local name = GetCurrentParams(script);
	UpdateInterfaceBuffers(interface);
end 

local function MenuUp()
	IMenuRight();
end

local function MenuDown()
	IMenuLeft();
end

local function MenuLeft()
	IMenuLeft();
end

local function MenuRight()
	IMenuRight();
end



local function AddObject()
	local obj = GetCurrentParams(script);
	interface = StringToPointer(obj);
	
	strBack = "mainmenu";
	
	if (current > GetNumRacesPassed())
	then
		current = GetNumRacesPassed();
	end
	SetWindowVisible(interface, "back_square", false);
	SetWindowVisible(interface, "single_square", false);
	SetWindowVisible(interface, "champ_square", false);
--	SetWindowVisible(interface, "square", true);
	SetWindowVisible(interface, "square", false); 
	
	UnlockButton(interface, "single");
	UnlockButton(interface, "champ");
	UnlockButton(interface, "back");
	
	IMenuRight();
	SetText(interface, "credits", "$"..string.format("%0.5d", GetMoney()));

	UpdateMissionInfo();
	UpdateLocks();
	UpdateButtons(current, GetNumRacesPassed());
end

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

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

commandMap["release_all"] 	 = ReleaseAll;
commandMap["add_object"] 	 = AddObject;
commandMap["delete_object"] = DeleteObject;
commandMap["mouse_button_over"] = MouseOver; 
commandMap["mouse_button_out"]  = MouseOut; 
commandMap["button"] 	= Button;
commandMap["window"] 	= Window;
commandMap["menu_up"] 	= MenuUp;
commandMap["menu_down"]	= MenuDown;
commandMap["menu_left"]	= MenuLeft;
commandMap["menu_right"]	= MenuRight;
commandMap["back"]		= function () strBack = GetCurrentParams(script); end

while(true)
do
	deltaTime = GetDeltaTime();
	if(flag)
	then 
		delay = delay + deltaTime; 
	end
--	if(delay >= 0.5)
--	then
--		SetWindowVisible(interface, "square", true); 
--		UpdateInterfaceBuffers(interface);
--	end
	ExecuteCommands();
	UpdateInterface(interface);
	Update();
end
