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

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

namespace CommunityServer.Blogs.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()
		{
            WeblogConfig = WeblogConfiguration.Instance();
        }

         protected WeblogConfiguration WeblogConfig = null;
         private Weblog weblog = null;
        
    
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender (e);

			CSContext context = CSContext.Current;

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

				if (WeblogConfiguration.Instance().EnableRawHeadEditing)
					Head.AddRawContent(CurrentWeblog.RawAdditionalHeader, context.Context);
			}
		}

        /// <summary>
        /// Returns the CurrentWeb and calls Authorize
        /// </summary>
        public virtual Weblog CurrentWeblog
        {
            get
            {
                if(weblog == null)
                    weblog = Weblogs.GetWeblog(CSContext.Current.ApplicationKey,true);
                return weblog;
            }
        }

        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;
        }

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

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

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