BOOL LLFloaterScriptLimits::postBuild()
{
	// a little cheap and cheerful - if there's an about land panel open default to showing parcel info,
	// otherwise default to showing attachments (avatar appearance)
	bool selectParcelPanel = false;
	
	LLFloaterLand* instance = LLFloaterReg::getTypedInstance<LLFloaterLand>("about_land");
	if(instance)
	{
		if(instance->isShown())
		{
			selectParcelPanel = true;
		}
	}

	mTab = getChild<LLTabContainer>("scriptlimits_panels");
	
	if(!mTab)
	{
		llwarns << "Error! couldn't get scriptlimits_panels, aborting Script Information setup" << llendl;
		return FALSE;
	}

	// contruct the panels
	std::string land_url = gAgent.getRegion()->getCapability("LandResources");
	if (!land_url.empty())
	{
		LLPanelScriptLimitsRegionMemory* panel_memory;
		panel_memory = new LLPanelScriptLimitsRegionMemory;
		mInfoPanels.push_back(panel_memory);
		LLUICtrlFactory::getInstance()->buildPanel(panel_memory, "panel_script_limits_region_memory.xml");
		mTab->addTabPanel(panel_memory);
	}
	
	std::string attachment_url = gAgent.getRegion()->getCapability("AttachmentResources");
	if (!attachment_url.empty())
	{
		LLPanelScriptLimitsAttachment* panel_attachments = new LLPanelScriptLimitsAttachment;
		mInfoPanels.push_back(panel_attachments);
		LLUICtrlFactory::getInstance()->buildPanel(panel_attachments, "panel_script_limits_my_avatar.xml");
		mTab->addTabPanel(panel_attachments);
	}
	
	if(mInfoPanels.size() > 0)
	{
		mTab->selectTab(0);
	}

	if(!selectParcelPanel && (mInfoPanels.size() > 1))
	{
		mTab->selectTab(1);
	}

	return TRUE;
}
BOOL LLPanelScriptLimitsRegionMemory::StartRequestChain()
{
	LLUUID region_id;
	
	LLFloaterLand* instance = LLFloaterReg::getTypedInstance<LLFloaterLand>("about_land");
	if(!instance)
	{
		childSetValue("loading_text", LLSD(std::string("")));
		//might have to do parent post build here
		//if not logic below could use early outs
		return FALSE;
	}
	LLParcel* parcel = instance->getCurrentSelectedParcel();
	LLViewerRegion* region = LLViewerParcelMgr::getInstance()->getSelectionRegion();
	
	LLUUID current_region_id = gAgent.getRegion()->getRegionID();

	if ((region) && (parcel))
	{
		LLVector3 parcel_center = parcel->getCenterpoint();
		
		region_id = region->getRegionID();
		
		if(region_id != current_region_id)
		{
			std::string msg_wrong_region = LLTrans::getString("ScriptLimitsRequestWrongRegion");
			childSetValue("loading_text", LLSD(msg_wrong_region));
			return FALSE;
		}
		
		LLVector3d pos_global = region->getCenterGlobal();
		
		LLSD body;
		std::string url = region->getCapability("RemoteParcelRequest");
		if (!url.empty())
		{
			body["location"] = ll_sd_from_vector3(parcel_center);
			if (!region_id.isNull())
			{
				body["region_id"] = region_id;
			}
			if (!pos_global.isExactlyZero())
			{
				U64 region_handle = to_region_handle(pos_global);
				body["region_handle"] = ll_sd_from_U64(region_handle);
			}
			LLHTTPClient::post(url, body, new LLRemoteParcelRequestResponder(getObserverHandle()));
		}
		else
		{
			llwarns << "Can't get parcel info for script information request" << region_id
					<< ". Region: "	<< region->getName()
					<< " does not support RemoteParcelRequest" << llendl;
					
			std::string msg_waiting = LLTrans::getString("ScriptLimitsRequestError");
			childSetValue("loading_text", LLSD(msg_waiting));
		}
	}
	else
	{
		std::string msg_waiting = LLTrans::getString("ScriptLimitsRequestNoParcelSelected");
		childSetValue("loading_text", LLSD(msg_waiting));
	}

	return LLPanelScriptLimitsInfo::postBuild();
}
BOOL LLPanelScriptLimitsRegionMemory::StartRequestChain()
{
	LLUUID region_id;

	LLFloaterLand* instance = LLFloaterLand::getInstance();
	if(!instance)
	{
		getChild<LLUICtrl>("loading_text")->setValue(LLSD(std::string("")));
		//might have to do parent post build here
		//if not logic below could use early outs
		return FALSE;
	}
	LLParcel* parcel = instance->getCurrentSelectedParcel();
	LLViewerRegion* region = LLViewerParcelMgr::getInstance()->getSelectionRegion();

	LLUUID current_region_id = gAgent.getRegion()->getRegionID();

	// <alchemy> Fall back to the parcel we're on if none is selected.
	// Fixes parcel script info intermittently working and broken in toolbar button.
	if (!parcel)
	{
		parcel = LLViewerParcelMgr::getInstance()->getAgentParcel();
		region = gAgent.getRegion();
	}
	// </alchemy>

	if ((region) && (parcel))
	{
		LLVector3 parcel_center = parcel->getCenterpoint();

		region_id = region->getRegionID();

		if(region_id != current_region_id)
		{
			std::string msg_wrong_region = LLTrans::getString("ScriptLimitsRequestWrongRegion");
			getChild<LLUICtrl>("loading_text")->setValue(LLSD(msg_wrong_region));
			return FALSE;
		}

		LLVector3d pos_global = region->getCenterGlobal();

		LLSD body;
		std::string url = region->getCapability("RemoteParcelRequest");
		if (!url.empty())
		{
			body["location"] = ll_sd_from_vector3(parcel_center);
			if (!region_id.isNull())
			{
				body["region_id"] = region_id;
			}
			if (!pos_global.isExactlyZero())
			{
				U64 region_handle = to_region_handle(pos_global);
				body["region_handle"] = ll_sd_from_U64(region_handle);
			}
			LLHTTPClient::post(url, body, new LLRemoteParcelRequestResponder(getObserverHandle()));
		}
		else
		{
			LL_WARNS() << "Can't get parcel info for script information request" << region_id
					<< ". Region: "	<< region->getName()
					<< " does not support RemoteParcelRequest" << LL_ENDL;

			std::string msg_waiting = LLTrans::getString("ScriptLimitsRequestError");
			getChild<LLUICtrl>("loading_text")->setValue(LLSD(msg_waiting));
		}
	}
	else
	{
		std::string msg_waiting = LLTrans::getString("ScriptLimitsRequestNoParcelSelected");
		getChild<LLUICtrl>("loading_text")->setValue(LLSD(msg_waiting));
	}

	return LLPanelScriptLimitsInfo::postBuild();
}