void LLPanelPlaceProfile::onForSaleBannerClick()
{
	LLViewerParcelMgr* mgr = LLViewerParcelMgr::getInstance();
	LLParcel* parcel = mgr->getFloatingParcelSelection()->getParcel();
	LLViewerRegion* selected_region =  mgr->getSelectionRegion();
	if(parcel && selected_region)
	{
		if(parcel->getLocalID() == mSelectedParcelID &&
				mLastSelectedRegionID ==selected_region->getRegionID())
		{
			S32 price = parcel->getSalePrice();

			if(price - gStatusBar->getBalance() > 0)
			{
				LLStringUtil::format_map_t args;
				args["AMOUNT"] = llformat("%d", price);
				LLBuyCurrencyHTML::openCurrencyFloater( LLTrans::getString("buying_selected_land", args), price );
			}
			else
			{
				LLViewerParcelMgr::getInstance()->startBuyLand();
			}
		}
		else
		{
			LL_WARNS("Places") << "User  is trying  to buy remote parcel.Operation is not supported"<< LL_ENDL;
		}

	}
}
void LLFloaterAuction::initialize()
{
	mParcelp = LLViewerParcelMgr::getInstance()->getParcelSelection();
	LLViewerRegion* region = LLViewerParcelMgr::getInstance()->getSelectionRegion();
	LLParcel* parcelp = mParcelp->getParcel();
	if(parcelp && region && !parcelp->getForSale())
	{
		mParcelHost = region->getHost();
		mParcelID = parcelp->getLocalID();

		childSetText("parcel_text", parcelp->getName());
		childEnable("snapshot_btn");
		childEnable("ok_btn");
	}
	else
	{
		mParcelHost.invalidate();
		if(parcelp && parcelp->getForSale())
		{
			childSetText("parcel_text", getString("already for sale"));
		}
		else
		{
			childSetText("parcel_text", LLStringUtil::null);
		}
		mParcelID = -1;
		childSetEnabled("snapshot_btn", false);
		childSetEnabled("ok_btn", false);
	}
	mImageID.setNull();
	mImage = NULL;
}
void KCWindlightInterface::onClickWLStatusButton()
{
	//clear the last notification if its still open
	if (mClearWLNotification && !mClearWLNotification->isRespondedTo())
	{
		LLSD response = mClearWLNotification->getResponseTemplate();
		response["Ignore"] = true;
		mClearWLNotification->respond(response);
	}

	if (WLset)
	{
		LLParcel *parcel = NULL;
 		parcel = LLViewerParcelMgr::getInstance()->getAgentParcel();
 		if (parcel)
		{
			//TODO: this could be better
			LLSD payload;
			payload["local_id"] = parcel->getLocalID();
			payload["land_owner"] = getOwnerID(parcel);

			LLSD args;
			args["PARCEL_NAME"] = parcel->getName();
			
			mClearWLNotification = LLNotifications::instance().add("PhoenixWLClear", args, payload, boost::bind(&KCWindlightInterface::callbackParcelWLClear, this, _1, _2));
		}
	}
}
//static
std::string LLWeb::expandURLSubstitutions(const std::string &url,
										  const LLSD &default_subs)
{
	LLSD substitution = default_subs;
	substitution["VERSION"] = LLVersionInfo::getVersion();
	substitution["VERSION_MAJOR"] = LLVersionInfo::getMajor();
	substitution["VERSION_MINOR"] = LLVersionInfo::getMinor();
	substitution["VERSION_PATCH"] = LLVersionInfo::getPatch();
	substitution["VERSION_BUILD"] = LLVersionInfo::getBuild();
	substitution["CHANNEL"] = LLVersionInfo::getChannel();
	// substitution["GRID"] = LLGridManager::getInstance()->getGridLabel();
	// substitution["GRID_LOWERCASE"] = utf8str_tolower(LLGridManager::getInstance()->getGridLabel());
	//NOTE: getGridLabel() returns e.g. "Second Life"
	//      getGridNick() returns e.g. "agni"
	substitution["GRID"] = LLGridManager::getInstance()->getGridNick();
	substitution["GRID_LOWERCASE"] = utf8str_tolower(LLGridManager::getInstance()->getGridNick());
	substitution["OS"] = LLAppViewer::instance()->getOSInfo().getOSStringSimple();
	substitution["SESSION_ID"] = gAgent.getSessionID();
	substitution["FIRST_LOGIN"] = gAgent.isFirstLogin();

	// work out the current language
	std::string lang = LLUI::getLanguage();
	if (lang == "en-us")
	{
		// *HACK: the correct fix is to change English.lproj/language.txt,
		// but we're late in the release cycle and this is a less risky fix
		lang = "en";
	}
	substitution["LANGUAGE"] = lang;

	// find the region ID
	LLUUID region_id;
	LLViewerRegion *region = gAgent.getRegion();
	if (region)
	{
		region_id = region->getRegionID();
	}
	substitution["REGION_ID"] = region_id;

	// find the parcel local ID
	S32 parcel_id = 0;
	LLParcel* parcel = LLViewerParcelMgr::getInstance()->getAgentParcel();
	if (parcel)
	{
		parcel_id = parcel->getLocalID();
	}
	substitution["PARCEL_ID"] = llformat("%d", parcel_id);

	// expand all of the substitution strings and escape the url
	std::string expanded_url = url;
	LLStringUtil::format(expanded_url, substitution);

	return LLWeb::escapeURL(expanded_url);
}
// static
void LLFloaterSellLandUI::doShowObjects(void *userdata)
{
	LLFloaterSellLandUI* self = (LLFloaterSellLandUI*)userdata;
	LLParcel* parcel = self->mParcelSelection->getParcel();
	if (!parcel) return;

	send_parcel_select_objects(parcel->getLocalID(), RT_SELL);

	// we shouldn't pass callback functor since it is registered in LLFunctorRegistration
	LLNotificationsUtil::add("TransferObjectsHighlighted",
						LLSD(), LLSD());
}
// static
void LLFloaterSellLandUI::doShowObjects(void *userdata)
{
	LLFloaterSellLandUI* self = (LLFloaterSellLandUI*)userdata;
	LLParcel* parcel = self->mParcelSelection->getParcel();
	if (!parcel) return;

	send_parcel_select_objects(parcel->getLocalID(), RT_SELL);

	LLNotificationsUtil::add("TransferObjectsHighlighted",
						LLSD(), LLSD(),
						&LLFloaterSellLandUI::callbackHighlightTransferable);
}
void LLFloaterAuction::initialize()
{
	mParcelUpdateCapUrl.clear();

	mParcelp = LLViewerParcelMgr::getInstance()->getParcelSelection();
	LLViewerRegion* region = LLViewerParcelMgr::getInstance()->getSelectionRegion();
	LLParcel* parcelp = mParcelp->getParcel();
	if(parcelp && region && !parcelp->getForSale())
	{
		mParcelHost = region->getHost();
		mParcelID = parcelp->getLocalID();
		mParcelUpdateCapUrl = region->getCapability("ParcelPropertiesUpdate");

		childSetText("parcel_text", parcelp->getName());
		childEnable("snapshot_btn");
		childEnable("reset_parcel_btn");
		childEnable("start_auction_btn");

		LLPanelEstateInfo* panel = LLFloaterRegionInfo::getPanelEstate();
		if (panel)
		{	// Only enable "Sell to Anyone" on Teen grid or if we don't know the ID yet
			U32 estate_id = panel->getEstateID();
			childSetEnabled("sell_to_anyone_btn", (estate_id == ESTATE_TEEN || estate_id == 0));
		}
		else
		{	// Don't have the panel up, so don't know if we're on the teen grid or not.  Default to enabling it
			childEnable("sell_to_anyone_btn");
		}
	}
	else
	{
		mParcelHost.invalidate();
		if(parcelp && parcelp->getForSale())
		{
			childSetText("parcel_text", getString("already for sale"));
		}
		else
		{
			childSetText("parcel_text", LLStringUtil::null);
		}
		mParcelID = -1;
		childSetEnabled("snapshot_btn", false);
		childSetEnabled("reset_parcel_btn", false);
		childSetEnabled("sell_to_anyone_btn", false);
		childSetEnabled("start_auction_btn", false);
	}

	mImageID.setNull();
	mImage = NULL;
}
void KCWindlightInterface::ParcelChange()
{
	if (!gSavedSettings.getBOOL("PhoenixWLParcelEnabled") ||
	(rlv_handler_t::isEnabled() && gRlvHandler.hasBehaviour(RLV_BHVR_SETENV)) )
		return;

	LLParcel *parcel = NULL;
	S32 this_parcel_id = 0;
	std::string desc;
 
 	parcel = LLViewerParcelMgr::getInstance()->getAgentParcel();
 
	if (parcel)
	{
		this_parcel_id = parcel->getLocalID();
		desc = parcel->getDesc();
	}

	if ( (this_parcel_id != mLastParcelID) || (mLastParcelDesc != desc) ) //parcel changed
	{
		//llinfos << "agent in new parcel: "<< this_parcel_id << " : "  << parcel->getName() << llendl;

		mLastParcelID = this_parcel_id;
		mLastParcelDesc = desc;
		mCurrentSpace = -2.f;
		mCurrentSettings.clear();
		setWL_Status(false); //clear the status bar icon
		const LLVector3& agent_pos_region = gAgent.getPositionAgent();
		mLastZ = lltrunc( agent_pos_region.mV[VZ] );

		//clear the last notification if its still open
		if (mSetWLNotification && !mSetWLNotification->isRespondedTo())
		{
			LLSD response = mSetWLNotification->getResponseTemplate();
			response["Ignore"] = true;
			mSetWLNotification->respond(response);
		}
		
		mEventTimer.reset();
		mEventTimer.start();
		
		// Apply new WL settings instantly on TP
		if (mTPing)
		{
			mTPing = false;
			tick();
		}
	}
}
void KCWindlightInterface::ApplySettings(const LLSD& settings)
{
	LLParcel *parcel = LLViewerParcelMgr::getInstance()->getAgentParcel();
	if (!settings.has("local_id") || (settings["local_id"].asInteger() == parcel->getLocalID()) )
	{
		mCurrentSettings = settings;

		ApplySkySettings(settings);

		if (settings.has("water"))
		{
			LLWaterParamManager::instance()->loadPreset(settings["water"].asString(), true);
			setWL_Status(true);
		}
	}
}
void LLFloaterAuction::initialize()
{
	mParcelUpdateCapUrl.clear();

	mParcelp = LLViewerParcelMgr::getInstance()->getParcelSelection();
	LLViewerRegion* region = LLViewerParcelMgr::getInstance()->getSelectionRegion();
	LLParcel* parcelp = mParcelp->getParcel();
	if(parcelp && region && !parcelp->getForSale())
	{
		mParcelHost = region->getHost();
		mParcelID = parcelp->getLocalID();
		mParcelUpdateCapUrl = region->getCapability("ParcelPropertiesUpdate");

		getChild<LLUICtrl>("parcel_text")->setValue(parcelp->getName());
		getChildView("snapshot_btn")->setEnabled(TRUE);
		getChildView("reset_parcel_btn")->setEnabled(TRUE);
		getChildView("start_auction_btn")->setEnabled(TRUE);

		U32 estate_id = LLEstateInfoModel::instance().getID();
		// Only enable "Sell to Anyone" on Teen grid or if we don't know the ID yet
		getChildView("sell_to_anyone_btn")->setEnabled(estate_id == ESTATE_TEEN || estate_id == 0);
	}
	else
	{
		mParcelHost.invalidate();
		if(parcelp && parcelp->getForSale())
		{
			getChild<LLUICtrl>("parcel_text")->setValue(getString("already for sale"));
		}
		else
		{
			getChild<LLUICtrl>("parcel_text")->setValue(LLStringUtil::null);
		}
		mParcelID = -1;
		getChildView("snapshot_btn")->setEnabled(false);
		getChildView("reset_parcel_btn")->setEnabled(false);
		getChildView("sell_to_anyone_btn")->setEnabled(false);
		getChildView("start_auction_btn")->setEnabled(false);
	}

	mImageID.setNull();
	mImage = NULL;
}
LLViewerParcelMediaInfo::LLViewerParcelMediaInfo(const std::string url,
												 const std::string type,
												 const LLUUID media_id,
												 const S32 width,
												 const S32 height,
												 const U8 scale,
												 const U8 loop)
	:
	mMediaURL(url),
	mMediaType(type),
	mMediaID(media_id),
	mMediaWidth(width),
	mMediaHeight(height),
	mMediaAutoScale(scale),
	mMediaLoop(loop)
{
	LLParcel *parcel = LLViewerParcelMgr::getInstance()->getAgentParcel();
	mParcelLocalID = parcel ? parcel->getLocalID() : 0;
}
// Reset all the values for the parcel in preparation for a sale
void LLFloaterAuction::doSellToAnyone()
{
	LLParcel* parcelp = mParcelp->getParcel();
	LLViewerRegion* region = LLViewerParcelMgr::getInstance()->getSelectionRegion();

	if (parcelp
		&& region
		&& !mParcelUpdateCapUrl.empty())
	{
		LLSD body;
		std::string empty;

		// request new properties update from simulator
		U32 message_flags = 0x01;
		body["flags"] = ll_sd_from_U32(message_flags);

		// Set all the default parcel properties for auction
		body["local_id"] = parcelp->getLocalID();

		// Set 'for sale' flag
		U32 parcel_flags = parcelp->getParcelFlags() | PF_FOR_SALE;
		// Ensure objects not included
		parcel_flags &= ~PF_FOR_SALE_OBJECTS;
		body["parcel_flags"] = ll_sd_from_U32(parcel_flags);
		
		body["sale_price"] = parcelp->getArea();	// Sell for L$1 per square meter
		body["auth_buyer_id"] = LLUUID::null;		// To anyone
// <FS:AW opensim currency support>
//		llinfos << "Sending parcel update to sell to anyone for L$1 via capability to: "
		llinfos << Tea::wrapCurrency("Sending parcel update to sell to anyone for L$1 via capability to: ")
// <FS:AW opensim currency support>
			<< mParcelUpdateCapUrl << llendl;
		LLHTTPClient::post(mParcelUpdateCapUrl, body, new LLHTTPClient::Responder());

		// clean up floater, and get out
		cleanupAndClose();
	}
}
// Reset all the values for the parcel in preparation for a sale
void LLFloaterAuction::doResetParcel()
{
	LLParcel* parcelp = mParcelp->getParcel();
	LLViewerRegion* region = LLViewerParcelMgr::getInstance()->getSelectionRegion();

	if (parcelp
		&& region
		&& !mParcelUpdateCapUrl.empty())
	{
		LLSD body;
		std::string empty;

		// request new properties update from simulator
		U32 message_flags = 0x01;
		body["flags"] = ll_sd_from_U32(message_flags);

		// Set all the default parcel properties for auction
		body["local_id"] = parcelp->getLocalID();

		U32 parcel_flags = PF_ALLOW_LANDMARK |
						   PF_ALLOW_FLY	|
						   PF_CREATE_GROUP_OBJECTS |
						   PF_ALLOW_ALL_OBJECT_ENTRY |
						   PF_ALLOW_GROUP_OBJECT_ENTRY |
						   PF_ALLOW_GROUP_SCRIPTS |
						   PF_RESTRICT_PUSHOBJECT |
						   PF_SOUND_LOCAL |
						   PF_ALLOW_VOICE_CHAT |
						   PF_USE_ESTATE_VOICE_CHAN;

		body["parcel_flags"] = ll_sd_from_U32(parcel_flags);
		
		// Build a parcel name like "Ahern (128,128) PG 4032m"
		std::ostringstream parcel_name;
		LLVector3 center_point( parcelp->getCenterpoint() );
		center_point.snap(0);		// Get rid of fractions
		parcel_name << region->getName() 
					<< " ("
					<< (S32) center_point.mV[VX]
					<< ","
					<< (S32) center_point.mV[VY]						
					<< ") "
					<< region->getSimAccessString()
					<< " "
					<< parcelp->getArea()
					<< "m";

		std::string new_name(parcel_name.str().c_str());
		body["name"] = new_name;
		getChild<LLUICtrl>("parcel_text")->setValue(new_name);	// Set name in dialog as well, since it won't get updated otherwise

		body["sale_price"] = (S32) 0;
		body["description"] = empty;
		body["music_url"] = empty;
		body["media_url"] = empty;
		body["media_desc"] = empty;
		body["media_type"] = LLMIMETypes::getDefaultMimeType();
		body["media_width"] = (S32) 0;
		body["media_height"] = (S32) 0;
		body["auto_scale"] = (S32) 0;
		body["media_loop"] = (S32) 0;
		body["obscure_media"] = (S32) 0; // OBSOLETE - no longer used
		body["obscure_music"] = (S32) 0; // OBSOLETE - no longer used
		body["media_id"] = LLUUID::null;
		body["group_id"] = MAINTENANCE_GROUP_ID;	// Use maintenance group
		body["pass_price"] = (S32) 10;		// Defaults to $10
		body["pass_hours"] = 0.0f;
		body["category"] = (U8) LLParcel::C_NONE;
		body["auth_buyer_id"] = LLUUID::null;
		body["snapshot_id"] = LLUUID::null;
		body["user_location"] = ll_sd_from_vector3( LLVector3::zero );
		body["user_look_at"] = ll_sd_from_vector3( LLVector3::zero );
		body["landing_type"] = (U8) LLParcel::L_DIRECT;

		llinfos << "Sending parcel update to reset for auction via capability to: "
			<< mParcelUpdateCapUrl << llendl;
		LLHTTPClient::post(mParcelUpdateCapUrl, body, new LLHTTPClient::Responder());

		// Send a message to clear the object return time
		LLMessageSystem *msg = gMessageSystem;
		msg->newMessageFast(_PREHASH_ParcelSetOtherCleanTime);
		msg->nextBlockFast(_PREHASH_AgentData);
		msg->addUUIDFast(_PREHASH_AgentID, gAgent.getID());
		msg->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID());
		msg->nextBlockFast(_PREHASH_ParcelData);
		msg->addS32Fast(_PREHASH_LocalID, parcelp->getLocalID());
		msg->addS32Fast(_PREHASH_OtherCleanTime, 5);			// 5 minute object auto-return

		msg->sendReliable(region->getHost());

		// Clear the access lists
		clearParcelAccessLists(parcelp, region);
	}
}
bool KCWindlightInterface::ChatCommand(std::string message, std::string from_name, LLUUID source_id, LLUUID owner_id)
{
	boost::cmatch match;
	const boost::regex prefix_exp("^\\)\\*\\((.*)");
	if(boost::regex_match(message.c_str(), match, prefix_exp))
	{
		std::string data(match[1].first, match[1].second);
		
		//TODO: expand these or good as is?
		/*const boost::regex setWLpreset_exp("^setWLpreset\\|(.*)");
		const boost::regex setWWpreset_exp("^setWWpreset\\|(.*)");
		if(boost::regex_match(data.c_str(), match, setWLpreset_exp))
		{
			llinfos << "got setWLpreset : " << match[1] << llendl;
			LLWLParamManager::instance()->mAnimator.mIsRunning = false;
			LLWLParamManager::instance()->mAnimator.mUseLindenTime = false;
			LLWLParamManager::instance()->loadPreset(match[1]);
			return true;
		}
		else if(boost::regex_match(data.c_str(), match, setWWpreset_exp))
		{
			llinfos << "got setWWpreset : " << match[1] << llendl;
			LLWaterParamManager::instance()->loadPreset(match[1], true);
			return true;
		}
		else 
		{*/
		
		//TODO: add save settings for reuse instead of just clearing on parcel change
		//TODO: add support for region wide settings on non-mainland
		//TODO: add support for targeting specfic users
		//TODO: add support for custom settings via notecards or something
		//TODO: improved data processing, possibly just use LLSD as input instead
		
			boost::smatch match2;
			const boost::regex Parcel_exp("^(Parcel),WLPreset=\"([^\"\\r\\n]+)\"(,WWPreset=\"([^\"\\r\\n]+)\")?$");
			//([\\w]{8}-[\\w]{4}-[\\w]{4}-[\\w]{4}-[\\w]{12})
			if(boost::regex_search(data, match2, Parcel_exp))
			{
				if (SetDialogVisible) //TODO: handle this better
					return true;

				if (match2[1]=="Parcel")
				{
					llinfos << "Got Parcel WL : " << match[2] << llendl;
					
					LLParcel *parcel = LLViewerParcelMgr::getInstance()->getAgentParcel();
					LLSD payload;
					payload["local_id"] = parcel->getLocalID();
					payload["land_owner"] = parcel->getOwnerID();
					payload["wlpreset"] = std::string(match2[2].first, match2[2].second);
					payload["wwpreset"] = std::string(match2[3].first, match2[3].second);

					LLSD args;
					args["PARCEL_NAME"] = parcel->getName();
					
					LLNotifications::instance().add("PhoenixWL", args, payload, boost::bind(&KCWindlightInterface::callbackParcelWL, this, _1, _2));
					SetDialogVisible = true;
				}
				return true;
			}
		/*}*/
	}
	return false;
}