sureExit_options = {"YES", "NO"}
sureExit_numOptions = 2
sureExit_alreadyPressed = 0

sureExit_restart = 0


function sureExit_onAfterDestroyPopup()
	if flow_gameMode == gameTypesIds["MULTIPLAYER"] then	
		cfunction("exitScriptSystem",UIReturnCodes["MULTIPLAYER_EXIT"])		
	elseif flow_gameMode == gameTypesIds["ARCADE"] then
		if sureExit_restart == 1 then
			cfunction("exitScriptSystem",UIReturnCodes["ARCADE_RESTART"])
		else
			cfunction("exitScriptSystem",UIReturnCodes["ARCADE_EXIT"])
		end			
	elseif flow_gameMode == gameTypesIds["CHALLENGE"] then			
		if sureExit_restart == 1 then
			cfunction("exitScriptSystem",UIReturnCodes["CHALLENGE_RESTART"])
		else
			cfunction("exitScriptSystem",UIReturnCodes["CHALLENGE_EXIT"])
		end
	elseif flow_gameMode == gameTypesIds["TIMETRIAL"] then
		if sureExit_restart == 1 then
			cfunction("exitScriptSystem",UIReturnCodes["TIMETRIAL_RESTART"])
		else
			cfunction("exitScriptSystem",UIReturnCodes["TIMETRIAL_EXIT"])
		end
	elseif flow_gameMode == gameTypesIds["TUTORIAL"] then
		if sureExit_restart == 1 then
			cfunction("exitScriptSystem",UIReturnCodes["TUTORIAL_RESTART"])
		else
			cfunction("exitScriptSystem",UIReturnCodes["TUTORIAL_EXIT"])
		end
	elseif flow_gameMode == gameTypesIds["TOURNAMENT"] then
		if sureExit_restart == 1 then
			cfunction("exitScriptSystem",UIReturnCodes["TOURNAMENT_RESTART"])
		else
			cfunction("exitScriptSystem",UIReturnCodes["TOURNAMENT_EXIT"])
		end
	else
		cfunction("debugPrintF", "Wrong gameMode in sureExit_onAfterDestroyPopup", flow_gameMode)
	end
end

function sureExit_afterPopupClosed()
	if sureExit_curSel == 1 then
		destroyPopup("sureExit_onAfterDestroyPopup", PopupOptions["FADETOLOADING"])
		cfunction("callCPPFunction", "fadeSound", "0", 0)
	elseif sureExit_curSel == 2 then
		cfunction("transitionToState", "pauseState")	
	end
end
	
function sureExit_onSubmitPressed()
	if sureExit_alreadyPressed == 0  then
		cfunction("showFontOverlay", "menu_content", 0, 100, 0, 100)
		closePopupWindow("sureExit_afterPopupClosed")
		sureExit_alreadyPressed = 1
	end
end

function sureExit_onCancelPressed()
	if sureExit_alreadyPressed == 0  then
		cfunction("showFontOverlay", "menu_content", 0, 100, 0, 100)
		closePopupWindow("sureExit_afterPopupClosed")
		sureExit_alreadyPressed = 1
		sureExit_curSel = 2
	end
end


function sureExit_onLeaveState()	
end

function sureExit_onEnterState()
	--cfunction("debugPrintF", "sureExit_onEnterState", 0)
	cfunction("enableInput", 0)
	sureExit_curSel = 1
	sureExit_alreadyPressed = 0	
	if flow_gameMode == gameTypesIds["TOURNAMENT"] then
		openPopupWindow("sureExit_showContentAfterOpening", 2.5)	
	else
		openPopupWindow("sureExit_showContentAfterOpening", 1.5)	
	end
end

function sureExit_showContentAfterOpening()
	--cfunction("showBitmapOverlay", "EndOfGameOptions", 1, 100, 240-32, 136-16)	
	cfunction("showFontOverlay", "menu_content", 1, 100, 0, 80)
	cfunction("enableInput", 1)
	sureExit_setSelectionCursor()
end


function sureExit_onDownPressed()
	if sureExit_alreadyPressed == 0  then
		sureExit_curSel = sureExit_curSel +1
		if sureExit_curSel ==3 then 
			sureExit_curSel =1
		end
		sureExit_setSelectionCursor()
	end
end

function sureExit_onUpPressed()
	if sureExit_alreadyPressed == 0  then
		sureExit_curSel = sureExit_curSel -1
		if sureExit_curSel ==0 then 
			sureExit_curSel = 2
		end
		sureExit_setSelectionCursor()
	end		
end

function sureExit_setSelectionCursor()
	xText = ""
	for i = 1, sureExit_numOptions do
		xText = xText .. '\05'
		if sureExit_curSel == i then
			xText = xText .. "@  "
		end
		xText = xText .. sureExit_options[i]
		if sureExit_curSel == i then
			xText = xText .. "  @"
		end
		xText = xText .. "\n"
	end
	
	addText = ""
	if sureExit_restart == 1 then
		addText = "ARE YOU SURE YOU WANT TO RESTART THIS "
	else
        addText = "ARE YOU SURE YOU WANT TO EXIT THIS "
	end
	
	
	if flow_gameMode == gameTypesIds["MULTIPLAYER"] then	
	
		addText = addText.."MULTIPLAYER RACE\n\n"
		
	elseif flow_gameMode == gameTypesIds["ARCADE"] then
	
		addText = addText.."RACE\n\n"
		
	elseif flow_gameMode == gameTypesIds["CHALLENGE"] then			
	
		addText = addText.."CHALLENGE\n\n"
		
	elseif flow_gameMode == gameTypesIds["TIMETRIAL"] then
		addText = addText.."TIMETRIAL\n\n"
	elseif flow_gameMode == gameTypesIds["TUTORIAL"] then
		addText = addText.."TUTORIAL\n\n"
	elseif flow_gameMode == gameTypesIds["TOURNAMENT"] then
		addText = addText.."TOURNAMENT\n\n"
	end
	
	xText = addText..xText
	
	if flow_gameMode == gameTypesIds["TOURNAMENT"] then
		xText = xText.."\n\nYOUR PROGRESS\nIN THIS TOURNAMENT WILL BE LOST"
	else
		xText = "\n\n"..xText
	end
	cfunction("setFontOverlayText", "menu_content", xText)
end
