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

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

namespace CommunityServer.Galleries.Controls
{
	/// <summary>
	/// Summary description for GalleryTitleHeader.
	/// </summary>
	public class GalleryTitleHeader : GalleryThemedControl
	{

		#region Child Controls

		private HyperLink title;
		private UserAvatar avatar;
		private IText subtitle;
		private HyperLink site;
		private IText totalCategories;
		private IText totalPictures;

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			title = (HyperLink)FindControl( "GalleryTitle" );
			avatar = (UserAvatar)FindControl( "Avatar" );
			subtitle = FindText( "GallerySubTitle" );
			site = (HyperLink)FindControl("SiteName");
			totalCategories = FindText( "TotalCategories" );
			totalPictures = FindText( "TotalPictures" );
		}

		#endregion

		#region Event Overrides

		protected override void OnLoad(System.EventArgs e)
		{
			base.OnLoad(e);
			this.DataBind();
		}
		
		public override void DataBind()
		{
			base.DataBind();
			BindData();

		}

		#endregion

		private void BindData()
		{
			if(title != null)
			{
				title.Text = CurrentGallery.Name;
				title.NavigateUrl = GalleryUrls.Instance().ViewGallery( CurrentGallery.ApplicationKey );
			}

			if(avatar != null)
			{
				User owner = null;
				if(CurrentGallery.OwnerArray.Length > 0)
					owner = Users.FindUserByUsername(CurrentGallery.OwnerArray[0]);

				if(owner != null)
					avatar.User = owner;
			}

			if(subtitle != null)
				subtitle.Text = CurrentGallery.Description;

			if(site != null)
			{
				site.Text = CSContext.Current.SiteSettings.SiteName;
				site.NavigateUrl = Globals.GetSiteUrls().Home;
			}

			if(totalPictures != null)
				totalPictures.Text = CurrentGallery.TotalThreads.ToString();
			
			if(totalCategories != null)
				totalCategories.Text = PostCategories.GetCategories(CurrentGallery.SectionID, false, false, -1).Count.ToString();
		}



	}
}
