-- tournamentVictory_

function tournamentVictory_sub_afterCloseWindow()
	if tournamentVictory_curState > tournamentData[flow_tournamentId]["numberOfUnlocks"] then
		destroyPopup("tournamentVictory_afterPopupDestroy", PopupOptions["FADETOLOADING"])
		if tournamentVictory_lastImage then
			cfunction("showBitmapOverlay", tournamentVictory_lastImage, 0, 100, 155, 92)
		end	
	else
		indx = tournamentVictory_curState
		tId = tournamentData[flow_tournamentId]["unlocks"][indx]["id"]
		targetType = tournamentData[flow_tournamentId]["unlocks"][indx]["type"]
		tournamentVictory_unlockType = targetType		
		cfunction("callCPPFunction", "unlockContent", "tournamentVictory_unlockSucces", tId)
		if tournamentVictory_unlockSucces == 1 then
			openPopupWindow("tournamentVictory_sub_showContentAfterOpening", 2.6, 0)
		else
			tournamentVictory_curState = tournamentVictory_curState + 1
			tournamentVictory_sub_afterCloseWindow()
		end
	end
end

function tournamentVictory_sub_showContentAfterOpening()	
	indx = tournamentVictory_curState
	tId = tournamentData[flow_tournamentId]["unlocks"][indx]["id"]
	targetType = tournamentData[flow_tournamentId]["unlocks"][indx]["type"]
	tournamentVictory_unlockType = targetType
	
	
	xText = '\05'
	xTextHelp = '\05'
	xImgString = ""
	if targetType == unlockableTypes["ARCADETRACK"] then
		xText = xText..string.format(lng_TOURNVICTOR1, trackData[tId][2])
		xTextHelp = xTextHelp..lng_TOURNVICTORDESC1		
		xImgString = "unlock_A_"..trackData[tId][1]			

	elseif targetType == unlockableTypes["CHARACTER"] then
		xText = xText..string.format(lng_TOURNVICTOR2, characterData[tId][1])
		xTextHelp = xTextHelp..lng_TOURNVICTORDESC2	
		xImgString = characterData[tId][2]			

	elseif targetType == unlockableTypes["TOURNAMENT"] then
		xText = xText..string.format(lng_TOURNVICTOR3, tournamentData[tId]["name"])
		xTextHelp = xTextHelp..lng_TOURNVICTORDESC3		
		xImgString = tournamentData[tId]["unlockImg"]			

	elseif targetType == unlockableTypes["CHALLENGE"] then
		xText = xText..string.format(lng_TOURNVICTOR4, challengeData[tId][1])
		xTextHelp = xTextHelp..lng_TOURNVICTORDESC4		
		xImgString = challengeData[tId][2]

	elseif targetType == unlockableTypes["STATEMORPH"] then
		xText = xText..string.format(lng_TOURNVICTOR5, statemorphData[tId])
		xTextHelp = xTextHelp..lng_TOURNVICTORDESC5
		xImgString = "unlock_S_"..statemorphData[tId]						

	elseif targetType == unlockableTypes["VEHICLELEVEL"] then
		xText = xText..lng_TOURNVICTOR6
		xTextHelp = xTextHelp..lng_TOURNVICTORDESC6
		xImgString = "Unlock_U_Upgrade0"..tId-1
	else
		xText = xText.."YOU HAVE UNLOCK SOMETHING\nDONT KNOW WHAT\nFIXME\nTYPE IS "..targetType
		xText = xText.." \nID "..tId
	end
	--[[
	cfunction("showBitmapOverlay", xImgString, 1, 0, 160, 96)
	cfunction("fadeOverlay", xImgString, 0, 100, 300)
	cfunction("scaleBitmapOverlay",xImgString, 1, 1, 1.25, 1.25, 0)
	--]]
	cfunction("showBitmapOverlay", xImgString, 1, 0, -16, 8)
	cfunction("enableBitmapOverlayShading", xImgString, 1)
	
	cfunction("fadeOverlay", xImgString, 0, 100, 200)
	cfunction("scaleBitmapOverlay",xImgString, 4, 4, 1.5, 1.5, 200)
	cfunction("setOverlayDestination", xImgString, 160,90,200)
	cfunction("startSoundSample", "unlockImpact", 1, 100)
	
	
	tournamentVictory_lastImage = xImgString			

	cfunction("showFontOverlay", "menu_content", 1,100,0,67)
	cfunction("showFontOverlay", "tournamentStatus", 1,100,0,233)
	
	
	cfunction("setFontOverlayText", "menu_content", xText)
	cfunction("setFontOverlayText", "tournamentStatus", xTextHelp)
	cfunction("enableInput", 1)
end

function tournamentVictory_onSubmitPressed()
	cfunction("showFontOverlay", "menu_content", 0,100,0,100)
	cfunction("showFontOverlay", "tournamentStatus", 0,100,0,100)
		
	if tournamentVictory_lastImage then
		cfunction("showBitmapOverlay", tournamentVictory_lastImage, 0, 100, 155, 92)
	end		
	
	tournamentVictory_curState = tournamentVictory_curState +1
	cfunction("enableInput", 0)
	closePopupWindow("tournamentVictory_sub_afterCloseWindow")	
end

function tournamentVictory_afterPopupDestroy()
	cfunction("callCPPFunction", "tournamentCompleted", "", flow_tournamentCurrentPointsPlayer)
	cfunction("exitScriptSystem", UIReturnCodes["TOURNAMENT_EXIT"])
end

function tournamentVictory_onEnterState()		
	cfunction("enableInput", 0)
	openPopupWindow("tournamentVictory_showContentAfterOpening", 2.8, 0)
end

function tournamentVictory_showContentAfterOpening() 
	tournamentVictory_lastImage = NIL
	tournamentVictory_curState = 0	
	cfunction("enableInput", 1)
	cfunction("showFontOverlay", "menu_content", 1,100,0,60)
	cfunction("showFontOverlay", "tournamentStatus", 1,100,0,233)
	xText = '\05'..lng_TOURNVICTORCONGRAT.."\n\n\n\n\n\n\n\n\n\n\n"..tournamentData[flow_tournamentId]["name"].." "..lng_TOURNRESULTNAME
	cfunction("setFontOverlayText", "menu_content", xText)	
	cfunction("setFontOverlayText", "tournamentStatus", "")
	xImgString = tournamentData[flow_tournamentId]["finishedImg"]
	cfunction("showBitmapOverlay", xImgString, 1, 100, 177, 72)
	tournamentVictory_lastImage = xImgString
	
end

function tournamentVictory_onLeaveState()		
end