#region using
using System;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
#endregion

#region *** DISCLAIMER ***
/// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
/// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
/// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
/// PARTICULAR PURPOSE.
/// 
/// IN NO EVENT SHALL CHRIS BLANKENSHIP @ WWW.DSCODUC.COM BE
/// LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
/// DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
/// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
/// ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
/// OF THIS CODE OR INFORMATION.
#endregion

[Extension("Enable drop down functionality for Widgets", "1.0", "<a target=\"_blank\" href=\"http://www.dscoduc.com\">Chris Blankenship</a>")]
public class DropDownWidget
{
    // Name of the extension
    private string extensionName = "DropDownWidget";

    public DropDownWidget()
    {
        BlogEngine.Core.Page.Serving += new EventHandler<ServingEventArgs>(executeExtension);
        BlogEngine.Core.Post.Serving += new EventHandler<ServingEventArgs>(executeExtension);
        ExtensionSettings settings = new ExtensionSettings(extensionName);
    }

    private void executeExtension(object sender, ServingEventArgs e)
    {
        HttpContext context = HttpContext.Current;
        if (context.CurrentHandler is System.Web.UI.Page)
            if (context.Items[extensionName] == null)
            {
                // prep page handler
                System.Web.UI.Page page = (System.Web.UI.Page)context.CurrentHandler;

                // Build style syntax
                StringBuilder sbStyle = new StringBuilder();
                sbStyle.Append(@".webpartExpanded { width: 100%; visibility: visible; display: block; }");
                sbStyle.Append(@".webpartCollapsed { width: 100%; visibility: hidden; display: none; }");
                
                // Add style tag
                HtmlGenericControl hgcStyle = new HtmlGenericControl("style");
                hgcStyle.InnerText = sbStyle.ToString();
                page.Header.Controls.Add(hgcStyle);

                // Build script syntax
                StringBuilder sbScript = new StringBuilder();
                sbScript.AppendLine("var wp_img_expanded = '/pics/up.png';");
                sbScript.AppendLine("var wp_img_collapsed = '/pics/down.png';");
                sbScript.AppendLine("(new Image(15,15)).src = wp_img_expanded;");
                sbScript.AppendLine("(new Image(15,15)).src = wp_img_collapsed;");
                sbScript.AppendLine("function webpartExpand(htmlNode,imgNode) {");
                sbScript.AppendLine("  if (document.getElementById(htmlNode) != null) {");
                sbScript.AppendLine("    document.getElementById(imgNode).src=wp_img_expanded;");
                sbScript.AppendLine("    document.getElementById(htmlNode).className='webpartExpanded';");
                sbScript.AppendLine("  }");
                sbScript.AppendLine("}");
                sbScript.AppendLine("function webpartCollapse(htmlNode,imgNode) {");
                sbScript.AppendLine("  if (document.getElementById(htmlNode) !=  null) {");
                sbScript.AppendLine("    document.getElementById(imgNode).src=wp_img_collapsed;");
                sbScript.AppendLine("    document.getElementById(htmlNode).className='webpartCollapsed';");
                sbScript.AppendLine("  }");
                sbScript.AppendLine("}");
                sbScript.AppendLine("function webpartToggleExpansionStatus(nodeName) {");
                sbScript.AppendLine("  htmlNode = nodeName + 'Panel';");
                sbScript.AppendLine("  imgNode = nodeName + 'Image';");
                sbScript.AppendLine("  if (document.getElementById(htmlNode) !=  null) ");
                sbScript.AppendLine("    { nodeState = document.getElementById(htmlNode).className; }");
                sbScript.AppendLine("  if (nodeState == 'webpartCollapsed') { webpartExpand(htmlNode,imgNode); }");
                sbScript.AppendLine("  else { webpartCollapse(htmlNode,imgNode); }");
                sbScript.AppendLine("  return false;");
                sbScript.AppendLine("}");

                // Add script tag
                HtmlGenericControl script = new HtmlGenericControl("script");
                script.Attributes.Add("type", "text/javascript");
                script.InnerText = sbScript.ToString();
                page.Header.Controls.Add(script);

                // finish up                
                context.Items[extensionName] = 1;
            }
    }
