LoadFile("DATA:GUI/Dialogs_Screen.lua")

-----------------------------------------------------

local PuzzleMapBG = CreateSprite("PuzzleMapBG", [[DATA:background\background\forest2.png]])

local buttonOG = 1.1
local BackButton = CreatePushButton("BackButton", [[GUI\generic_dialogs\back.anim]], { 70 - SCREEN_WIDTH / 2, 50 - SCREEN_HEIGHT / 2 }, buttonOG)

local buttonWidth = 450
local buttonHeight = 750

function SetupWorldButton(name, button, collectible)

	local children = {}
	
	if collectible then
	
		local collectibleText = CreateStaticText(name.."CollectibleText", "", { 0, 185 }, "button", "centre centy")
		collectibleText.width = 170
		collectibleText.height = 40
		collectibleText.text_ext.colour = WHITE
		collectibleText.text_ext.hover_colour = WHITE
		collectibleText.enabled = false
		table.insert(children, collectibleText)
		
		local collectibleImage = CreateGroup(name.."CollectibleImage",
		{
			CreateImage(name.."_"..collectible, [[collectiblegui\]]..collectible..[[_filled.anim]], { -85, 0 }),
			CreateImage(name.."_"..collectible.."Tray", [[gui\world_map\collectible_count_tray.anim]]),
		}, 1, 1, { 0, 185 })
		collectibleImage.enabled = false
		table.insert(children, collectibleImage)
		
	end
	
	local worldButton = CreatePushButton(name.."Button", button)
	worldButton.button_ext.activate_on_up = LIST_BOX_ACTIVATE_ON_UP
	worldButton.button_ext.click_up_time = 0
	worldButton.button_ext.click_down_time = 0
	worldButton.sound_ext.enter = nil
	worldButton.text_ext =
	{
		font = "header",
		style = "centre centy",
		colour = WHITE,
		hover_colour = WHITE,
		additional_scale = 0.7,
		offset = { 0, -185 },
	}
	table.insert(children, worldButton)

	local group = CreateGroup(name.."Group", children, buttonWidth, buttonHeight, { 0, 0 })
	group.hit_top_left = { -buttonWidth / 2, -buttonHeight / 2 }
	group.hit_bottom_right = { buttonWidth / 2, buttonHeight / 2 }
	
	return group
	
end

--Scroller
local listboxPadding = 26
local listboxX = listboxPadding - SCREEN_WIDTH / 2
local listboxY = 0
local listboxWidth = SCREEN_WIDTH - listboxPadding * 2
local listboxHeight = buttonHeight
local scrollerWidth = 400
local scrollerHeight = 20
local scrollerX = 0
local scrollerY = listboxHeight / 2 - 60

--Scrollbar button position and range.
local ScrollerButton = CreateScrollerButton("ScrollerButton", [[GUI\buttons\scrollbar_dot.anim]])
local WorldScrollbar = CreateScrollBar("WorldScrollbar", nil, nil, ScrollerButton, { scrollerX - listboxX - scrollerWidth / 2, scrollerY - listboxY })
WorldScrollbar.width = scrollerWidth
WorldScrollbar.height = scrollerHeight
WorldScrollbar.scrollbar_ext.start_point = { 0, 0 }
WorldScrollbar.scrollbar_ext.end_point = { 1, 0 }
WorldScrollbar.scrollbar_ext.is_modal = false
WorldScrollbar.scrollbar_ext.scroller_offset = { 0, 0 }

--List Box of items with scrollbar button attached
local WorldListBox = CreateListBox("WorldListBox", listboxWidth, listboxHeight, WorldScrollbar, { listboxX, listboxY })
WorldListBox.listbox_ext.horizontal = true
WorldListBox.listbox_ext.page_align_always = false
table.insert(WorldListBox.parent_ext.children, SetupWorldButton("World1", [[GUI\world_map\world_forest.anim]], "lollipop"))
table.insert(WorldListBox.parent_ext.children, SetupWorldButton("World2", [[GUI\world_map\world_kaboom.anim]], "sundae"))
table.insert(WorldListBox.parent_ext.children, SetupWorldButton("World3", [[GUI\world_map\world_caves.anim]], "jello"))
table.insert(WorldListBox.parent_ext.children, SetupWorldButton("World4", [[GUI\world_map\world_peaks.anim]], "pie"))
table.insert(WorldListBox.parent_ext.children, SetupWorldButton("Bonus", [[GUI\world_map\world_bonus.anim]]))

--Expand list box hit region to full screen for capturing drag and mouse wheel
WorldListBox.hit_top_left = { -SCREEN_WIDTH / 2 - listboxX, -SCREEN_HEIGHT / 2 - listboxY }
WorldListBox.hit_bottom_right = { SCREEN_WIDTH / 2 - listboxX, SCREEN_HEIGHT / 2 - listboxY }

--Platform dependent overrides
if GetVersion() == "IOS" then
	WorldScrollbar.scrollbar_ext.pip_anim = [[GUI\buttons\scrollbar_notch.anim]]
	WorldScrollbar.scrollbar_ext.pip_spacing = 40
	WorldScrollbar.scrollbar_ext.force_hidden = true
end

PuzzleMapDialog = CreateModalDialog("PuzzleMapDialog",
{
	BackButton,
	WorldListBox,
	PuzzleMapBG,
},
nil, { SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 })
PuzzleMapDialog.width = SCREEN_WIDTH
PuzzleMapDialog.height = SCREEN_HEIGHT

if GetVersion() ~= "IOS" then
	local ScrollbarImage = CreateImage("ScrollbarImage", [[GUI\buttons\scrollbar_800.anim]], { scrollerX, scrollerY })
	table.insert(PuzzleMapDialog.parent_ext.children, 3, ScrollbarImage)
end

--------------------------------------------------------
-- Level Map Dialog
---------------------------------------------------------

--Global Constants
LevelMapItemsPerRow = 3
LevelMapItemWidth = 230 --includes spacing in between items
LevelMapItemHeight = 177 --includes spacing in between items
LevelMapHubTitle =
{
	name = "HubTitleGroup",
	width = SCREEN_WIDTH,
	height = LevelMapItemHeight,
	parent_ext =
	{
		--wrapping inside parent extension so that we can set an offset position
		--we can't set the base widget position because it is added to a list box which controls its position
		children =
		{
			{
				name = "HubTitle",
				position = { 0, 6 },
				text_ext =
				{
					name = "HubTitleText",
					font = "header",
					style = "centre centy",
					colour = WHITE,
					hover_colour = WHITE,
					offset = { 0, 10 },
				},
				anim_ext =
				{
					animation = [[GUI\fe_dialogs\generic_banner.anim]],
					do_not_animate = true,
				},
			},
		},
	},
}

--Components for defining level map item templates
function SetUITooltip(widget, txt, offset)
	local tooltip = CreateStaticText(widget.name.."Tooltip", txt, { 0, 0 }, "unicode", "centre centy")
	tooltip.text_ext.colour = WHITE
	tooltip.text_ext.additional_scale = 0.8
	SetTooltip(widget, tooltip, offset, 0.5)
end
local buttonOG = 1.07
local buttonScale = 1.075
local LevelName =
{
	name = "Name",
	width = LevelMapItemWidth,
	height = LevelMapItemHeight,
	position = { -20, 49.5 },
	text_ext =
	{
		--placeholder for loading level name
		font = "unicode",
		style = "centre centy dropshadow",
		colour = WHITE,
		hover_colour = WHITE,
		dropshadow_colour = { r = 65, g = 21, b = 17, a = 220 },
		dropshadow_distance = 1,
	},
	anim_ext =
	{
		animation = [[GUI\level_select\label_back.anim]],
		do_not_animate = true,
		additional_scale = buttonScale,
	},
}
local LevelNameButton = inherit(LevelName)
{
	button_ext =
	{
		over_growth = buttonOG,
		under_growth = 0.95,
		activate_on_up = LIST_BOX_ACTIVATE_ON_UP,
	},
	sound_ext =
	{
		enter = { patch = "eets2/GUI/mouseOver" },
		click = { patch = "eets2/GUI/click" },
	},
}
SetUITooltip(LevelNameButton, "$00318", { 90, -38 })
local LevelNumber =
{
	name = "Number",
	width = LevelMapItemWidth,
	height = LevelMapItemHeight,
	position = { 0, 29.5 },
	enabled = false,
	text_ext =
	{
		font = "large_digit",
		style = "centre centy",
		colour = WHITE,
		hover_colour = WHITE,
	},
}
local LevelNumberBonus = inherit(LevelNumber) {}
LevelNumberBonus.position[1] = -25
LevelNumberBonus.text_ext.style = "centy"
local LevelCollectibles =
{
	name = "Collectibles",
	position = { 0, -45.5 },
	width = 1,
	height = 1,
	enabled = false,
	parent_ext =
	{
		children =
		{
			{
				name = "1",
				position = { 0, -5 },
				width = 1,
				height = 1,
				enabled = false,
				anim_ext =
				{
					auto_hitregion = false,
					do_not_animate = true,
				},
			},
			{
				name = "0",
				position = { -45, 0 },
				width = 1,
				height = 1,
				enabled = false,
				anim_ext =
				{
					auto_hitregion = false,
					do_not_animate = true,
					additional_scale = 0.9,
					facing = { 1, -0.5 },
				},
			},
			{
				name = "2",
				position = { 45, 0 },
				width = 1,
				height = 1,
				enabled = false,
				anim_ext =
				{
					auto_hitregion = false,
					do_not_animate = true,
					additional_scale = 0.9,
					facing = { 1, 0.5 },
				},
			},
		},
	},
}
local LevelCollectiblesTray = inherit(LevelCollectibles)
{
	name = "CollectiblesTray",
}
local LevelGoalFilled = CreateImage("Goal", [[collectiblegui\goal_filled.anim]], { -60, 34.5 })
LevelGoalFilled.enabled = false
LevelGoalFilled.anim_ext.do_not_animate = true
LevelGoalFilled.anim_ext.additional_scale = 0.8
local LevelGoalEmpty = inherit(LevelGoalFilled) {}
LevelGoalEmpty.anim_ext.animation = [[collectiblegui\goal_empty.anim]]
local LevelCollectiblesCustomSpread = 32
local LevelCollectiblesCustom =
{
	name = "Collectibles",
	position = { LevelName.position[1] - 1.5 * LevelCollectiblesCustomSpread, 11.5 },
	width = 1,
	height = 1,
	enabled = false,
	parent_ext =
	{
		children =
		{
			{
				name = "0",
				position = { 0, 0 },
				width = 1,
				height = 1,
				enabled = false,
				anim_ext =
				{
					auto_hitregion = false,
					do_not_animate = true,
					additional_scale = 0.5,
				},
			},
			{
				name = "1",
				position = { LevelCollectiblesCustomSpread, 0 },
				width = 1,
				height = 1,
				enabled = false,
				anim_ext =
				{
					auto_hitregion = false,
					do_not_animate = true,
					additional_scale = 0.5,
				},
			},
			{
				name = "2",
				position = { LevelCollectiblesCustomSpread * 2, 0 },
				width = 1,
				height = 1,
				enabled = false,
				anim_ext =
				{
					auto_hitregion = false,
					do_not_animate = true,
					additional_scale = 0.5,
				},
			},
		},
	},
}
local LevelCollectiblesTrayCustom = inherit(LevelCollectiblesCustom)
{
	name = "CollectiblesTray",
}
local LevelBadge = CreateBadge("Badge", [[GUI\buttons\badge.anim]], { -93, -67.5 })
LevelBadge.text_ext.text = "!"
local LevelThumbnailCompleted =
{
	name = "Thumbnail",
	width = LevelMapItemWidth,
	height = LevelMapItemHeight,
	enabled = false,
	sprite_ext =
	{
		--placeholder for loading thumbnail
		background_colour = WHITE,
		alpha = 0.8,
	},
}
local LevelThumbnailIncomplete = inherit(LevelThumbnailCompleted) {}
LevelThumbnailIncomplete.sprite_ext.background_colour = BLACK
LevelThumbnailIncomplete.sprite_ext.alpha = 0.7
local LevelThumbnailNew = inherit(LevelThumbnailIncomplete) {}
LevelThumbnailNew.sprite_ext.alpha = 0.4
local buttonSpread = 43
local buttonX = 90
local buttonY = -64.5
local buttonOG = 1.1
local buttonScale = 1.1
function CreateLevelButton(name, anim, pos)
	local button = CreatePushButton(name, anim, pos, buttonOG)
	button.button_ext.additional_scale = buttonScale
	button.button_ext.activate_on_up = LIST_BOX_ACTIVATE_ON_UP
	return button
end
function AddLevelButton(name, anim)
	local button = CreateLevelButton(name, anim, { buttonX, buttonY })
	buttonX = buttonX - buttonSpread
	return button
end
local LevelDeleteButton = AddLevelButton("DeleteButton", [[GUI\level_select\btn_delete.anim]])
SetUITooltip(LevelDeleteButton, "$00319", { 0, -42 })
local LevelFriendsButton
if GetVersion() == "IOS" then
	LevelFriendsButton = AddLevelButton("FriendsButton", [[GUI\level_select\ios\btn_friends.anim]])
else
	LevelFriendsButton = AddLevelButton("FriendsButton", [[GUI\level_select\steam\btn_workshop.anim]])
	SetUITooltip(LevelFriendsButton, "$00320", { 0, -42 })
end
local NewLevelButton = CreateLevelButton("NewButton", [[GUI\level_select\new_level.anim]])
NewLevelButton.width = 100
NewLevelButton.height = 100
NewLevelButton.hit_bottom_right = { NewLevelButton.width * 0.5, NewLevelButton.height * 0.5 }
NewLevelButton.hit_top_left = { -NewLevelButton.hit_bottom_right[1], -NewLevelButton.hit_bottom_right[2] }
NewLevelButton.anim_ext.auto_hitregion = false
SetUITooltip(NewLevelButton, "$00321", { 0, -84 })

--Level map item templates
LevelMapItemLocked =
{
	name = "LevelMapItemLocked", --will be renamed to level name by game code
	anim_ext =
	{
		--locked level bg image always drawn on the bottom
		animation = [[GUI\level_select\level_frame_locked.anim]],
		do_not_animate = true,
	},
	button_ext =
	{
		over_growth = 1,
		under_growth = 1,
		activate_on_up = LIST_BOX_ACTIVATE_ON_UP,
	},
	sound_ext =
	{
		click = { patch = "eets2/GUI/click" },
	},
}
LevelMapItemCompleted = inherit(LevelMapItemLocked)
{
	name = "LevelMapItemCompleted",
	parent_ext =
	{
		children =
		{
			LevelNumber,
			LevelCollectibles,
			LevelCollectiblesTray,
			LevelThumbnailCompleted,
		},
	},
}
LevelMapItemIncomplete = inherit(LevelMapItemLocked)
{
	name = "LevelMapItemIncomplete",
	parent_ext =
	{
		children =
		{
			LevelNumber,
			LevelThumbnailIncomplete,
		},
	},
}
LevelMapBonusItemCompleted = inherit(LevelMapItemLocked)
{
	name = "LevelMapBonusItemCompleted",
	parent_ext =
	{
		children =
		{
			LevelNumberBonus,
			LevelGoalFilled,
			LevelThumbnailCompleted,
		},
	},
}
LevelMapBonusItemIncomplete = inherit(LevelMapItemLocked)
{
	name = "LevelMapBonusItemIncomplete",
	parent_ext =
	{
		children =
		{
			LevelNumberBonus,
			LevelGoalEmpty,
			LevelThumbnailIncomplete,
		},
	},
}
LevelMapCustomItem = inherit(LevelMapItemLocked)
{
	name = "LevelMapCustomItem",
	parent_ext =
	{
		children =
		{
			LevelDeleteButton,
			LevelFriendsButton,
			LevelNameButton,
			LevelCollectiblesCustom,
			LevelCollectiblesTrayCustom,
			LevelThumbnailCompleted,
		},
	},
}
--swap edit level bg and add tooltip for custom levels
LevelMapCustomItem.anim_ext.animation = [[GUI\level_select\level_frame_edit.anim]]
SetUITooltip(LevelMapCustomItem, "$00322", { -84, -68 })
LevelMapCustomItemNew = inherit(LevelMapItemLocked)
{
	name = "LevelMapCustomItemNew",
	parent_ext =
	{
		children =
		{
			LevelBadge,
			LevelDeleteButton,
			LevelFriendsButton,
			LevelNameButton,
			LevelThumbnailNew,
		},
	},
}
--swap edit level bg and add tooltip for custom levels
LevelMapCustomItemNew.anim_ext.animation = [[GUI\level_select\level_frame_edit.anim]]
SetUITooltip(LevelMapCustomItemNew, "$00322", { -56, -83 })
LevelMapNewItem =
{
	name = "LevelMapNewItem",
	anim_ext =
	{
		animation = [[GUI\level_select\level_frame_new.anim]],
		do_not_animate = true,
	},
	parent_ext =
	{
		children =
		{
			NewLevelButton,
		},
	},
}
if GetVersion() == "IOS" then
	local LevelCreator =
	{
		name = "Creator",
		width = LevelMapItemWidth,
		height = LevelMapItemHeight,
		position = { 0, -63 },
		text_ext =
		{
			--placeholder for loading player name
			font = "unicode",
			style = "centy",
			colour = WHITE,
			hover_colour = WHITE,
			offset = { -73, 0 },
			additional_scale = 0.7,
		},
		anim_ext =
		{
			animation = [[GUI\level_select\ios\friend_bar.anim]],
			do_not_animate = true,
		},
	}
	LevelMapGameCenterItem = inherit(LevelMapItemLocked)
	{
		name = "LevelMapGameCenterItem",
		parent_ext =
		{
			children =
			{
				LevelDeleteButton,
				LevelName,
				LevelCollectiblesCustom,
				LevelCollectiblesTrayCustom,
				LevelCreator,
				LevelThumbnailCompleted,
			},
		},
	}
	LevelMapGameCenterItemNew = inherit(LevelMapItemLocked)
	{
		name = "LevelMapGameCenterItemNew",
		parent_ext =
		{
			children =
			{
				LevelBadge,
				LevelDeleteButton,
				LevelName,
				LevelCreator,
				LevelThumbnailNew,
			},
		},
	}
else
	local WorkshopButton = CreateLevelButton("WorkshopButton", [[GUI\level_select\steam\workshop_link.anim]])
	WorkshopButton.width = 100
	WorkshopButton.height = 100
	WorkshopButton.hit_bottom_right = { WorkshopButton.width * 0.5, WorkshopButton.height * 0.5 }
	WorkshopButton.hit_top_left = { -WorkshopButton.hit_bottom_right[1], -WorkshopButton.hit_bottom_right[2] }
	WorkshopButton.anim_ext.auto_hitregion = false
	SetUITooltip(WorkshopButton, "$00369", { 0, -84 })
	
	LevelMapWorkshopLink =
	{
		name = "LevelMapWorkshopLink",
		anim_ext =
		{
			animation = [[GUI\level_select\level_frame_new.anim]],
			do_not_animate = true,
		},
		parent_ext =
		{
			children =
			{
				WorkshopButton,
			},
		},
	}
	LevelMapWorkshopItem = inherit(LevelMapItemLocked)
	{
		name = "LevelMapWorkshopItem",
		parent_ext =
		{
			children =
			{
				LevelName,
				LevelCollectiblesCustom,
				LevelCollectiblesTrayCustom,
				LevelThumbnailCompleted,
			},
		},
	}
	LevelMapWorkshopItemNew = inherit(LevelMapItemLocked)
	{
		name = "LevelMapWorkshopItemNew",
		parent_ext =
		{
			children =
			{
				LevelBadge,
				LevelName,
				LevelThumbnailNew,
			},
		},
	}
end

--Dialog
local LevelMapPanel = CreateImage("LevelMapPanel", [[GUI\fe_dialogs\generic_panel_tall.anim]], { 0, 0 })

--Scroller
local listboxWidth = SCREEN_WIDTH
local listboxHeight = SCREEN_HEIGHT
local listboxX = 0
local listboxY = -SCREEN_HEIGHT / 2
local scrollerWidth = 20
local scrollerHeight = 618
local scrollerX = SCREEN_WIDTH / 2 - 60
local scrollerY = 0

--Scrollbar button position and range.
local ScrollerButton = CreateScrollerButton("ScrollerButton", [[GUI\buttons\scrollbar_dot.anim]])
ScrollerButton.anim_ext.facing = { 0, 1 }
local LevelScrollbar = CreateScrollBar("LevelScrollbar", nil, nil, ScrollerButton, { scrollerX - listboxX, scrollerY - listboxY - scrollerHeight / 2 })
LevelScrollbar.width = scrollerWidth
LevelScrollbar.height = scrollerHeight
LevelScrollbar.scrollbar_ext.start_point = { 0, 0 }
LevelScrollbar.scrollbar_ext.end_point = { 0, 1 }
LevelScrollbar.scrollbar_ext.is_modal = false
LevelScrollbar.scrollbar_ext.scroller_offset = { 0, 0 }

--List Box of items with scrollbar button attached
local LevelListBox = CreateListBox("LevelListBox", listboxWidth, listboxHeight, LevelScrollbar, { listboxX, listboxY })
LevelListBox.listbox_ext.horizontal = false
LevelListBox.listbox_ext.page_align_always = false
LevelListBox.listbox_ext.list_end_padding = 14

--Expand list box hit region to full screen for capturing drag and mouse wheel
LevelListBox.hit_top_left = { -SCREEN_WIDTH / 2 - listboxX, -SCREEN_HEIGHT / 2 - listboxY }
LevelListBox.hit_bottom_right = { SCREEN_WIDTH / 2 - listboxX, SCREEN_HEIGHT / 2 - listboxY }

--Platform dependent overrides
if GetVersion() == "IOS" then
	LevelScrollbar.scrollbar_ext.force_hidden = true
end

local LevelMapDialog = CreateModalDialog("LevelMapDialog", 
{ 
	inherit(BackButton){ name = "LevelMap"..BackButton.name },
	LevelListBox,
	LevelMapPanel,
	PuzzleMapBG,
},
nil, { SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 })
LevelMapDialog.width = SCREEN_WIDTH
LevelMapDialog.height = SCREEN_HEIGHT

if GetVersion() ~= "IOS" then
	local ScrollerBarImage = CreateImage("ScrollerBarImage", [[GUI\buttons\scrollbar.anim]], { scrollerX, scrollerY }, { 0, 1 })
	table.insert(LevelMapDialog.parent_ext.children, 3, ScrollerBarImage)
end

---------------------------------------------------------
-- Save As dialog
---------------------------------------------------------	
	
local SaveAsTitle = CreateStaticText("SaveAsTitle", "$00306", { 0, -130 }, "header", "centre")
SaveAsTitle.width = 300
SaveAsTitle.height = 30
SaveAsTitle.text_ext.colour = WHITE

local buttonSpread = 120
local buttonY = 100
local buttonOG = 1.1
local buttonScale = 0.8

local SaveAsAcceptButton = CreatePushButton("SaveAsAcceptButton", [[GUI\generic_dialogs\generic_textbutton.anim]], { -buttonSpread, buttonY }, buttonOG)
SaveAsAcceptButton.button_ext.additional_scale = buttonScale
SetButtonText(SaveAsAcceptButton, "$00408")

local SaveAsCancelButton = CreatePushButton("SaveAsCancelButton", [[GUI\generic_dialogs\generic_textbutton.anim]], { buttonSpread, buttonY }, buttonOG)
SaveAsCancelButton.button_ext.additional_scale = buttonScale
SetButtonText(SaveAsCancelButton, "$00409")

local SaveAsEditBoxArt = CreateImage("SaveAsEditBoxArt", [[GUI\fe_dialogs\generic_textfield.anim]], { 0, -10 })
SaveAsEditBoxArt.anim_ext.additional_scale = 0.7

local SaveAsEditBox = CreateEditText("SaveAsEditBox", "filename", { -115, -21 }, 230)
SaveAsEditBox.text_ext.colour = { r = 67, g = 22, b = 18, alpha = 255 }
SaveAsEditBox.width = 379 * SaveAsEditBoxArt.anim_ext.additional_scale
SaveAsEditBox.height = 42 * SaveAsEditBoxArt.anim_ext.additional_scale
SaveAsEditBox.hit_top_left = { SaveAsEditBoxArt.position[1] - SaveAsEditBox.position[1] - SaveAsEditBox.width / 2, SaveAsEditBoxArt.position[2] - SaveAsEditBox.position[2] - SaveAsEditBox.height / 2 }
SaveAsEditBox.hit_bottom_right = { SaveAsEditBox.hit_top_left[1] + SaveAsEditBox.width, SaveAsEditBox.hit_top_left[2] + SaveAsEditBox.height }

local SaveAsDialog = CreateModalDialog("SaveAsDialog",
{
	SaveAsAcceptButton,
	SaveAsCancelButton,
	SaveAsEditBox,
	SaveAsEditBoxArt,
	SaveAsTitle,
},
[[GUI\generic_dialogs\generic_dialog_medium.anim]], { SCREEN_WIDTH / 2, 225 })
SaveAsDialog.modal_ext.fade_colour = { r = 0, g = 0, b = 0, alpha = 100 }

--------------------------------------------------------
-- Challenge Start (CS) dialog box
--------------------------------------------------------

--Global Constants
PlayerListItemsPerRow = 2
PlayerListItemWidth = 332 --includes spacing in between items
PlayerListItemHeight = 96 --includes spacing in between items

--Scroller
local dialogX = SCREEN_WIDTH / 2
local dialogY = 330
local listboxWidth = SCREEN_WIDTH
if GetVersion() == "IOS" then
	listboxWidth = 770
end
local listboxHeight = 300
local listboxX = 0
local listboxY = -23 - listboxHeight / 2
local scrollerWidth = 20
local scrollerHeight = 400
local scrollerX = listboxWidth / 2 - 80
local scrollerY = 0

--Scrollbar button position and range.
local CSScrollerButton = CreateScrollerButton("CSScrollerButton", [[GUI\buttons\scrollbar_dot.anim]])
CSScrollerButton.anim_ext.facing = { 0, 1 }
local CSScrollbar = CreateScrollBar("CSScrollbar", nil, nil, CSScrollerButton, { scrollerX - listboxX, scrollerY - listboxY - scrollerHeight / 2 })
CSScrollbar.width = scrollerWidth
CSScrollbar.height = scrollerHeight
CSScrollbar.scrollbar_ext.start_point = { 0, 0 }
CSScrollbar.scrollbar_ext.end_point = { 0, 1 }
CSScrollbar.scrollbar_ext.is_modal = false
CSScrollbar.scrollbar_ext.scroller_offset = { 0, 0 }
--CSScrollbar.scrollbar_ext.pip_anim = [[GUI\buttons\scrollbar_notch.anim]]

--List Box of items with scrollbar button attached
local CSPlayerList = CreateListBox("CSPlayerList", listboxWidth, listboxHeight, CSScrollbar, { listboxX, listboxY })
CSPlayerList.listbox_ext.horizontal = false
CSPlayerList.listbox_ext.item_offset = { 0, -26 }

AddListBoxMask(CSPlayerList, CreateImage("CSPlayerListMaskTop", [[GUI\challenge\list_mask_top.anim]], { -listboxX, -listboxY }))
AddListBoxMask(CSPlayerList, CreateImage("CSPlayerListMaskBottom", [[GUI\challenge\list_mask_bottom.anim]], { -listboxX, -listboxY }))

--Platform dependent overrides
local CSScrollerBarImage = nil
if GetVersion() == "IOS" then
	CSScrollbar.scrollbar_ext.force_hidden = true
	--Expand list box hit region to dialog window only for capturing drag
	CSPlayerList.hit_top_left = { -listboxWidth / 2, listboxY }
	CSPlayerList.hit_bottom_right = { listboxWidth / 2, listboxY + 500 }
else
	CSScrollerBarImage = CreateImage("CSScrollerBarImage", [[GUI\buttons\scrollbar_800.anim]], { scrollerX, scrollerY })
	CSScrollerBarImage.anim_ext.facing = { 0, 1 }
	--Expand list box hit region to full screen for capturing drag and mouse wheel
	CSPlayerList.hit_top_left = { -listboxX - dialogX, -listboxY - dialogY }
	CSPlayerList.hit_bottom_right = { SCREEN_WIDTH - listboxX - dialogX, SCREEN_HEIGHT - listboxY - dialogY }
end

--Player list item template
CSPlayerListItem = CreatePushButton("CSPlayerListItem", [[GUI\challenge\name_bar.anim]], { -166, -150 }, 1.05)
CSPlayerListItem.button_ext.activate_on_up = LIST_BOX_ACTIVATE_ON_UP
CSPlayerListItem.button_ext.under_growth = 0.95
CSPlayerListItem.anim_ext.additional_scale = 1.05
SetButtonText(CSPlayerListItem, "")
CSPlayerListItem.text_ext.font = "unicode"
CSPlayerListItem.text_ext.style = "centy"
CSPlayerListItem.text_ext.offset = { -66, 0 }

local CSFriendsIcon = CreateImage("CSFriendsIcon", [[GUI\challenge\friends_icon.anim]], { 351, -254 })
CSFriendsIcon.enabled = false

local CSBottomText = nil
local CSBottomIcon = nil
if GetVersion() == "IOS" then
	CSBottomText = CreateStaticText("CSBottomText", "$02002", { -250, 172 }, "dark_label", "centy")
	CSBottomText.text_ext.colour = WHITE
	CSBottomText.text_ext.additional_scale = 0.875
	CSBottomText.enabled = false
	
	CSBottomIcon = CreateImage("CSBottomIcon", [[GUI\challenge\game_center_icon_ios7.anim]], { -292, 172 })
	CSBottomIcon.enabled = false
	local iosVer = GetIOSVersion()
	if tonumber(iosVer:sub(1, iosVer:find("%."))) < 7 then
		CSBottomIcon.anim_ext.animation = [[GUI\challenge\game_center_icon.anim]]
	end
end

local CSCloseButton = CreatePushButton("CSCloseButton", [[GUI\generic_dialogs\generic_textbutton.anim]], { 0, 245 }, 1.1)
SetButtonText(CSCloseButton, "$02001")

ChallengeStartDialog = CreateModalDialog("ChallengeStartDialog",
{
	CSFriendsIcon,
	CSBottomText,
	CSBottomIcon,
	CSCloseButton,
	CSPlayerList,
	CSScrollerBarImage,
},
[[GUI\generic_dialogs\generic_dialog_large.anim]], { dialogX, dialogY })
ChallengeStartDialog.modal_ext.fade_colour = { r = 0, g = 0, b = 0, alpha = 150 }

-----------------------------------------------------

widgets =
{
	SaveAsDialog,
	LevelMapDialog,
	PuzzleMapDialog,
	MessageBox,
}

if GetVersion() == "IOS" then
	table.insert(widgets, ChallengeStartDialog)
end