--timetrial result
timetrialResult_options = {lng_TIMETRIALRESULTRETRY, lng_TIMETRIALRESULTMENU}
timetrialResult_numOptions = 2
timetrialResult_mainText = ""

function timetrialResult_onDownPressed()
	if timetrialResult_curSel == 1 then 
		timetrialResult_curSel = 2
	else
		timetrialResult_curSel = 1
	end
	timetrialResult_setSelectionCursor()
end

function timetrialResult_onUpPressed()
	if timetrialResult_curSel == 1 then 
		timetrialResult_curSel = 2
	else
		timetrialResult_curSel = 1
	end
	timetrialResult_setSelectionCursor()
end

function timetrialResult_onSubmitPressed()			
	cfunction("enableInput", 0)
	cfunction("showFontOverlay", "menu_content", 0, 100, 0, 100)		
	closePopupWindow("timetrialResult_onAfterClosePopup")	
end

function timetrialResult_onAfterClosePopup()
	destroyPopup("timetrialResult_onAfterDestroyPopup", PopupOptions["FADETOLOADING"])
	cfunction("callCPPFunction", "fadeSound", "0", 0)
end

function timetrialResult_onAfterDestroyPopup()
	if timetrialResult_curSel == 1 then
		cfunction("exitScriptSystem", UIReturnCodes["TIMETRIAL_RESTART"])
	else
		cfunction("exitScriptSystem", UIReturnCodes["TIMETRIAL_EXIT"])
	end
end

function timetrialResult_onEnterState()		
	timetrialResult_curSel = 1
	timetrialResult_newRecord = 0
	timetrialResult_newRecordMin = 0
	timetrialResult_newRecordSec = 0
	timetrialResult_newRecordHSec = 0

	cfunction("callCPPFunction", "timetrial_createResultScreen", "timetrialResult_newRecord", 0)
	cfunction("enableInput", 0)
	if timetrialResult_failed == 0 then
		createWindowFrame("timetrialResult_afterWindowOpen", 3.0, 0)
	else
		createWindowFrame("timetrialResult_afterWindowOpen", 1.5, 0)
	end
end

function timetrialResult_afterWindowOpen()
	cfunction("enableInput", 1)
	cfunction("showFontOverlay", "menu_content", 1, 100, 0, 105)
	
	if timetrialResult_newRecord == 1 then		
		timetrialResult_mainText = '\05'..lng_TIMETRIALRESULTRECORD.." "..getReadableTime(timetrialResult_newRecordMin, timetrialResult_newRecordSec, timetrialResult_newRecordHSec).."\n\n"
	else
		timetrialResult_mainText = '\05'..lng_TIMETRIALRESULTNORECORD.."\n\n"		
	end
	
	timetrialResult_setSelectionCursor()
	
end

function timetrialResult_onLeaveState()

end

function timetrialResult_setSelectionCursor()
	xText = timetrialResult_mainText
	for i = 1, timetrialResult_numOptions do
		xText = xText .. '\05'
		if timetrialResult_curSel == i then
			xText = xText .. "@  "
		end
		xText = xText .. timetrialResult_options[i]
		if timetrialResult_curSel == i then
			xText = xText .. "  @"
		end
		xText = xText .. "\n"
	end
	cfunction("setFontOverlayText", "menu_content", xText)
end
