//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Donut control allows you to surround a literal control and selectively hide not only the literal but the wrapping HTML
	/// </summary>
	/// <example>
	/// <code>
	/// <CS:Donut runat = "Server" id="theDonut">
	///     <div style="background-color:green;">
	///         <asp:Literal runat = "Server" />
	///     </div>
	/// </CS:Donut>
	/// 
	/// theDonut.Text = "Frogs";
	/// </code>
	/// </example>
	[PersistChildren(true)]
	[ParseChildren(false)]
	public class Donut : Control, IText
	{
		public Donut()
		{
		}

		public string Text
		{
			get{return ViewState["Text"] as string;}
			set{ ViewState["Text"] = value;}
		}

		private Literal theLiteral = null;
		private bool foundFirst = false;

        public Control Control
        {
            get{ return this;}
        }


		protected override void AddParsedSubObject(object obj)
		{
			if(!foundFirst)
			{
				Literal literal = obj as Literal;
				if(literal != null)
				{
					//We can not set .Text here, it is way
					//too early
					theLiteral = literal;
					foundFirst = true;
				}
			}

			base.AddParsedSubObject (obj);

			
		}

		protected override void OnPreRender(EventArgs e)
		{
			if(theLiteral != null)
				theLiteral.Text = this.Text;

			base.OnPreRender (e);
		}


	}
}
