require( "lib" )

Loader = {}

function Loader.start( mapname )
	StopThreads()
	Loader.map_thread = NewGlobalThread( dofile("levels/"..mapname..".lua") )
	Resume( Loader.map_thread )
end


function Loader.completeLevel( a, b )

	for i=1,players_num do
		players[i].mortal = false
	end

	local sx, sy = GetCaptionSize( "default", "LEVEL COMPLETE" )
	local pause_widget = CreateWidget(constants.wt_Label, "fade_box", nil, (CONFIG.scr_width-sx)/2, (CONFIG.scr_height-sy)/2, 1, 1)
	WidgetSetVisible(pause_widget, true)
	WidgetSetCaptionColor(pause_widget, {1,1,1,1}, false )
	WidgetSetCaption( pause_widget, "LEVEL COMPLETE" )
	useless_widgets[pause_widget] = pause_widget
	sx, sy = GetCaptionSize( "default", "THE PASSWORD IS "..b )
	pause_widget = CreateWidget(constants.wt_Label, "fade_box", nil, (CONFIG.scr_width-sx)/2, (CONFIG.scr_height-sy)/2+2*sy, 2*sx, 1)
	WidgetSetVisible(pause_widget, true)
	WidgetSetCaptionColor(pause_widget, {1,1,1,1}, false )
	WidgetSetCaption( pause_widget, "THE PASSWORD IS /cEEBB22"..b, true )
	useless_widgets[pause_widget] = pause_widget
	Resume( NewGlobalThread ( function()
		Wait( 8000 )
		local stop = {}
		while #stop < players_num do
			for i=1,players_num do
				players[i].mortal = false
				players[i].lock_controls = true
				players[i].y = players[i].y - 1
				if players[i].y < -500 then
					stop[i] = true
				end
				if players[i].sprite then
					SetObjPos( players[i].sprite, players[i].x, players[i].y )
				end
			end
			Wait(1)
		end
		in_game = false
		pause_widget = CreateWidget(constants.wt_Widget, "fade_box", nil, 0, 0, CONFIG.scr_width, CONFIG.scr_height)
		WidgetSetZ(pause_widget, 0.5)
		WidgetSetVisible(pause_widget, true)
		WidgetSetColorBox(pause_widget, {0, 0, 0, 1} )
		useless_widgets[pause_widget] = pause_widget
		sx, sy = GetCaptionSize( "default", "PRESS FIRE" )
		pause_widget = CreateWidget(constants.wt_Label, "fade_box", nil, (CONFIG.scr_width-sx)/2, (CONFIG.scr_height-sy)/2+4*sy, 1, 1)
		WidgetSetVisible(pause_widget, true)
		WidgetSetCaptionColor(pause_widget, {1,1,1,1}, false )
		WidgetSetCaption( pause_widget, "PRESS FIRE" )
		useless_widgets[pause_widget] = pause_widget
		Wait( 1000 )
		while not (keys_held[ config.controls[1].fire ] or keys_held[ config.controls[2].fire ] or keys_held.mouse0) do
			Wait(1)
		end
		ShutDownEverything( true )
		Loader.start(a)
	end ))
	
end