//static
// returns true if the instance exists
bool LLFloaterReg::hideInstance(const std::string& name, const LLSD& key) 
{ 
	LLFloater* instance = findInstance(name, key); 
	if (instance)
	{
		instance->closeHostedFloater();
	}
	return (instance != NULL);
}
//static
// returns true if the instance is visible when completed
bool LLFloaterReg::toggleInstance(const std::string& name, const LLSD& key)
{
	LLFloater* instance = findInstance(name, key); 
	if (LLFloater::isShown(instance))
	{
		instance->closeHostedFloater();
		return false;
	}
	else
	{
		return showInstance(name, key, TRUE) ? true : false;
	}
}
//static
void LLFloaterReg::toggleInstanceOrBringToFront(const LLSD& sdname, const LLSD& key)
{
	//
	// Floaters controlled by the toolbar behave a bit differently from others.
	// Namely they have 3-4 states as defined in the design wiki page here:
	//   https://wiki.lindenlab.com/wiki/FUI_Button_states
	//
	// The basic idea is this:
	// * If the target floater is minimized, this button press will un-minimize it.
	// * Else if the target floater is closed open it.
	// * Else if the target floater does not have focus, give it focus.
	//       * Also, if it is not on top, bring it forward when focus is given.
	// * Else the target floater is open, close it.
	// 
	std::string name = sdname.asString();
	LLFloater* instance = getInstance(name, key); 
	

	if (!instance)
	{
		LL_DEBUGS() << "Unable to get instance of floater '" << name << "'" << LL_ENDL;
		return;
	}
	
	// If hosted, we need to take that into account
	LLFloater* host = instance->getHost();
	
	if (host)
	{
		//FS:LO from above: * Else if the target floater does not have focus, give it focus. * Also, if it is not on top, bring it forward when focus is given.
		//if (host->isMinimized() || !host->isShown() || !host->isFrontmost())
		if (host->isMinimized() || !host->isShown() || (!host->hasFocus() || !host->isFrontmost()))
		{
			host->setMinimized(FALSE);
			instance->openFloater(key);
			instance->setVisibleAndFrontmost(true, key);
		}
		else if (!instance->getVisible())
		{
			instance->openFloater(key);
			instance->setVisibleAndFrontmost(true, key);
			instance->setFocus(TRUE);
		}
		else
		{
			instance->closeHostedFloater();
		}
	}
	else
	{
		if (instance->isMinimized())
		{
			instance->setMinimized(FALSE);
			instance->setVisibleAndFrontmost(true, key);
		}
		else if (!instance->isShown())
		{
			instance->openFloater(key);
			instance->setVisibleAndFrontmost(true, key);
		}
		//FS:LO from above: * Else if the target floater does not have focus, give it focus. * Also, if it is not on top, bring it forward when focus is given.
		//else if (!instance->isFrontmost())
		else if (!instance->hasFocus() || !instance->isFrontmost())
		{
			instance->setVisibleAndFrontmost(true, key);
		}
		else
		{
			instance->closeHostedFloater();
		}
	}
}