
levels = {}

levels[1] = {}
levels[1].name = trident.name
levels[1].file = "Scripts/trident.lua"
levels[1].music = "Music/07-particule.ogg"
levels[1].locked = false

levels[2] = {}
levels[2].name = the_gate.name
levels[2].file = "Scripts/the_gate.lua"
levels[2].music = "Music/05-efface.ogg"
levels[2].locked = true

levels[3] = {}
levels[3].name = exact_change.name
levels[3].file = "Scripts/exact_change.lua"
levels[3].music = "Music/06-sale.ogg"
levels[3].locked = true

levels[4] = {}
levels[4].name = the_stream.name
levels[4].file = "Scripts/the_stream.lua"
levels[4].music = "Music/09-followme.ogg"
levels[4].locked = true

levels[5] = {}
levels[5].name = grid.name
levels[5].file = "Scripts/grid.lua"
levels[5].music = "Music/07-particule.ogg"
levels[5].locked = true

levels[6] = {}
levels[6].name = dispenser.name
levels[6].file = "Scripts/dispenser.lua"
levels[6].music = "Music/04-experience.ogg"
levels[6].locked = true

levels[7] = {}
levels[7].name = gulliver.name
levels[7].file = "Scripts/gulliver.lua"
levels[7].music = "Music/05-efface.ogg"
levels[7].locked = true

levels[8] = {}
levels[8].name = gravity.name
levels[8].file = "Scripts/gravity.lua"
levels[8].music = "Music/06-sale.ogg"
levels[8].locked = true

levels[9] = {}
levels[9].name = clamps.name
levels[9].file = "Scripts/clamps.lua"
levels[9].music = "Music/09-followme.ogg"
levels[9].locked = true

levels[10] = {}
levels[10].name = pyramid.name
levels[10].file = "Scripts/pyramid.lua"
levels[10].music = "Music/05-efface.ogg"
levels[10].locked = true

levels[11] = {}
levels[11].name = coathanger.name
levels[11].file = "Scripts/coathanger.lua"
levels[11].music = "Music/02-realite.ogg"
levels[11].locked = true

levels[12] = {}
levels[12].name = jaws.name
levels[12].file = "Scripts/jaws.lua"
levels[12].music = "Music/04-experience.ogg"
levels[12].locked = true

levels[13] = {}
levels[13].name = pile.name
levels[13].file = "Scripts/pile.lua"
levels[13].music = "Music/09-followme.ogg"
levels[13].locked = true

levels[14] = {}
levels[14].name = artillery.name
levels[14].file = "Scripts/artillery.lua"
levels[14].music = "Music/06-sale.ogg"
levels[14].locked = true

levels[15] = {}
levels[15].name = the_perfectionist.name
levels[15].file = "Scripts/the_perfectionist.lua"
levels[15].music = "Music/02-realite.ogg"
levels[15].locked = true

levels[16] = {}
levels[16].name = the_plant.name
levels[16].file = "Scripts/the_plant.lua"
levels[16].music = "Music/05-efface.ogg"
levels[16].locked = true

levels[17] = {}
levels[17].name = flippers.name
levels[17].file = "Scripts/flippers.lua"
levels[17].music = "Music/07-particule.ogg"
levels[17].locked = true

levels[18] = {}
levels[18].name = scale.name
levels[18].file = "Scripts/scale.lua"
levels[18].music = "Music/09-followme.ogg"
levels[18].locked = true

levels[19] = {}
levels[19].name = the_mill.name
levels[19].file = "Scripts/the_mill.lua"
levels[19].music = "Music/02-realite.ogg"
levels[19].locked = true

levels[20] = {}
levels[20].name = time_dilation.name
levels[20].file = "Scripts/time_dilation.lua"
levels[20].music = "Music/04-experience.ogg"
levels[20].locked = true

cleared = 0
lost = 0
longest_chain = 0
chains = 0
cleared_record = false
longest_time_record = false
time = 0
time_sz = ''

current_level = 0

level_gui = {}
function ShowLevelGUI ( )
	local w = silkscreen11:get_width ( text.menu )
	local b = CreateButton ( resx_hw - 20 - w, resy_hh - 30, -5000, silkscreen11, text.menu, WHITE, RED )
	b.OnLeftButtonPress = function ( button, x, y )
		state = 'paused'
		ShowQuitMenu ( )
	end
	table.insert ( level_gui, b )
end

function HideLevelGUI ( )
	Destroy ( level_gui )
end


function EnterLevel ( i )
	current_level = i
	local level = levels[i].file
	EnterLevelFile ( level )

	if music ~= nil and music_volume > 0 then
		-- load the music
		music:load ( levels[i].music )
		music:play_loop ( )
		music.volume = music_volume
	end


	local t = ShowGoal ( )
	t.depth = -10000
	t.alpha = 0
	display:add_child ( t )
	local effect = Fade:Create ( t, 1 )
	effects:AddEffect ( effect, 500, 0 )
	local effect = Fade:Create ( t, 0 )
	effects:AddEffect ( effect, 2000, 5000 )
	local effect = Remove:Create ( t, display )
	effects:AddEffect ( effect, 0, 7000 )

end


function EnterLevelFile ( level )
	allowclose = false

	-- purge the effects queue
	while effects:IsDone ( ) == false do
		effects:Update ( 16 )
	end

	-- drop the curtain
	level_curtain.alpha = 1

	-- load the level
	game:Load ( level )


	-- raise the curtain
	level_curtain.visible = true
	local effect = Fade:Create ( level_curtain, 0 )
	effects:AddEffect ( effect, 1000, 1000 )
	local effect = ChangeVisibility:Create ( level_curtain, false )
	effects:AddEffect ( effect, 0, 2001 )

	-- center the camera
	local cm = Move:Create ( camera, world.center_x, world.center_y )
	effects:AddEffect ( cm, 2000, 0 )
	local cm = Resize:Create ( camera, world.scale * camera_scaler, world.scale * camera_scaler )
	effects:AddEffect ( cm, 1100, 900 )

if colorblind == false then
	local cm = Modulate:Create ( camera, world.color )
	effects:AddEffect ( cm, 1100, 0 )
else
	local cm = Modulate:Create ( camera, GRAY )
	effects:AddEffect ( cm, 1100, 0 )
end



	cleared = 0
	lost = 0
	longest_chain = 0
	chains = 0
	cleared_record = false
	longest_time_record = false
	time = 0
	time_sz = ''

	ShowLevelGUI ( )



	local clickable = Clickable:Create ( -resx_hw, -resy_hh, 20, resx, resy )
	table.insert ( gui_elements, clickable )

	function clickable:OnPress ( button )
		if state == 'playing' then
			if button == MBUTTON_LEFT then
				game:Pick ( mouse.xaxis, mouse.yaxis )
			else
				game:DeselectAll ( )
			end
		end
	end

	function clickable:OnRelease ( button )
		if state == 'playing' then
			if button == MBUTTON_LEFT then
				game:ClearAll ( )
			end
		end
	end
	table.insert ( clickable, b )

	ts = Sprite ( )
	ts.depth = -1000
	display:add_child ( ts )

end


function ExitLevel ( )
	allowclose = true

	game:Unload ( )
	if music ~= nil then
		music:stop ( )
		music:unload ( )
	end

	HideLevelGUI ( )

	display:remove_child ( ts )
	if tse ~= nil then
		effects:RemoveEffect ( tse )
		tse = nil
	end
end

function ShowTip ( sz, x, y, d )
	if showtips == false then
		return
	end

	tip:play ( )

	ShowTipQuiet ( sz, x, y, d )
end

function ShowTipQuiet ( sz, x, y, d )
	if showtips == false then
		return
	end

	x = x / 400 * resx_hw
	y = y / 300 * resy_hh

	if tse ~= nil then
		effects:RemoveEffect ( tse )
		tse = nil
	end

	local w = silkscreen11:get_width ( sz )
	local h = silkscreen11:get_size ( )
	local bl = y + h / 2
	ts.alpha = 1
	ts.canvas:clear ( )
	ts.canvas:move_to ( x - w / 2 - 15, bl + h + 15 )
	ts.canvas:line_to ( x + w / 2 + 15, bl + h + 15 )
	ts.canvas:line_to ( x + w / 2 + 15, bl - 15 )
	ts.canvas:line_to ( x - w / 2 - 15, bl - 15 )
	ts.canvas:set_fill_style ( WHITE, 0.4 )
	ts.canvas:fill ( )
	ts.canvas:set_font ( silkscreen11, WHITE )
	ts.canvas:move_to ( x - w / 2, bl )
	ts.canvas:write ( sz )
	tse = Fade:Create ( ts, 0 )
	effects:AddEffect ( tse, 2000, d )
end




quit_gui = {}
function ShowQuitMenu ( )


	-- purge the effects queue
	while effects:IsDone ( ) == false do
		effects:Update ( 16 )
	end


	if swoosh ~= nil and sound_volume > 0 then
		swoosh:play ( )
	end

	HideLevelGUI ( )



if current_level > 0 then


	hiscore = ShowGoal ( )
	hiscore.depth = -10000
	display:add_child ( hiscore )
	hiscore.alpha = 0

	-- display the hiscore
	local l = records[current_level]
	hiscore.canvas:move_to ( -100, -100 )
	local y = -140
	if l.longest_chain[difficulty] > 0 and l.longest_chain_leader[difficulty] ~= '' then
		local sz = text.longest_chain .. ": "
		local w = silkscreen11:get_width ( sz )
		hiscore.canvas:set_font ( silkscreen11, WHITE )
		hiscore.canvas:move_to ( -w - 5, y )
		hiscore.canvas:write ( sz )
		hiscore.canvas:move_to ( 0, y )
		hiscore.canvas:write ( l.longest_chain[difficulty] .. " (" .. l.longest_chain_leader[difficulty] .. ")" )
		y = y - 20
	end
	if l.cleared[difficulty] > 0 and l.cleared_leader[difficulty] ~= '' then
		local sz = text.most_cleared .. ": "
		local w = silkscreen11:get_width ( sz )
		hiscore.canvas:set_font ( silkscreen11, WHITE )
		hiscore.canvas:move_to ( -w - 5, y )
		hiscore.canvas:write ( sz )
		hiscore.canvas:move_to ( 0, y )
		hiscore.canvas:write ( l.cleared[difficulty] .. " (" .. l.cleared_leader[difficulty] .. ")" )
		y = y - 20
	end
	if l.longest_time[difficulty] > 0 and l.longest_time_leader[difficulty] ~= '' then
		local time_sz
		local mins = math.floor ( l.longest_time[difficulty] / 1000 / 60 )
		local secs = math.floor ( l.longest_time[difficulty] / 1000 ) - ( mins * 60 )
		if secs < 10 then
			time_sz = ( mins .. ":0" .. secs  )
		else
			time_sz = ( mins .. ":" .. secs  )
		end

		local sz = text.longest_playing_time .. ": "
		local w = silkscreen11:get_width ( sz )
		hiscore.canvas:set_font ( silkscreen11, WHITE )
		hiscore.canvas:move_to ( -w - 5, y )
		hiscore.canvas:write ( sz )
		hiscore.canvas:move_to ( 0, y )
		hiscore.canvas:write ( time_sz .. " (" .. l.longest_time_leader[difficulty] .. ")" )
		y = y - 20
	end
	if l.fastest[difficulty] > 0 and l.fastest_leader[difficulty] ~= '' then
		local time_sz
		local mins = math.floor ( l.fastest[difficulty] / 1000 / 60 )
		local secs = math.floor ( l.fastest[difficulty] / 1000 ) - ( mins * 60 )
		if secs < 10 then
			time_sz = ( mins .. ":0" .. secs  )
		else
			time_sz = ( mins .. ":" .. secs  )
		end

		local sz = text.fastest_completion .. ": "
		local w = silkscreen11:get_width ( sz )
		hiscore.canvas:set_font ( silkscreen11, WHITE )
		hiscore.canvas:move_to ( -w - 5, y )
		hiscore.canvas:write ( sz )
		hiscore.canvas:move_to ( 0, y )
		hiscore.canvas:write ( time_sz .. " (" .. l.fastest_leader[difficulty] .. ")" )
		y = y - 20
	end
	if l.optimal[difficulty] > 0 and l.optimal_leader[difficulty] ~= '' then
		local sz = text.optimal_completion .. ": "
		local w = silkscreen11:get_width ( sz )
		hiscore.canvas:set_font ( silkscreen11, WHITE )
		hiscore.canvas:move_to ( -w - 5, y )
		hiscore.canvas:write ( sz )
		hiscore.canvas:move_to ( 0, y )
		hiscore.canvas:write ( l.optimal[difficulty] .. " chains (" .. l.optimal_leader[difficulty] .. ")" )
		y = y - 20
	end
	if y ~= -140 then
		local dsz
		if difficulty == 1 then
			dsz = text.easy
		elseif difficulty == 2 then
			dsz = text.normal
		else
			dsz = text.hard
		end
		local sz = text.level_records .. " (" .. dsz .. ")"
		local w = silkscreen14:get_width ( sz )
		hiscore.canvas:set_font ( silkscreen14, WHITE )
		hiscore.canvas:move_to ( -w / 2, -100 )
		hiscore.canvas:write ( sz )
	end

	local sz = levels[current_level].name
	local w = silkscreen17:get_width ( sz )
	hiscore.canvas:set_font ( silkscreen17, WHITE )
	hiscore.canvas:move_to ( -w / 2, -70 )
	hiscore.canvas:write ( sz )


	local effect = Fade:Create ( hiscore, 1 )
	effects:AddEffect ( effect, 500, 0 )
end



	level_curtain.visible = true
	local effect = Fade:Create ( level_curtain, 0.5 )
	effects:AddEffect ( effect, 500, 0 )


	local w = silkscreen11:get_width ( text.resume )
	local x = resx_hw - w - 20
	local b = CreateButton ( x, resy_hh - 30, -5000, silkscreen11, text.resume, WHITE, RED )
	b.OnLeftButtonPress = function ( button, x, y )
		HideQuitMenu ( )
		state = 'playing'
		ShowLevelGUI ( )
	end
	table.insert ( quit_gui, b )

	local w = silkscreen11:get_width ( text.quit )
	x = x - w - 10
	local b = CreateButton ( x, resy_hh - 30, -5000, silkscreen11, text.quit, WHITE, RED )
	b.OnLeftButtonPress = function ( button, x, y )
		HideQuitMenu ( )
		ExitLevel ( )
		state = 'menu'
		EnterMainMenu ( )
	end
	table.insert ( quit_gui, b )

	local w = silkscreen11:get_width ( text.restart )
	x = x - w - 10
	local b = CreateButton ( x, resy_hh - 30, -5000, silkscreen11, text.restart, WHITE, RED )
	b.OnLeftButtonPress = function ( button, x, y )
		HideQuitMenu ( )
		ExitLevel ( )
		if current_level > 0 then
			EnterLevel ( current_level )
		else
			EnterTheEndLevel ( )
		end
		state = 'playing'
	end
	table.insert ( quit_gui, b )


	local b = CreateButton ( -resx_hw + 20, resy_hh - 30, -5000, silkscreen14, profiles[current_profile], RED, RED )
	table.insert ( quit_gui, b )

end

function HideQuitMenu ( )

	local effect = Fade:Create ( level_curtain, 0 )
	effects:AddEffect ( effect, 250, 0 )
	local effect = ChangeVisibility:Create ( level_curtain, false )
	effects:AddEffect ( effect, 0, 251 )
	Destroy ( quit_gui )

	if hiscore ~= nil then
		display:remove_child ( hiscore )
		hiscore = nil
	end
end



function EnterTheEndLevel ( )
	EnterLevelFile ( "Scripts/the_end.lua" )
	state = 'playing'

	if music ~= nil and music_volume > 0 then
		-- load the ending theme
		music:load ( "Music/08-larmes.ogg" )
		music:play_loop ( )
		music.volume = music_volume
	end
	current_level = 0
end





gameovermenu_gui = {}
function ShowGameoverMenu ( )
	level_curtain.visible = true
	local effect = Fade:Create ( level_curtain, 0.75 )
	effects:AddEffect ( effect, 2000, 500 )

	local w = silkscreen17:get_width ( text.game_over )
	local b = CreateButton ( -w / 2, 0, -5000, silkscreen17, text.game_over, WHITE, WHITE )
	table.insert ( gameovermenu_gui, b )
	b.sprite.alpha = 0
	b.sprite.depth = -5000
	local effect = Fade:Create ( b.sprite, 1 )
	effects:AddEffect ( effect, 2000, 500 )

	local w = silkscreen11:get_width ( text.try_again )
	local b = CreateButton ( -w / 2, -20, -5000, silkscreen11, text.try_again, WHITE, WHITE )
	table.insert ( gameovermenu_gui, b )
	b.sprite.alpha = 0
	b.sprite.depth = -5000
	local effect = Fade:Create ( b.sprite, 1 )
	effects:AddEffect ( effect, 2000, 500 )

	game:DeselectAll ( )

	local b = CreateButton ( -65, -60, -5000, silkscreen14, text.yes, WHITE, RED )
	b.OnLeftButtonPress = function ( button, x, y )
		HideGameoverMenu ( )
		ExitLevel ( )
		EnterLevel ( current_level )
		state = "playing"
	end
	table.insert ( gameovermenu_gui, b )
	b.sprite.alpha = 0
	b.sprite.depth = -5000
	local effect = Fade:Create ( b.sprite, 1 )
	effects:AddEffect ( effect, 2000, 500 )

	local b = CreateButton ( 30, -60, -5000, silkscreen14, text.no, WHITE, RED )
	b.OnLeftButtonPress = function ( button, x, y )
		HideGameoverMenu ( )
		ExitLevel ( )
		state = 'menu'
		EnterMainMenu ( )
	end
	table.insert ( gameovermenu_gui, b )
	b.sprite.alpha = 0
	b.sprite.depth = -5000
	local effect = Fade:Create ( b.sprite, 1 )
	effects:AddEffect ( effect, 2000, 500 )
end

function HideGameoverMenu ( )
	local effect = Fade:Create ( level_curtain, 0 )
	effects:AddEffect ( effect, 250, 0 )
	local effect = ChangeVisibility:Create ( level_curtain, false )
	effects:AddEffect ( effect, 0, 251 )
	Destroy ( gameovermenu_gui )
end








closedialog_gui = {}
function ShowCloseDialog ( )
	if old_state ~= nil then
		return
	end
	old_state = state
	state = 'paused'

	diag:play ( )

	level_curtain4 = Sprite ( )
	level_curtain4.depth = -30001
	level_curtain4.canvas:rectangle ( resx, resy )
	level_curtain4.canvas:set_fill_style ( DARKRED, 1 )
	level_curtain4.canvas:fill ( )
	display:add_child ( level_curtain4 )

	level_curtain4.alpha = 0
	local effect = Fade:Create ( level_curtain4, 0.75 )
	effects:AddEffect ( effect, 250, 0 )

	-- grab mouse input
	clickable2 = Clickable:Create ( -resx_hw, -resy_hh, -30001, resx, resy )
	AddGUIElement ( clickable2 )

	local title = dialog_titles[#dialog_titles]
	local message = dialog_messages[#dialog_messages]

	local w = silkscreen14:get_width ( text.quitdialog )
	local b = CreateButton ( -w / 2, 0, -30002, silkscreen14, text.quitdialog, WHITE, WHITE )
	table.insert ( closedialog_gui, b )

	local w = silkscreen11:get_width ( text.quitsure )
	local b = CreateButton ( -w / 2, -20, -30002, silkscreen11, text.quitsure, WHITE, WHITE )
	table.insert ( closedialog_gui, b )

	local b = CreateButton ( -65, -60, -30002, silkscreen14, text.yes, WHITE, RED )
	b.OnLeftButtonPress = function ( button, x, y )
		HideCloseDialog ( )
		display:destroy ( )
	end
	table.insert ( closedialog_gui, b )

	local b = CreateButton ( 30, -60, -30002, silkscreen14, text.no, WHITE, RED )
	b.OnLeftButtonPress = function ( button, x, y )
		state = old_state
		old_state = nil
		HideCloseDialog ( )
	end
	table.insert ( closedialog_gui, b )
end

function HideCloseDialog ( )
	display:remove_child ( level_curtain4 )
	level_curtain3 = nil

	RemoveGUIElement ( clickable2 )
	clickable2:Destroy ( )
	clickable2 = nil

	Destroy ( closedialog_gui )
end











theend_gui = {}
function ShowTheEndMenu ( )
	HideLevelGUI ( )

	level_curtain.visible = true
	local effect = Fade:Create ( level_curtain, 0.75 )
	effects:AddEffect ( effect, 500, 0 )

	local w = silkscreen14:get_width ( text.final )
	local b = CreateButton ( -w / 2, 0, -5000, silkscreen14, text.final, WHITE, WHITE )
	table.insert ( theend_gui, b )

	local w = silkscreen11:get_width ( text.gotoend )
	local b = CreateButton ( -w / 2, -20, -5000, silkscreen11, text.gotoend, WHITE, WHITE )
	table.insert ( theend_gui, b )

	local b = CreateButton ( -65, -60, -5000, silkscreen14, text.yes, WHITE, RED )
	b.OnLeftButtonPress = function ( button, x, y )
		HideTheEndMenu ( )
		ExitLevel ( )
		EnterLevelFile ( "Scripts/the_end.lua" )
		state = 'playing'

		if music ~= nil and music_volume > 0 then
			-- load the ending theme
			music:load ( "Music/08-larmes.ogg" )
			music:play_loop ( )
			music.volume = music_volume
		end
		current_level = 0
	end
	table.insert ( theend_gui, b )
	local b = CreateButton ( 30, -60, -5000, silkscreen14, text.no, WHITE, RED )
	b.OnLeftButtonPress = function ( button, x, y )
		HideTheEndMenu ( )
		state = 'playing'
		ShowLevelGUI ( )

	end
	table.insert ( theend_gui, b )
end

function HideTheEndMenu ( )
	local effect = Fade:Create ( level_curtain, 0 )
	effects:AddEffect ( effect, 250, 0 )
	local effect = ChangeVisibility:Create ( level_curtain, false )
	effects:AddEffect ( effect, 0, 251 )
	Destroy ( theend_gui )
end












winmenu_gui = {}
function ShowWinMenu ( )
	HideLevelGUI ( )

	level_curtain.visible = true
	local effect = Fade:Create ( level_curtain, 0.75 )
	effects:AddEffect ( effect, 500, 0 )

	local w = silkscreen14:get_width ( text.objective )
	local b = CreateButton ( -w / 2, 0, -5000, silkscreen14, text.objective, WHITE, WHITE )
	table.insert ( winmenu_gui, b )

	local w = silkscreen11:get_width ( text.keepplaying )
	local b = CreateButton ( -w / 2, -20, -5000, silkscreen11, text.keepplaying, WHITE, WHITE )
	table.insert ( winmenu_gui, b )

	local b = CreateButton ( -65, -60, -5000, silkscreen14, text.yes, WHITE, RED )
	b.OnLeftButtonPress = function ( button, x, y )
		HideWinMenu ( )
		state = 'playing'
		ShowLevelGUI ( )
	end
	table.insert ( winmenu_gui, b )
	local b = CreateButton ( 30, -60, -5000, silkscreen14, text.no, WHITE, RED )
	b.OnLeftButtonPress = function ( button, x, y )
		HideWinMenu ( )
		ExitLevel ( )
		state = 'menu'
		EnterMainMenu ( )
	end
	table.insert ( winmenu_gui, b )
end

function HideWinMenu ( )
	local effect = Fade:Create ( level_curtain, 0 )
	effects:AddEffect ( effect, 250, 0 )
	local effect = ChangeVisibility:Create ( level_curtain, false )
	effects:AddEffect ( effect, 0, 251 )
	Destroy ( winmenu_gui )
end




function ShowGoal ( )

	local y = 0

	-- display the goal
	local w = silkscreen17:get_width ( world.goal )
	local h = silkscreen17:get_size ( )
	local t = Sprite ( )

	t.canvas:move_to ( -w / 2 - 15, y + h + 15 )
	t.canvas:line_to ( w / 2 + 15, y + h + 15 )
	t.canvas:line_to ( w / 2 + 15, y - 15 )
	t.canvas:line_to ( -w / 2 - 15, y - 15 )
	t.canvas:set_fill_style ( WHITE, 0.4 )
	t.canvas:fill ( )

	t.canvas:set_font ( silkscreen17, WHITE )
	t.canvas:move_to ( -w / 2, 0 )
	t.canvas:write ( world.goal )


	return t
end





unlockmenu_gui = {}
function ShowUnlockMenu ( )
	HideLevelGUI ( )

	level_curtain.visible = true
	local effect = Fade:Create ( level_curtain, 0.75 )
	effects:AddEffect ( effect, 500, 0 )

	local sz = text.unlock .. " '" .. levels[current_level + 1].name .. "'"
	local w = silkscreen14:get_width ( sz )
	local b = CreateButton ( -w / 2, 0, -5000, silkscreen14, sz, WHITE, WHITE )
	table.insert ( unlockmenu_gui, b )

	local w = silkscreen11:get_width ( text.gotherenow )
	local b = CreateButton ( -w / 2, -20, -5000, silkscreen11, text.gotherenow, WHITE, WHITE )
	table.insert ( unlockmenu_gui, b )

	local b = CreateButton ( -65, -60, -5000, silkscreen14, text.yes, WHITE, RED )
	b.OnLeftButtonPress = function ( button, x, y )
		HideUnlockMenu ( )
		ExitLevel ( )
		EnterLevel ( current_level + 1 )
		state = 'playing'
	end
	table.insert ( unlockmenu_gui, b )
	local b = CreateButton ( 30, -60, -5000, silkscreen14, text.no, WHITE, RED )
	b.OnLeftButtonPress = function ( button, x, y )
		HideUnlockMenu ( )
		state = 'playing'
		ShowLevelGUI ( )
	end
	table.insert ( unlockmenu_gui, b )
end

function HideUnlockMenu ( )
	local effect = Fade:Create ( level_curtain, 0 )
	effects:AddEffect ( effect, 250, 0 )
	local effect = ChangeVisibility:Create ( level_curtain, false )
	effects:AddEffect ( effect, 0, 251 )
	Destroy ( unlockmenu_gui )
end







demoover_gui = {}
function ShowDemoOverMenu ( )
	HideLevelGUI ( )

	level_curtain.visible = true
	local effect = Fade:Create ( level_curtain, 0.75 )
	effects:AddEffect ( effect, 500, 0 )

	local w = silkscreen14:get_width ( text.demoover )
	local b = CreateButton ( -w / 2, 0, -5000, silkscreen14, text.demoover, WHITE, WHITE )
	table.insert ( demoover_gui, b )

	local w = silkscreen11:get_width ( text.demoexplain )
	local b = CreateButton ( -w / 2, -20, -5000, silkscreen11, text.demoexplain, WHITE, WHITE )
	table.insert ( demoover_gui, b )

	local b = CreateButton ( -65, -60, -5000, silkscreen14, text.yes, WHITE, RED )
	b.OnLeftButtonPress = function ( button, x, y )
		open_url ( "http://www.2dengine.com/chains/" )
	end
	table.insert ( demoover_gui, b )
	local b = CreateButton ( 30, -60, -5000, silkscreen14, text.no, WHITE, RED )
	b.OnLeftButtonPress = function ( button, x, y )
		HideDemoOverMenu ( )
		state = 'playing'
		ShowLevelGUI ( )
	end
	table.insert ( demoover_gui, b )
end

function HideDemoOverMenu ( )
	local effect = Fade:Create ( level_curtain, 0 )
	effects:AddEffect ( effect, 250, 0 )
	local effect = ChangeVisibility:Create ( level_curtain, false )
	effects:AddEffect ( effect, 0, 251 )
	Destroy ( demoover_gui )
end

