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

using System;
using System.Web.UI;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{
	/// <summary>
	/// Provides the wire up necessary to use the CommunityServer.Controls.LayoutTemplate control 
	/// with Weblog Theming. Unlike controls deriving from WeblogThemedControl, this Layout control 
	/// provides NO security checks. It simply loads it child controls.
	/// </summary>
	public class LayoutTemplate : CommunityServer.Controls.LayoutTemplate
	{
		public LayoutTemplate() : base()
		{
			GalleryConfig = GalleryConfiguration.Instance();
		}

		protected GalleryConfiguration GalleryConfig = null;
		private Gallery gallery = null;

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender (e);

			CSContext context = CSContext.Current;

			if (CurrentGallery != null)
			{
				Head.AddMetaDescription(CurrentGallery.MetaTagDescription, context.Context);
				Head.AddMetaKeywords(CurrentGallery.MetaTagKeywords, context.Context);
			}
		}

		/// <summary>
		/// Returns the CurrentWeb and calls Authorize
		/// </summary>
		public virtual Gallery CurrentGallery
		{
			get
			{
				if(gallery == null)
					gallery = Galleries.GetGallery(CSContext.Current.ApplicationKey);
				return gallery;
			}
		}

        protected override bool LoadThemedControl()
        {
            if(this.Page != null)
            {
                string path = ThemeManager.Instance().SkinPath(this.ThemeName,Template);
                Control skin = this.Page.LoadControl( path );
                skin.ID="_";
                this.Controls.Add( skin );
                return true;
            }

            return false;
        }


        protected override bool LoadDefaultThemedControl()
        {
            return false;
        }


		protected override void OnLoad(System.EventArgs e)
		{
			base.OnLoad (e);
//			if(!Page.IsPostBack)
				DataBind();
		}

		public override void DataBind()
		{
			if(hideSideBar && (FindControl(sideBarID) != null))
				FindControl(sideBarID).Visible = false;

			base.DataBind();
		}


		/// <summary>
		/// Return our current theme
		/// </summary>
		protected override string ThemeName
		{
			get { return CurrentGallery.Theme; }
		}

		protected override string ExternalSkinFileName
		{
			get { return Template; }
		}

		private string template = "LayoutTemplate.ascx";
		public string Template
		{
			get { return template;}
			set { template = value;}
		}

		private bool hideSideBar = false;
		public bool HideSideBar
		{
			get { return hideSideBar; }
			set { hideSideBar = value; }
		}

		private string sideBarID = "leftmenu";
		public string SideBarID
		{
			get { return sideBarID; }
			set { sideBarID = value; }
		}
	}
}