PopupStates = { NONE = 1, CREATING = 2, CLOSED = 3, OPENING = 4, OPEN = 5, CLOSING = 6, DESTROYING = 7}
PopupOptions = {FADETOBLACK = 1, FADETONORMAL = 2, FADETOLOADING = 3}

popup_currentWindow = {}
popup_currentWindow.state = PopupStates["NONE"]

popup_define_frameContentHeight = 64
popup_define_frameTopHeight = 8

popup_define_frameFadeInTime = 250
popup_define_openingTime = 250

function createWindowFrame(sContentCallback, iHeight, iOffset)
	if popup_currentWindow.state == PopupStates["NONE"] then	
		popup_currentWindow.state = PopupStates["CREATING"]
		popup_currentWindow.paneHeightModifier  = iHeight
		popup_currentWindow.extraOffset = iOffset 
		
		cfunction("showBitmapOverlay", "menuBgBlack", 1,0,0,0)	
		cfunction("fadeOverlay", "menuBgBlack", 0,50,popup_define_frameFadeInTime)	
		cfunction("scaleBitmapOverlay", "menuBgBlack", 1, 1, 480, 272, 0)
		
		cfunction("showBitmapOverlay", "frameTop", 1,0,0,136-popup_define_frameTopHeight+popup_currentWindow.extraOffset)	
		cfunction("scaleBitmapOverlay", "frameTop", 1,1,1.875,1,0)	
		cfunction("fadeOverlay", "frameTop", 0,100,popup_define_frameFadeInTime)	
		
		cfunction("showBitmapOverlay", "frameBottom", 1,0,0,136+popup_currentWindow.extraOffset)	
		cfunction("scaleBitmapOverlay", "frameBottom", 1,1,1.875,1,0)	
		cfunction("fadeOverlay", "frameBottom", 0,100,popup_define_frameFadeInTime)		
		
		cfunction("setStringTimer", popup_define_frameFadeInTime, "popupCallback", sContentCallback)
	else
		cfunction("debugPrintF", "Creating window but CurrentState is not NONE: ", popup_currentWindow.state)
	end
end

function destroyPopup(sCallback, iOptions)	
	if popup_currentWindow.state == PopupStates["CLOSED"] then	
		popup_currentWindow.state = PopupStates["DESTROYING"]
		cfunction("fadeOverlay", "frameTop", 100,0,popup_define_frameFadeInTime)	
		cfunction("fadeOverlay", "frameBottom", 100,0,popup_define_frameFadeInTime)	
		if iOptions == PopupOptions["FADETONORMAL"] then
			cfunction("fadeOverlay", "menuBgBlack", 50,0,popup_define_frameFadeInTime)	
		elseif iOptions == PopupOptions["FADETOBLACK"] then
			cfunction("fadeOverlay", "menuBgBlack", 50,100,popup_define_frameFadeInTime)	
		elseif iOptions == PopupOptions["FADETOLOADING"] then
			cfunction("fadeOverlay", "menuBgBlack", 50,100,popup_define_frameFadeInTime)	
			--cfunction("showBitmapOverlay", "generalLoading", 1, 0, 176, 120)
			--cfunction("fadeOverlay", "generalLoading", 0,100,popup_define_frameFadeInTime)	
		else
			cfunction("debugPrintF", "destroyPopup: Unknown options is ", iOptions)
		end
				
		cfunction("setStringTimer", popup_define_frameFadeInTime, "popupCallback", sCallback)					
	else
		cfunction("debugPrintF", "destroyPopup: Unknown CurrentState is ", popup_currentWindow.state)
	end
end

function openPopupWindow(sContentCallback, iHeight, iOffset)	
	popup_currentWindow.state = PopupStates["OPENING"]	
	popup_currentWindow.contentCallback = sContentCallback
	
	if iHeight == -1 then
		--use current height
	else
		popup_currentWindow.paneHeightModifier = iHeight
	end	
	
	if iOffset == -1 or iOffset == NIL then
		--use current offset
	else
		popup_currentWindow.extraOffset = iOffset
	end
	
	pcwphm = popup_currentWindow.paneHeightModifier
	cfunction("showBitmapOverlay", "frameContent", 1,100,0,136+popup_currentWindow.extraOffset)	
	--cfunction("scaleBitmapOverlay", "frameContent", 1, 0, 1.875, 1, 0)
	cfunction("scaleBitmapOverlay", "frameContent", 1.875, 0, 1.875, pcwphm, popup_define_openingTime)
	cfunction("setOverlayDestination", "frameContent", 0, 136-(popup_define_frameContentHeight*pcwphm*0.5)+popup_currentWindow.extraOffset, popup_define_openingTime)
	cfunction("setOverlayDestination", "frameTop", 0, 136-popup_define_frameTopHeight-(popup_define_frameContentHeight*0.5*pcwphm)+popup_currentWindow.extraOffset, popup_define_openingTime)
	cfunction("setOverlayDestination", "frameBottom", 0, 136+(popup_define_frameContentHeight*0.5*pcwphm)+popup_currentWindow.extraOffset, popup_define_openingTime)
	cfunction("setNumberTimer", popup_define_openingTime, "popupCallback", popup_numberOfActiveWindows)		
	cfunction("setTimer", popup_define_openingTime, popup_currentWindow.contentCallback)				
end

function closePopupWindow(sCallback)
	if popup_currentWindow.state == PopupStates["OPEN"] then	
		popup_currentWindow.state = PopupStates["CLOSING"]
		popup_currentWindow.autoDestroy = 0
		pcwphm = popup_currentWindow.paneHeightModifier
		
		cfunction("setOverlayDestination", "frameTop", 0, 136-popup_define_frameTopHeight+popup_currentWindow.extraOffset, popup_define_openingTime)
		cfunction("setOverlayDestination", "frameBottom", 0, 136+popup_currentWindow.extraOffset, popup_define_openingTime)
		cfunction("setOverlayDestination", "frameContent", 0, 136+popup_currentWindow.extraOffset, popup_define_openingTime)
		cfunction("scaleBitmapOverlay", "frameContent", 1.875, pcwphm, 1.875, 0, popup_define_openingTime)
		cfunction("setStringTimer", popup_define_openingTime, "popupCallback", sCallback)		
	else
		cfunction("debugPrintF", "closePopupWindow: Unknown CurrentState is ", popup_currentWindow.state)
	end
end

function closeAndDestroyPopupWindow(sCallback, iOptions)	
	if popup_currentWindow.state == PopupStates["OPEN"] then			
		popup_currentWindow.state = PopupStates["CLOSING"]
		popup_currentWindow.autoDestroy = 1
		popup_currentWindow.autoDestroyOptions = iOptions
		
		pcwphm = popup_currentWindow.paneHeightModifier
		
		cfunction("setOverlayDestination", "frameTop", 0, 136-popup_define_frameTopHeight+popup_currentWindow.extraOffset, popup_define_openingTime)
		cfunction("setOverlayDestination", "frameBottom", 0, 136+popup_currentWindow.extraOffset, popup_define_openingTime)
		cfunction("setOverlayDestination", "frameContent", 0, 136+popup_currentWindow.extraOffset, popup_define_openingTime)
		cfunction("scaleBitmapOverlay", "frameContent", 1.875, pcwphm, 1.875, 0, popup_define_openingTime)
		cfunction("setStringTimer", popup_define_openingTime, "popupCallback", sCallback)		
	else
		cfunction("debugPrintF", "closeAndDestroyPopupWindow: Wrong CurrentState: ", popup_currentWindow.state)
	end
end

function instantRemovePopupWindow()	
	popup_currentWindow.state = PopupStates["NONE"]	
	cfunction("showBitmapOverlay", "menuBgBlack", 0,0,0,0)	
	cfunction("showBitmapOverlay", "frameTop", 0,0,112,104)	
	cfunction("showBitmapOverlay", "frameContent", 0,0,112,104)	
	cfunction("showBitmapOverlay", "frameBottom", 0,0,112,136)		
end



function popupCallback(i)	
	if popup_currentWindow.state == PopupStates["CREATING"] then	
		openPopupWindow(i, -1)
	elseif popup_currentWindow.state == PopupStates["OPENING"] then	
		popup_currentWindow.state = PopupStates["OPEN"]
		--cfunction("debugPrintF", "Window open: enable input", 0)		
	elseif popup_currentWindow.state == PopupStates["DESTROYING"] then	
		--cfunction("debugPrintF", "Done destroying calling "..i, 0)
		cfunction("setTimer", 1, i)
		popup_currentWindow.state = PopupStates["NONE"]		
	elseif popup_currentWindow.state == PopupStates["CLOSING"] then	
		popup_currentWindow.state = PopupStates["CLOSED"]
		cfunction("showBitmapOverlay", "frameContent", 0,100,330,82)		
		if popup_currentWindow.autoDestroy == 1 then
			destroyPopup(i, popup_currentWindow.autoDestroyOptions)
		else
			cfunction("setTimer", 1, i)		
		end		
	else
		cfunction("debugPrintF", "Unknown CurrentState is ", popup_currentWindow.state)
	end
end

function getReadableTime(iMin, iSec, iHSec)
	outTxt = ""..iMin..":"
	if iSec < 10 then
		outTxt = outTxt.."0"..iSec..":"
	else
		outTxt = outTxt..iSec..":"
	end

	if iHSec < 10 then
		outTxt = outTxt.."0"..iHSec
	else
		outTxt = outTxt..iHSec
	end
	return outTxt
end
