-- GLOBAL
Global( "MESSAGE_FADE_IN_TIME", 350 )
Global( "MESSAGE_FADE_SOLID_TIME", 4000 )
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 )
Global( "wtMessage", nil )
Global( "fadeStatus", WIDGET_FADE_TRANSPARENT )

Global( "ia", 0 )
Global( "ik", 0 )

-- EVENT_UNIT_DAMAGE_RECEIVED
function OnEventDamageReceived( params )
	local id = avatar.GetId()
	local ids = params.source
	local idt = params.target
	
	if id==ids and id~=idt then
		if not params.isCritical then
			wtMessage:SetVal( "skillname", params.ability );
                                                            wtMessage:SetVal( "value", common.FormatInt( params.amount, "%dK5" ) )
			wtMessage:SetVal( "value1", common.GetEmptyWString() )
			ia = ia + 1
		else
			wtMessage:SetVal( "skillname", params.ability );
                                                            wtMessage:SetVal( "value", common.GetEmptyWString() )
			wtMessage:SetVal( "value1", common.FormatInt( params.amount, "%dK5" ) )
			ik = ik + 1
			ia = ia + 1
		end
	
	wtMessage:SetVal( "krit", common.FormatFloat( 100 * ik / ia, "%.1f" ) )

	wtMessage:Show( true )
	wtMessage:PlayFadeEffect( 0.0, 1.0, MESSAGE_FADE_IN_TIME, EA_MONOTONOUS_INCREASE )
	fadeStatus = WIDGET_FADE_IN
	end
end

-- 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

-- INITIALIZATION
function Init()
	common.RegisterEventHandler( OnEventDamageReceived, "EVENT_UNIT_DAMAGE_RECEIVED" )
	common.RegisterEventHandler( OnEventEffectFinished, "EVENT_EFFECT_FINISHED" )
		
	wtMessage = mainForm:GetChildChecked( "Announce", false )
	wtMessage:Show( false )
end
--------
Init()
--------