-- hacky, but gets the job done
local mode = "regular";
local oldMode = mode; -- you can get away with this.

local t = Def.ActorFrame{
	Def.Actor{
		Name="ModeController";
		CodeMessageCommand=function(self,params)
			if params.Name == "ToggleMode" then
				oldMode = mode; -- store old mode for better transitions
				if mode == "regular" then
					mode = "nonstop";
				elseif mode == "nonstop" then
					mode = "endless"
				elseif mode == "endless" then
					mode = "regular";
				end;
				MESSAGEMAN:Broadcast("PlayModeSwitched");
			end;
		end;
		DifficultySelectedMessageCommand=function(self,params)
			local diffType = params.Type;

			-- check what the current play mode is and set accordingly!
			-- #COURSE:[type]Para [nonstop/endless];
			GAMESTATE:ApplyGameCommand("playmode,"..mode);

			if mode ~= "regular" then
				local crs = SONGMAN:FindCourse(diffType..'Para '..mode);
				if crs then
					GAMESTATE:SetCurrentCourse( crs );
				else
					-- xxx: this might not be handled well.
					local err = "Can't make a course for ".. diffType .." + ".. mode;
					Trace(err);
				end;
			end;
		end;
	};

	LoadActor( THEME:GetPathS("LifeMeterBattery","gain") )..{ PlayModeSwitchedMessageCommand=cmd(play); };

	-- todo: replace this with multiple graphics.
	Def.Sprite{
		InitCommand=cmd(x,SCREEN_CENTER_X;y,SCREEN_CENTER_Y;effectmagnitude,0,0,90);
		OnCommand=cmd(accelerate,0.15;diffusealpha,1;zoom,0.8;spin;effectmagnitude,0,0,-30);
		OffCommand=cmd(decelerate,0.25;zoom,1;diffusealpha,1;sleep,0;accelerate,0.3;diffusealpha,0;);

		PlayModeSwitchedMessageCommand=function(self,params)
			if mode == "regular" then
				self:Load( THEME:GetPathG("","_blank") );
			else
				self:Load( THEME:GetPathG("ScreenSelectPlayMode","scroll/mode"..mode) );
			end;
		end;
	};
};

return t;