//-----------------------------------------------------------------------------
// purgeExcessMotion()
//-----------------------------------------------------------------------------
void LLMotionController::purgeExcessMotions()
{
	if (mLoadedMotions.size() > MAX_MOTION_INSTANCES)
	{
		// clean up deprecated motions
		for (motion_set_t::iterator deprecated_motion_it = mDeprecatedMotions.begin(); 
			 deprecated_motion_it != mDeprecatedMotions.end(); )
		{
			motion_set_t::iterator cur_iter = deprecated_motion_it++;
			LLMotion* cur_motionp = *cur_iter;
			if (!isMotionActive(cur_motionp))
			{
				// Motion is deprecated so we know it's not cannonical,
				//  we can safely remove the instance
				removeMotionInstance(cur_motionp); // modifies mDeprecatedMotions
				mDeprecatedMotions.erase(cur_iter);
			}
		}
	}

	std::set<LLUUID> motions_to_kill;
	if (mLoadedMotions.size() > MAX_MOTION_INSTANCES)
	{
		// too many motions active this frame, kill all blenders
		mPoseBlender.clearBlenders();
		for (motion_set_t::iterator loaded_motion_it = mLoadedMotions.begin(); 
			 loaded_motion_it != mLoadedMotions.end(); 
			 ++loaded_motion_it)
		{
			LLMotion* cur_motionp = *loaded_motion_it;
			// motion isn't playing, delete it
			if (!isMotionActive(cur_motionp))
			{
				motions_to_kill.insert(cur_motionp->getID());
			}
		}
	}
	
	// clean up all inactive, loaded motions
	for (std::set<LLUUID>::iterator motion_it = motions_to_kill.begin();
		motion_it != motions_to_kill.end();
		++motion_it)
	{
		// look up the motion again by ID to get canonical instance
		// and kill it only if that one is inactive
		LLUUID motion_id = *motion_it;
		LLMotion* motionp = findMotion(motion_id);
		if (motionp && !isMotionActive(motionp))
		{
			removeMotion(motion_id);
		}
	}

	if (mLoadedMotions.size() > 2*MAX_MOTION_INSTANCES)
	{
		LL_WARNS_ONCE("Animation") << "> " << 2*MAX_MOTION_INSTANCES << " Loaded Motions" << llendl;
	}
}
//-----------------------------------------------------------------------------
// removeMotion()
//-----------------------------------------------------------------------------
void LLMotionController::removeMotion( const LLUUID& id)
{
	LLMotion* motionp = findMotion(id);
	//<singu>
	// If a motion is erased from mAllMotions, it must be deleted.
	if (motionp)
	{
		mAllMotions.erase(id);
		removeMotionInstance(motionp);
		delete motionp;
	}
	//</singu>
}
//-----------------------------------------------------------------------------
// deactivateMotionInstance()
//-----------------------------------------------------------------------------
BOOL LLMotionController::deactivateMotionInstance(LLMotion *motion)
{
	motion_set_t::iterator found_it = mDeprecatedMotions.find(motion);
	if (found_it != mDeprecatedMotions.end())
	{
		// deprecated motions need to be completely excised
		removeMotionInstance(motion);							// singu note: this deactivates motion and removes it from mDeprecatedMotions.
	}
	else
	{
		// for motions that we are keeping, simply remove from active queue
		motion->deactivate();									// singu note: moved here from the top of the function.
		mActiveMotions.remove(motion);
	}

	return TRUE;
}
//-----------------------------------------------------------------------------
// deactivateMotionInstance()
//-----------------------------------------------------------------------------
BOOL LLMotionController::deactivateMotionInstance(LLMotion *motion)
{
	motion->deactivate();

	motion_set_t::iterator found_it = mDeprecatedMotions.find(motion);
	if (found_it != mDeprecatedMotions.end())
	{
		// deprecated motions need to be completely excised
		removeMotionInstance(motion);	
		mDeprecatedMotions.erase(found_it);
	}
	else
	{
		// for motions that we are keeping, simply remove from active queue
		mActiveMotions.remove(motion);
	}

	return TRUE;
}
//-----------------------------------------------------------------------------
// removeMotion()
//-----------------------------------------------------------------------------
void LLMotionController::removeMotion( const LLUUID& id)
{
	LLMotion* motionp = findMotion(id);
	mAllMotions.erase(id);
	removeMotionInstance(motionp);
}