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

using System;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Summary description for InlineTextEdit.
	/// </summary>
	public class InlineTextEdit : Literal, IText
	{
		public InlineTextEdit()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		#region IText Members

		public System.Web.UI.Control Control
		{
			get
			{
				return this;
			}
		}

		protected virtual bool CanUserEdit
		{
			get{ return CSContext.Current.User.IsAdministrator;}
		}

		private string _updateFunction = null;
		public string AjaxFunction
		{
			get{ return _updateFunction;}
			set{_updateFunction = value;}
		}

		private string _ajaxID = null;
		public string AjaxClientID
		{
			get{return _ajaxID;}
			set{_ajaxID = value;}
		}

        public virtual HtmlTextWriterTag Tag 
        {
            get 
            {
                Object state = ViewState["Tag"];
                if ( state != null ) 
                {
                    return (HtmlTextWriterTag)state;
                }
                return HtmlTextWriterTag.Div;
            }
            set 
            {
                ViewState["Tag"] = value;
            }
        }

		protected override void Render(HtmlTextWriter writer)
		{
			bool enableEdit = CanUserEdit && !Globals.IsNullorEmpty(this.AjaxFunction);

			if(enableEdit)
				InlineEditorHelper.RenderStartInLine(Tag,writer,this.AjaxClientID,this.AjaxFunction);


			base.Render(writer);

			if(enableEdit)
				InlineEditorHelper.RenderEndInLine(Tag, writer);

            

		}
		#endregion
	}



	public class InlineEditorHelper
	{
		public static void RenderStartInLine(HtmlTextWriterTag tag, HtmlTextWriter writer, string ajaxID, string ajaxFunction)
		{
            

			writer.Write("<{0} ", GetTag(tag));
			writer.Write(" onmouseover=\"inLineEditOn(this)\"");
			writer.Write(" onmouseout=\"inLineEditOff(this)\"");
			writer.Write(" contenteditable=\"true\"");
			writer.Write(" indicateeditable=\"true\" ");
			writer.Write(" designMode=\"On\"");
			writer.Write(" id=\"{0}\"",ajaxID);
			writer.Write(" onblur=\"{0}\"", ajaxFunction);
			writer.Write(">");		

		}

		public static void RenderEndInLine(HtmlTextWriterTag tag, HtmlTextWriter writer)
		{
			writer.Write("</{0}>", GetTag(tag));
		}

        private static string GetTag(HtmlTextWriterTag tag)
        {
            switch(tag)
            {
                case HtmlTextWriterTag.Div:
                    return "div";
                case HtmlTextWriterTag.H1:
                    return "h1";
                case HtmlTextWriterTag.H2:
                    return "h2";
                case HtmlTextWriterTag.H3:
                    return "h3";
                case HtmlTextWriterTag.H4:
                    return "h4";
                case HtmlTextWriterTag.H5:
                    return "h5";
                case HtmlTextWriterTag.H6:
                    return "h6";
                default:
                    return "span";
            }
        }

	}
}
