resultList_define_initialDelay = 2000
resultList_define_fadeInItemTime = 200
resultList_define_itemMoveTime = 300
resultList_define_betweenItemMoveDelay = 10
resultList_define_lineHeight = 18
resultList_define_startItems = 70


resultList_fontOverlayItems = {"track_resultListItem1", "track_resultListItem2", "track_resultListItem3", "track_resultListItem4","track_resultListItem5","track_resultListItem6","track_resultListItem7", "track_resultListItem8", "track_resultListItem9"}

resultListStates = { NONE = 1, FADEIN = 2, OPEN =3, MOVING = 4, FADEOUT = 5}

resultList_curState = resultListStates["NONE"]

resultList_data = {}
resultList_dataHeader = ""

resultList_numberOfOpponents = 0
resultList_playerIndex = 0
resultList_scrollIndex = 0
resultList_isDynamic = 0

function resultList_setNumberOfOpponents(i)
	if resultList_isDynamic == 0 then
		if resultList_curState == resultListStates["NONE"] then
		else
			cfunction("debugPrintF", "[resultList_setNumberOfOpponents] Can not set numberOfOpponents while state != NONE. State = ", resultList_curState)		
		end
	end
	resultList_numberOfOpponents = i
	
end

function resultList_setDynamic(i)
	if resultList_curState == resultListStates["NONE"] then
	else
		cfunction("debugPrintF", "[resultList_setDynamic] Can not set Dynamic while state != NONE. State = ", resultList_curState)		
	end
	resultList_isDynamic = i
end
function resultList_setHeader(s)
	if resultList_isDynamic == 0 then
		if resultList_curState == resultListStates["NONE"] then
		else
			cfunction("debugPrintF", "[resultList_setHeader] Can not set dataHeader while state != NONE. State = ", resultList_curState)		
		end
	end
	resultList_dataHeader = s
end

function resultList_setPlayerIndex(i)
	if resultList_isDynamic == 0 then
		if resultList_curState == resultListStates["NONE"] then
		else
			cfunction("debugPrintF", "[resultList_setPlayerIndex] Can not set playerIndex while state != NONE. State = ", resultList_curState)		
		end
	end
	resultList_playerIndex = i
end

function resultList_inputUp()
	if resultList_isDynamic == 0 then
		if resultList_curState == resultListStates["OPEN"] then
			if resultList_numberOfOpponents > 8 then
				if resultList_scrollIndex > 0 then
					resultList_curState = resultListStates["MOVING"]
					resultList_moveOneRowDown()
				end
			end
		end
	else
		cfunction("debugPrintF", "[resultList_inputUp] Can not set input while result list is dynamic.")		
	end
end

function resultList_inputDown()	
	if resultList_isDynamic == 0 then
		if resultList_curState == resultListStates["OPEN"] then	
			if resultList_numberOfOpponents > 8 then
				if resultList_scrollIndex == 0 then
					resultList_curState = resultListStates["MOVING"]
					resultList_moveOneRowUp()			
				end
			end
		end
	else	
		cfunction("debugPrintF", "[resultList_inputDown] Can not set input while result list is dynamic.")		
	end
end

function resultList_hide()
	iMaxRows = 9
	if resultList_numberOfOpponents < iMaxRows then
		iMaxRows = resultList_numberOfOpponents
	end
	
	for i = 1, iMaxRows do
		cfunction("showFontOverlay", resultList_fontOverlayItems[i], 0,0,0,0)
	end
	cfunction("showFontOverlay", "track_resultListHeader", 0,0,0,0)
	resultList_curState = resultListStates["NONE"]
end

function resultList_show()
	if resultList_curState == resultListStates["NONE"] then
	else
		cfunction("debugPrintF", "[0] Invalid resultList State: ", resultList_curState)		
	end
	
	cfunction("showFontOverlay", "track_resultListHeader", 1,100,0,resultList_define_startItems)
	cfunction("setFontOverlayText","track_resultListHeader", resultList_dataHeader)

	resultList_curState = resultListStates["OPEN"]
	
	if resultList_numberOfOpponents > 8 then			
		if resultList_playerIndex > 8 then
			resultList_scrollIndex =0		
			cfunction("setTimer", resultList_define_initialDelay, "resultList_moveOneRowUp")				
		else
			resultList_scrollIndex = resultList_numberOfOpponents-8
			cfunction("setTimer", resultList_define_initialDelay, "resultList_moveOneRowDown")				
		end
		resultList_curState = resultListStates["MOVING"]
		
		for i = 1, 8 do

			xText = resultList_data[i+resultList_scrollIndex]
			if i+resultList_scrollIndex == resultList_playerIndex then
				xText = '\02'..'099'..xText			
			end

			cfunction("showFontOverlay", resultList_fontOverlayItems[i], 1,0,0,resultList_define_startItems+i*resultList_define_lineHeight)
			cfunction("setFontOverlayText",resultList_fontOverlayItems[i], xText)
			cfunction("fadeOverlay", resultList_fontOverlayItems[i], 0, 100, 100*i)
		end
	else --not more then 8 opponents
		for i = 1, resultList_numberOfOpponents do			
			xText = resultList_data[i]
			if i == resultList_playerIndex then
				xText = '\02'..'099'..xText			
			end

			cfunction("showFontOverlay", resultList_fontOverlayItems[i], 1,0,0,resultList_define_startItems+i*resultList_define_lineHeight)
			cfunction("setFontOverlayText",resultList_fontOverlayItems[i], xText)
			cfunction("fadeOverlay", resultList_fontOverlayItems[i], 0, 100, 100*i)
		end
	end
end





function resultList_moveOneRowUp()
	if resultList_curState == resultListStates["MOVING"] then
		cfunction("fadeOverlay", resultList_fontOverlayItems[1], 100, 0, resultList_define_fadeInItemTime)

		xText = resultList_data[9+resultList_scrollIndex]
		if resultList_scrollIndex+9 == resultList_playerIndex then
			xText = '\02'..'099'..xText		
		end

		cfunction("showFontOverlay", resultList_fontOverlayItems[9], 1,0,0,resultList_define_startItems+9*resultList_define_lineHeight)
		cfunction("setFontOverlayText",resultList_fontOverlayItems[9], xText)
		cfunction("fadeOverlay", resultList_fontOverlayItems[9], 0, 100, resultList_define_fadeInItemTime)	


		for i = 1, 9 do
			cfunction("setOverlayDestination", resultList_fontOverlayItems[i], 0, resultList_define_startItems+(i-1)*resultList_define_lineHeight, resultList_define_itemMoveTime)
		end
		resultList_scrollIndex = resultList_scrollIndex +1
		cfunction("setTimer", resultList_define_betweenItemMoveDelay+resultList_define_itemMoveTime, "resultList_moveOneRowUpDone")
	end
end



function resultList_moveOneRowDown()	
	if resultList_curState == resultListStates["MOVING"] then
		cfunction("fadeOverlay", resultList_fontOverlayItems[8], 100, 0, resultList_define_fadeInItemTime)

		xText = resultList_data[resultList_scrollIndex]
		if resultList_scrollIndex == resultList_playerIndex then
			xText = '\02'..'099'..xText		
		end

		cfunction("showFontOverlay", resultList_fontOverlayItems[9], 1,0,0,resultList_define_startItems)
		cfunction("setFontOverlayText",resultList_fontOverlayItems[9], xText)
		cfunction("fadeOverlay", resultList_fontOverlayItems[9], 0, 100, resultList_define_fadeInItemTime)	


		cfunction("setOverlayDestination", resultList_fontOverlayItems[9], 0, resultList_define_startItems+resultList_define_lineHeight, resultList_define_itemMoveTime)
		for i = 1, 8 do
			cfunction("setOverlayDestination", resultList_fontOverlayItems[i], 0, resultList_define_startItems+(i+1)*resultList_define_lineHeight, resultList_define_itemMoveTime)
		end
		resultList_scrollIndex = resultList_scrollIndex -1

		cfunction("setTimer", resultList_define_betweenItemMoveDelay+resultList_define_itemMoveTime, "resultList_moveOneRowDownDone")
	end
end	

function resultList_moveOneRowUpDone()
	if resultList_curState == resultListStates["MOVING"] then
		resultList_setCorrectTextToRows()	
		if 8 + resultList_scrollIndex < resultList_numberOfOpponents then
			resultList_moveOneRowUp()
		else
			resultList_curState = resultListStates["OPEN"]
		end
	end
end

function resultList_moveOneRowDownDone()
	if resultList_curState == resultListStates["MOVING"] then
		resultList_setCorrectTextToRows()	
		if resultList_scrollIndex == 0 then
			resultList_curState = resultListStates["OPEN"]		
		else
			resultList_moveOneRowDown()
		end
	end
end

function resultList_setCorrectTextToRows()	
	iMaxRows = 8
	if resultList_numberOfOpponents < iMaxRows then
		iMaxRows = resultList_numberOfOpponents
	end
	for i = 1, iMaxRows do
		cfunction("showFontOverlay", resultList_fontOverlayItems[i], 1,100,0,resultList_define_startItems+i*resultList_define_lineHeight)
		cfunction("stopOverlayDestination", resultList_fontOverlayItems[i])
		xText = resultList_data[i+resultList_scrollIndex]
		if i+resultList_scrollIndex == resultList_playerIndex then
			xText = '\03'..'3'..'\02'..'099'..xText
		end
		cfunction("setFontOverlayText",resultList_fontOverlayItems[i], xText)		
	end
	cfunction("showFontOverlay", resultList_fontOverlayItems[9], 0,0,0,0)
	
end

function resultList_reloadData()
	if resultList_curState == resultListStates["OPEN"] then
		resultList_setCorrectTextToRows()
		cfunction("showFontOverlay", "track_resultListHeader", 1,100,0,resultList_define_startItems)
		cfunction("setFontOverlayText","track_resultListHeader", resultList_dataHeader)
	else
		cfunction("debugPrintF", "resultList_reloadData() failed because state is not open but: ", resultList_curState)
	end
end	
