challengeResult_options = {lng_CHALRESULTRETRY, lng_CHALRESULTMENU}
challengeResult_medalNames = {lng_MEDALNAME1, lng_MEDALNAME2, lng_MEDALNAME3}

challengeResult_numOptions = 2
challengeResult_mainText = ""
challengeResult_winMedal = 0

function challengeResult_onDownPressed()
	if challengeResult_curSel == 1 then 
		challengeResult_curSel = 2
	else
		challengeResult_curSel = 1
	end
	challengeResult_setSelectionCursor()
end

function challengeResult_onUpPressed()
	if challengeResult_curSel == 1 then 
		challengeResult_curSel = 2
	else
		challengeResult_curSel = 1
	end
	challengeResult_setSelectionCursor()
end

function challengeResult_onSubmitPressed()			
	cfunction("enableInput", 0)
	cfunction("showFontOverlay", "menu_content", 0, 100, 0, 100)	
	--destroyPopup("challengeResult_onAfterDestroyPopup", PopupOptions["FADETOLOADING"])
	closePopupWindow("challengeResult_onAfterClosePopup")	
end

function challengeResult_onAfterClosePopup()
	destroyPopup("challengeResult_onAfterDestroyPopup", PopupOptions["FADETOLOADING"])
	cfunction("callCPPFunction", "fadeSound", "0", 0)
end

function challengeResult_onAfterDestroyPopup()
	if challengeResult_curSel == 1 then
		cfunction("exitScriptSystem", UIReturnCodes["CHALLENGE_RESTART"])
	else
		cfunction("exitScriptSystem", UIReturnCodes["CHALLENGE_EXIT"])
	end
end

function challengeResult_onEnterState()		
	challengeResult_curSel = 1
	challengeResult_winMedal = 0
	cfunction("enableInput", 0)
	if challengeResult_failed == 0 then
		createWindowFrame("challengeResult_afterWindowOpen", 3.0, 0)
	else
		createWindowFrame("challengeResult_afterWindowOpen", 1.5, 0)
	end
end

function challengeResult_afterWindowOpen()
	cfunction("enableInput", 1)
	if challengeResult_failed == 0 then
		cfunction("showFontOverlay", "menu_content", 1, 100, 0, 100)	
		if challengeResult_winMedal > 0 then
			challengeResult_mainText =  '\05'.. string.format(lng_CHALRESULTWONMEDAL, challengeResult_medalNames[challengeResult_winMedal]) .. "\n\n"
			
		else
			challengeResult_mainText =  '\05'..lng_CHALRESULTWONNOTHING.."\n\n"			
		end
		
		if challengeResult_winMedal < 3 and  challengeResult_winMedal > 0 then
			challengeResult_options[1] = string.format(lng_CHALRESULTRACEMEDAL, challengeResult_medalNames[challengeResult_winMedal+1])
		else
			challengeResult_options[1] = lng_CHALRESULTRACEAGAIN
		end		
	else
		cfunction("showFontOverlay", "menu_content", 1, 100, 0, 105)
		challengeResult_mainText = '\05'..lng_CHALRESULTFAILED.."\n\n"
	end
	challengeResult_setSelectionCursor()
end

function challengeResult_onLeaveState()

end

function challengeResult_setSelectionCursor()
	xText = challengeResult_mainText
	for i = 1, challengeResult_numOptions do
		xText = xText .. '\05'
		if challengeResult_curSel == i then
			xText = xText .. "@  "
		end
		xText = xText .. challengeResult_options[i]
		if challengeResult_curSel == i then
			xText = xText .. "  @"
		end
		xText = xText .. "\n"
	end
	cfunction("setFontOverlayText", "menu_content", xText)
end
