using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Drawing;
using System.ComponentModel;
using System.Web;
using System.Globalization;

namespace Westwind.Web.Controls
{
    /// <summary>
    /// A draggable Panel control that can be dragged around the current browser window.
    /// Dragging is supported for the panel and initiated through a drag handle control - 
    /// a control that is contained in the window and acts as the draggable hot spot that
    /// initiates dragging. You can also make the Panel itself the drag handle.
    /// 
    /// The control can also optionally display a close button that allows hiding the control
    /// by changing its client side visibility.
    /// </summary>
    [ToolboxBitmap(typeof(Panel)),
    ToolboxData("<{0}:wwDragPanel runat=\"server\"><%-- Drag Handle: Make sure to assign Unique ID and set matching DragHandleID --%><div runat=\"server\"></div><%-- Content Panel: Assign Unique ID --%><div></div></{0}:wwDragPanel>")]
    public class wwDragPanel : Panel
    {
        /// <summary>
        /// The ID of the control that is used as the drag handle to initiate a drag operation.
        /// </summary>
        [Description("The ID of the control that is used as the drag handle to initiate a drag operation")]
        [DefaultValue("")]
        [Category("Dragging")]
        public string DragHandleID
        {
            get { return _DragHandleID; }
            set { _DragHandleID = value; }
        }
        private string _DragHandleID = "";

        /// <summary>
        /// Determines whether this control is draggable
        /// </summary>
        [Description("Determines whether this control is draggable")]
        [DefaultValue("true"), Category("Dragging")]
        public bool Draggable
        {
            get { return _Draggable; }
            set { _Draggable = value; }
        }
        private bool _Draggable = true;


        /// <summary>
        /// Flag that determines whether a closebox is rendered into the &lt;div&gt; 
        /// specified as a drag handle.
        /// <seealso>Class wwDragPanel</seealso>
        /// </summary>
        [Category("Dragging")]
        [Description("Flag that determines whether a closebox is rendered into the <div> specified as a drag handle.")]
        [DefaultValue(false)]
        public bool Closable
        {
            get { return _ShowCloseBoxOnDragHandle; }
            set { _ShowCloseBoxOnDragHandle = value; }
        }
        private bool _ShowCloseBoxOnDragHandle = false;

        /// <summary>
        /// Optional Image used for close box if ShowCloseBoxOnDragHandle is true.
        /// </summary>
        [Category("Dragging")]
        [Description("Optional Image used for close box if ShowCloseBoxOnDragHandle is true.")]
        [DefaultValue("WebResource")]
        public string CloseBoxImage
        {
            get { return _CloseBoxImage; }
            set { _CloseBoxImage = value; }
        }
        private string _CloseBoxImage = "WebResource";

        /// <summary>
        /// Fades out the window when it is closed with the close button
        /// </summary>
        [Description("Fades out the window when it is closed with the close button")]
        [Category("Dragging"), DefaultValue(false)]
        public bool FadeOnClose
        {
            get { return _FadeOnClose; }
            set { _FadeOnClose = value; }
        }
        private bool _FadeOnClose = false;


       /// <summary>
        /// Optional Opacity level in decimal percentage values (ie. 0.65 for 65%) for the panel background. Supported only in Mozilla and IE browsers. The value is given as fractional percentage.
        /// </summary>
        [Description("Optional Opacity level in full percentage points for the panel background. Supported only in Mozilla and IE browsers. The value is given as fractional percentage."),
        Category("Panel Display"), DefaultValue(typeof(decimal),"1.0")]
        public decimal PanelOpacity
        {
            get { return _PanelOpacity; }
            set { _PanelOpacity = value; }
        }
        private decimal _PanelOpacity = 1.0M;


        /// <summary>
        /// Optionally used to specify a shadow below the panel. If 0 no shadow is created. If greater than 0 the panel is rendered.
        /// </summary>
        [Description("Optionally used to specify a shadow below the panel. If 0 no shadow is created. If greater than 0 the panel is rendered."),
        Category("Panel Display"), DefaultValue(0)]
        public int ShadowOffset
        {
            get { return _PanelShadowOffset; }
            set { _PanelShadowOffset = value; }
        }
        private int _PanelShadowOffset = 0;

        /// <summary>
        /// The opacity of the Panel's shadow if PanelShadoOffset is set.
        /// </summary>
        [Description("The opacity of the Panel's shadow if PanelShadoOffset is set."),
        Category("Panel Display"), DefaultValue(typeof(decimal),".25")]
        public decimal ShadowOpacity
        {
            get { return _PanelShadowOpacity; }
            set { _PanelShadowOpacity = value; }
        }
        private decimal _PanelShadowOpacity = .25M;


        /// <summary>
        /// Client side event handler called when the panel is closed via the CloseBox 
        /// icon.
        /// </summary>
        [Description("Client side event handler called when the panel is closed via the Closebox"),
         Category("Dragging")]
        public string OnClientCloseHandler
        {
            get { return _OnClientCloseHandler; }
            set { _OnClientCloseHandler = value; }
        }
        private string _OnClientCloseHandler = "";

        


        protected override void OnPreRender(EventArgs e)
        {
            // *** Use ScriptProxy for MS Ajax Compatibility
            ClientScriptProxy ScriptProxy = ClientScriptProxy.Current;

            string DhId = this.DragHandleID;
            Control Ctl = this.FindControl(this.DragHandleID);
            if (Ctl != null)
                DhId = Ctl.ClientID;

            if (this.Closable && !string.IsNullOrEmpty(DragHandleID) )
            {
                string Img = "<img src='" + ScriptProxy.GetWebResourceUrl(this, this.GetType(), ControlResources.CLOSE_ICON_RESOURCE) +
                             "' border='0' />";

                string Handler = "";
                if (!string.IsNullOrEmpty(this.OnClientCloseHandler))
                    Handler = this.OnClientCloseHandler + "();";                
                
                // DhId
                string Script = 
@"wwEvent.load(window,function() {
var Ctl = $w(""" + this.DragHandleID +  @""");
Ctl.innerHTML = ""<div style='position: absolute;  right: 2px; top: 2px;'><a href='javascript:;' onclick='" + this.ID + @"_DragBehavior" + (this.FadeOnClose ? ".windowObj.fadeout(true)" : ".hide()") + ";" + Handler + "' style='text-decoration:none;'>" + Img + @"</a></div>"" + Ctl.innerHTML;
} );
";
                ScriptProxy.RegisterStartupScript(this, this.GetType(), this.ID + "_CloseBox", Script, true);
            }

            ControlResources.LoadwwScriptLibrary(this);
            //ScriptProxy.RegisterClientScriptResource(this, typeof(ControlResources), ControlResources.SCRIPTLIBRARY_SCRIPT_RESOURCE);

            string StartupScript = 
                "var " + this.ID + "_DragBehavior = new wwDragBehavior(\"" + this.ClientID + "\",\"" + DhId + "\");\r\n";

            if (this.ShadowOffset != 0)
                StartupScript += 
                   this.ID + "_DragBehavior.windowObj.showShadow(" + 
                                   this.ShadowOpacity.ToString(CultureInfo.InvariantCulture.NumberFormat) + "," +
                                   this.ShadowOffset.ToString() + ");\r\n";

            ScriptProxy.RegisterStartupScript(this, this.GetType(), this.ID + "_DragBehavior",StartupScript,true);                

            
            base.OnPreRender(e);
        }
    }
}
