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

using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CA = ComponentArt.Web.UI;

namespace CommunityServer.ControlPanel.BlogAdmin
{
	/// <summary>
	/// Summary description for OrganizePage.
	/// </summary>
	public class OrganizePage : BaseBlogAdminPage
	{

		#region Child Controls
		
		protected DataList GroupList;
        protected Repeater BlogList;
		
		#endregion
		
		string AvailableGroups  = string.Empty;
		
		private void Page_Load(object sender, EventArgs e)
		{
            //Ajax.Utility.RegisterTypeForAjax(typeof(OrganizePage));

			AjaxManager.Register(this,"OrganizePage");

			if(!IsPostBack && !IsCallBack)
			{
				ArrayList groups = WeblogGroups.GetWeblogGroups(false,true,false);
                GroupList.DataSource = groups;
                GroupList.DataBind();

                for(int i = 0; i<groups.Count; i++)
                {
                    Group g = groups[i] as Group;
                    if(i == 0)
                        AvailableGroups = g.GroupID.ToString();
                    else
                        AvailableGroups += "," + g.GroupID.ToString();
                }

                
                ArrayList blogs = Weblogs.GetWeblogs(false,true,false);

                BlogList.ItemDataBound +=new RepeaterItemEventHandler(BlogList_ItemDataBound);

                
                BlogList.DataSource = blogs;
                BlogList.DataBind();

                

			}


           


		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new EventHandler(this.Page_Load);
            

		}
        #endregion

        private void BlogList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Weblog w = e.Item.DataItem as Weblog;
            	CA.Snap snap = e.Item.FindControl("Snap1") as CA.Snap;
                snap.ID = "BLOG" +  w.SectionID.ToString();

                string desc = Globals.IsNullorEmpty(w.Description) ? "No Description" : w.Description;
                snap.Content.Controls.Add(new SnapItemControl(w.Name,desc,snap.ClientID));
                snap.DockingContainers = AvailableGroups;
            }
        }

        [AjaxMethod(IncludeControlValuesWithCallBack=false)]
        public void MoveBlog(string controlID, int groupid, int index)
        {
            if(CSContext.Current.User.IsBlogAdministrator)
            {
                int sectionID = Int32.Parse(controlID.Substring(controlID.IndexOf("_BLOG") + 5 ));
                
                Sections.MoveOrReOrder(groupid,sectionID,index);
            }

        }
    }

    public class SnapItemControl : Control
    {
        private string name;
        private string desc;
        private string id;

        public SnapItemControl(string name, string desc, string id)
        {
            this.name = name;
            this.desc = desc;
            this.id = id;
        }

        private static readonly string row = "<tr class=\"{0}\"><td>{1}</td></tr>";
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("<table width = \"180px\" cellpadding=\"0\" cellspacing=\"0\" class = \"moveableItem\" onmousedown=\"{0}.StartDragging(event);\">",this.id);
            writer.Write(row,"moveableTitleItem", this.name);
            writer.Write(row,"moveableDescItem", this.desc);
            writer.Write("</table>");
            
        }

    }
}
