------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------

if ( CTraceEffectMovie == nil ) then
	CTraceEffectMovie =
	{
		s_iEffectCount = 0
	}
end

function CTraceEffectMovie:Create( i_oParent, i_sEffectName, i_sMovieName, iOpt_fDur, iOpt_mIrpFunc, i_Opt_bFadeOnDeath )

    local oRet = i_oParent:CreateInstance( i_sMovieName, i_sMovieName .. CTraceEffectMovie.s_iEffectCount )
	CTraceEffectMovie.s_iEffectCount = CTraceEffectMovie.s_iEffectCount + 1
    
    -- inherit some class behaviors
	CIrpMan:Attach( oRet )
	
	-- bind into our class
	CTraceEffectMovie:Bind( oRet )
	
	-- initialization
	oRet:Init( i_oParent, i_sEffectName, i_sMovieName, iOpt_fDur, iOpt_mIrpFunc, i_Opt_bFadeOnDeath )
	
	return oRet
end

function CTraceEffectMovie:SetDeleteHoldTime( i_fSetDeleteHoldTime )
	self.m_fDeleteHoldTime = i_fSetDeleteHoldTime
end

function CTraceEffectMovie:Init( i_oParent, i_sEffectName, i_sMovieName, iOpt_fDur, iOpt_mIrpFunc, i_Opt_bFadeOnDeath )

	self.m_bFadeOnDeath = i_Opt_bFadeOnDeath
	if ( self.m_bFadeOnDeath == nil ) then
		self.m_bFadeOnDeath = false
	end

	self.m_fFadeOnDeathSpeed = 0.02
	self.m_bReadyForDelete = false
	self.m_aEffectNames = {}
	self.m_fDeleteHoldTime = 0.0
	
	-- if no duration specified, just use the frame duration of the movie
	if ( iOpt_fDur == nil ) then	
		iOpt_fDur = self:GetNumFrames() / 60
	end
	
	if ( iOpt_mIrpFunc == nil ) then
		iOpt_mIrpFunc = CIrpFuncs.mLinear
	end
	
	local iMarkerCount = 0
	local oMarker = self[ "_marker" ]
	
	if ( oMarker == nil ) then
		oMarker = self[ "_marker" .. iMarkerCount ]
	end
	
	assert( oMarker ~= nil, "Couldn't find first marker of movie '" .. i_sMovieName .. "'!" )

	self:SetOnUpdateEnabled( true )
	
	while ( oMarker ~= nil ) do

		-- create the frame irp
		local oIrp = self:CreateIrp( 0, self:GetNumFrames() - 1, iOpt_fDur, iOpt_mIrpFunc, 0 )
		oIrp.m_oHookContext = self
		oIrp.m_sMarkerName = oMarker:GetName()
		oIrp.m_iLastFrame = self:GetFrame()
		
		-- create the effect
		oIrp.m_oEffect = self:CreateParticleEffect( i_sEffectName, "", oMarker:GetX(), oMarker:GetY() );

		table.insert( self.m_aEffectNames, oIrp.m_oEffect:GetName() )

		-- set initial position		
		oIrp.m_oEffect:SelectRootParticle()
		oIrp.m_oEffect:SetSelectedParticlePosition( oMarker:GetX(), oMarker:GetY(), true )

		oIrp.mOnChange = function( i_oIrp, i_oContext )
			local curFrame = i_oContext:GetFrame()
			local nextFrame = math.floor( i_oIrp:GetVal() )
	
			-- only one irp need set this		
			if ( curFrame ~= nextFrame ) then
				i_oContext:SetFrame( math.max( 0, math.min( i_oContext:GetNumFrames(), nextFrame ) ) )
			end	
			
			-- all irps must check to see if on new frame
			if ( i_oIrp.m_iLastFrame ~= nextFrame ) then
				
				i_oIrp.m_iLastFrame = nextFrame
				local oMarker = self[ i_oIrp.m_sMarkerName ]
				
				if ( oMarker == nil ) then
					assert( oMarker ~= nil, "Couldn't find marker in frame " .. nextFrame .. " of movie '" .. i_sMovieName .. "', marker '" .. i_oIrp.m_sMarkerName .. "'!" )
				else
log( "::" .. nextFrame .. ":" .. oMarker:GetX() .. "," .. oMarker:GetY() )
					i_oIrp.m_oEffect:SelectRootParticle()
					i_oIrp.m_oEffect:SetSelectedParticlePosition( oMarker:GetX(), oMarker:GetY(), true)
				end
			end		
		end
		
		oIrp.mOnComplete = function( i_oIrp, i_oContext )
			i_oIrp.m_oEffect:DestroyAutoEmitParticle( "Root" )

			-- now flag for self deletion
			i_oContext.m_bReadyForDelete = true
		end
		
		-- keep looking for markers
		iMarkerCount = iMarkerCount + 1
		oMarker = self[ "_marker" .. iMarkerCount ]
	end
	
end

function CTraceEffectMovie:Kill( i_bImmediate )

	self:KillAllIrps()
	
	if ( i_bImmediate ) then
		self:DeleteSelf()
	else
		for i = 1, #self.m_aEffectNames, 1 do
			local oEffect = self[ self.m_aEffectNames[ i ] ]
			if ( oEffect ~= nil ) then
				oEffect:DestroyAutoEmitParticle( "Root" )
			end
		end
		self.m_bReadyForDelete = true
	end
end

function CTraceEffectMovie:OnUpdate()

	if ( self.m_bReadyForDelete ) then

		local bDone = true

		if ( self.m_fDeleteHoldTime > 0 ) then
			bDone = false

			--if ( self.m_fDeleteHoldTime < 0.55 ) then
			--	self:SetOpacity( math.min( 1, self:GetOpacity() + 0.05 ) )
			--end
			
			self.m_fDeleteHoldTime = self.m_fDeleteHoldTime - 1 / 60
			
		
		elseif ( self.m_bFadeOnDeath ) then
			self:SetOpacity( math.max( 0, self:GetOpacity() - self.m_fFadeOnDeathSpeed ) )
			
			if ( self:GetOpacity() ~= 0 ) then
				bDone = false
			end
				
		else
			for i = #self.m_aEffectNames, 1, -1 do
				local oEffect = self[ self.m_aEffectNames[ i ] ]
				if ( oEffect ~= nil ) then
					bDone = false
				else
					table.remove( self.m_aEffectNames, i )
				end
			end
		end
			
		if ( bDone ) then
		log( "CLEAN!" )
			self:DeleteSelf()
		end
	else
		self:UpdateIrps( 1 / 60 )
	end
end

-- call this in C++ after a reload to rebind functions
function CTraceEffectMovie:Bind( i_oObj )
	BindObj( CTraceEffectMovie, i_oObj )
	return i_oObj
end