
g_currentMenuSelection = 0;
g_mainMenuStartPosX = 40;
g_mainMenuStartPosY = 280;
g_totalLevels = 7;
g_totalMenuSelections = g_totalLevels+1; //for the quit button
g_menuSelectSpacerY = 47;

function SetupMainMenu()
	GetMapManager:UnloadMapByName("Intro");
	GetMapManager:UnloadMapByName("Game");
	GetMapManager:SetActiveMapByName("Intro");
	g_SmartSound:SetMusic("audio/intro.ogg", false, true);

 	GetInputManager:AddBinding("control", "OnMainMenuSelect", C_ENTITY_NONE);	
  GetInputManager:AddBinding("return", "OnMainMenuSelect", C_ENTITY_NONE);	
	GetInputManager:AddBinding("up, always", "OnMainMenuUp", C_ENTITY_NONE);	
	GetInputManager:AddBinding("down, always", "OnMainMenuDown", C_ENTITY_NONE);	

	g_currentWave = 0;
	

	local entCursor = GetEntityByName("cursor_template"):Clone(nil, Vector2(0, 0));
	entCursor:SetPersistent(false);
	entCursor:SetName("cursor"); //so we can find it again later
	ModMenuSelection(0);
	
	ProcessUnlockedLevels();
	
	//some cool bg visuals
	entWall = CreateEntity(GetMapManager:GetActiveMap(), Vector2(0,0), "entity/doomwall_attract.lua"); 	

end

function ProcessUnlockedLevels()
	for i=1, g_totalLevels do
		if (not IsLocked(i)) then
		  DeleteEntityByNameIfExists("locked" .. i);
		end
	end

end

function IsLocked(level)
	if (GetGameLogic:Data():Exists(tostring(level))) then
	return false;
	else
	return true;
	end
end

function UnlockLevel(level)

	GetGameLogic:Data():Set(tostring(level), "");
end

function PlayLevel(level)

				if ( level > 1) then
				//first check to see if it is unlocked
					if (IsLocked(level)) then
						GetSoundManager:Play("audio/blocked.wav");
						return;
					end
					
				end
				
				GetSoundManager:Play("audio/wall_break.ogg");
			
				KillMainMenu();
				g_level = level;
				SetupGame();

end

function OnMainMenuSelect(bKeyDown)
	if (bKeyDown) then
			
				if (g_currentMenuSelection == g_totalLevels) then
				GetGameLogic:Quit(); //quits the game
			end

		//well, must be one of the other levels, is it unlocked?
		
		PlayLevel(g_currentMenuSelection+1);
			
		end

	return false;  //continue to process key callbacks for this key stroke?
end


function ModMenuSelection(modNum)

g_currentMenuSelection = g_currentMenuSelection + modNum;

	if (g_currentMenuSelection == g_totalMenuSelections) then
		g_currentMenuSelection = 0; //wrap around
	end 

	if (g_currentMenuSelection < 0) then
	g_currentMenuSelection = g_totalMenuSelections-1; //wrap around
	end
	if (modNum != 0) then
		GetSoundManager:Play("audio/move.wav");
	end
	
	//actually move the cursor
		local entCursor = GetEntityByName("cursor"):SetPos(Vector2(g_mainMenuStartPosX, g_mainMenuStartPosY + (g_currentMenuSelection * g_menuSelectSpacerY)));

end


function OnMainMenuDown(bKeyDown)
	if (bKeyDown) then
				ModMenuSelection(1);
			end
		
	return false;  //continue to process key callbacks for this key stroke?
end

function OnMainMenuUp(bKeyDown)
	if (bKeyDown) then
				ModMenuSelection(-1);
			end
		
	return false;  //continue to process key callbacks for this key stroke?
end

function KillMainMenu()

  GetInputManager:RemoveBinding("control", "OnMainMenuSelect", C_ENTITY_NONE);	
  GetInputManager:RemoveBinding("return", "OnMainMenuSelect", C_ENTITY_NONE);	
	GetInputManager:RemoveBinding("up, always", "OnMainMenuUp", C_ENTITY_NONE);	
	GetInputManager:RemoveBinding("down, always", "OnMainMenuDown", C_ENTITY_NONE);	
end

