function OnSceneLoad(screen)        
	profiles = screen
    
    game_state = {}
    game_state.active_object = nil
    
    LoadData(screen)
    
    game_state.custom_cursors = {}
    game_state.locked_objects = {}
    --level:AddTaskSafe(CreateTask('', '', ''))
    --zone = CreateZoomZone('')
    game_state.locker = ReferenceCounter()
    game_state.active_zone = nil
    profiles.listitems = {}
    screen:SetLayer(100)
    
    local all_objects = CreateAutoRegisterEntity()
    
    screen:GetObject("BigWindow"):AttachTo(all_objects)    
	
	InitDialogWheels(screen)
	
	all_objects:SetZ(-200.0);
	
	game_state.center = {}
	game_state.center.x = 525
	game_state.center.y = 466	
	
	InitButtons(screen)	
	
	PostLoad(screen)
	
	screen:SetY(100)
	
	game_state.is_arrow_moving = false
	
	profiles.active_item_color = '0x55FAEAA0'
	profiles.inactive_item_color = '0x01FFFFFF'
	
	BuildList()
	FillList()
end

if (FillList ~= nil) and (profiles ~= nil) then
    FillList()
end

function OnButtonMouseEnter(title)
end

function OnButtonMouseLeave(title)
end

function OnScreenShown()
end

function CreatePlayerItem(index, y)
    local te = CreateTextEntity("Bookman18", "");-- player:GetName())
    te:AttachTo(profiles)
    te:SetX(530)
    te:SetY(y)
    te:SetZ(140)
    local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
    anim:SetHotSpot(4,4)
    anim:SetBlendMode(3)    
    
    local index = table.maxn(profiles.listitems) + 1
    profiles.listitems[index] = {}
    profiles.listitems[index].button = profiles:CreateObject(tostring(index),anim,0,0,-1,0,45,5)
    profiles.listitems[index].caption = te
    profiles.listitems[index].player = nil
    profiles.listitems[index].button:AttachTo(te)
end

function PostLoad(screen)
	game_state.te = CreateTextEntity("Calisto_MT_31", GetString("PROFILES"))
	game_state.te:AttachTo(screen)
	game_state.te:SetZ(140)
	game_state.te:SetX(530)
	game_state.te:SetY(110)
end

function BuildList()
	profiles.list_items = {}
    local profile_manager = GetProfileManager()
	for i = 0, 4 do
		CreatePlayerItem(i + 1, 160 + i * 40)
	end	
end

function FillList()
	local profile_manager = GetProfileManager() 
	for i, v in ipairs(profiles.listitems) do
        v.button:GetAnimation():SetColor(profiles.inactive_item_color)
		v.caption:Stop()
		v.caption:Hide()
		if not profiles:IsIgnoredOnTestPoint(v.button) then
            profiles:IgnoreOnTestPoint(v.button:GetName())
        end
    end	
	
    local player = profile_manager:GetFirstProfile()
    local i = 0
    while player ~= nil do
		local item = profiles.listitems[i + 1]		
        item.player = player
		item.caption:SetText(player:GetName())
		item.caption:Show()
		item.caption:Go()
		if profile_manager:GetCurrentProfile() ~= nil then		    
            if profile_manager:GetCurrentProfile():GetName() == player:GetName() then
                item.button:GetAnimation():SetColor(profiles.active_item_color)
				profiles.active_item = i + 1
            end
        else
            if profile_manager:GetProfileCount() ~= 0 then
                profile_manager:SetCurrentProfile(profile_manager:GetFirstProfile():GetName())
                item = profiles.listitems[1]
                item.button:GetAnimation():SetColor(profiles.active_item_color)
				profiles.active_item = 1
            end
		end		
        i = i + 1
		--[[if player:IsActive() then
			profile_manager:SetCurrentProfile(player:GetName())	    
		end]]
		if profiles:IsIgnoredOnTestPoint(item.button) then
            profiles:DoNotIgnoreOnTestPoint(item.button:GetName())            
        end
		
        player = profile_manager:GetNextProfile()
    end	
end

function GetWindow()
    return profiles:GetObject('BigWindow');
end

function InitButtons(screen)
    local up = screen:GetObject("newup"):GetAnimationPtr()
	screen:GetObject("newup"):SetVisible(false)
	local over = screen:GetObject("newover"):GetAnimationPtr()
	screen:GetObject("newover"):SetVisible(false)
	local down = screen:GetObject("newdown"):GetAnimationPtr()
	screen:GetObject("newdown"):SetVisible(false)
	game_state.new_button = CreateButton(up, over, down, "New", screen)
	game_state.new_button:AttachTo(screen)
	game_state.new_button:SetXY(487, 414)
	game_state.new_button:SetZ(211)
	
	local up = screen:GetObject("deleteup"):GetAnimationPtr()
	screen:GetObject("deleteup"):SetVisible(false)
	local over = screen:GetObject("deleteover"):GetAnimationPtr()
	screen:GetObject("deleteover"):SetVisible(false)
	local down = screen:GetObject("deletedown"):GetAnimationPtr()
	screen:GetObject("deletedown"):SetVisible(false)
	game_state.delete_button = CreateButton(up, over, down, "Delete", screen)
	game_state.delete_button:AttachTo(screen)
	game_state.delete_button:SetXY(527, 525)
	game_state.delete_button:SetZ(211)
	
	local up = screen:GetObject("okup"):GetAnimationPtr()
	screen:GetObject("okup"):SetVisible(false)
	local over = screen:GetObject("okover"):GetAnimationPtr()
	screen:GetObject("okover"):SetVisible(false)
	local down = screen:GetObject("okdown"):GetAnimationPtr()
	screen:GetObject("okdown"):SetVisible(false)
	game_state.ok_button = CreateButton(up, over, down, "Resume", screen)
	game_state.ok_button:AttachTo(screen)
	game_state.ok_button:SetXY(565, 414)
	game_state.ok_button:SetZ(211)
end


function OnUpdate(dt)
end

function UpdateSound()
end

function UpdateMusic()
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)    
    local obj_name = object:GetName()
    
    local lower = 49
    local upper = 49 + table.getn(profiles.listitems)
    local code = string.byte(obj_name)
    if code >= lower and code <= upper then
        if profiles.listitems[tonumber(obj_name)].player ~= nil then
            for i, v in ipairs(profiles.listitems) do
                if (v.player ~= nil) then --and (v.player:IsActive()) then
                    v.player:SetActive(false)
                    v.button:GetAnimation():SetColor(profiles.inactive_item_color)
                end
            end
            profiles.active_item = tonumber(obj_name)
			--profiles.listitems[tonumber(obj_name)].player:SetActive(true)
            profiles.listitems[tonumber(obj_name)].button:GetAnimationPtr():SetColor(profiles.active_item_color)
        end
        --game_state.listitems[obj_name]
    end
    
end

function OnObjectMouseUp(object)
    if profiles.destroying then
        return
    end
	game_state.active_object = nil
end

function OnToolFound(tool)
end

function OnToolApply(tool, object)
    ShowToolFailEffect()
end

function OnObjectReachPanel(object)
end

function OnButtonPress(title)
    if profiles.destroying or not profiles:IsFocused() then
        return
    end
    
    if game_state.is_arrow_moving then	
		return
	end
    
    local move_time = 0.1
    
    DefaultOnButtonPress(title)
    
    local arrow = profiles:GetObject('strelka')
	
	local af = CreateAnimationEffector()	
	af:AttachTo(arrow)
	af:SetMode(_ANIM_FWD + _ANIM_NOLOOP)
	af:SetKey(_TRACK_A, 0, arrow:GetAngle());
    
    local OnMoveFinish = function(title)
        if title == "Resume" then 
			GetProfileManager():SetCurrentProfile(profiles.listitems[profiles.active_item].player:GetName());		
            profiles:ScreenDestroy()
            profiles = nil
        end
        if title == "New" then
            load_screen('data/Lua/Screens/DialogPlayerName.lua');	
        end
        if title == "Delete" then
			local profile_manager = GetProfileManager()		
			--profile_manager:SetCurrentProfile(profiles.listitems[profiles.active_item].player:GetName());		
            local cur_profile = profile_manager:GetCurrentProfile()
            if profile_manager:GetProfileCount() == 0 then
                return
            end
            
			load_screen('data/Lua/Screens/DialogDelete.lua');
			dialog.profile_to_delete = profiles.listitems[profiles.active_item].player:GetName()
        end
    end
    
	if title == "Resume" then
		af:SetKey(_TRACK_A, move_time, 40 / 180 * M_PI);
	end
	if title == "New" then
	    af:SetKey(_TRACK_A, move_time, -40 / 180 * M_PI);		
	end
	if title == "Delete" then
	    af:SetKey(_TRACK_A, move_time, -180 / 180 * M_PI);
	end
	af:Play()
	TimerDo(profiles:GetTimer(), move_time, OnMoveFinish, title)
end

function OnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
end

function CanBeHinted(obj)    
end

-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function LoadData(screen)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('pixel',anim,592,327,137,0,5,5)
	local anim = CreateAnimation('data/Texture/GUI/Options/buttonbig.png',0,0,166,70,1,0)
	anim:SetHotSpot(83,35)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonbigup',anim,870,113,140,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/buttonbig.png',0,70,166,70,1,0)
	anim:SetHotSpot(83,35)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonbigover',anim,864,159,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/buttonbig.png',0,140,166,70,1,0)
	anim:SetHotSpot(83,35)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonbigdown',anim,858,221,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/yes.png',0,0,88,72,1,0)
	anim:SetHotSpot(44,36)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonleftup',anim,522,48,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/yes.png',0,72,88,72,1,0)
	anim:SetHotSpot(44,36)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonleftover',anim,530,88,101,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/yes.png',0,144,88,72,1,0)
	anim:SetHotSpot(44,36)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonleftdown',anim,529,139,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/no.png',0,0,86,68,1,0)
	anim:SetHotSpot(43,34)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonrightup',anim,866,51,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/no.png',0,68,86,68,1,0)
	anim:SetHotSpot(43,34)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonrightover',anim,860,109,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/no.png',0,136,86,68,1,0)
	anim:SetHotSpot(43,34)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonrightdown',anim,856,167,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/strelka.png',0,0,62,153,1,0)
	anim:SetHotSpot(26,116)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('strelka',anim,525,470,134,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Window/BigWindow.png',0,0,582,516,1,0)
	anim:SetHotSpot(291,258)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BigWindow',anim,525,291,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Logo/BFGSmallLogo.png',0,0,150,125,1,0)
	anim:SetHotSpot(75,62)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('DistrLogo',anim,910,104,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Loading/background.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('BG',anim,904,251,100,0,0.2,0.2)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/front.png',0,0,595,189,1,0)
	anim:SetHotSpot(297,94)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('front',anim,526,470,135,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/ok.png',0,0,81,65,1,0)
	anim:SetHotSpot(40,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('okup',anim,567,417,300,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/ok.png',0,65,81,65,1,0)
	anim:SetHotSpot(40,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('okover',anim,886,432,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/ok.png',0,130,81,65,1,0)
	anim:SetHotSpot(40,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('okdown',anim,884,501,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/new.png',0,0,84,65,1,0)
	anim:SetHotSpot(42,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('newup',anim,486,416,300,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/new.png',0,65,84,65,1,0)
	anim:SetHotSpot(42,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('newover',anim,82,442,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/new.png',0,130,84,65,1,0)
	anim:SetHotSpot(42,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('newdown',anim,84,493,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/delete.png',0,0,158,66,1,0)
	anim:SetHotSpot(79,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('deleteup',anim,526,529,300,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/delete.png',0,66,158,66,1,0)
	anim:SetHotSpot(79,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('deleteover',anim,541,660,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/delete.png',0,132,158,66,1,0)
	anim:SetHotSpot(79,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('deletedown',anim,542,711,100,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
