--------------------------------------------------------------------------------
-- GLOBALS
--------------------------------------------------------------------------------

-- CONSTANTS
Global( "MESSAGE_FADE_IN_TIME", 350 )
Global( "MESSAGE_FADE_SOLID_TIME", 600 )
Global( "MESSAGE_FADE_OUT_TIME", 1000 )

Global( "WIDGET_FADE_TRANSPARENT", 1 )
Global( "WIDGET_FADE_IN", 2 )
Global( "WIDGET_FADE_SOLID", 3 )
Global( "WIDGET_FADE_OUT", 4 )

-- WIDGETS
Global( "wtMessage", nil )

-- OTHER
Global( "fadeStatus", WIDGET_FADE_TRANSPARENT )
Global( "CounterOn", nil)

--------------------------------------------------------------------------------
-- EVENT HANDLERS
--------------------------------------------------------------------------------


-- EVENT_EFFECT_FINISHED
function OnEventEffectFinished ( params )
	if params.wtOwner:IsEqual( wtMessage ) then
		if fadeStatus == WIDGET_FADE_IN then
			wtMessage:PlayFadeEffect( 1.0, 1.0, MESSAGE_FADE_SOLID_TIME, EA_MONOTONOUS_INCREASE )
			fadeStatus = WIDGET_FADE_SOLID		
		elseif fadeStatus == WIDGET_FADE_SOLID then
			wtMessage:PlayFadeEffect( 1.0, 0.0, MESSAGE_FADE_OUT_TIME, EA_MONOTONOUS_INCREASE )
			fadeStatus = WIDGET_FADE_OUT		
		elseif fadeStatus == WIDGET_FADE_OUT then
			fadeStatus = WIDGET_FADE_TRANSPARENT
			wtMessage:Show( false )
		end
	end
end

-- EVENT_TRACK_REMOVED
function OnEventTrackRemoved( params )
	if CounterOn == nil then
		CounterOn = 60
		common.RegisterEventHandler( Timer, "EVENT_SECOND_TIMER" )
	end
end

-- EVENT_SECOND_TIMER
function Timer ( params )
	if CounterOn ~= nil then
		wtMessage:SetVal( "value", common.FormatInt( CounterOn, "%d" ) )
		--wtMessage:SetVal( "skillname", nil );
	
		wtMessage:Show( true )

		wtMessage:PlayFadeEffect( 0.0, 1.0, MESSAGE_FADE_IN_TIME, EA_MONOTONOUS_INCREASE )
		fadeStatus = WIDGET_FADE_IN
		
		if CounterOn > 0 then
			CounterOn = CounterOn - 1
		else
			CounterOn = nil
			common.UnRegisterEventHandler( Timer, "EVENT_SECOND_TIMER" )
		end
	end
end

-- EVENT_TRACK_ADDED
function OnEventTrackAdded ( params )
	CounterOn = nil
	common.UnRegisterEventHandler( Timer, "EVENT_SECOND_TIMER" )
end


--------------------------------------------------------------------------------
-- INITIALIZATION
--------------------------------------------------------------------------------
function Init()
	common.RegisterEventHandler( OnEventEffectFinished, "EVENT_EFFECT_FINISHED" )
	common.RegisterEventHandler( OnEventTrackRemoved, "EVENT_TRACK_REMOVED" )
	common.RegisterEventHandler( OnEventTrackAdded, "EVENT_TRACK_ADDED" )
	

	wtMessage = mainForm:GetChildChecked( "Announce", false )
	wtMessage:Show( false )
end
--------------------------------------------------------------------------------
Init()
--------------------------------------------------------------------------------