local script = globalObject;

local delay = 0; 
local delay2 = 0;
local menu_item = 1;

local menu_wnd = {};
menu_wnd[1] = "blend1";
menu_wnd[2] = "blend2";
menu_wnd[3] = "blend3";

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 function Button()
	local name = GetCurrentParams(script);
	if (name == "back")
	then
		LockButton(interface, "back"); 
		LockButton(interface, "select"); 
		SendCommandByName("main", "mainmenu", "");
		--SendCommandByName("main", "team", ""); 	
	elseif (name == "select")
	then 
		LockButton(interface, "select"); 
		LockButton(interface, "back"); 
--		SendCommandByName("main", "mission", "mission" .. GetNumRacesPassed());
		SendCommandByName("main", "load_choice", "");
	end
end


local flag = true; 

local function MouseOver()
	flag = false; 
	delay = 0; 
	local name = GetCurrentParams(script);
	SetWindowVisible(interface, name .. "_square", true); 
--	SetWindowVisible(interface, "square", false); 
	UpdateInterfaceBuffers(interface);
end

local function MouseOut()
	flag = true;
	delay = 0.3;
	SetWindowVisible(interface, "select_square", false);
	SetWindowVisible(interface, "back_square", false);
	UpdateInterfaceBuffers(interface);
end 

local cur_team; 

local function ClearAllStrings()
	SetTextString(interface, "simon", ""); 
	SetTextString(interface, "simon_red", ""); 
	SetTextString(interface, "ilya", ""); 
	SetTextString(interface, "ilya_red", ""); 
	SetTextString(interface, "lena", ""); 
	SetTextString(interface, "lena_red", ""); 
	SetTextString(interface, "alice", ""); 
	SetTextString(interface, "alice_red", ""); 
	SetTextString(interface, "denis", ""); 
	SetTextString(interface, "denis_red", ""); 
	SetTextString(interface, "kostya", ""); 
	SetTextString(interface, "kostya_red", ""); 
end

local flag1 = false; 
local flag2 = false; 
local flag3 = false; 
local flag4 = false; 
local flag5 = false; 
local flag6 = false; 

local function Locking(num)
	if (num == 0)
	then
		UnlockButton(interface, "select");
		SendCommandByName("main", "lock", "false");
		SendCommandByName("main", "lock2", "false");
		
	elseif (num == 1)
	then
		LockButton(interface, "select");
		SendCommandByName("main", "lock", "true");
		SendCommandByName("main", "lock2", "false");
	elseif (num == 2)
	then
		LockButton(interface, "select");
		SendCommandByName("main", "lock", "false");
		SendCommandByName("main", "lock2", "true");
	end
end

local function Window()
	local name = GetCurrentParams(script);
	if (cur_team == "night_dozor")
	then 
		if (name == "blend1")
		then
			ClearAllStrings();  
			SetPlayerName("zil_130_"); 
			SetTextString(interface, "simon_red", "simon"); 
			SetTextString(interface, "ilya", "ilya"); 
			SetTextString(interface, "lena", "lena"); 
			SetWindowVisible(interface, "blank3", false);
			SetWindowVisible(interface, "blank2", false); 
			SetWindowVisible(interface, "blank1", true); 
			if (not flag1)
			then 
				--SendCommandByName("main", "movie_team", "simon_movie");
				SendCommandByName("main", "char_picture", "simon");
				--SetPlayerType: true - day watch, false - night watch
				SetPlayerType(false);
				flag1 = true; 
				flag2 = false; 
				flag3 = false; 
			end
			UpdateInterfaceBuffers(interface);
		elseif (name == "blend2")
		then 
			ClearAllStrings(); 
			SetPlayerName("uaz"); 
			SetTextString(interface, "ilya_red", "ilya"); 
			SetTextString(interface, "simon", "simon"); 
			SetTextString(interface, "lena", "lena"); 
			SetWindowVisible(interface, "blank1", false);
			SetWindowVisible(interface, "blank3", false);
			SetWindowVisible(interface, "blank2", true); 
			if (not flag2)
			then 
				--SendCommandByName("main", "movie_team", "ilya_movie");
				SendCommandByName("main", "char_picture", "ilya");
				SetPlayerType(false);
				flag1 = false; 
				flag2 = true; 
				flag3 = false; 
			end
			UpdateInterfaceBuffers(interface);
		elseif (name == "blend3")
		then 
			ClearAllStrings(); 
			SetPlayerName("gaz_3110_main"); 
			SetTextString(interface, "lena_red", "lena"); 
			SetTextString(interface, "simon", "simon"); 
			SetTextString(interface, "ilya", "ilya"); 
			SetWindowVisible(interface, "blank1", false);
			SetWindowVisible(interface, "blank2", false);
			SetWindowVisible(interface, "blank3", true); 
			if (not flag3)
			then 
				--SendCommandByName("main", "movie_team", "lena_movie");
				SendCommandByName("main", "char_picture", "lena");
				SetPlayerType(false);
				flag1 = false; 
				flag2 = false; 
				flag3 = true; 
			end
			UpdateInterfaceBuffers(interface);
		end
	elseif (cur_team == "day_dozor")
	then 
		if (name == "blend1")
		then
			Locking(0);
			ClearAllStrings(); 
			SetPlayerName("audi_tt"); 
			SetTextString(interface, "alice_red", "alice"); 
--			if (GetNumRacesPassed() > 2)
	--		then
				SetTextString(interface, "denis", "denis"); 
--			end
			
--			if (GetNumRacesPassed() > 4)
--			then
				SetTextString(interface, "kostya", "kostya"); 
--			end
			SetWindowVisible(interface, "blank3", false);
			SetWindowVisible(interface, "blank2", false); 
			SetWindowVisible(interface, "blank1", true); 
			if (not flag4) 
			then 
				--SendCommandByName("main", "movie_team", "alice_movie"); 
				SendCommandByName("main", "char_picture", "alice"); 
				SetPlayerType(true);
				flag4 = true; 
				flag5 = false; 
				flag6 = false; 
			end
			UpdateInterfaceBuffers(interface);
		elseif (name == "blend2")-- and GetNumRacesPassed() > 2)
		then 
		if (GetNumRacesPassed() > 2)
		then
			Locking(0);
		else
			Locking(1);
		end
				--SendCommandByName("main", "lock", "true");
	--			SendCommandByName("lock", "text", "alice"); 
			ClearAllStrings(); 
			SetPlayerName("bmw"); 
			SetTextString(interface, "alice", "alice"); 
--			if (GetNumRacesPassed() > 2)
--			then
				SetTextString(interface, "denis_red", "denis"); 
--			end
			
	--		if (GetNumRacesPassed() > 4)
	--		then
				SetTextString(interface, "kostya", "kostya"); 
--			end
			SetWindowVisible(interface, "blank1", false);
			SetWindowVisible(interface, "blank3", false);
			SetWindowVisible(interface, "blank2", true); 
			if (not flag5) 
			then 
				--SendCommandByName("main", "movie_team", "denis_movie");
				SendCommandByName("main", "char_picture", "denis");
				SetPlayerType(true);
				flag5 = true; 
				flag4 = false; 
				flag6 = false; 
			end
			UpdateInterfaceBuffers(interface);
		elseif (name == "blend3")--and GetNumRacesPassed() > 4)
		then 
		if (GetNumRacesPassed() > 4)
		then
			Locking(0);
		else
			Locking(2);
		end
				--SendCommandByName("main", "lock2", "true");
			ClearAllStrings(); 
			SetPlayerName("volswagen_toureg"); 
			SetTextString(interface, "kostya_red", "kostya");
			SetTextString(interface, "denis", "denis");
			SetTextString(interface, "alice", "alice");
			SetWindowVisible(interface, "blank1", false);
			SetWindowVisible(interface, "blank2", false);
			SetWindowVisible(interface, "blank3", true);
			if (not flag6) 
			then 
				--SendCommandByName("main", "movie_team", "kostya_movie"); 
				SendCommandByName("main", "char_picture", "kostya");
				SetPlayerType(true);
				flag6 = true; 
				flag5 = false; 
				flag4 = false; 
			end 
			UpdateInterfaceBuffers(interface);
		end
	end
end 

local function Dozor()
	local name = GetCurrentParams(script);
	if((name ~= "day_dozor") and (name ~= "night_dozor"))
	then
		name = "night_dozor";
	end
	
	cur_team = name; 
	if (name == "day_dozor")
	then 
		current_team = "day_dozor";  
		ClearAllStrings();
		SetPlayerName("audi_tt"); 
		SetPlayerType(true);
		SetWindowVisible(interface, "blank1", true);  
		SetWindowVisible(interface, "blank2", false); 
		SetWindowVisible(interface, "blank3", false); 
		SetTextString(interface, "alice_red", "alice");
--		if (GetNumRacesPassed() > 2)
--		then
			SetTextString(interface, "denis", "denis"); 
--		else
--			SetText(interface, "denis", ""); 
--		end
		
--		if (GetNumRacesPassed() > 4)
--		then
			SetTextString(interface, "kostya", "kostya"); 
--		else
--			SetText(interface, "kostya", ""); 
--		end
		
	elseif (name == "night_dozor")
	then
		current_team = "night_dozor"; 
		ClearAllStrings(); 
		SetPlayerName("zil_130_"); 
		SetPlayerType(false);
		SetWindowVisible(interface, "blank1", true);  
		SetWindowVisible(interface, "blank2", false); 
		SetWindowVisible(interface, "blank3", false); 
		SetTextString(interface, "simon_red", "simon"); 
		if (GetNumRacesPassed() > 2)
		then
			SetTextString(interface, "ilya", "ilya"); 
		else
			SetText(interface, "ilya", ""); 
		end
		
		if (GetNumRacesPassed() > 4)
		then
			SetTextString(interface, "lena", "lena"); 
		else
			SetText(interface, "lena", ""); 
		end
	end
	UpdateInterfaceBuffers(interface);
end

local function MenuUp()
	if (delay2 > 0.1)
	then
		delay2 = 0;
--~ 		local mmm = 1;
--~ 		if (GetNumRacesPassed() > 2)
--~ 		then
--~ 			mmm = 2;
--~ 		end
--~ 		if (GetNumRacesPassed() > 4)
--~ 		then
--~ 			mmm = 3;
--~ 		end
		menu_item = menu_item + 1;
		if(menu_item > 3) then
			menu_item = 1;
		end
		
		SendCommandByName("char", "window", "blend" .. menu_item);
	end
end


local function MenuDown()
	if (delay2 > 0.1)
	then
		delay2 = 0;
--~ 		local mmm = 1;
--~ 		if (GetNumRacesPassed() > 2)
--~ 		then
--~ 			mmm = 2;
--~ 		end
--~ 		if (GetNumRacesPassed() > 4)
--~ 		then
--~ 			mmm = 3;
--~ 		end
		menu_item = menu_item - 1;
		if(menu_item < 1) then
			menu_item = 3;
		end
		
		SendCommandByName("char", "window", "blend" .. menu_item);
	end
end

local function MenuLeft()
	IMenuLeft();
end

local function MenuRight()
	IMenuRight();
end

local function AddObject()
	local obj = GetCurrentParams(script);
	interface = StringToPointer(obj);
	
	ClearAllStrings();
	
	SetWindowVisible(interface, "blank1", true);  
	SetWindowVisible(interface, "blank2", false); 
	SetWindowVisible(interface, "blank3", false);  
	
	if (current_team == "night_dozor")
	then
		SetPlayerType(false);
		SetPlayerName("zil_130_"); 
		SetTextString(interface, "simon_red", "simon"); 
		SetTextString(interface, "ilya", "ilya"); 
		SetTextString(interface, "lena", "lena"); 
	elseif (current_team == "day_dozor")
	then 
		SetPlayerType(true);
		SetPlayerName("audi_tt"); 
		SetTextString(interface, "alice_red", "alice");
	--	if (GetNumRacesPassed() > 2)
	--	then
			SetTextString(interface, "denis", "denis");
--		end
--		if (GetNumRacesPassed() > 4)
--		then
			SetTextString(interface, "kostya", "kostya");
--		end
	end 
	
	SetWindowVisible(interface, "select_square", false); 
	SetWindowVisible(interface, "back_square", false);

	UnlockButton(interface, "back");
	UnlockButton(interface, "select");
	
	menu_item = 1;
	SetWindowVisible(interface, "square", false); 
				SendCommandByName("main", "char_picture", "alice");
			SetPlayerName("audi_tt"); 
flag1 = false; 
flag2 = false; 
flag3 = false; 
flag4 = false;
flag5 = false;
flag6 = false;	
	
	IMenuRight();
	UpdateInterfaceBuffers(interface);
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["dozor"] = Dozor; 
commandMap["window"] = Window;
commandMap["menu_up"] = MenuUp;
commandMap["menu_down"] = MenuDown;
commandMap["menu_left"] = MenuLeft;
commandMap["menu_right"] = MenuRight;

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